Skip to content

Commit

Permalink
cmd/hivechain: use calldata hash as log topic in emit contract
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl authored and Eikix committed Mar 1, 2024
1 parent 81422e6 commit 93245d7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
Binary file modified cmd/hivechain/bytecode/emit.bin
Binary file not shown.
20 changes: 11 additions & 9 deletions cmd/hivechain/contracts/emit.eas
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
;;; -*- mode: asm -*-
;;; Example contract which emits storage and logs when invoked.
;;; The storage entry uses the hash of calldata as the value.
;;; In the log, the same hash is used as the second topic.

#define s_counter 0

Expand All @@ -19,17 +21,17 @@
dup3 ; [hash, counter, counter, hash]
sstore ; [counter, hash]

;; increment counter
push 1 ; [1, counter, hash]
add ; [counter+1, hash]
push s_counter ; [slot, counter+1, hash]
sstore ; [hash]
;; increment counter in storage
dup1 ; [counter, counter, hash]
push 1 ; [1, counter, counter, hash]
add ; [counter+1, counter, hash]
push s_counter ; [slot, counter+1, counter, hash]
sstore ; [counter, hash]

;; emit log
push 0 ; [offset, hash]
mstore ; []
origin ; [topic2]
push "emit" ; [topic1, topic2]
push 0 ; [offset, counter, hash]
mstore ; [hash]
push "emit" ; [topic1, hash=topic2]
push 32 ; [size, topic1, topic2]
push 0 ; [offset, size, topic1, topic2]
log2 ; []
22 changes: 12 additions & 10 deletions cmd/hivechain/mod_txinvoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ type modInvokeEmit struct {
}

type invokeEmitTxInfo struct {
TxHash common.Hash `json:"txhash"`
Sender common.Address `json:"sender"`
Block hexutil.Uint64 `json:"block"`
Index int `json:"indexInBlock"`
DataHash common.Hash `json:"datahash"`
TxHash common.Hash `json:"txhash"`
Sender common.Address `json:"sender"`
Block hexutil.Uint64 `json:"block"`
Index int `json:"indexInBlock"`
LogTopic0 common.Hash `json:"logtopic0"`
LogTopic1 common.Hash `json:"logtopic1"`
}

func (m *modInvokeEmit) apply(ctx *genBlockContext) bool {
Expand Down Expand Up @@ -158,11 +159,12 @@ func (m *modInvokeEmit) apply(ctx *genBlockContext) bool {
txindex := ctx.TxCount()
tx := ctx.AddNewTx(sender, txdata)
m.txs = append(m.txs, invokeEmitTxInfo{
Block: hexutil.Uint64(ctx.NumberU64()),
Sender: sender.addr,
TxHash: tx.Hash(),
Index: txindex,
DataHash: datahash,
Block: hexutil.Uint64(ctx.NumberU64()),
Sender: sender.addr,
TxHash: tx.Hash(),
Index: txindex,
LogTopic0: common.HexToHash("0x00000000000000000000000000000000000000000000000000000000656d6974"),
LogTopic1: datahash,
})
return true
}
Expand Down

0 comments on commit 93245d7

Please sign in to comment.