good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit 27f9dbd1 authored by Jaynti Kanani's avatar Jaynti Kanani Committed by GitHub
Browse files

Merge pull request #52 from maticnetwork/dev-roothash

new: Add bor.getRootHash(start, end) api method (MAT-1302)
parents 88e5d4cb 50c0b662
No related branches found
No related tags found
No related merge requests found
...@@ -17,10 +17,24 @@ ...@@ -17,10 +17,24 @@
package bor package bor
import ( import (
"math"
"math/big"
"strconv"
"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"
"github.com/maticnetwork/bor/crypto"
"github.com/maticnetwork/bor/rpc" "github.com/maticnetwork/bor/rpc"
"github.com/xsleonard/go-merkle"
"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
...@@ -28,6 +42,7 @@ import ( ...@@ -28,6 +42,7 @@ import (
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.
...@@ -105,3 +120,71 @@ func (api *API) GetCurrentValidators() ([]*Validator, error) { ...@@ -105,3 +120,71 @@ func (api *API) GetCurrentValidators() ([]*Validator, error) {
} }
return snap.ValidatorSet.Validators, nil return snap.ValidatorSet.Validators, nil
} }
// GetRootHash returns the merkle root of the start to end block headers
func (api *API) GetRootHash(start int64, end int64) ([]byte, error) {
if err := api.initializeRootHashCache(); err != nil {
return nil, err
}
key := getRootHashKey(start, end)
if root, known := api.rootHashCache.Get(key); known {
return root.([]byte), 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)
concurrent := make(chan bool, 20)
for i := start; i <= end; i++ {
wg.Add(1)
concurrent <- true
go func(number int64) {
blockHeaders[number-start] = api.chain.GetHeaderByNumber(uint64(number))
<-concurrent
wg.Done()
}(i)
}
wg.Wait()
close(concurrent)
headers := make([][32]byte, nextPowerOfTwo(length))
for i := 0; i < len(blockHeaders); i++ {
blockHeader := blockHeaders[i]
header := crypto.Keccak256(appendBytes32(
blockHeader.Number.Bytes(),
new(big.Int).SetUint64(blockHeader.Time).Bytes(),
blockHeader.TxHash.Bytes(),
blockHeader.ReceiptHash.Bytes(),
))
var arr [32]byte
copy(arr[:], header)
headers[i] = arr
}
tree := merkle.NewTreeWithOpts(merkle.TreeOptions{EnableHashSorting: false, DisableHashLeaves: true})
if err := tree.Generate(convert(headers), sha3.NewLegacyKeccak256()); err != nil {
return nil, err
}
root := tree.Root().Hash
api.rootHashCache.Add(key, root)
return root, nil
}
func (api *API) initializeRootHashCache() error {
var err error
if api.rootHashCache == nil {
api.rootHashCache, err = lru.NewARC(10)
}
return err
}
func getRootHashKey(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,
)
}
package bor
func appendBytes32(data ...[]byte) []byte {
var result []byte
for _, v := range data {
paddedV, err := convertTo32(v)
if err == nil {
result = append(result, paddedV[:]...)
}
}
return result
}
func nextPowerOfTwo(n uint64) uint64 {
if n == 0 {
return 1
}
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n |= n >> 32
n++
return n
}
func convertTo32(input []byte) (output [32]byte, err error) {
l := len(input)
if l > 32 || l == 0 {
return
}
copy(output[32-l:], input[:])
return
}
func convert(input []([32]byte)) [][]byte {
var output [][]byte
for _, in := range input {
newInput := make([]byte, len(in[:]))
copy(newInput, in[:])
output = append(output, newInput)
}
return output
}
...@@ -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
}
...@@ -60,6 +60,7 @@ require ( ...@@ -60,6 +60,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208
github.com/xsleonard/go-merkle v1.1.0
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4
golang.org/x/net v0.0.0-20200301022130-244492dfa37a golang.org/x/net v0.0.0-20200301022130-244492dfa37a
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
......
...@@ -176,6 +176,8 @@ github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZ ...@@ -176,6 +176,8 @@ github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZ
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs= github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 h1:1cngl9mPEoITZG8s8cVcUy5CeIBYhEESkOB7m6Gmkrk= github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 h1:1cngl9mPEoITZG8s8cVcUy5CeIBYhEESkOB7m6Gmkrk=
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees= github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees=
github.com/xsleonard/go-merkle v1.1.0 h1:fHe1fuhJjGH22ZzVTAH0jqHLhTGhOq3wQjJN+8P0jQg=
github.com/xsleonard/go-merkle v1.1.0/go.mod h1:cW4z+UZ/4f2n9IJgIiyDCdYguchoDyDAPmpuOWGxdGg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 h1:QmwruyY+bKbDDL0BaglrbZABEali68eoMFhTZpCjYVA= golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 h1:QmwruyY+bKbDDL0BaglrbZABEali68eoMFhTZpCjYVA=
......
...@@ -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
......
...@@ -149,6 +149,11 @@ web3._extend({ ...@@ -149,6 +149,11 @@ web3._extend({
call: 'bor_getCurrentValidators', call: 'bor_getCurrentValidators',
params: 0 params: 0
}), }),
new web3._extend.Method({
name: 'getRootHash',
call: 'bor_getRootHash',
params: 2,
}),
] ]
}); });
` `
......
...@@ -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