Skip to content

Commit

Permalink
fix other fields and block unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Oct 25, 2024
1 parent f103a96 commit 10be631
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 56 deletions.
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)
return ethHeader, nil
}

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)
return ethHeader, nil
}

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)
msgs, additionals := b.EthMsgsFromTendermintBlock(resBlock, blockRes)

txs := []*ethtypes.Transaction{}
Expand Down
26 changes: 24 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,7 @@ 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 +1308,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 +1361,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {

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

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

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
},
true,
},
Expand All @@ -1397,7 +1406,7 @@ 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 +1419,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 +1463,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 +1491,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 +1534,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 +1558,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 +1595,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 +1676,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 @@ -36,6 +36,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

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

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
}

// 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())

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"`
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

0 comments on commit 10be631

Please sign in to comment.