mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
chg: Add GetRootHash to ethClient / use uint64
This commit is contained in:
parent
27f9dbd14f
commit
67c29d7ce3
7 changed files with 34 additions and 24 deletions
|
|
@ -17,6 +17,7 @@
|
|||
package bor
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"math"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
|
@ -122,21 +123,21 @@ 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)
|
||||
|
|
@ -144,7 +145,7 @@ func (api *API) GetRootHash(start int64, end int64) ([]byte, error) {
|
|||
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()
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue