Skip to content

Commit

Permalink
fix: wrong block hash in subscribe new heads (#3047)
Browse files Browse the repository at this point in the history
* fix wrong hash in subscribe new heads

* fix other fields and block unit tests

* changelog and fmt

* fix unit tests missing mocks

* remove unused field from header

* PR comment
  • Loading branch information
skosito authored Oct 28, 2024
1 parent 3eefe9c commit 4c8c926
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 34 deletions.
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
23 changes: 18 additions & 5 deletions rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,12 @@ func (b *Backend) HeaderByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Heade
)
}

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

return rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee, validatorAccount), nil
}

// HeaderByHash returns the block header identified by hash.
Expand Down Expand Up @@ -440,8 +444,12 @@ func (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
)
}

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

return rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee, validatorAccount), nil
}

// BlockBloom query block bloom filter from block results
Expand Down Expand Up @@ -613,7 +621,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)
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)

},
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
11 changes: 11 additions & 0 deletions rpc/backend/call_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (suite *BackendTestSuite) TestResend() {
gasPrice := new(hexutil.Big)
toAddr := tests.GenerateAddress()
chainID := (*hexutil.Big)(suite.backend.chainID)
validator := sdk.AccAddress(tests.GenerateAddress().Bytes())
callArgs := evmtypes.TransactionArgs{
From: nil,
To: &toAddr,
Expand Down Expand Up @@ -69,6 +70,7 @@ func (suite *BackendTestSuite) TestResend() {
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
RegisterBaseFeeDisabled(queryClient)
RegisterValidatorAccount(queryClient, validator)
},
evmtypes.TransactionArgs{
Nonce: &txNonce,
Expand All @@ -91,6 +93,7 @@ func (suite *BackendTestSuite) TestResend() {
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
evmtypes.TransactionArgs{
Nonce: &txNonce,
Expand All @@ -110,6 +113,7 @@ func (suite *BackendTestSuite) TestResend() {
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
RegisterBaseFeeDisabled(queryClient)
RegisterValidatorAccount(queryClient, validator)
},
evmtypes.TransactionArgs{
Nonce: &txNonce,
Expand Down Expand Up @@ -163,6 +167,7 @@ func (suite *BackendTestSuite) TestResend() {
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
evmtypes.TransactionArgs{
Nonce: &txNonce,
Expand All @@ -186,6 +191,7 @@ func (suite *BackendTestSuite) TestResend() {
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
evmtypes.TransactionArgs{
Nonce: &txNonce,
Expand All @@ -210,6 +216,7 @@ func (suite *BackendTestSuite) TestResend() {
RegisterParams(queryClient, &header, 1)
RegisterParamsWithoutHeader(queryClient, 1)
RegisterUnconfirmedTxsError(client, nil)
RegisterValidatorAccount(queryClient, validator)
},
evmtypes.TransactionArgs{
Nonce: &txNonce,
Expand Down Expand Up @@ -238,6 +245,7 @@ func (suite *BackendTestSuite) TestResend() {
RegisterParams(queryClient, &header, 1)
RegisterParamsWithoutHeader(queryClient, 1)
RegisterUnconfirmedTxsEmpty(client, nil)
RegisterValidatorAccount(queryClient, validator)
},
evmtypes.TransactionArgs{
Nonce: &txNonce,
Expand Down Expand Up @@ -448,6 +456,7 @@ func (suite *BackendTestSuite) TestDoCall() {

func (suite *BackendTestSuite) TestGasPrice() {
defaultGasPrice := (*hexutil.Big)(big.NewInt(1))
validator := sdk.AccAddress(tests.GenerateAddress().Bytes())

testCases := []struct {
name string
Expand All @@ -467,6 +476,7 @@ func (suite *BackendTestSuite) TestGasPrice() {
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, sdk.NewInt(1))
RegisterValidatorAccount(queryClient, validator)
},
defaultGasPrice,
true,
Expand All @@ -483,6 +493,7 @@ func (suite *BackendTestSuite) TestGasPrice() {
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, sdk.NewInt(1))
RegisterValidatorAccount(queryClient, validator)
},
defaultGasPrice,
false,
Expand Down
4 changes: 4 additions & 0 deletions rpc/backend/sign_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (suite *BackendTestSuite) TestSendTransaction() {
from := common.BytesToAddress(priv.PubKey().Address().Bytes())
nonce := hexutil.Uint64(1)
baseFee := sdk.NewInt(1)
validator := sdk.AccAddress(tests.GenerateAddress().Bytes())
callArgsDefault := evmtypes.TransactionArgs{
From: &from,
To: &toAddr,
Expand Down Expand Up @@ -82,6 +83,7 @@ func (suite *BackendTestSuite) TestSendTransaction() {
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
evmtypes.TransactionArgs{
From: &from,
Expand Down Expand Up @@ -115,6 +117,7 @@ func (suite *BackendTestSuite) TestSendTransaction() {
txBytes, err := txEncoder(tx)
suite.Require().NoError(err)
RegisterBroadcastTxError(client, txBytes)
RegisterValidatorAccount(queryClient, validator)
},
callArgsDefault,
common.Hash{},
Expand Down Expand Up @@ -142,6 +145,7 @@ func (suite *BackendTestSuite) TestSendTransaction() {
txBytes, err := txEncoder(tx)
suite.Require().NoError(err)
RegisterBroadcastTx(client, txBytes)
RegisterValidatorAccount(queryClient, validator)
},
callArgsDefault,
hash,
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)
}
Loading

0 comments on commit 4c8c926

Please sign in to comment.