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

add sei2_getBlock endpoints to include bank transfers #2064

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 50 additions & 2 deletions evmrpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"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/lib/ethapi"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
"github.com/sei-protocol/sei-chain/x/evm/state"
"github.com/sei-protocol/sei-chain/x/evm/types"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/coretypes"
Expand All @@ -37,6 +39,7 @@
connectionType ConnectionType
namespace string
includeShellReceipts bool
includeBankTransfers bool
}

type SeiBlockAPI struct {
Expand All @@ -52,6 +55,7 @@
txConfig: txConfig,
connectionType: connectionType,
includeShellReceipts: false,
includeBankTransfers: false,
namespace: "eth",
}
}
Expand All @@ -71,6 +75,7 @@
txConfig: txConfig,
connectionType: connectionType,
includeShellReceipts: true,
includeBankTransfers: false,
namespace: "sei",
}
return &SeiBlockAPI{
Expand All @@ -79,6 +84,20 @@
}
}

func NewSei2BlockAPI(
tmClient rpcclient.Client,
k *keeper.Keeper,
ctxProvider func(int64) sdk.Context,
txConfig client.TxConfig,
connectionType ConnectionType,
isPanicTx func(ctx context.Context, hash common.Hash) (bool, error),
) *SeiBlockAPI {
blockAPI := NewSeiBlockAPI(tmClient, k, ctxProvider, txConfig, connectionType, isPanicTx)
blockAPI.namespace = "sei2"
blockAPI.includeBankTransfers = true
return blockAPI
}

func (a *SeiBlockAPI) GetBlockByNumberExcludeTraceFail(ctx context.Context, number rpc.BlockNumber, fullTx bool) (result map[string]interface{}, returnErr error) {
// exclude synthetic txs
return a.getBlockByNumber(ctx, number, fullTx, false, a.isPanicTx)
Expand Down Expand Up @@ -130,7 +149,7 @@
return nil, err
}
blockBloom := a.keeper.GetBlockBloom(a.ctxProvider(block.Block.Height))
return EncodeTmBlock(a.ctxProvider(block.Block.Height), block, blockRes, blockBloom, a.keeper, a.txConfig.TxDecoder(), fullTx, a.includeShellReceipts, isPanicTx)
return EncodeTmBlock(a.ctxProvider(block.Block.Height), block, blockRes, blockBloom, a.keeper, a.txConfig.TxDecoder(), fullTx, a.includeBankTransfers, includeSyntheticTxs, isPanicTx)
}

func (a *BlockAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (result map[string]interface{}, returnErr error) {
Expand Down Expand Up @@ -186,7 +205,7 @@
return nil, err
}
blockBloom := a.keeper.GetBlockBloom(a.ctxProvider(block.Block.Height))
return EncodeTmBlock(a.ctxProvider(block.Block.Height), block, blockRes, blockBloom, a.keeper, a.txConfig.TxDecoder(), fullTx, includeSyntheticTxs, isPanicTx)
return EncodeTmBlock(a.ctxProvider(block.Block.Height), block, blockRes, blockBloom, a.keeper, a.txConfig.TxDecoder(), fullTx, a.includeBankTransfers, includeSyntheticTxs, isPanicTx)
}

func (a *BlockAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (result []map[string]interface{}, returnErr error) {
Expand Down Expand Up @@ -266,6 +285,7 @@
k *keeper.Keeper,
txDecoder sdk.TxDecoder,
fullTx bool,
includeBankTransfers bool,
includeSyntheticTxs bool,
isPanicOrSynthetic func(ctx context.Context, hash common.Hash) (bool, error),
) (map[string]interface{}, error) {
Expand Down Expand Up @@ -342,6 +362,34 @@
TransactionIndex: (*hexutil.Uint64)(&ti),
})
}
case *banktypes.MsgSend:
if !includeBankTransfers {
continue
}
th := sha256.Sum256(block.Block.Txs[i])
if !fullTx {
transactions = append(transactions, "0x"+hex.EncodeToString(th[:]))

Check warning on line 371 in evmrpc/block.go

View check run for this annotation

Codecov / codecov/patch

evmrpc/block.go#L371

Added line #L371 was not covered by tests
} else {
rpcTx := &ethapi.RPCTransaction{
BlockHash: &blockhash,
BlockNumber: (*hexutil.Big)(number),
Hash: th,
}
senderSeiAddr, err := sdk.AccAddressFromBech32(m.FromAddress)
if err != nil {
continue

Check warning on line 380 in evmrpc/block.go

View check run for this annotation

Codecov / codecov/patch

evmrpc/block.go#L380

Added line #L380 was not covered by tests
}
rpcTx.From = k.GetEVMAddressOrDefault(ctx, senderSeiAddr)
recipientSeiAddr, err := sdk.AccAddressFromBech32(m.ToAddress)
if err != nil {
continue

Check warning on line 385 in evmrpc/block.go

View check run for this annotation

Codecov / codecov/patch

evmrpc/block.go#L385

Added line #L385 was not covered by tests
}
recipientEvmAddr := k.GetEVMAddressOrDefault(ctx, recipientSeiAddr)
rpcTx.To = &recipientEvmAddr
amt := m.Amount.AmountOf("usei").Mul(state.SdkUseiToSweiMultiplier)
rpcTx.Value = (*hexutil.Big)(amt.BigInt())
transactions = append(transactions, rpcTx)
}
}
}
}
Expand Down
64 changes: 61 additions & 3 deletions evmrpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestEncodeTmBlock_EmptyTransactions(t *testing.T) {
}

// Call EncodeTmBlock with empty transactions
result, err := evmrpc.EncodeTmBlock(ctx, block, blockRes, ethtypes.Bloom{}, k, Decoder, true, false, nil)
result, err := evmrpc.EncodeTmBlock(ctx, block, blockRes, ethtypes.Bloom{}, k, Decoder, true, false, false, nil)
require.Nil(t, err)

// Assert txHash is equal to ethtypes.EmptyTxsHash
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestEncodeBankMsg(t *testing.T) {
},
},
}
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, false, nil)
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, false, false, nil)
require.Nil(t, err)
txs := res["transactions"].([]interface{})
require.Equal(t, 0, len(txs))
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestEncodeWasmExecuteMsg(t *testing.T) {
},
},
}
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, true, nil)
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, false, true, nil)
require.Nil(t, err)
txs := res["transactions"].([]interface{})
require.Equal(t, 1, len(txs))
Expand All @@ -319,3 +319,61 @@ func TestEncodeWasmExecuteMsg(t *testing.T) {
S: nil,
}, txs[0].(*ethapi.RPCTransaction))
}

func TestEncodeBankTransferMsg(t *testing.T) {
k := &testkeeper.EVMTestApp.EvmKeeper
ctx := testkeeper.EVMTestApp.GetContextForDeliverTx(nil)
fromSeiAddr, fromEvmAddr := testkeeper.MockAddressPair()
k.SetAddressMapping(ctx, fromSeiAddr, fromEvmAddr)
toSeiAddr, _ := testkeeper.MockAddressPair()
b := TxConfig.NewTxBuilder()
b.SetMsgs(&banktypes.MsgSend{
FromAddress: fromSeiAddr.String(),
ToAddress: toSeiAddr.String(),
Amount: sdk.NewCoins(sdk.NewCoin("usei", sdk.OneInt())),
})
tx := b.GetTx()
bz, _ := Encoder(tx)
resBlock := coretypes.ResultBlock{
BlockID: MockBlockID,
Block: &tmtypes.Block{
Header: mockBlockHeader(MockHeight8),
Data: tmtypes.Data{
Txs: []tmtypes.Tx{bz},
},
LastCommit: &tmtypes.Commit{
Height: MockHeight8 - 1,
},
},
}
resBlockRes := coretypes.ResultBlockResults{
TxsResults: []*abci.ExecTxResult{
{
Data: bz,
},
},
ConsensusParamUpdates: &types2.ConsensusParams{
Block: &types2.BlockParams{
MaxBytes: 100000000,
MaxGas: 200000000,
},
},
}
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, true, false, nil)
require.Nil(t, err)
txs := res["transactions"].([]interface{})
require.Equal(t, 1, len(txs))
bh := common.HexToHash(MockBlockID.Hash.String())
to := common.Address(toSeiAddr)
require.Equal(t, &ethapi.RPCTransaction{
BlockHash: &bh,
BlockNumber: (*hexutil.Big)(big.NewInt(MockHeight8)),
From: fromEvmAddr,
To: &to,
Value: (*hexutil.Big)(big.NewInt(1_000_000_000_000)),
Hash: common.Hash(sha256.Sum256(bz)),
V: nil,
R: nil,
S: nil,
}, txs[0].(*ethapi.RPCTransaction))
}
4 changes: 4 additions & 0 deletions evmrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func NewEVMHTTPServer(
Namespace: "sei",
Service: NewSeiBlockAPI(tmClient, k, ctxProvider, txConfig, ConnectionTypeHTTP, isPanicOrSyntheticTxFunc),
},
{
Namespace: "sei2",
Service: NewSei2BlockAPI(tmClient, k, ctxProvider, txConfig, ConnectionTypeHTTP, isPanicOrSyntheticTxFunc),
},
{
Namespace: "eth",
Service: txAPI,
Expand Down
Loading