les: expose getCheckpoint API

This commit is contained in:
rjl493456442 2018-06-01 15:14:02 +08:00
parent 40626c7ff1
commit 6fb5e75e64
3 changed files with 40 additions and 10 deletions

View file

@ -636,12 +636,19 @@ web3._extend({
const LES_JS = ` const LES_JS = `
web3._extend({ web3._extend({
property: 'les', property: 'les',
methods: [], methods:
[
new web3._extend.Method({
name: 'getCheckpoint',
call: 'les_getCheckpoint',
params: 1
}),
],
properties: properties:
[ [
new web3._extend.Property({ new web3._extend.Property({
name: 'checkpoint', name: 'latestCheckpoint',
getter: 'les_checkpoint' getter: 'les_latestCheckpoint'
}), }),
] ]
}); });

View file

@ -48,9 +48,9 @@ func NewPublicLesServerAPI(server *LesServer) *PublicLesServerAPI {
// result[1], 32 bytes hex encoded latest section head hash // result[1], 32 bytes hex encoded latest section head hash
// result[2], 32 bytes hex encoded latest section canonical hash trie root 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 // 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 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{}) { if sectionHead == (common.Hash{}) || chtRoot == (common.Hash{}) || bloomTrieRoot == (common.Hash{}) {
return res, errNoStableCheckpoint 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() res[1], res[2], res[3] = sectionHead.Hex(), chtRoot.Hex(), bloomTrieRoot.Hex()
return res, nil 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
}

View file

@ -155,14 +155,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 // post-processed trie roots (CHT and BloomTrie) associated with
// the appropriate section index and head hash as a checkpoint package. // 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 // 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 // 4k section size for backwards server compatibility. For bloomTrie, the size
// of the section used for indexer is 32K. // 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() chtCount, _, _ := s.chtIndexer.Sections()
bloomTrieCount, _, _ := s.bloomTrieIndexer.Sections() bloomTrieCount, _, _ := s.bloomTrieIndexer.Sections()
count := chtCount / (light.CHTFrequencyClient / light.CHTFrequencyServer) count := chtCount / (light.CHTFrequencyClient / light.CHTFrequencyServer)
@ -174,13 +174,20 @@ func (s *LesServer) getCheckpoint() (uint64, common.Hash, common.Hash, common.Ha
// No checkpoint information can be provided. // No checkpoint information can be provided.
return 0, common.Hash{}, common.Hash{}, common.Hash{} 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 // 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) sectionHead := s.chtIndexer.SectionHead(latest)
chtRoot := light.GetChtRoot(s.protocolManager.chainDb, latest, sectionHead) chtRoot := light.GetChtRoot(s.protocolManager.chainDb, latest, sectionHead)
bloomTrieRoot := light.GetBloomTrieRoot(s.protocolManager.chainDb, count-1, sectionHead) bloomTrieRoot := light.GetBloomTrieRoot(s.protocolManager.chainDb, index, sectionHead)
return count - 1, sectionHead, chtRoot, bloomTrieRoot return sectionHead, chtRoot, bloomTrieRoot
} }
type requestCosts struct { type requestCosts struct {