github.com/core-coin/go-core@v1.1.7/contracts/checkpointoracle/oracle.go (about)

     1  // Copyright 2019 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package checkpointoracle is a an on-chain light client checkpoint oracle.
    18  package checkpointoracle
    19  
    20  //go:generate abigen --sol contract/oracle.sol --pkg contract --out contract/oracle.go
    21  
    22  import (
    23  	"errors"
    24  	"math/big"
    25  
    26  	"github.com/core-coin/go-core/accounts/abi/bind"
    27  	"github.com/core-coin/go-core/common"
    28  	"github.com/core-coin/go-core/contracts/checkpointoracle/contract"
    29  	"github.com/core-coin/go-core/core/types"
    30  	"github.com/core-coin/go-core/crypto"
    31  )
    32  
    33  // CheckpointOracle is a Go wrapper around an on-chain checkpoint oracle contract.
    34  type CheckpointOracle struct {
    35  	address  common.Address
    36  	contract *contract.CheckpointOracle
    37  }
    38  
    39  // NewCheckpointOracle binds checkpoint contract and returns a registrar instance.
    40  func NewCheckpointOracle(contractAddr common.Address, backend bind.ContractBackend) (*CheckpointOracle, error) {
    41  	c, err := contract.NewCheckpointOracle(contractAddr, backend)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return &CheckpointOracle{address: contractAddr, contract: c}, nil
    46  }
    47  
    48  // ContractAddr returns the address of contract.
    49  func (oracle *CheckpointOracle) ContractAddr() common.Address {
    50  	return oracle.address
    51  }
    52  
    53  // Contract returns the underlying contract instance.
    54  func (oracle *CheckpointOracle) Contract() *contract.CheckpointOracle {
    55  	return oracle.contract
    56  }
    57  
    58  // LookupCheckpointEvents searches checkpoint event for specific section in the
    59  // given log batches.
    60  func (oracle *CheckpointOracle) LookupCheckpointEvents(blockLogs [][]*types.Log, section uint64, hash common.Hash) []*contract.CheckpointOracleNewCheckpointVote {
    61  	var votes []*contract.CheckpointOracleNewCheckpointVote
    62  
    63  	for _, logs := range blockLogs {
    64  		for _, log := range logs {
    65  			event, err := oracle.contract.ParseNewCheckpointVote(*log)
    66  			if err != nil {
    67  				continue
    68  			}
    69  			if event.Index == section && common.Hash(event.CheckpointHash) == hash {
    70  				votes = append(votes, event)
    71  			}
    72  		}
    73  	}
    74  	return votes
    75  }
    76  
    77  // RegisterCheckpoint registers the checkpoint with a batch of associated signatures
    78  // that are collected off-chain and sorted by lexicographical order.
    79  //
    80  // Notably all signatures given should be transformed to "core style" which transforms
    81  func (oracle *CheckpointOracle) RegisterCheckpoint(opts *bind.TransactOpts, index uint64, hash []byte, rnum *big.Int, rhash [32]byte, sigs [][]byte) (*types.Transaction, error) {
    82  	for i := 0; i < len(sigs); i++ {
    83  		if len(sigs[i]) != crypto.ExtendedSignatureLength {
    84  			return nil, errors.New("invalid signature")
    85  		}
    86  	}
    87  	return nil, errors.New("reimplement CheckpointOralce with ed448")
    88  }