-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(evm-precompile):Emit EVM events created to reflect the ABCI even…
…ts that occur outside the EVM to make sure that block explorers and indexers can find indexed ABCI event information.
- Loading branch information
1 parent
790b062
commit 0711426
Showing
19 changed files
with
353 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lts/jod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
x/evm/embeds/artifacts/contracts/NibiruEvmUtils.sol/INibiruEvm.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"_format": "hh-sol-artifact-1", | ||
"contractName": "INibiruEvm", | ||
"sourceName": "contracts/NibiruEvmUtils.sol", | ||
"abi": [ | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": true, | ||
"internalType": "string", | ||
"name": "eventType", | ||
"type": "string" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "bytes", | ||
"name": "attrs", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "AbciEvent", | ||
"type": "event" | ||
} | ||
], | ||
"bytecode": "0x", | ||
"deployedBytecode": "0x", | ||
"linkReferences": {}, | ||
"deployedLinkReferences": {} | ||
} |
4 changes: 2 additions & 2 deletions
4
...ontracts/TestERC20TransferThenPrecompileSend.sol/TestERC20TransferThenPrecompileSend.json
Large diffs are not rendered by default.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...rtifacts/contracts/TestFunTokenPrecompileLocalGas.sol/TestFunTokenPrecompileLocalGas.json
Large diffs are not rendered by default.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...acts/contracts/TestNativeSendThenPrecompileSend.sol/TestNativeSendThenPrecompileSend.json
Large diffs are not rendered by default.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...ds/artifacts/contracts/TestPrecompileSelfCallRevert.sol/TestPrecompileSelfCallRevert.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package precompile | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
gethcommon "github.com/ethereum/go-ethereum/common" | ||
gethcore "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
|
||
"github.com/NibiruChain/nibiru/v2/x/common/set" | ||
"github.com/NibiruChain/nibiru/v2/x/evm/embeds" | ||
"github.com/NibiruChain/nibiru/v2/x/evm/statedb" | ||
) | ||
|
||
const EvmEventAbciEvent = "AbciEvent" | ||
|
||
// EmitEventAbciEvents adds a sequence of ABCI events to the EVM state DB so that | ||
// they can be emitted at the end of the "EthereumTx". These events are indexed | ||
// by their ABCI event type and help communicate non-EVM events in Ethereum-based | ||
// block explorers and indexers by saving the event attributes in JSON form. | ||
func EmitEventAbciEvents( | ||
ctx sdk.Context, | ||
db *statedb.StateDB, | ||
abciEvents []sdk.Event, | ||
emittingAddr gethcommon.Address, | ||
) { | ||
blockNumber := uint64(ctx.BlockHeight()) | ||
event := embeds.SmartContract_Wasm.ABI.Events[EvmEventAbciEvent] | ||
for _, abciEvent := range abciEvents { | ||
topics := make([]gethcommon.Hash, 2) | ||
// Why 2? Because 2 = event ID + number of indexed event fields | ||
topics[0] = event.ID | ||
|
||
// eventType is the first (and only) indexed event | ||
topics[1] = EventTopicFromString(abciEvent.Type) | ||
|
||
attrsBz := AttrsToJSON(abciEvent.Attributes) | ||
db.AddLog(&gethcore.Log{ | ||
Address: emittingAddr, | ||
Topics: topics, | ||
Data: attrsBz, | ||
BlockNumber: blockNumber, | ||
}) | ||
} | ||
} | ||
|
||
// AttrsToJSON creates a deterministic JSON encoding for the | ||
func AttrsToJSON(attrs []abci.EventAttribute) []byte { | ||
if len(attrs) == 0 { | ||
return []byte("") | ||
} | ||
keysSeen := set.New[string]() | ||
|
||
// Create JSON object from the key-value tuples | ||
var buf bytes.Buffer | ||
buf.WriteByte('{') | ||
for i, attr := range attrs { | ||
// Keys must be unique to guarantee valid JSON object | ||
if keysSeen.Has(attr.Key) { | ||
continue | ||
} | ||
keysSeen.Add(attr.Key) | ||
|
||
if i > 0 { | ||
buf.WriteByte(',') | ||
} | ||
|
||
// Quote key and value | ||
_, _ = fmt.Fprintf(&buf, `"%s":"%s"`, attr.Key, attr.Value) | ||
} | ||
buf.WriteByte('}') | ||
|
||
return buf.Bytes() | ||
} | ||
|
||
// EventTopicFromBytes creates an [abi.Event] | ||
func EventTopicFromBytes(bz []byte) (topic gethcommon.Hash) { | ||
hash := crypto.Keccak256Hash(bz) | ||
copy(topic[:], hash[:]) | ||
return topic | ||
} | ||
func EventTopicFromString(str string) (topic gethcommon.Hash) { | ||
return EventTopicFromBytes([]byte(str)) | ||
} |
Oops, something went wrong.