Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong block hash in subscribe new heads #3047

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* [2909](https://github.com/zeta-chain/node/pull/2909) - add legacy messages back to codec for querier backward compatibility
* [3018](https://github.com/zeta-chain/node/pull/3018) - support `DepositAndCall` and `WithdrawAndCall` with empty payload
* [3030](https://github.com/zeta-chain/node/pull/3030) - Avoid storing invalid Solana gateway address in the `SetGatewayAddress`
* [3047](https://github.com/zeta-chain/node/pull/3047) - wrong block hash in subscribe new heads

## v20.0.0

Expand Down
21 changes: 18 additions & 3 deletions rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,12 @@ func (b *Backend) HeaderByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Heade
)
}

ethHeader := rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee)
validatorAccount, err := GetValidatorAccount(&resBlock.Block.Header, b.queryClient)
if err != nil {
return nil, err
}

ethHeader := rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee, validatorAccount)
skosito marked this conversation as resolved.
Show resolved Hide resolved
return ethHeader, nil
skosito marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -440,7 +445,12 @@ func (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
)
}

ethHeader := rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee)
validatorAccount, err := GetValidatorAccount(&resBlock.Block.Header, b.queryClient)
if err != nil {
return nil, err
}

ethHeader := rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee, validatorAccount)
skosito marked this conversation as resolved.
Show resolved Hide resolved
return ethHeader, nil
skosito marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -613,7 +623,12 @@ func (b *Backend) EthBlockFromTendermintBlock(
)
}

ethHeader := rpctypes.EthHeaderFromTendermint(block.Header, bloom, baseFee)
validatorAccount, err := GetValidatorAccount(&resBlock.Block.Header, b.queryClient)
if err != nil {
return nil, err
}

ethHeader := rpctypes.EthHeaderFromTendermint(block.Header, bloom, baseFee, validatorAccount)
skosito marked this conversation as resolved.
Show resolved Hide resolved
msgs, additionals := b.EthMsgsFromTendermintBlock(resBlock, blockRes)

txs := []*ethtypes.Transaction{}
Expand Down
36 changes: 34 additions & 2 deletions rpc/backend/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ func (suite *BackendTestSuite) TestHeaderByNumber() {
var expResultBlock *tmrpctypes.ResultBlock

_, bz := suite.buildEthereumTx()
validator := sdk.AccAddress(tests.GenerateAddress().Bytes())

testCases := []struct {
name string
Expand Down Expand Up @@ -1218,6 +1219,7 @@ func (suite *BackendTestSuite) TestHeaderByNumber() {
height := blockNum.Int64()
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockNotFound(client, height)

skosito marked this conversation as resolved.
Show resolved Hide resolved
},
false,
},
Expand Down Expand Up @@ -1245,6 +1247,7 @@ func (suite *BackendTestSuite) TestHeaderByNumber() {

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFeeError(queryClient)
RegisterValidatorAccount(queryClient, validator)
},
true,
},
Expand All @@ -1260,6 +1263,7 @@ func (suite *BackendTestSuite) TestHeaderByNumber() {

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
true,
},
Expand All @@ -1275,6 +1279,7 @@ func (suite *BackendTestSuite) TestHeaderByNumber() {

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
true,
},
Expand All @@ -1287,7 +1292,12 @@ func (suite *BackendTestSuite) TestHeaderByNumber() {
header, err := suite.backend.HeaderByNumber(tc.blockNumber)

if tc.expPass {
expHeader := ethrpc.EthHeaderFromTendermint(expResultBlock.Block.Header, ethtypes.Bloom{}, tc.baseFee)
expHeader := ethrpc.EthHeaderFromTendermint(
expResultBlock.Block.Header,
ethtypes.Bloom{},
tc.baseFee,
validator,
)
suite.Require().NoError(err)
suite.Require().Equal(expHeader, header)
} else {
Expand All @@ -1303,6 +1313,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
_, bz := suite.buildEthereumTx()
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
validator := sdk.AccAddress(tests.GenerateAddress().Bytes())

testCases := []struct {
name string
Expand Down Expand Up @@ -1355,6 +1366,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFeeError(queryClient)
RegisterValidatorAccount(queryClient, validator)
},
true,
},
Expand All @@ -1370,6 +1382,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
true,
},
Expand All @@ -1385,6 +1398,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
true,
},
Expand All @@ -1397,7 +1411,12 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
header, err := suite.backend.HeaderByHash(tc.hash)

if tc.expPass {
expHeader := ethrpc.EthHeaderFromTendermint(expResultBlock.Block.Header, ethtypes.Bloom{}, tc.baseFee)
expHeader := ethrpc.EthHeaderFromTendermint(
expResultBlock.Block.Header,
ethtypes.Bloom{},
tc.baseFee,
validator,
)
suite.Require().NoError(err)
suite.Require().Equal(expHeader, header)
} else {
Expand All @@ -1410,6 +1429,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
func (suite *BackendTestSuite) TestEthBlockByNumber() {
msgEthereumTx, bz := suite.buildEthereumTx()
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
validator := sdk.AccAddress(tests.GenerateAddress().Bytes())

testCases := []struct {
name string
Expand Down Expand Up @@ -1453,12 +1473,14 @@ func (suite *BackendTestSuite) TestEthBlockByNumber() {
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
baseFee := sdk.NewInt(1)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
ethtypes.NewBlock(
ethrpc.EthHeaderFromTendermint(
emptyBlock.Header,
ethtypes.Bloom{},
sdk.NewInt(1).BigInt(),
validator,
),
[]*ethtypes.Transaction{},
nil,
Expand All @@ -1479,12 +1501,14 @@ func (suite *BackendTestSuite) TestEthBlockByNumber() {
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
baseFee := sdk.NewInt(1)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
ethtypes.NewBlock(
ethrpc.EthHeaderFromTendermint(
emptyBlock.Header,
ethtypes.Bloom{},
sdk.NewInt(1).BigInt(),
validator,
),
[]*ethtypes.Transaction{msgEthereumTx.AsTransaction()},
nil,
Expand Down Expand Up @@ -1520,6 +1544,7 @@ func (suite *BackendTestSuite) TestEthBlockByNumber() {
func (suite *BackendTestSuite) TestEthBlockFromTendermintBlock() {
msgEthereumTx, bz := suite.buildEthereumTx()
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
validator := sdk.AccAddress(tests.GenerateAddress().Bytes())

testCases := []struct {
name string
Expand All @@ -1543,12 +1568,14 @@ func (suite *BackendTestSuite) TestEthBlockFromTendermintBlock() {
func(baseFee sdkmath.Int, blockNum int64) {
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
ethtypes.NewBlock(
ethrpc.EthHeaderFromTendermint(
emptyBlock.Header,
ethtypes.Bloom{},
sdk.NewInt(1).BigInt(),
validator,
),
[]*ethtypes.Transaction{},
nil,
Expand Down Expand Up @@ -1578,12 +1605,14 @@ func (suite *BackendTestSuite) TestEthBlockFromTendermintBlock() {
func(baseFee sdkmath.Int, blockNum int64) {
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
ethtypes.NewBlock(
ethrpc.EthHeaderFromTendermint(
emptyBlock.Header,
ethtypes.Bloom{},
sdk.NewInt(1).BigInt(),
validator,
),
[]*ethtypes.Transaction{msgEthereumTx.AsTransaction()},
nil,
Expand Down Expand Up @@ -1657,13 +1686,16 @@ func (suite *BackendTestSuite) TestEthAndSyntheticEthBlockByNumber() {
msgEthereumTx, _ := suite.buildEthereumTx()
realTx := suite.signAndEncodeEthTx(msgEthereumTx)

validator := sdk.AccAddress(tests.GenerateAddress().Bytes())

suite.backend.indexer = nil
client := suite.backend.clientCtx.Client.(*mocks.Client)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
// block contains block real and synthetic tx
RegisterBlock(client, 1, []tmtypes.Tx{realTx, tx})
RegisterBlockResultsWithTxResults(client, 1, []*types.ResponseDeliverTx{{}, &txRes})
RegisterBaseFee(queryClient, sdk.NewInt(1))
RegisterValidatorAccount(queryClient, validator)

// only real should be returned
block, err := suite.backend.EthBlockByNumber(1)
Expand Down
14 changes: 14 additions & 0 deletions rpc/backend/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/proto/tendermint/crypto"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -318,3 +319,16 @@ func GetHexProofs(proof *crypto.ProofOps) []string {
}
return proofs
}

func GetValidatorAccount(header *tmtypes.Header, qc *types.QueryClient) (sdk.AccAddress, error) {
res, err := qc.ValidatorAccount(
types.ContextWithHeight(header.Height),
&evmtypes.QueryValidatorAccountRequest{
ConsAddress: sdk.ConsAddress(header.ProposerAddress).String(),
},
)
if err != nil {
return nil, fmt.Errorf("failed to get validator account %w", err)
}
return sdk.AccAddressFromBech32(res.AccountAddress)
}
56 changes: 43 additions & 13 deletions rpc/namespaces/ethereum/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import (
tmtypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/rpc"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"

"github.com/zeta-chain/node/rpc/backend"
"github.com/zeta-chain/node/rpc/types"
)

Expand Down Expand Up @@ -81,12 +83,13 @@ type filter struct {
// PublicFilterAPI offers support to create and manage filters. This will allow external clients to retrieve various
// information related to the Ethereum protocol such as blocks, transactions and logs.
type PublicFilterAPI struct {
logger log.Logger
clientCtx client.Context
backend Backend
events *EventSystem
filtersMu sync.Mutex
filters map[rpc.ID]*filter
logger log.Logger
clientCtx client.Context
backend Backend
events *EventSystem
filtersMu sync.Mutex
filters map[rpc.ID]*filter
queryClient *types.QueryClient
}

// NewPublicAPI returns a new PublicFilterAPI instance.
Expand All @@ -98,11 +101,12 @@ func NewPublicAPI(
) *PublicFilterAPI {
logger = logger.With("api", "filter")
api := &PublicFilterAPI{
logger: logger,
clientCtx: clientCtx,
backend: backend,
filters: make(map[rpc.ID]*filter),
events: NewEventSystem(logger, tmWSClient),
logger: logger,
clientCtx: clientCtx,
backend: backend,
filters: make(map[rpc.ID]*filter),
events: NewEventSystem(logger, tmWSClient),
queryClient: types.NewQueryClient(clientCtx),
}

go api.timeoutLoop()
Expand Down Expand Up @@ -368,9 +372,35 @@ func (api *PublicFilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, er

baseFee := types.BaseFeeFromEvents(data.ResultBeginBlock.Events)

validatorAccount, err := backend.GetValidatorAccount(&data.Header, api.queryClient)
if err != nil {
api.logger.Error("failed to get validator account", "err", err)
continue
}
skosito marked this conversation as resolved.
Show resolved Hide resolved

// TODO: fetch bloom from events
header := types.EthHeaderFromTendermint(data.Header, ethtypes.Bloom{}, baseFee)
err = notifier.Notify(rpcSub.ID, header)
header := types.EthHeaderFromTendermint(data.Header, ethtypes.Bloom{}, baseFee, validatorAccount)

var enc types.Header
enc.ParentHash = header.ParentHash
enc.UncleHash = header.UncleHash
enc.Coinbase = header.Coinbase.Hex()
enc.Root = header.Root
enc.TxHash = header.TxHash
enc.ReceiptHash = header.ReceiptHash
enc.Bloom = header.Bloom
enc.Difficulty = (*hexutil.Big)(header.Difficulty)
enc.Number = (*hexutil.Big)(header.Number)
enc.GasLimit = hexutil.Uint64(header.GasLimit)
enc.GasUsed = hexutil.Uint64(header.GasUsed)
enc.Time = hexutil.Uint64(header.Time)
enc.Extra = header.Extra
enc.MixDigest = header.MixDigest
enc.Nonce = header.Nonce
enc.BaseFee = (*hexutil.Big)(header.BaseFee)
enc.Hash = common.BytesToHash(data.Header.Hash())

skosito marked this conversation as resolved.
Show resolved Hide resolved
err = notifier.Notify(rpcSub.ID, enc)
if err != nil {
api.logger.Debug("failed to notify", "error", err.Error())
}
Expand Down
25 changes: 25 additions & 0 deletions rpc/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/spf13/cast"
ethermint "github.com/zeta-chain/ethermint/types"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -210,3 +211,27 @@ func (bnh *BlockNumberOrHash) decodeFromString(input string) error {
}
return nil
}

// https://github.com/ethereum/go-ethereum/blob/release/1.11/core/types/gen_header_json.go#L18
type Header struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
// update string avoid lost checksumed miner after MarshalText
Coinbase string `json:"miner"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
skosito marked this conversation as resolved.
Show resolved Hide resolved
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
Bloom ethtypes.Bloom `json:"logsBloom" gencodec:"required"`
Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
Number *hexutil.Big `json:"number" gencodec:"required"`
GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
Time hexutil.Uint64 `json:"timestamp" gencodec:"required"`
Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
MixDigest common.Hash `json:"mixHash"`
Nonce ethtypes.BlockNonce `json:"nonce"`
BaseFee *hexutil.Big `json:"baseFeePerGas" rlp:"optional"`
WithdrawalsHash *common.Hash `json:"withdrawalsRoot" rlp:"optional"`
// overwrite rlpHash
Hash common.Hash `json:"hash"`
}
Loading
Loading