github.com/bloxroute-labs/bor@v0.1.4/les/api.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"errors"
    21  
    22  	"github.com/maticnetwork/bor/common/hexutil"
    23  )
    24  
    25  var (
    26  	errNoCheckpoint = errors.New("no local checkpoint provided")
    27  	errNotActivated = errors.New("checkpoint registrar is not activated")
    28  )
    29  
    30  // PrivateLightAPI provides an API to access the LES light server or light client.
    31  type PrivateLightAPI struct {
    32  	backend *lesCommons
    33  	reg     *checkpointOracle
    34  }
    35  
    36  // NewPrivateLightAPI creates a new LES service API.
    37  func NewPrivateLightAPI(backend *lesCommons, reg *checkpointOracle) *PrivateLightAPI {
    38  	return &PrivateLightAPI{
    39  		backend: backend,
    40  		reg:     reg,
    41  	}
    42  }
    43  
    44  // LatestCheckpoint returns the latest local checkpoint package.
    45  //
    46  // The checkpoint package consists of 4 strings:
    47  //   result[0], hex encoded latest section index
    48  //   result[1], 32 bytes hex encoded latest section head hash
    49  //   result[2], 32 bytes hex encoded latest section canonical hash trie root hash
    50  //   result[3], 32 bytes hex encoded latest section bloom trie root hash
    51  func (api *PrivateLightAPI) LatestCheckpoint() ([4]string, error) {
    52  	var res [4]string
    53  	cp := api.backend.latestLocalCheckpoint()
    54  	if cp.Empty() {
    55  		return res, errNoCheckpoint
    56  	}
    57  	res[0] = hexutil.EncodeUint64(cp.SectionIndex)
    58  	res[1], res[2], res[3] = cp.SectionHead.Hex(), cp.CHTRoot.Hex(), cp.BloomRoot.Hex()
    59  	return res, nil
    60  }
    61  
    62  // GetLocalCheckpoint returns the specific local checkpoint package.
    63  //
    64  // The checkpoint package consists of 3 strings:
    65  //   result[0], 32 bytes hex encoded latest section head hash
    66  //   result[1], 32 bytes hex encoded latest section canonical hash trie root hash
    67  //   result[2], 32 bytes hex encoded latest section bloom trie root hash
    68  func (api *PrivateLightAPI) GetCheckpoint(index uint64) ([3]string, error) {
    69  	var res [3]string
    70  	cp := api.backend.getLocalCheckpoint(index)
    71  	if cp.Empty() {
    72  		return res, errNoCheckpoint
    73  	}
    74  	res[0], res[1], res[2] = cp.SectionHead.Hex(), cp.CHTRoot.Hex(), cp.BloomRoot.Hex()
    75  	return res, nil
    76  }
    77  
    78  // GetCheckpointContractAddress returns the contract contract address in hex format.
    79  func (api *PrivateLightAPI) GetCheckpointContractAddress() (string, error) {
    80  	if api.reg == nil {
    81  		return "", errNotActivated
    82  	}
    83  	return api.reg.config.Address.Hex(), nil
    84  }