new: Add GetRootHash to api backend

This commit is contained in:
atvanguard 2020-05-05 14:45:56 +05:30
parent 5bd72779d5
commit bbe6522f35
6 changed files with 109 additions and 11 deletions

View file

@ -17,9 +17,12 @@
package bor
import (
"math"
"math/big"
"strconv"
"sync"
lru "github.com/hashicorp/golang-lru"
"github.com/maticnetwork/bor/common"
"github.com/maticnetwork/bor/consensus"
"github.com/maticnetwork/bor/core/types"
@ -29,11 +32,17 @@ import (
"golang.org/x/crypto/sha3"
)
var (
// MaxCheckpointLength is the maximum number of blocks that can be requested for constructing a checkpoint root hash
MaxCheckpointLength = uint64(math.Pow(2, 15))
)
// API is a user facing RPC API to allow controlling the signer and voting
// mechanisms of the proof-of-authority scheme.
type API struct {
chain consensus.ChainReader
bor *Bor
chain consensus.ChainReader
bor *Bor
rootHashCache *lru.ARCCache
}
// GetSnapshot retrieves the state snapshot at a given block.
@ -112,22 +121,38 @@ func (api *API) GetCurrentValidators() ([]*Validator, error) {
return snap.ValidatorSet.Validators, nil
}
// GetRootHash gets the current validators
func (api *API) GetRootHash(start uint64, end uint64) ([]byte, error) {
// GetRootHash returns the merkle root of the start to end block headers
func (api *API) GetRootHash(start int64, end int64) ([]byte, error) {
key := getKey(start, end)
if root, err := api.lookupCache(key); err != nil {
return nil, err
} else if root != nil {
return root, nil
}
length := uint64(end - start + 1)
if length > MaxCheckpointLength {
return nil, &MaxCheckpointLengthExceededError{start, end}
}
currentHeaderNumber := api.chain.CurrentHeader().Number.Int64()
if start > end || end > currentHeaderNumber {
return nil, &InvalidStartEndBlockError{start, end, currentHeaderNumber}
}
blockHeaders := make([]*types.Header, end-start+1)
wg := new(sync.WaitGroup)
// do we want to limit the # of concurrent go routines?
concurrent := make(chan bool, 20)
for i := start; i <= end; i++ {
wg.Add(1)
go func(number uint64) {
blockHeaders[number-start] = api.chain.GetHeaderByNumber(number)
concurrent <- true
go func(number int64) {
blockHeaders[number-start] = api.chain.GetHeaderByNumber(uint64(number))
<-concurrent
wg.Done()
}(i)
}
wg.Wait()
close(concurrent)
expectedLength := nextPowerOfTwo(end - start + 1)
headers := make([][32]byte, expectedLength)
headers := make([][32]byte, nextPowerOfTwo(length))
for i := 0; i < len(blockHeaders); i++ {
blockHeader := blockHeaders[i]
header := crypto.Keccak256(appendBytes32(
@ -146,6 +171,24 @@ func (api *API) GetRootHash(start uint64, end uint64) ([]byte, error) {
if err := tree.Generate(convert(headers), sha3.NewLegacyKeccak256()); err != nil {
return nil, err
}
return tree.Root().Hash, nil
root := tree.Root().Hash
api.rootHashCache.Add(key, root)
return root, nil
}
func (api *API) lookupCache(key string) ([]byte, error) {
var err error
if api.rootHashCache == nil {
if api.rootHashCache, err = lru.NewARC(10); err != nil {
return nil, err
}
}
if root, known := api.rootHashCache.Get(key); known {
return root.([]byte), nil
}
return nil, nil
}
func getKey(start int64, end int64) string {
return strconv.FormatInt(start, 10) + "-" + strconv.FormatInt(end, 10)
}

View file

@ -40,3 +40,31 @@ func (e *TotalVotingPowerExceededError) Error() string {
e.Validators,
)
}
type InvalidStartEndBlockError struct {
Start int64
End int64
CurrentHeader int64
}
func (e *InvalidStartEndBlockError) Error() string {
return fmt.Sprintf(
"Invalid parameters start: %d and end block: %d params",
e.Start,
e.End,
)
}
type MaxCheckpointLengthExceededError struct {
Start int64
End int64
}
func (e *MaxCheckpointLengthExceededError) Error() string {
return fmt.Sprintf(
"Start: %d and end block: %d exceed max allowed checkpoint length: %d",
e.Start,
e.End,
MaxCheckpointLength,
)
}

View file

@ -248,3 +248,17 @@ func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma
go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
}
}
func (b *EthAPIBackend) GetRootHash(ctx context.Context, starBlockNr rpc.BlockNumber, endBlockNr rpc.BlockNumber) ([]byte, 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())
if err != nil {
return nil, err
}
return root, nil
}

View file

@ -716,6 +716,14 @@ 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) {
root, err := s.b.GetRootHash(ctx, starBlockNr, endBlockNr)
if err != nil {
return nil, err
}
return root, nil
}
// CallArgs represents the arguments for a call.
type CallArgs struct {
From *common.Address `json:"from"`

View file

@ -62,6 +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)
// Transaction pool API
SendTx(ctx context.Context, signedTx *types.Transaction) error

View file

@ -223,3 +223,7 @@ func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma
go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
}
}
func (b *LesApiBackend) GetRootHash(ctx context.Context, starBlockNr rpc.BlockNumber, endBlockNr rpc.BlockNumber) ([]byte, error) {
return nil, errors.New("Not implemented")
}