Skip to content

Commit

Permalink
feat(eth): add rpc methods for retrieving previous 256 parentHashes /…
Browse files Browse the repository at this point in the history
… blocks from blockID (#144)

* skeleton

* Update taiko_api_backend.go

* add getl2parenthashes test

* use blocks[i].hash instead of str

* add getl2parentblocks test

* standardize

* changes according to comments

* refactor according to comments

* refactor again

* spaces
  • Loading branch information
RogerLamTd authored Dec 12, 2023
1 parent 4ebbdef commit bdd165f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
23 changes: 23 additions & 0 deletions eth/taiko_api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,26 @@ func (s *TaikoAPIBackend) TxPoolContent(
maxTransactionsLists,
)
}

// Get L2ParentHashes retrieves the preceding 256 parent hashes given a block number.
func (s *TaikoAPIBackend) GetL2ParentHashes(blockID uint64) ([]common.Hash, error) {
var hashes []common.Hash
headers, err := s.GetL2ParentHeaders(blockID)
if err != nil {
return nil, err
}

for _, x := range headers {
hashes = append(hashes, x.Hash())
}
return hashes, nil
}

// Get L2ParentBlocks retrieves the preceding 256 parent blocks given a block number.
func (s *TaikoAPIBackend) GetL2ParentHeaders(blockID uint64) ([]*types.Header, error) {
var headers []*types.Header
for i := blockID; i != 0 && (blockID-i) < 256; i-- {
headers = append(headers, s.eth.blockchain.GetHeaderByNumber(blockID-i))
}
return headers, nil
}
21 changes: 21 additions & 0 deletions ethclient/taiko_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/rawdb"
)
Expand All @@ -29,3 +30,23 @@ func (ec *Client) L1OriginByID(ctx context.Context, blockID *big.Int) (*rawdb.L1

return res, nil
}

func (ec *Client) GetL2ParentHashes(ctx context.Context, blockID uint64) ([]common.Hash, error) {
var res []common.Hash

if err := ec.c.CallContext(ctx, &res, "taiko_getL2ParentHashes", blockID); err != nil {
return nil, err
}

return res, nil
}

func (ec *Client) GetL2ParentHeaders(ctx context.Context, blockID uint64) ([]map[string]interface{}, error) {
var res []map[string]interface{}

if err := ec.c.CallContext(ctx, &res, "taiko_getL2ParentHeaders", blockID); err != nil {
return nil, err
}

return res, nil
}
49 changes: 49 additions & 0 deletions ethclient/taiko_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,55 @@ func TestL1OriginByID(t *testing.T) {
require.Equal(t, testL1Origin, l1OriginFound)
}

func TestGetL2ParentHashes(t *testing.T) {
ec, blocks, _ := newTaikoAPITestClient(t)

hashes, err := ec.GetL2ParentHashes(context.Background(), common.Big3.Uint64())
require.Nil(t, err)

log.Info("hashes", "a", hashes)
require.Equal(t, 3, len(hashes))
require.Equal(t, blocks[0].Hash(), hashes[0])
require.Equal(t, blocks[1].Hash(), hashes[1])
require.Equal(t, blocks[2].Hash(), hashes[2])

hashes, err = ec.GetL2ParentHashes(context.Background(), common.Big2.Uint64())
require.Nil(t, err)

require.Equal(t, 2, len(hashes))
require.Equal(t, blocks[0].Hash(), hashes[0])
require.Equal(t, blocks[1].Hash(), hashes[1])

hashes, err = ec.GetL2ParentHashes(context.Background(), common.Big0.Uint64())
require.Nil(t, err)

require.Equal(t, 0, len(hashes))
}

func TestGetL2ParentBlocks(t *testing.T) {
ec, blocks, _ := newTaikoAPITestClient(t)

res, err := ec.GetL2ParentHeaders(context.Background(), common.Big3.Uint64())
require.Nil(t, err)

require.Equal(t, 3, len(res))
require.Equal(t, res[0]["hash"], blocks[0].Hash().String())
require.Equal(t, res[1]["hash"], blocks[1].Hash().String())
require.Equal(t, res[2]["hash"], blocks[2].Hash().String())

res, err = ec.GetL2ParentHeaders(context.Background(), common.Big2.Uint64())
require.Nil(t, err)

require.Equal(t, 2, len(res))
require.Equal(t, res[0]["hash"], blocks[0].Hash().String())
require.Equal(t, res[1]["hash"], blocks[1].Hash().String())

res, err = ec.GetL2ParentHeaders(context.Background(), common.Big0.Uint64())
require.Nil(t, err)

require.Equal(t, 0, len(res))
}

// randomHash generates a random blob of data and returns it as a hash.
func randomHash() common.Hash {
var hash common.Hash
Expand Down

0 comments on commit bdd165f

Please sign in to comment.