go-ethereum/les/api.go
2018-06-01 14:46:24 +08:00

60 lines
2.2 KiB
Go

// 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
}