good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit e03cd94e authored by Arpit Agarwal's avatar Arpit Agarwal Committed by GitHub
Browse files

Merge pull request #53 from maticnetwork/dev-update-ethclient

chg: Add GetRootHash to ethClient / use uint64
parents 27f9dbd1 67c29d7c
Branches
Tags
No related merge requests found
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package bor package bor
import ( import (
"encoding/hex"
"math" "math"
"math/big" "math/big"
"strconv" "strconv"
...@@ -122,21 +123,21 @@ func (api *API) GetCurrentValidators() ([]*Validator, error) { ...@@ -122,21 +123,21 @@ func (api *API) GetCurrentValidators() ([]*Validator, error) {
} }
// GetRootHash returns the merkle root of the start to end block headers // 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 { if err := api.initializeRootHashCache(); err != nil {
return nil, err return "", err
} }
key := getRootHashKey(start, end) key := getRootHashKey(start, end)
if root, known := api.rootHashCache.Get(key); known { if root, known := api.rootHashCache.Get(key); known {
return root.([]byte), nil return root.(string), nil
} }
length := uint64(end - start + 1) length := uint64(end - start + 1)
if length > MaxCheckpointLength { 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 { if start > end || end > currentHeaderNumber {
return nil, &InvalidStartEndBlockError{start, end, currentHeaderNumber} return "", &InvalidStartEndBlockError{start, end, currentHeaderNumber}
} }
blockHeaders := make([]*types.Header, end-start+1) blockHeaders := make([]*types.Header, end-start+1)
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
...@@ -144,7 +145,7 @@ func (api *API) GetRootHash(start int64, end int64) ([]byte, error) { ...@@ -144,7 +145,7 @@ func (api *API) GetRootHash(start int64, end int64) ([]byte, error) {
for i := start; i <= end; i++ { for i := start; i <= end; i++ {
wg.Add(1) wg.Add(1)
concurrent <- true concurrent <- true
go func(number int64) { go func(number uint64) {
blockHeaders[number-start] = api.chain.GetHeaderByNumber(uint64(number)) blockHeaders[number-start] = api.chain.GetHeaderByNumber(uint64(number))
<-concurrent <-concurrent
wg.Done() wg.Done()
...@@ -170,9 +171,9 @@ func (api *API) GetRootHash(start int64, end int64) ([]byte, error) { ...@@ -170,9 +171,9 @@ func (api *API) GetRootHash(start int64, end int64) ([]byte, error) {
tree := merkle.NewTreeWithOpts(merkle.TreeOptions{EnableHashSorting: false, DisableHashLeaves: true}) tree := merkle.NewTreeWithOpts(merkle.TreeOptions{EnableHashSorting: false, DisableHashLeaves: true})
if err := tree.Generate(convert(headers), sha3.NewLegacyKeccak256()); err != nil { 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) api.rootHashCache.Add(key, root)
return root, nil return root, nil
} }
...@@ -185,6 +186,6 @@ func (api *API) initializeRootHashCache() error { ...@@ -185,6 +186,6 @@ func (api *API) initializeRootHashCache() error {
return err return err
} }
func getRootHashKey(start int64, end int64) string { func getRootHashKey(start uint64, end uint64) string {
return strconv.FormatInt(start, 10) + "-" + strconv.FormatInt(end, 10) return strconv.FormatUint(start, 10) + "-" + strconv.FormatUint(end, 10)
} }
...@@ -42,9 +42,9 @@ func (e *TotalVotingPowerExceededError) Error() string { ...@@ -42,9 +42,9 @@ func (e *TotalVotingPowerExceededError) Error() string {
} }
type InvalidStartEndBlockError struct { type InvalidStartEndBlockError struct {
Start int64 Start uint64
End int64 End uint64
CurrentHeader int64 CurrentHeader uint64
} }
func (e *InvalidStartEndBlockError) Error() string { func (e *InvalidStartEndBlockError) Error() string {
...@@ -56,8 +56,8 @@ func (e *InvalidStartEndBlockError) Error() string { ...@@ -56,8 +56,8 @@ func (e *InvalidStartEndBlockError) Error() string {
} }
type MaxCheckpointLengthExceededError struct { type MaxCheckpointLengthExceededError struct {
Start int64 Start uint64
End int64 End uint64
} }
func (e *MaxCheckpointLengthExceededError) Error() string { func (e *MaxCheckpointLengthExceededError) Error() string {
......
...@@ -249,16 +249,16 @@ func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma ...@@ -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 var api *bor.API
for _, _api := range b.eth.Engine().APIs(b.eth.BlockChain()) { for _, _api := range b.eth.Engine().APIs(b.eth.BlockChain()) {
if _api.Namespace == "bor" { if _api.Namespace == "bor" {
api = _api.Service.(*bor.API) api = _api.Service.(*bor.API)
} }
} }
root, err := api.GetRootHash(starBlockNr.Int64(), endBlockNr.Int64()) root, err := api.GetRootHash(starBlockNr, endBlockNr)
if err != nil { if err != nil {
return nil, err return "", err
} }
return root, nil return root, nil
} }
...@@ -522,6 +522,15 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er ...@@ -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)) 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{} { func toCallArg(msg ethereum.CallMsg) interface{} {
arg := map[string]interface{}{ arg := map[string]interface{}{
"from": msg.From, "from": msg.From,
......
...@@ -716,10 +716,10 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A ...@@ -716,10 +716,10 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A
return res[:], state.Error() 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) root, err := s.b.GetRootHash(ctx, starBlockNr, endBlockNr)
if err != nil { if err != nil {
return nil, err return "", err
} }
return root, nil return root, nil
} }
......
...@@ -62,7 +62,7 @@ type Backend interface { ...@@ -62,7 +62,7 @@ type Backend interface {
SubscribeStateEvent(ch chan<- core.NewStateChangeEvent) event.Subscription SubscribeStateEvent(ch chan<- core.NewStateChangeEvent) event.Subscription
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) 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 // Transaction pool API
SendTx(ctx context.Context, signedTx *types.Transaction) error SendTx(ctx context.Context, signedTx *types.Transaction) error
......
...@@ -224,6 +224,6 @@ func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma ...@@ -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) { func (b *LesApiBackend) GetRootHash(ctx context.Context, starBlockNr uint64, endBlockNr uint64) (string, error) {
return nil, errors.New("Not implemented") return "", errors.New("Not implemented")
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment