Commit 61e99b00 authored by Hamdi Allam's avatar Hamdi Allam

ability to compute storage hash for client

parent ad5a7ce1
...@@ -18,8 +18,8 @@ import ( ...@@ -18,8 +18,8 @@ import (
type Indexer struct { type Indexer struct {
db *database.DB db *database.DB
l1Processor *processor.L1Processor L1Processor *processor.L1Processor
l2Processor *processor.L2Processor L2Processor *processor.L2Processor
} }
// NewIndexer initializes an instance of the Indexer // NewIndexer initializes an instance of the Indexer
...@@ -38,7 +38,7 @@ func NewIndexer(cfg config.Config) (*Indexer, error) { ...@@ -38,7 +38,7 @@ func NewIndexer(cfg config.Config) (*Indexer, error) {
L1StandardBridge: common.HexToAddress("0x6900000000000000000000000000000000000003"), L1StandardBridge: common.HexToAddress("0x6900000000000000000000000000000000000003"),
L1ERC721Bridge: common.HexToAddress("0x6900000000000000000000000000000000000004"), L1ERC721Bridge: common.HexToAddress("0x6900000000000000000000000000000000000004"),
} }
l1EthClient, err := node.NewEthClient(cfg.RPCs.L1RPC) l1EthClient, err := node.DialEthClient(cfg.RPCs.L1RPC)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -49,7 +49,7 @@ func NewIndexer(cfg config.Config) (*Indexer, error) { ...@@ -49,7 +49,7 @@ func NewIndexer(cfg config.Config) (*Indexer, error) {
// L2Processor // L2Processor
l2Contracts := processor.L2ContractPredeploys() // Make this configurable l2Contracts := processor.L2ContractPredeploys() // Make this configurable
l2EthClient, err := node.NewEthClient(cfg.RPCs.L2RPC) l2EthClient, err := node.DialEthClient(cfg.RPCs.L2RPC)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -60,8 +60,8 @@ func NewIndexer(cfg config.Config) (*Indexer, error) { ...@@ -60,8 +60,8 @@ func NewIndexer(cfg config.Config) (*Indexer, error) {
indexer := &Indexer{ indexer := &Indexer{
db: db, db: db,
l1Processor: l1Processor, L1Processor: l1Processor,
l2Processor: l2Processor, L2Processor: l2Processor,
} }
return indexer, nil return indexer, nil
...@@ -86,8 +86,8 @@ func (i *Indexer) Run(ctx context.Context) error { ...@@ -86,8 +86,8 @@ func (i *Indexer) Run(ctx context.Context) error {
} }
// Kick off the processors // Kick off the processors
go run(i.l1Processor.Start) go run(i.L1Processor.Start)
go run(i.l2Processor.Start) go run(i.L2Processor.Start)
err := <-errCh err := <-errCh
// ensure both processors have halted before returning // ensure both processors have halted before returning
......
...@@ -3,6 +3,7 @@ package node ...@@ -3,6 +3,7 @@ package node
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"math/big" "math/big"
"time" "time"
...@@ -29,6 +30,8 @@ type EthClient interface { ...@@ -29,6 +30,8 @@ type EthClient interface {
BlockHeadersByRange(*big.Int, *big.Int) ([]*types.Header, error) BlockHeadersByRange(*big.Int, *big.Int) ([]*types.Header, error)
BlockHeaderByHash(common.Hash) (*types.Header, error) BlockHeaderByHash(common.Hash) (*types.Header, error)
StorageHash(common.Address, *big.Int) (common.Hash, error)
RawRpcClient() *rpc.Client RawRpcClient() *rpc.Client
} }
...@@ -36,7 +39,7 @@ type client struct { ...@@ -36,7 +39,7 @@ type client struct {
rpcClient *rpc.Client rpcClient *rpc.Client
} }
func NewEthClient(rpcUrl string) (EthClient, error) { func DialEthClient(rpcUrl string) (EthClient, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultDialTimeout) ctxwt, cancel := context.WithTimeout(context.Background(), defaultDialTimeout)
defer cancel() defer cancel()
...@@ -49,6 +52,10 @@ func NewEthClient(rpcUrl string) (EthClient, error) { ...@@ -49,6 +52,10 @@ func NewEthClient(rpcUrl string) (EthClient, error) {
return client, nil return client, nil
} }
func NewEthClient(rpcClient *rpc.Client) EthClient {
return &client{rpcClient}
}
func (c *client) RawRpcClient() *rpc.Client { func (c *client) RawRpcClient() *rpc.Client {
return c.rpcClient return c.rpcClient
} }
...@@ -136,15 +143,33 @@ func (c *client) BlockHeadersByRange(startHeight, endHeight *big.Int) ([]*types. ...@@ -136,15 +143,33 @@ func (c *client) BlockHeadersByRange(startHeight, endHeight *big.Int) ([]*types.
return headers, nil return headers, nil
} }
// StorageHash returns the sha3 of the storage root for the specified account
func (c *client) StorageHash(address common.Address, blockNumber *big.Int) (common.Hash, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
proof := struct{ StorageHash common.Hash }{}
err := c.rpcClient.CallContext(ctxwt, &proof, "eth_getProof", address, nil, toBlockNumArg(blockNumber))
if err != nil {
return common.Hash{}, err
}
return proof.StorageHash, nil
}
func toBlockNumArg(number *big.Int) string { func toBlockNumArg(number *big.Int) string {
if number == nil { if number == nil {
return "latest" return "latest"
} else if number.Sign() >= 0 {
return hexutil.EncodeBig(number)
} }
pending := big.NewInt(-1) // It's negative.
if number.Cmp(pending) == 0 { if number.IsInt64() {
return "pending" tag, _ := rpc.BlockNumber(number.Int64()).MarshalText()
return string(tag)
} }
return hexutil.EncodeBig(number) // It's negative and large, which is invalid.
return fmt.Sprintf("<invalid %d>", number)
} }
...@@ -31,6 +31,11 @@ func (m *MockEthClient) BlockHeaderByHash(hash common.Hash) (*types.Header, erro ...@@ -31,6 +31,11 @@ func (m *MockEthClient) BlockHeaderByHash(hash common.Hash) (*types.Header, erro
return args.Get(0).(*types.Header), args.Error(1) return args.Get(0).(*types.Header), args.Error(1)
} }
func (m *MockEthClient) StorageHash(address common.Address, blockNumber *big.Int) (common.Hash, error) {
args := m.Called(address, blockNumber)
return args.Get(0).(common.Hash), args.Error(1)
}
func (m *MockEthClient) RawRpcClient() *rpc.Client { func (m *MockEthClient) RawRpcClient() *rpc.Client {
args := m.Called() args := m.Called()
return args.Get(0).(*rpc.Client) return args.Get(0).(*rpc.Client)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment