This commit is contained in:
gary rong 2018-06-07 10:07:17 +00:00 committed by GitHub
commit f6b43a157f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 0 deletions

View file

@ -54,6 +54,7 @@ import (
type LesServer interface { type LesServer interface {
Start(srvr *p2p.Server) Start(srvr *p2p.Server)
Stop() Stop()
APIs() []rpc.API
Protocols() []p2p.Protocol Protocols() []p2p.Protocol
SetBloomBitsIndexer(bbIndexer *core.ChainIndexer) SetBloomBitsIndexer(bbIndexer *core.ChainIndexer)
} }
@ -247,6 +248,11 @@ func (s *Ethereum) APIs() []rpc.API {
// Append any APIs exposed explicitly by the consensus engine // Append any APIs exposed explicitly by the consensus engine
apis = append(apis, s.engine.APIs(s.BlockChain())...) apis = append(apis, s.engine.APIs(s.BlockChain())...)
// Append any APIs exposed explicitly by the les server
if s.lesServer != nil {
apis = append(apis, s.lesServer.APIs()...)
}
// Append all the local APIs and return // Append all the local APIs and return
return append(apis, []rpc.API{ return append(apis, []rpc.API{
{ {

View file

@ -30,6 +30,7 @@ var Modules = map[string]string{
"shh": Shh_JS, "shh": Shh_JS,
"swarmfs": SWARMFS_JS, "swarmfs": SWARMFS_JS,
"txpool": TxPool_JS, "txpool": TxPool_JS,
"les": LES_JS,
} }
const Chequebook_JS = ` const Chequebook_JS = `
@ -631,3 +632,24 @@ web3._extend({
] ]
}); });
` `
const LES_JS = `
web3._extend({
property: 'les',
methods:
[
new web3._extend.Method({
name: 'getCheckpoint',
call: 'les_getCheckpoint',
params: 1
}),
],
properties:
[
new web3._extend.Property({
name: 'latestCheckpoint',
getter: 'les_latestCheckpoint'
}),
]
});
`

76
les/api.go Normal file
View file

@ -0,0 +1,76 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package les
import (
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
var (
errNoStableCheckpoint = errors.New("no stable checkpoint provided")
)
// PublicLesServerAPI provides an API to access the les server.
// It offers only methods that operate on public data that is freely available to anyone.
type PublicLesServerAPI struct {
server *LesServer
}
// NewPublicLesServerAPI creates a new les server API.
func NewPublicLesServerAPI(server *LesServer) *PublicLesServerAPI {
return &PublicLesServerAPI{
server: server,
}
}
// Checkpoint returns the latest checkpoint package.
//
// The checkpoint package consists of 4 strings:
// result[0], hex encoded latest section index
// 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) LatestCheckpoint() ([4]string, error) {
var res [4]string
sectionIdx, sectionHead, chtRoot, bloomTrieRoot := api.server.latestCheckpoint()
if sectionHead == (common.Hash{}) || chtRoot == (common.Hash{}) || bloomTrieRoot == (common.Hash{}) {
return res, errNoStableCheckpoint
}
res[0] = hexutil.Encode(big.NewInt(int64(sectionIdx)).Bytes())
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
}

View file

@ -35,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discv5" "github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
) )
type LesServer struct { type LesServer struct {
@ -142,6 +143,53 @@ func (s *LesServer) Stop() {
s.protocolManager.Stop() s.protocolManager.Stop()
} }
// APIs implements LesServer, returns all API service provided by les server.
func (s *LesServer) APIs() []rpc.API {
return []rpc.API{
{
Namespace: "les",
Version: "1.0",
Service: NewPublicLesServerAPI(s),
Public: true,
},
}
}
// 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) latestCheckpoint() (uint64, common.Hash, common.Hash, common.Hash) {
chtCount, _, _ := s.chtIndexer.Sections()
bloomTrieCount, _, _ := s.bloomTrieIndexer.Sections()
count := chtCount / (light.CHTFrequencyClient / light.CHTFrequencyServer)
// Cap the section index if the two sections are not consistent.
if count > bloomTrieCount {
count = bloomTrieCount
}
if count == 0 {
// 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 := (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, index, sectionHead)
return sectionHead, chtRoot, bloomTrieRoot
}
type requestCosts struct { type requestCosts struct {
baseCost, reqCost uint64 baseCost, reqCost uint64
} }