Skip to content

Commit

Permalink
Merge pull request ethereum#53 from maticnetwork/dev-update-ethclient
Browse files Browse the repository at this point in the history
chg: Add GetRootHash to ethClient / use uint64
  • Loading branch information
atvanguard authored May 7, 2020
2 parents 27f9dbd + 67c29d7 commit e03cd94
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
23 changes: 12 additions & 11 deletions consensus/bor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package bor

import (
"encoding/hex"
"math"
"math/big"
"strconv"
Expand Down Expand Up @@ -122,29 +123,29 @@ func (api *API) GetCurrentValidators() ([]*Validator, error) {
}

// GetRootHash returns the merkle root of the start to end block headers
func (api *API) GetRootHash(start int64, end int64) ([]byte, error) {
func (api *API) GetRootHash(start uint64, end uint64) (string, error) {
if err := api.initializeRootHashCache(); err != nil {
return nil, err
return "", err
}
key := getRootHashKey(start, end)
if root, known := api.rootHashCache.Get(key); known {
return root.([]byte), nil
return root.(string), nil
}
length := uint64(end - start + 1)
if length > MaxCheckpointLength {
return nil, &MaxCheckpointLengthExceededError{start, end}
return "", &MaxCheckpointLengthExceededError{start, end}
}
currentHeaderNumber := api.chain.CurrentHeader().Number.Int64()
currentHeaderNumber := api.chain.CurrentHeader().Number.Uint64()
if start > end || end > currentHeaderNumber {
return nil, &InvalidStartEndBlockError{start, end, currentHeaderNumber}
return "", &InvalidStartEndBlockError{start, end, currentHeaderNumber}
}
blockHeaders := make([]*types.Header, end-start+1)
wg := new(sync.WaitGroup)
concurrent := make(chan bool, 20)
for i := start; i <= end; i++ {
wg.Add(1)
concurrent <- true
go func(number int64) {
go func(number uint64) {
blockHeaders[number-start] = api.chain.GetHeaderByNumber(uint64(number))
<-concurrent
wg.Done()
Expand All @@ -170,9 +171,9 @@ func (api *API) GetRootHash(start int64, end int64) ([]byte, error) {

tree := merkle.NewTreeWithOpts(merkle.TreeOptions{EnableHashSorting: false, DisableHashLeaves: true})
if err := tree.Generate(convert(headers), sha3.NewLegacyKeccak256()); err != nil {
return nil, err
return "", err
}
root := tree.Root().Hash
root := hex.EncodeToString(tree.Root().Hash)
api.rootHashCache.Add(key, root)
return root, nil
}
Expand All @@ -185,6 +186,6 @@ func (api *API) initializeRootHashCache() error {
return err
}

func getRootHashKey(start int64, end int64) string {
return strconv.FormatInt(start, 10) + "-" + strconv.FormatInt(end, 10)
func getRootHashKey(start uint64, end uint64) string {
return strconv.FormatUint(start, 10) + "-" + strconv.FormatUint(end, 10)
}
10 changes: 5 additions & 5 deletions consensus/bor/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (e *TotalVotingPowerExceededError) Error() string {
}

type InvalidStartEndBlockError struct {
Start int64
End int64
CurrentHeader int64
Start uint64
End uint64
CurrentHeader uint64
}

func (e *InvalidStartEndBlockError) Error() string {
Expand All @@ -56,8 +56,8 @@ func (e *InvalidStartEndBlockError) Error() string {
}

type MaxCheckpointLengthExceededError struct {
Start int64
End int64
Start uint64
End uint64
}

func (e *MaxCheckpointLengthExceededError) Error() string {
Expand Down
6 changes: 3 additions & 3 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,16 @@ func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma
}
}

func (b *EthAPIBackend) GetRootHash(ctx context.Context, starBlockNr rpc.BlockNumber, endBlockNr rpc.BlockNumber) ([]byte, error) {
func (b *EthAPIBackend) GetRootHash(ctx context.Context, starBlockNr uint64, endBlockNr uint64) (string, error) {
var api *bor.API
for _, _api := range b.eth.Engine().APIs(b.eth.BlockChain()) {
if _api.Namespace == "bor" {
api = _api.Service.(*bor.API)
}
}
root, err := api.GetRootHash(starBlockNr.Int64(), endBlockNr.Int64())
root, err := api.GetRootHash(starBlockNr, endBlockNr)
if err != nil {
return nil, err
return "", err
}
return root, nil
}
9 changes: 9 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,15 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", common.ToHex(data))
}

// GetRootHash returns the merkle root of the block headers
func (ec *Client) GetRootHash(ctx context.Context, startBlockNumber uint64, endBlockNumber uint64) (string, error) {
var rootHash string
if err := ec.c.CallContext(ctx, &rootHash, "eth_getRootHash", startBlockNumber, endBlockNumber); err != nil {
return "", err
}
return rootHash, nil
}

func toCallArg(msg ethereum.CallMsg) interface{} {
arg := map[string]interface{}{
"from": msg.From,
Expand Down
4 changes: 2 additions & 2 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,10 +716,10 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A
return res[:], state.Error()
}

func (s *PublicBlockChainAPI) GetRootHash(ctx context.Context, starBlockNr rpc.BlockNumber, endBlockNr rpc.BlockNumber) ([]byte, error) {
func (s *PublicBlockChainAPI) GetRootHash(ctx context.Context, starBlockNr uint64, endBlockNr uint64) (string, error) {
root, err := s.b.GetRootHash(ctx, starBlockNr, endBlockNr)
if err != nil {
return nil, err
return "", err
}
return root, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Backend interface {
SubscribeStateEvent(ch chan<- core.NewStateChangeEvent) event.Subscription
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
GetRootHash(ctx context.Context, starBlockNr rpc.BlockNumber, endBlockNr rpc.BlockNumber) ([]byte, error)
GetRootHash(ctx context.Context, starBlockNr uint64, endBlockNr uint64) (string, error)

// Transaction pool API
SendTx(ctx context.Context, signedTx *types.Transaction) error
Expand Down
4 changes: 2 additions & 2 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,6 @@ func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma
}
}

func (b *LesApiBackend) GetRootHash(ctx context.Context, starBlockNr rpc.BlockNumber, endBlockNr rpc.BlockNumber) ([]byte, error) {
return nil, errors.New("Not implemented")
func (b *LesApiBackend) GetRootHash(ctx context.Context, starBlockNr uint64, endBlockNr uint64) (string, error) {
return "", errors.New("Not implemented")
}

0 comments on commit e03cd94

Please sign in to comment.