gitee.com/liu-zhao234568/cntest@v1.0.0/contracts/checkpointoracle/oracle.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 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  	"gitee.com/liu-zhao234568/cntest/accounts/abi/bind"
    27  	"gitee.com/liu-zhao234568/cntest/common"
    28  	"gitee.com/liu-zhao234568/cntest/contracts/checkpointoracle/contract"
    29  	"gitee.com/liu-zhao234568/cntest/core/types"
    30  )
    31  
    32  // CheckpointOracle is a Go wrapper around an on-chain checkpoint oracle contract.
    33  type CheckpointOracle struct {
    34  	address  common.Address
    35  	contract *contract.CheckpointOracle
    36  }
    37  
    38  // NewCheckpointOracle binds checkpoint contract and returns a registrar instance.
    39  func NewCheckpointOracle(contractAddr common.Address, backend bind.ContractBackend) (*CheckpointOracle, error) {
    40  	c, err := contract.NewCheckpointOracle(contractAddr, backend)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	return &CheckpointOracle{address: contractAddr, contract: c}, nil
    45  }
    46  
    47  // ContractAddr returns the address of contract.
    48  func (oracle *CheckpointOracle) ContractAddr() common.Address {
    49  	return oracle.address
    50  }
    51  
    52  // Contract returns the underlying contract instance.
    53  func (oracle *CheckpointOracle) Contract() *contract.CheckpointOracle {
    54  	return oracle.contract
    55  }
    56  
    57  // LookupCheckpointEvents searches checkpoint event for specific section in the
    58  // given log batches.
    59  func (oracle *CheckpointOracle) LookupCheckpointEvents(blockLogs [][]*types.Log, section uint64, hash common.Hash) []*contract.CheckpointOracleNewCheckpointVote {
    60  	var votes []*contract.CheckpointOracleNewCheckpointVote
    61  
    62  	for _, logs := range blockLogs {
    63  		for _, log := range logs {
    64  			event, err := oracle.contract.ParseNewCheckpointVote(*log)
    65  			if err != nil {
    66  				continue
    67  			}
    68  			if event.Index == section && event.CheckpointHash == hash {
    69  				votes = append(votes, event)
    70  			}
    71  		}
    72  	}
    73  	return votes
    74  }
    75  
    76  // RegisterCheckpoint registers the checkpoint with a batch of associated signatures
    77  // that are collected off-chain and sorted by lexicographical order.
    78  //
    79  // Notably all signatures given should be transformed to "ethereum style" which transforms
    80  // v from 0/1 to 27/28 according to the yellow paper.
    81  func (oracle *CheckpointOracle) RegisterCheckpoint(opts *bind.TransactOpts, index uint64, hash []byte, rnum *big.Int, rhash [32]byte, sigs [][]byte) (*types.Transaction, error) {
    82  	var (
    83  		r [][32]byte
    84  		s [][32]byte
    85  		v []uint8
    86  	)
    87  	for i := 0; i < len(sigs); i++ {
    88  		if len(sigs[i]) != 65 {
    89  			return nil, errors.New("invalid signature")
    90  		}
    91  		r = append(r, common.BytesToHash(sigs[i][:32]))
    92  		s = append(s, common.BytesToHash(sigs[i][32:64]))
    93  		v = append(v, sigs[i][64])
    94  	}
    95  	return oracle.contract.SetCheckpoint(opts, rnum, rhash, common.BytesToHash(hash), index, v, r, s)
    96  }