github.com/daefrom/go-dae@v1.0.1/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 solc contract/oracle.sol --combined-json bin,bin-runtime,srcmap,srcmap-runtime,abi,userdoc,devdoc,metadata,hashes --optimize -o ./ --overwrite
    21  //go:generate go run ../../cmd/abigen --pkg contract --out contract/oracle.go --combined-json ./combined.json
    22  
    23  import (
    24  	"errors"
    25  	"math/big"
    26  
    27  	"github.com/daefrom/go-dae/accounts/abi/bind"
    28  	"github.com/daefrom/go-dae/common"
    29  	"github.com/daefrom/go-dae/contracts/checkpointoracle/contract"
    30  	"github.com/daefrom/go-dae/core/types"
    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 && 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 "ethereum style" which transforms
    81  // v from 0/1 to 27/28 according to the yellow paper.
    82  func (oracle *CheckpointOracle) RegisterCheckpoint(opts *bind.TransactOpts, index uint64, hash []byte, rnum *big.Int, rhash [32]byte, sigs [][]byte) (*types.Transaction, error) {
    83  	var (
    84  		r [][32]byte
    85  		s [][32]byte
    86  		v []uint8
    87  	)
    88  	for i := 0; i < len(sigs); i++ {
    89  		if len(sigs[i]) != 65 {
    90  			return nil, errors.New("invalid signature")
    91  		}
    92  		r = append(r, common.BytesToHash(sigs[i][:32]))
    93  		s = append(s, common.BytesToHash(sigs[i][32:64]))
    94  		v = append(v, sigs[i][64])
    95  	}
    96  	return oracle.contract.SetCheckpoint(opts, rnum, rhash, common.BytesToHash(hash), index, v, r, s)
    97  }