les: expose les API as private API service

This commit is contained in:
rjl493456442 2018-07-11 16:48:08 +08:00
parent f923674365
commit ca0ac58f18
2 changed files with 38 additions and 34 deletions

View file

@ -25,51 +25,50 @@ import (
) )
var ( var (
errNoStableCheckpoint = errors.New("no stable checkpoint provided") errNoCheckpoint = errors.New("no local checkpoint provided")
) )
// PublicLesServerAPI provides an API to access the les server. // PrivateLesServerAPI provides a private API to access the les server.
// It offers only methods that operate on public data that is freely available to anyone. type PrivateLesServerAPI struct {
type PublicLesServerAPI struct {
server *LesServer server *LesServer
} }
// NewPublicLesServerAPI creates a new les server API. // NewPrivateLesServerAPI creates a new les server API.
func NewPublicLesServerAPI(server *LesServer) *PublicLesServerAPI { func NewPrivateLesServerAPI(server *LesServer) *PrivateLesServerAPI {
return &PublicLesServerAPI{ return &PrivateLesServerAPI{
server: server, server: server,
} }
} }
// Checkpoint returns the latest checkpoint package. // Checkpoint returns the latest local checkpoint package.
// //
// The checkpoint package consists of 4 strings: // The checkpoint package consists of 4 strings:
// result[0], hex encoded latest section index // result[0], hex encoded latest section index
// 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) LatestCheckpoint() ([4]string, error) { func (api *PrivateLesServerAPI) LatestCheckpoint() ([4]string, error) {
var res [4]string var res [4]string
sectionIdx, sectionHead, chtRoot, bloomTrieRoot := api.server.latestCheckpoint() 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, errNoCheckpoint
} }
res[0] = hexutil.Encode(big.NewInt(int64(sectionIdx)).Bytes()) res[0] = hexutil.Encode(big.NewInt(int64(sectionIdx)).Bytes())
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. // GetCheckpoint returns the specific local checkpoint package.
// //
// The checkpoint package consists of 3 strings: // The checkpoint package consists of 3 strings:
// result[0], 32 bytes hex encoded latest section head hash // result[0], 32 bytes hex encoded latest section head hash
// result[1], 32 bytes hex encoded latest section canonical hash trie root 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 // result[2], 32 bytes hex encoded latest section bloom trie root hash
func (api *PublicLesServerAPI) GetCheckpoint(index uint64) ([3]string, error) { func (api *PrivateLesServerAPI) GetCheckpoint(index uint64) ([3]string, error) {
var res [3]string var res [3]string
sectionHead, chtRoot, bloomTrieRoot := api.server.getCheckpoint(index) sectionHead, chtRoot, bloomTrieRoot := api.server.getCheckpoint(index)
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, errNoCheckpoint
} }
res[0], res[1], res[2] = sectionHead.Hex(), chtRoot.Hex(), bloomTrieRoot.Hex() res[0], res[1], res[2] = sectionHead.Hex(), chtRoot.Hex(), bloomTrieRoot.Hex()
return res, nil return res, nil

View file

@ -188,15 +188,15 @@ func (s *LesServer) APIs() []rpc.API {
{ {
Namespace: "les", Namespace: "les",
Version: "1.0", Version: "1.0",
Service: NewPublicLesServerAPI(s), Service: NewPrivateLesServerAPI(s),
Public: true, Public: false,
}, },
} }
} }
// latestCheckpoint 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 local 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
@ -219,6 +219,9 @@ func (s *LesServer) latestCheckpoint() (uint64, common.Hash, common.Hash, common
// getCheckpoint returns a set of post-processed trie roots (CHT and BloomTrie) // getCheckpoint returns a set of post-processed trie roots (CHT and BloomTrie)
// associated with the appropriate head hash by specific section index. // associated with the appropriate head hash by specific section index.
//
// The returned checkpoint is only the checkpoint generated by the local indexers,
// not the stable checkpoint registered in the registrar contract.
func (s *LesServer) getCheckpoint(index uint64) (common.Hash, common.Hash, common.Hash) { 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 := (index+1)*(light.CHTFrequencyClient/light.CHTFrequencyServer) - 1 latest := (index+1)*(light.CHTFrequencyClient/light.CHTFrequencyServer) - 1
@ -315,6 +318,7 @@ func (s *LesServer) recoverCheckpoint() *light.TrustedCheckpoint {
headHash = rawdb.ReadHeadHeaderHash(s.chaindb) headHash = rawdb.ReadHeadHeaderHash(s.chaindb)
headNumber = rawdb.ReadHeaderNumber(s.chaindb, headHash) headNumber = rawdb.ReadHeaderNumber(s.chaindb, headHash)
) )
// Short circuit if there is no local checkpoint generated.
if headNumber == nil || sectionCnt == 0 { if headNumber == nil || sectionCnt == 0 {
return nil return nil
} }
@ -322,24 +326,25 @@ func (s *LesServer) recoverCheckpoint() *light.TrustedCheckpoint {
for stable == nil || stable.SectionIdx < unstableIdx { for stable == nil || stable.SectionIdx < unstableIdx {
if (unstableIdx+1)*light.CheckpointFrequency+light.CheckpointConfirmations <= *headNumber { if (unstableIdx+1)*light.CheckpointFrequency+light.CheckpointConfirmations <= *headNumber {
iter, err := s.registrar.FilterNewCheckpointEvent(*headNumber, unstableIdx, light.CheckpointFrequency, light.CheckpointProcessConfirmations) iter, err := s.registrar.FilterNewCheckpointEvent(*headNumber, unstableIdx, light.CheckpointFrequency, light.CheckpointProcessConfirmations)
if err == nil { if err != nil {
for iter.Next() { continue
sectionHead := s.bloomTrieIndexer.SectionHead(unstableIdx)
checkpoint := &light.TrustedCheckpoint{
SectionIdx: unstableIdx,
SectionHead: sectionHead,
ChtRoot: light.GetChtV2Root(s.chaindb, unstableIdx, sectionHead),
BloomTrieRoot: light.GetBloomTrieRoot(s.chaindb, unstableIdx, sectionHead),
}
if checkpoint.HashEqual(common.Hash(iter.Event.CheckpointHash)) {
light.WriteTrustedCheckpoint(s.chaindb, checkpoint)
iter.Close()
log.Info("Recover checkpoint", "index", checkpoint.SectionIdx, "hash", checkpoint.Hash().Hex())
return checkpoint
}
}
iter.Close()
} }
for iter.Next() {
sectionHead := s.bloomTrieIndexer.SectionHead(unstableIdx)
checkpoint := &light.TrustedCheckpoint{
SectionIdx: unstableIdx,
SectionHead: sectionHead,
ChtRoot: light.GetChtV2Root(s.chaindb, unstableIdx, sectionHead),
BloomTrieRoot: light.GetBloomTrieRoot(s.chaindb, unstableIdx, sectionHead),
}
if checkpoint.HashEqual(common.Hash(iter.Event.CheckpointHash)) {
light.WriteTrustedCheckpoint(s.chaindb, checkpoint)
iter.Close()
log.Info("Recover stable checkpoint", "index", checkpoint.SectionIdx, "hash", checkpoint.Hash().Hex())
return checkpoint
}
}
iter.Close()
} }
if unstableIdx == 0 { if unstableIdx == 0 {
break break
@ -349,7 +354,7 @@ func (s *LesServer) recoverCheckpoint() *light.TrustedCheckpoint {
if stable == nil { if stable == nil {
log.Info("No stable checkpoint") log.Info("No stable checkpoint")
} else { } else {
log.Info("Recover checkpoint", "index", stable.SectionIdx, "hash", stable.Hash().Hex()) log.Info("Recover stable checkpoint", "index", stable.SectionIdx, "hash", stable.Hash().Hex())
} }
return stable return stable
} }