diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index a4e95b2978..88675a9659 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -636,12 +636,19 @@ web3._extend({ const LES_JS = ` web3._extend({ property: 'les', - methods: [], + methods: + [ + new web3._extend.Method({ + name: 'getCheckpoint', + call: 'les_getCheckpoint', + params: 1 + }), + ], properties: [ new web3._extend.Property({ - name: 'checkpoint', - getter: 'les_checkpoint' + name: 'latestCheckpoint', + getter: 'les_latestCheckpoint' }), ] }); diff --git a/les/api.go b/les/api.go index 6987031913..b58cf0fc82 100644 --- a/les/api.go +++ b/les/api.go @@ -48,9 +48,9 @@ func NewPublicLesServerAPI(server *LesServer) *PublicLesServerAPI { // result[1], 32 bytes hex encoded latest section head hash // result[2], 32 bytes hex encoded latest section canonical hash trie root hash // result[3], 32 bytes hex encoded latest section bloom trie root hash -func (api *PublicLesServerAPI) Checkpoint() ([4]string, error) { +func (api *PublicLesServerAPI) LatestCheckpoint() ([4]string, error) { var res [4]string - sectionIdx, sectionHead, chtRoot, bloomTrieRoot := api.server.getCheckpoint() + sectionIdx, sectionHead, chtRoot, bloomTrieRoot := api.server.latestCheckpoint() if sectionHead == (common.Hash{}) || chtRoot == (common.Hash{}) || bloomTrieRoot == (common.Hash{}) { return res, errNoStableCheckpoint } @@ -58,3 +58,19 @@ func (api *PublicLesServerAPI) Checkpoint() ([4]string, error) { res[1], res[2], res[3] = sectionHead.Hex(), chtRoot.Hex(), bloomTrieRoot.Hex() return res, nil } + +// GetCheckpoint returns the specific checkpoint package. +// +// The checkpoint package consists of 3 strings: +// result[0], 32 bytes hex encoded latest section head hash +// result[1], 32 bytes hex encoded latest section canonical hash trie root hash +// result[2], 32 bytes hex encoded latest section bloom trie root hash +func (api *PublicLesServerAPI) GetCheckpoint(index uint64) ([3]string, error) { + var res [3]string + sectionHead, chtRoot, bloomTrieRoot := api.server.getCheckpoint(index) + if sectionHead == (common.Hash{}) || chtRoot == (common.Hash{}) || bloomTrieRoot == (common.Hash{}) { + return res, errNoStableCheckpoint + } + res[0], res[1], res[2] = sectionHead.Hex(), chtRoot.Hex(), bloomTrieRoot.Hex() + return res, nil +} diff --git a/les/server.go b/les/server.go index f3182b68ff..7d935d1a31 100644 --- a/les/server.go +++ b/les/server.go @@ -169,14 +169,14 @@ func (s *LesServer) APIs() []rpc.API { } } -// getCheckpoint finds the common stored section index and returns a set of +// latestCheckpoint finds the common stored section index and returns a set of // post-processed trie roots (CHT and BloomTrie) associated with // the appropriate section index and head hash as a checkpoint package. // // Note for cht, the section size in LES1 is 4K, so indexer still uses LES/1 // 4k section size for backwards server compatibility. For bloomTrie, the size // of the section used for indexer is 32K. -func (s *LesServer) getCheckpoint() (uint64, common.Hash, common.Hash, common.Hash) { +func (s *LesServer) latestCheckpoint() (uint64, common.Hash, common.Hash, common.Hash) { chtCount, _, _ := s.chtIndexer.Sections() bloomTrieCount, _, _ := s.bloomTrieIndexer.Sections() count := chtCount / (light.CHTFrequencyClient / light.CHTFrequencyServer) @@ -188,13 +188,20 @@ func (s *LesServer) getCheckpoint() (uint64, common.Hash, common.Hash, common.Ha // No checkpoint information can be provided. return 0, common.Hash{}, common.Hash{}, common.Hash{} } + sectionHead, chtRoot, bloomTrieRoot := s.getCheckpoint(count - 1) + return count - 1, sectionHead, chtRoot, bloomTrieRoot +} + +// getCheckpoint returns a set of post-processed trie roots (CHT and BloomTrie) +// associated with the appropriate head hash by specific section index. +func (s *LesServer) getCheckpoint(index uint64) (common.Hash, common.Hash, common.Hash) { // convert last LES/2 section index back to LES/1 index for chtIndexer.SectionHead - latest := count*(light.CHTFrequencyClient/light.CHTFrequencyServer) - 1 + latest := (index+1)*(light.CHTFrequencyClient/light.CHTFrequencyServer) - 1 sectionHead := s.chtIndexer.SectionHead(latest) chtRoot := light.GetChtRoot(s.protocolManager.chainDb, latest, sectionHead) - bloomTrieRoot := light.GetBloomTrieRoot(s.protocolManager.chainDb, count-1, sectionHead) - return count - 1, sectionHead, chtRoot, bloomTrieRoot + bloomTrieRoot := light.GetBloomTrieRoot(s.protocolManager.chainDb, index, sectionHead) + return sectionHead, chtRoot, bloomTrieRoot } // checkpointLoop starts a standalone goroutine to watch new checkpoint event and updates local's stable checkpoint.