|
4 | 4 | package ethapi |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
7 | 8 | "errors" |
8 | 9 | "math/big" |
9 | 10 | "testing" |
10 | 11 |
|
11 | 12 | "github.com/ava-labs/libevm/common" |
| 13 | + "github.com/ava-labs/libevm/common/hexutil" |
12 | 14 | "github.com/ava-labs/libevm/core/types" |
13 | 15 | "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
14 | 17 | "go.uber.org/mock/gomock" |
15 | 18 |
|
| 19 | + "github.com/ava-labs/coreth/consensus/dummy" |
| 20 | + "github.com/ava-labs/coreth/core" |
| 21 | + "github.com/ava-labs/coreth/params" |
16 | 22 | "github.com/ava-labs/coreth/rpc" |
| 23 | + |
| 24 | + ethparams "github.com/ava-labs/libevm/params" |
17 | 25 | ) |
18 | 26 |
|
| 27 | +func TestBlockchainAPI_GetChainConfig(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + ctrl := gomock.NewController(t) |
| 31 | + defer ctrl.Finish() |
| 32 | + |
| 33 | + wantConfig := ¶ms.ChainConfig{ |
| 34 | + ChainID: big.NewInt(43114), |
| 35 | + } |
| 36 | + backend := NewMockBackend(ctrl) |
| 37 | + backend.EXPECT().ChainConfig().Return(wantConfig) |
| 38 | + |
| 39 | + api := NewBlockChainAPI(backend) |
| 40 | + |
| 41 | + gotConfig := api.GetChainConfig(context.Background()) |
| 42 | + assert.Equal(t, wantConfig, gotConfig) |
| 43 | +} |
| 44 | + |
| 45 | +// Copy one test case from TestCall |
| 46 | +func TestBlockchainAPI_CallDetailed(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + // Initialize test accounts |
| 49 | + var ( |
| 50 | + accounts = newAccounts(2) |
| 51 | + genesis = &core.Genesis{ |
| 52 | + Config: params.TestChainConfig, |
| 53 | + Alloc: types.GenesisAlloc{ |
| 54 | + accounts[0].addr: {Balance: big.NewInt(params.Ether)}, |
| 55 | + accounts[1].addr: {Balance: big.NewInt(params.Ether)}, |
| 56 | + }, |
| 57 | + } |
| 58 | + genBlocks = 10 |
| 59 | + signer = types.HomesteadSigner{} |
| 60 | + blockNumber = rpc.LatestBlockNumber |
| 61 | + ) |
| 62 | + api := NewBlockChainAPI(newTestBackend(t, genBlocks, genesis, dummy.NewCoinbaseFaker(), func(i int, b *core.BlockGen) { |
| 63 | + // Transfer from account[0] to account[1] |
| 64 | + // value: 1000 wei |
| 65 | + // fee: 0 wei |
| 66 | + tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{Nonce: uint64(i), To: &accounts[1].addr, Value: big.NewInt(1000), Gas: ethparams.TxGas, GasPrice: b.BaseFee(), Data: nil}), signer, accounts[0].key) |
| 67 | + b.AddTx(tx) |
| 68 | + })) |
| 69 | + |
| 70 | + result, err := api.CallDetailed( |
| 71 | + context.Background(), |
| 72 | + TransactionArgs{ |
| 73 | + From: &accounts[0].addr, |
| 74 | + To: &accounts[1].addr, |
| 75 | + Value: (*hexutil.Big)(big.NewInt(1000)), |
| 76 | + }, |
| 77 | + rpc.BlockNumberOrHash{BlockNumber: &blockNumber}, |
| 78 | + nil, |
| 79 | + ) |
| 80 | + require.NoError(t, err) |
| 81 | + require.NotNil(t, result) |
| 82 | + require.Equal(t, 0, result.ErrCode) |
| 83 | + require.Nil(t, result.ReturnData) |
| 84 | + require.Equal(t, ethparams.TxGas, result.UsedGas) |
| 85 | + require.Empty(t, result.Err) |
| 86 | +} |
| 87 | + |
19 | 88 | func TestBlockChainAPI_stateQueryBlockNumberAllowed(t *testing.T) { |
20 | 89 | t.Parallel() |
21 | 90 |
|
|
0 commit comments