good morning!!!!

Skip to content
Snippets Groups Projects
Commit bbe6522f authored by atvanguard's avatar atvanguard
Browse files

new: Add GetRootHash to api backend

parent 5bd72779
Branches
Tags
No related merge requests found
...@@ -17,9 +17,12 @@ ...@@ -17,9 +17,12 @@
package bor package bor
import ( import (
"math"
"math/big" "math/big"
"strconv"
"sync" "sync"
lru "github.com/hashicorp/golang-lru"
"github.com/maticnetwork/bor/common" "github.com/maticnetwork/bor/common"
"github.com/maticnetwork/bor/consensus" "github.com/maticnetwork/bor/consensus"
"github.com/maticnetwork/bor/core/types" "github.com/maticnetwork/bor/core/types"
...@@ -29,11 +32,17 @@ import ( ...@@ -29,11 +32,17 @@ import (
"golang.org/x/crypto/sha3" "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 // API is a user facing RPC API to allow controlling the signer and voting
// mechanisms of the proof-of-authority scheme. // mechanisms of the proof-of-authority scheme.
type API struct { type API struct {
chain consensus.ChainReader chain consensus.ChainReader
bor *Bor bor *Bor
rootHashCache *lru.ARCCache
} }
// GetSnapshot retrieves the state snapshot at a given block. // GetSnapshot retrieves the state snapshot at a given block.
...@@ -112,22 +121,38 @@ func (api *API) GetCurrentValidators() ([]*Validator, error) { ...@@ -112,22 +121,38 @@ func (api *API) GetCurrentValidators() ([]*Validator, error) {
return snap.ValidatorSet.Validators, nil return snap.ValidatorSet.Validators, nil
} }
// GetRootHash gets the current validators // GetRootHash returns the merkle root of the start to end block headers
func (api *API) GetRootHash(start uint64, end uint64) ([]byte, error) { 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) blockHeaders := make([]*types.Header, end-start+1)
wg := new(sync.WaitGroup) 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++ { for i := start; i <= end; i++ {
wg.Add(1) wg.Add(1)
go func(number uint64) { concurrent <- true
blockHeaders[number-start] = api.chain.GetHeaderByNumber(number) go func(number int64) {
blockHeaders[number-start] = api.chain.GetHeaderByNumber(uint64(number))
<-concurrent
wg.Done() wg.Done()
}(i) }(i)
} }
wg.Wait() wg.Wait()
close(concurrent)
expectedLength := nextPowerOfTwo(end - start + 1) headers := make([][32]byte, nextPowerOfTwo(length))
headers := make([][32]byte, expectedLength)
for i := 0; i < len(blockHeaders); i++ { for i := 0; i < len(blockHeaders); i++ {
blockHeader := blockHeaders[i] blockHeader := blockHeaders[i]
header := crypto.Keccak256(appendBytes32( header := crypto.Keccak256(appendBytes32(
...@@ -146,6 +171,24 @@ func (api *API) GetRootHash(start uint64, end uint64) ([]byte, error) { ...@@ -146,6 +171,24 @@ func (api *API) GetRootHash(start uint64, end uint64) ([]byte, error) {
if err := tree.Generate(convert(headers), sha3.NewLegacyKeccak256()); err != nil { if err := tree.Generate(convert(headers), sha3.NewLegacyKeccak256()); err != nil {
return nil, err return nil, err
} }
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
}
return tree.Root().Hash, nil func getKey(start int64, end int64) string {
return strconv.FormatInt(start, 10) + "-" + strconv.FormatInt(end, 10)
} }
...@@ -40,3 +40,31 @@ func (e *TotalVotingPowerExceededError) Error() string { ...@@ -40,3 +40,31 @@ func (e *TotalVotingPowerExceededError) Error() string {
e.Validators, 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,
)
}
...@@ -248,3 +248,17 @@ func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma ...@@ -248,3 +248,17 @@ func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma
go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 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
}
...@@ -716,6 +716,14 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A ...@@ -716,6 +716,14 @@ 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) {
root, err := s.b.GetRootHash(ctx, starBlockNr, endBlockNr)
if err != nil {
return nil, err
}
return root, nil
}
// CallArgs represents the arguments for a call. // CallArgs represents the arguments for a call.
type CallArgs struct { type CallArgs struct {
From *common.Address `json:"from"` From *common.Address `json:"from"`
......
...@@ -62,6 +62,7 @@ type Backend interface { ...@@ -62,6 +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)
// Transaction pool API // Transaction pool API
SendTx(ctx context.Context, signedTx *types.Transaction) error SendTx(ctx context.Context, signedTx *types.Transaction) error
......
...@@ -223,3 +223,7 @@ func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma ...@@ -223,3 +223,7 @@ func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma
go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 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")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment