mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
les: expose les server API
This commit is contained in:
parent
38c7eb0f26
commit
40626c7ff1
4 changed files with 122 additions and 0 deletions
|
|
@ -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{
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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,17 @@ web3._extend({
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const LES_JS = `
|
||||||
|
web3._extend({
|
||||||
|
property: 'les',
|
||||||
|
methods: [],
|
||||||
|
properties:
|
||||||
|
[
|
||||||
|
new web3._extend.Property({
|
||||||
|
name: 'checkpoint',
|
||||||
|
getter: 'les_checkpoint'
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
});
|
||||||
|
`
|
||||||
|
|
|
||||||
60
les/api.go
Normal file
60
les/api.go
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
// 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) Checkpoint() ([4]string, error) {
|
||||||
|
var res [4]string
|
||||||
|
sectionIdx, sectionHead, chtRoot, bloomTrieRoot := api.server.getCheckpoint()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
@ -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,46 @@ 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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getCheckpoint 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) {
|
||||||
|
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{}
|
||||||
|
}
|
||||||
|
// convert last LES/2 section index back to LES/1 index for chtIndexer.SectionHead
|
||||||
|
latest := count*(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
|
||||||
|
}
|
||||||
|
|
||||||
type requestCosts struct {
|
type requestCosts struct {
|
||||||
baseCost, reqCost uint64
|
baseCost, reqCost uint64
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue