diff --git a/eth/backend.go b/eth/backend.go index e07d5efc9a..4ffbc76161 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -54,6 +54,7 @@ import ( type LesServer interface { Start(srvr *p2p.Server) Stop() + APIs() []rpc.API Protocols() []p2p.Protocol SetBloomBitsIndexer(bbIndexer *core.ChainIndexer) } @@ -247,6 +248,11 @@ func (s *Ethereum) APIs() []rpc.API { // Append any APIs exposed explicitly by the consensus engine 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 return append(apis, []rpc.API{ { diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 9d6ce8c6c8..af50d3adc8 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -30,6 +30,7 @@ var Modules = map[string]string{ "shh": Shh_JS, "swarmfs": SWARMFS_JS, "txpool": TxPool_JS, + "les": LES_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' + }), + ] +}); +` diff --git a/les/api.go b/les/api.go new file mode 100644 index 0000000000..6987031913 --- /dev/null +++ b/les/api.go @@ -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 . + +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 +} diff --git a/les/server.go b/les/server.go index 9eb8ee7f97..1dabafc9d5 100644 --- a/les/server.go +++ b/les/server.go @@ -35,6 +35,7 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discv5" "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/rpc" ) type LesServer struct { @@ -142,6 +143,46 @@ func (s *LesServer) 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 { baseCost, reqCost uint64 }