github.com/mthler/ftm-go-ethereum@v1.9.7-0.20220419132500-d25de565c2de/les/api.go (about)

     1  // Copyright 2019 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/Fantom-foundation/go-ethereum/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  }
    34  
    35  // NewPrivateLightAPI creates a new LES service API.
    36  func NewPrivateLightAPI(backend *lesCommons) *PrivateLightAPI {
    37  	return &PrivateLightAPI{backend: backend}
    38  }
    39  
    40  // LatestCheckpoint returns the latest local checkpoint package.
    41  //
    42  // The checkpoint package consists of 4 strings:
    43  //   result[0], hex encoded latest section index
    44  //   result[1], 32 bytes hex encoded latest section head hash
    45  //   result[2], 32 bytes hex encoded latest section canonical hash trie root hash
    46  //   result[3], 32 bytes hex encoded latest section bloom trie root hash
    47  func (api *PrivateLightAPI) LatestCheckpoint() ([4]string, error) {
    48  	var res [4]string
    49  	cp := api.backend.latestLocalCheckpoint()
    50  	if cp.Empty() {
    51  		return res, errNoCheckpoint
    52  	}
    53  	res[0] = hexutil.EncodeUint64(cp.SectionIndex)
    54  	res[1], res[2], res[3] = cp.SectionHead.Hex(), cp.CHTRoot.Hex(), cp.BloomRoot.Hex()
    55  	return res, nil
    56  }
    57  
    58  // GetLocalCheckpoint returns the specific local checkpoint package.
    59  //
    60  // The checkpoint package consists of 3 strings:
    61  //   result[0], 32 bytes hex encoded latest section head hash
    62  //   result[1], 32 bytes hex encoded latest section canonical hash trie root hash
    63  //   result[2], 32 bytes hex encoded latest section bloom trie root hash
    64  func (api *PrivateLightAPI) GetCheckpoint(index uint64) ([3]string, error) {
    65  	var res [3]string
    66  	cp := api.backend.localCheckpoint(index)
    67  	if cp.Empty() {
    68  		return res, errNoCheckpoint
    69  	}
    70  	res[0], res[1], res[2] = cp.SectionHead.Hex(), cp.CHTRoot.Hex(), cp.BloomRoot.Hex()
    71  	return res, nil
    72  }
    73  
    74  // GetCheckpointContractAddress returns the contract contract address in hex format.
    75  func (api *PrivateLightAPI) GetCheckpointContractAddress() (string, error) {
    76  	if api.backend.oracle == nil {
    77  		return "", errNotActivated
    78  	}
    79  	return api.backend.oracle.config.Address.Hex(), nil
    80  }