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

Implemented GetHeaderByNumber and GetHeaderByHash #149

Merged
merged 29 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
07ada94
implemented methods: eth_getHeaderByNumber
novosandara Mar 8, 2024
22a5ece
The following methods were created: eth_createAccessList, eth_coinbas…
novosandara Mar 10, 2024
202ae4a
fix lint
novosandara Mar 10, 2024
c3cbd86
CR fix, part 1
novosandara Mar 11, 2024
8df5959
CR fix, part 2
novosandara Mar 12, 2024
d07f2e4
lint fix
novosandara Mar 12, 2024
b0115c6
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 14, 2024
1fc6f10
created UTs for methods: eth_createAccessList, eth_getBlockReceipts, …
novosandara Mar 14, 2024
681665b
e2e test for eth_getBlockReceipts
novosandara Mar 14, 2024
297ea34
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 14, 2024
c7960fe
e2e remove
novosandara Mar 15, 2024
5847e35
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 15, 2024
775749e
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 18, 2024
ace4b58
CR fix
novosandara Mar 19, 2024
4ece04e
Remove dummy comment
Stefan-Ethernal Mar 20, 2024
29f7454
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 20, 2024
a2ce3d5
CR fix
novosandara Mar 20, 2024
8df9653
lint fix
novosandara Mar 22, 2024
e1bbc87
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 22, 2024
6f9b1d3
CR fix and new tests e2e have been added
novosandara Mar 25, 2024
fe5e7f0
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 25, 2024
685e4e0
Address comments
Stefan-Ethernal Mar 26, 2024
b95b8f4
Minor simplification
Stefan-Ethernal Mar 26, 2024
845579b
Minor changes in eth_getBlockReceipts
Stefan-Ethernal Mar 26, 2024
fb0ee29
e2e test fix and cr fix
novosandara Mar 26, 2024
469ca4f
lint fix
novosandara Mar 26, 2024
329f1b9
e2e
novosandara Mar 26, 2024
3421560
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 26, 2024
1295b73
Merge branch 'develop' into implementing_methods_eth_txpool
novosandara Mar 26, 2024
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
4 changes: 3 additions & 1 deletion e2e-polybft/e2e/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ func TestE2E_Bridge_RootchainTokensTransfers(t *testing.T) {
framework.WithBridge(),
framework.WithSecretsCallback(func(addrs []types.Address, tcc *framework.TestClusterConfig) {
for i := 0; i < len(addrs); i++ {
tcc.StakeAmounts = append(tcc.StakeAmounts, ethgo.Ether(10))
// premine receivers, so that they are able to do withdrawals
tcc.StakeAmounts = append(tcc.StakeAmounts, ethgo.Ether(10))
}

tcc.Premine = append(tcc.Premine, receivers...)
}))

defer cluster.Stop()

cluster.WaitForReady(t)
Expand Down Expand Up @@ -1345,6 +1346,7 @@ func TestE2E_Bridge_NonMintableERC20Token_WithPremine(t *testing.T) {
checkBalancesFn(types.Address(rewardWalletKey.Address()), bigZero, command.DefaultPremineBalance, true)

validatorsExpectedBalance := new(big.Int).Sub(command.DefaultPremineBalance, command.DefaultStake)

for _, server := range cluster.Servers {
validatorAccount, err := validatorHelper.GetAccountFromDir(server.DataDir())
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions e2e-polybft/e2e/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ func TestE2E_Consensus_MintableERC20NativeToken(t *testing.T) {
config.Premine = append(config.Premine, fmt.Sprintf("%s:%d", addr, initValidatorsBalance))
config.StakeAmounts = append(config.StakeAmounts, new(big.Int).Set(initValidatorsBalance))
validatorsAddrs[i] = addr

initialTotalSupply.Add(initialTotalSupply, initValidatorsBalance)
}
}))
Expand Down
1 change: 1 addition & 0 deletions e2e-polybft/e2e/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func TestE2E_JsonRPC(t *testing.T) {
Value: newBalance,
})
require.NoError(t, err)

txPrice := gasPrice * estimatedGas
// subtract gasPrice * estimatedGas from the balance and transfer the rest to the other account
// in order to leave no funds on the account
Expand Down
3 changes: 3 additions & 0 deletions e2e-polybft/e2e/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ func TestE2E_TxPool_Transfer(t *testing.T) {
if err != nil {
return true
}

t.Logf("Balance %s %s", receiver, balance)

if balance.Uint64() != uint64(sendAmount) {
return false
}
Expand Down Expand Up @@ -295,6 +297,7 @@ func TestE2E_TxPool_BroadcastTransactions(t *testing.T) {
for _, srv := range cluster.Servers {
balance, err := srv.WaitForNonZeroBalance(ethgo.Address(recipient), time.Second*10)
assert.NoError(t, err)

if balance != nil && balance.BitLen() > 0 {
assert.Equal(t, sentAmount, balance)
} else {
Expand Down
3 changes: 3 additions & 0 deletions e2e-polybft/property/property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ func TestProperty_DropValidators(t *testing.T) {
// check that block production is stoped
currentBlock, err = activeValidator.JSONRPC().Eth().BlockNumber()
require.NoError(t, err)

oldBlockNumber := currentBlock

time.Sleep(2 * blockTime)

currentBlock, err = activeValidator.JSONRPC().Eth().BlockNumber()
require.NoError(t, err)
require.Equal(t, oldBlockNumber, currentBlock)
Expand Down
2 changes: 2 additions & 0 deletions jsonrpc/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
assert.Error(t, err)
} else {
assert.NoError(t, err)

if tt.expectedBnh.BlockNumber != nil {
assert.Equal(t, *bnh.BlockNumber, *tt.expectedBnh.BlockNumber)
}

if tt.expectedBnh.BlockHash != nil {
assert.Equal(t, bnh.BlockHash.String(), tt.expectedBnh.BlockHash.String())
}
Expand Down
1 change: 1 addition & 0 deletions jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (d *Dispatcher) handleSubscribe(req Request, conn wsConn) (string, Error) {
if err != nil {
return "", NewInternalError(err.Error())
}

filterID = d.filterManager.NewLogFilter(logQuery, conn)
} else if subscribeMethod == "newPendingTransactions" {
filterID = d.filterManager.NewPendingTxFilter(conn)
Expand Down
4 changes: 4 additions & 0 deletions jsonrpc/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,20 +467,24 @@ func TestDispatcherBatchRequest(t *testing.T) {
assert.Equal(t, c.err, resp.Error)
} else {
var batchResp []SuccessResponse

assert.NoError(t, expectBatchJSONResult(res, &batchResp))

if c.name == "leading-whitespace" {
assert.Len(t, batchResp, 4)

for index, resp := range batchResp {
assert.Equal(t, c.batchResponse[index].Error, resp.Error)
}
} else if c.name == "valid-batch-req" {
assert.Len(t, batchResp, 6)

for index, resp := range batchResp {
assert.Equal(t, c.batchResponse[index].Error, resp.Error)
}
} else if c.name == "no-limits" {
assert.Len(t, batchResp, 12)

for index, resp := range batchResp {
assert.Equal(t, c.batchResponse[index].Error, resp.Error)
}
Expand Down
211 changes: 211 additions & 0 deletions jsonrpc/eth_blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jsonrpc
import (
"errors"
"math/big"
"strconv"
"testing"

"github.com/0xPolygon/polygon-edge/blockchain"
Expand Down Expand Up @@ -68,6 +69,59 @@ func TestEth_Block_GetBlockByHash(t *testing.T) {
assert.Nil(t, res)
}

func TestEth_Block_GetHeaderByNumber(t *testing.T) {
store := &mockBlockStore{}
for i := 0; i < 10; i++ {
store.add(newTestBlock(uint64(i), hash1))
}

eth := newTestEthEndpoint(store)

cases := []struct {
description string
blockNum BlockNumber
isNotNil bool
err bool
}{
{"should be able to get the latest block number", LatestBlockNumber, true, false},
{"should be able to get the earliest block number", EarliestBlockNumber, true, false},
{"should not be able to get block with negative number", BlockNumber(-50), false, true},
{"should be able to get block with number 0", BlockNumber(0), true, false},
{"should be able to get block with number 2", BlockNumber(2), true, false},
{"should be able to get block with number greater than latest block", BlockNumber(50), false, false},
}
for _, c := range cases {
res, err := eth.GetHeaderByNumber(c.blockNum)

if c.isNotNil {
assert.NotNil(t, res, "expected to return block, but got nil")
} else {
assert.Nil(t, res, "expected to return nil, but got data")
}

if c.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
}
}

func TestEth_Block_GetHeaderByHash(t *testing.T) {
store := &mockBlockStore{}
store.add(newTestBlock(1, hash1))

eth := newTestEthEndpoint(store)

res, err := eth.GetHeaderByHash(hash1)
assert.NoError(t, err)
assert.NotNil(t, res)

res, err = eth.GetHeaderByHash(hash2)
assert.NoError(t, err)
assert.Nil(t, res)
}

func TestEth_Block_BlockNumber(t *testing.T) {
store := &mockBlockStore{}
store.add(&types.Block{
Expand Down Expand Up @@ -261,8 +315,10 @@ func TestEth_GetTransactionReceipt(t *testing.T) {
eth := newTestEthEndpoint(store)
block := newTestBlock(1, hash4)
store.add(block)

txn0 := newTestTransaction(uint64(0), addr0)
txn1 := newTestTransaction(uint64(1), addr1)

block.Transactions = []*types.Transaction{txn0, txn1}
receipt1 := &types.Receipt{
Logs: []*types.Log{
Expand All @@ -287,6 +343,7 @@ func TestEth_GetTransactionReceipt(t *testing.T) {
},
}
receipt1.SetStatus(types.ReceiptSuccess)

receipt2 := &types.Receipt{
Logs: []*types.Log{
{
Expand Down Expand Up @@ -315,6 +372,68 @@ func TestEth_GetTransactionReceipt(t *testing.T) {
})
}

func TestEth_GetBlockReceipts(t *testing.T) {
store := newMockBlockStore()
eth := newTestEthEndpoint(store)
block := newTestBlock(1, hash4)
store.add(block)

txn0 := newTestTransaction(uint64(0), addr0)
txn1 := newTestTransaction(uint64(1), addr1)

block.Transactions = []*types.Transaction{txn0, txn1}
receipt1 := &types.Receipt{
Logs: []*types.Log{
{
// log 0
Topics: []types.Hash{
hash1,
},
},
{
// log 1
Topics: []types.Hash{
hash2,
},
},
{
// log 2
Topics: []types.Hash{
hash3,
},
},
},
}
receipt1.SetStatus(types.ReceiptSuccess)

receipt2 := &types.Receipt{
Logs: []*types.Log{
{
// log 3
Topics: []types.Hash{
hash4,
},
},
},
}
receipt2.SetStatus(types.ReceiptSuccess)
store.receipts[hash4] = []*types.Receipt{receipt1, receipt2}

res, err := eth.GetBlockReceipts(1)

assert.NoError(t, err)
assert.NotNil(t, res)

response := res.([]*receipt)
assert.Equal(t, txn1.Hash(), response[1].TxHash)
assert.Equal(t, 2, len(response))
assert.Equal(t, block.Hash(), response[1].BlockHash)
assert.NotNil(t, response[1].Logs)
assert.Len(t, response[1].Logs, 1)
assert.Equal(t, uint64(3), uint64(response[1].Logs[0].LogIndex))
assert.Equal(t, uint64(1), uint64(response[1].Logs[0].TxIndex))
}

func TestEth_Syncing(t *testing.T) {
store := newMockBlockStore()
eth := newTestEthEndpoint(store)
Expand Down Expand Up @@ -508,6 +627,79 @@ func TestEth_Call(t *testing.T) {
})
}

func TestEth_CreateAccessList(t *testing.T) {
store := newMockBlockStore()
hashs := make([]types.Hash, 10)
latest := LatestBlockNumber
blockNum1 := BlockNumber(1)
blockNum2 := BlockNumber(2)

for i := 0; i < 10; i++ {
hashs[i] = types.StringToHash(strconv.Itoa(i))
block := newTestBlock(uint64(i), hashs[i])
block.Header.GasUsed = uint64(i * 10)
store.add(block)
}

eth := newTestEthEndpoint(store)

txn := &txnArgs{
From: &addr0,
To: &addr1,
Gas: argUintPtr(100000),
GasPrice: argBytesPtr([]byte{0x64}),
Value: argBytesPtr([]byte{0x64}),
Data: nil,
Nonce: argUintPtr(0),
}

cases := []struct {
filter BlockNumberOrHash
err bool
}{
{
// both fields are empty
BlockNumberOrHash{},
true,
},
{
// return the latest block number
BlockNumberOrHash{BlockNumber: &latest},
true,
},
{
// specific real block number
BlockNumberOrHash{BlockNumber: &blockNum1},
true,
},
{
// specific block number (not found)
BlockNumberOrHash{BlockNumber: &blockNum2},
true,
},
{
// specific block by hash (found). By default all blocks in the mock have hash zero
BlockNumberOrHash{BlockHash: &types.ZeroHash},
false,
},
{
// specific block by hash (not found)
BlockNumberOrHash{BlockHash: &hashs[8]},
true,
},
}

for _, c := range cases {
res, err := eth.CreateAccessList(txn, c.filter)
if c.err {
assert.NoError(t, err)
assert.NotNil(t, res)
} else {
assert.Nil(t, err)
novosandara marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

type testStore interface {
ethStore
}
Expand Down Expand Up @@ -558,6 +750,24 @@ func (m *mockBlockStore) appendBlocksToStore(blocks []*types.Block) {
}
}

func (m *mockBlockStore) GetHeaderByNumber(num uint64) (*types.Header, bool) {
block, ok := m.GetBlockByNumber(num, true)
if !ok {
return nil, false
}

return block.Header, block.Header != nil
}

func (m *mockBlockStore) GetHeaderByHash(hash types.Hash) (*types.Header, bool) {
block, ok := m.GetBlockByHash(hash, true)
if !ok {
return nil, false
}

return block.Header, block.Header != nil
}

func (m *mockBlockStore) setupLogs() {
m.receipts = make(map[types.Hash][]*types.Receipt)

Expand Down Expand Up @@ -700,6 +910,7 @@ func (m *mockBlockStore) ApplyTxn(_ *types.Header, _ *types.Transaction, _ types
return &runtime.ExecutionResult{
Err: m.ethCallError,
ReturnValue: m.returnValue,
AccessList: runtime.NewAccessList(),
}, nil
}

Expand Down
Loading
Loading