Skip to content

Commit

Permalink
fix(evm)!: adjusted generation and parsing of the block bloom events (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
onikonychev authored Sep 4, 2024
1 parent 9c46873 commit e2bc002
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 79 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2017](https://github.com/NibiruChain/nibiru/pull/2017) - fix(evm): Fix DynamicFeeTx gas cap parameters
- [#2019](https://github.com/NibiruChain/nibiru/pull/2019) - chore(evm): enabled debug rpc api on localnet.
- [#2020](https://github.com/NibiruChain/nibiru/pull/2020) - test(evm): e2e tests for debug namespace
- [#2023](https://github.com/NibiruChain/nibiru/pull/2023) - fix(evm)!: adjusted generation and parsing of the block bloom events

#### Dapp modules: perp, spot, oracle, etc

Expand Down
86 changes: 12 additions & 74 deletions e2e/evm/test/eth_queries.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { describe, expect, it, jest } from "@jest/globals"
import {
toBigInt,
parseEther,
keccak256,
AbiCoder,
TransactionRequest,
} from "ethers"
import { parseEther, keccak256, AbiCoder, TransactionRequest } from "ethers"
import { account, provider } from "./setup"
import {
INTRINSIC_TX_GAS,
Expand All @@ -16,48 +10,9 @@ import {
sendTestNibi,
} from "./utils"

describe("Basic Queries", () => {
describe("eth queries", () => {
jest.setTimeout(15e3)

it("Simple transfer, balance check", async () => {
const amountToSend = toBigInt(5e12) * toBigInt(1e6) // unibi
const senderBalanceBefore = await provider.getBalance(account)
const recipientBalanceBefore = await provider.getBalance(alice)
expect(senderBalanceBefore).toBeGreaterThan(0)
expect(recipientBalanceBefore).toEqual(BigInt(0))

const tenPow12 = toBigInt(1e12)

// Execute EVM transfer
const transaction: TransactionRequest = {
gasLimit: toBigInt(100e3),
to: alice,
value: amountToSend,
}
const txResponse = await account.sendTransaction(transaction)
await txResponse.wait(1, 10e3)
expect(txResponse).toHaveProperty("blockHash")

const senderBalanceAfter = await provider.getBalance(account)
const recipientBalanceAfter = await provider.getBalance(alice)

// Assert balances with logging
const gasUsed = 50000n // 50k gas for the transaction
const txCostMicronibi = amountToSend / tenPow12 + gasUsed
const txCostWei = txCostMicronibi * tenPow12
const expectedSenderWei = senderBalanceBefore - txCostWei
console.debug("DEBUG should send via transfer method %o:", {
senderBalanceBefore,
amountToSend,
expectedSenderWei,
senderBalanceAfter,
})
expect(senderBalanceAfter).toEqual(expectedSenderWei)
expect(recipientBalanceAfter).toEqual(amountToSend)
})
})

describe("eth queries", () => {
it("eth_accounts", async () => {
const accounts = await provider.listAccounts()
expect(accounts).not.toHaveLength(0)
Expand Down Expand Up @@ -153,7 +108,7 @@ describe("eth queries", () => {
const contract = await deployContractTestERC20()
const contractAddr = await contract.getAddress()
const filter = {
fromBlock: "latest",
fromBlock: "0x1",
address: contractAddr,
}
// Create the filter for a contract
Expand All @@ -177,17 +132,18 @@ describe("eth queries", () => {
})

// Skipping as the method is not implemented
it.skip("eth_getFilterLogs", async () => {
it("eth_getFilterLogs", async () => {
// Deploy ERC-20 contract
const contract = await deployContractTestERC20()
const contractAddr = await contract.getAddress()
const filter = {
fromBlock: "latest",
fromBlock: "0x1",
address: contractAddr,
}
// Execute some contract TX
const tx = await contract.transfer(alice, parseEther("0.01"))
await tx.wait(1, 5e3)
await new Promise((resolve) => setTimeout(resolve, 3000))

// Create the filter for a contract
const filterId = await provider.send("eth_newFilter", [filter])
Expand All @@ -202,16 +158,19 @@ describe("eth queries", () => {
})

// Skipping as the method is not implemented
it.skip("eth_getLogs", async () => {
it("eth_getLogs", async () => {
// Deploy ERC-20 contract
const contract = await deployContractTestERC20()
const contractAddr = await contract.getAddress()
console.log(contractAddr)
const filter = {
fromBlock: "latest",
fromBlock: "0x1",
address: contractAddr,
}
// Execute some contract TX
const _tx = await contract.transfer(alice, parseEther("0.01"))
const tx = await contract.transfer(alice, parseEther("0.01"))
await tx.wait(1, 5e3)
await new Promise((resolve) => setTimeout(resolve, 3000))

// Assert logs
const changes = await provider.send("eth_getLogs", [filter])
Expand Down Expand Up @@ -252,27 +211,6 @@ describe("eth queries", () => {
}
})

// Skipping as the method is not implemented
it.skip("eth_getLogs", async () => {
// Deploy ERC-20 contract
const contract = await deployContractTestERC20()
const contractAddr = await contract.getAddress()
const filter = {
fromBlock: "latest",
address: contractAddr,
}
// Execute some contract TX
const tx = await contract.transfer(alice, parseEther("0.01"))
await tx.wait(1, 5e3)

// Assert logs
const logs = await provider.send("eth_getLogs", [filter])
expect(logs.length).toBeGreaterThan(0)
expect(logs[0]).toHaveProperty("address")
expect(logs[0]).toHaveProperty("data")
expect(logs[0]).toHaveProperty("topics")
})

it("eth_getProof", async () => {
const contract = await deployContractTestERC20()
const contractAddr = await contract.getAddress()
Expand Down
12 changes: 10 additions & 2 deletions eth/rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import (
"math"
"math/big"
"strconv"
"strings"

tmrpcclient "github.com/cometbft/cometbft/rpc/client"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/gogoproto/proto"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
"github.com/pkg/errors"
"github.com/status-im/keycard-go/hexutils"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

Expand Down Expand Up @@ -352,14 +355,19 @@ func (b *Backend) HeaderByHash(blockHash common.Hash) (*gethcore.Header, error)

// BlockBloom query block bloom filter from block results
func (b *Backend) BlockBloom(blockRes *tmrpctypes.ResultBlockResults) (gethcore.Bloom, error) {
msgType := proto.MessageName((*evm.EventBlockBloom)(nil))
for _, event := range blockRes.EndBlockEvents {
if event.Type != evm.EventTypeBlockBloom {
if event.Type != msgType {
continue
}

for _, attr := range event.Attributes {
if attr.Key == evm.AttributeKeyEthereumBloom {
return gethcore.BytesToBloom([]byte(attr.Value)), nil
return gethcore.BytesToBloom(
hexutils.HexToBytes( // Bloom stores hex bytes
strings.ReplaceAll(attr.Value, `"`, ""), // Unquote typed event
),
), nil
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions eth/rpc/backend/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"

"cosmossdk.io/math"
"github.com/cosmos/gogoproto/proto"

"github.com/cometbft/cometbft/abci/types"
cmtrpc "github.com/cometbft/cometbft/rpc/core/types"
Expand Down Expand Up @@ -845,7 +846,7 @@ func (s *BackendSuite) TestBlockBloom() {
&cmtrpc.ResultBlockResults{
EndBlockEvents: []types.Event{
{
Type: evm.EventTypeBlockBloom,
Type: proto.MessageName((*evm.EventBlockBloom)(nil)),
Attributes: []types.EventAttribute{
{Key: evm.AttributeKeyEthereumTxHash},
},
Expand All @@ -860,7 +861,7 @@ func (s *BackendSuite) TestBlockBloom() {
&cmtrpc.ResultBlockResults{
EndBlockEvents: []types.Event{
{
Type: evm.EventTypeBlockBloom,
Type: proto.MessageName((*evm.EventBlockBloom)(nil)),
Attributes: []types.EventAttribute{
{Key: evm.AttributeKeyEthereumBloom},
},
Expand Down
4 changes: 3 additions & 1 deletion x/evm/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package keeper
import (
abci "github.com/cometbft/cometbft/abci/types"

"github.com/NibiruChain/nibiru/v2/eth"

"github.com/NibiruChain/nibiru/v2/x/evm"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -18,7 +20,7 @@ func (k *Keeper) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Valida
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
bloom := gethcoretypes.BytesToBloom(k.EvmState.GetBlockBloomTransient(ctx).Bytes())
_ = ctx.EventManager().EmitTypedEvent(&evm.EventBlockBloom{
Bloom: string(bloom.Bytes()),
Bloom: eth.BytesToHex(bloom.Bytes()),
})
// The bloom logic doesn't update the validator set.
return []abci.ValidatorUpdate{}
Expand Down

0 comments on commit e2bc002

Please sign in to comment.