github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/contracts/checkpointoracle/oracle.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // Package checkpointoracle is a an on-chain light client checkpoint oracle.
    19  package checkpointoracle
    20  
    21  //go:generate abigen --sol contract/oracle.sol --pkg contract --out contract/oracle.go
    22  
    23  import (
    24  	"errors"
    25  	"math/big"
    26  
    27  	"github.com/AigarNetwork/aigar/accounts/abi/bind"
    28  	"github.com/AigarNetwork/aigar/common"
    29  	"github.com/AigarNetwork/aigar/contracts/checkpointoracle/contract"
    30  	"github.com/AigarNetwork/aigar/core/types"
    31  )
    32  
    33  // CheckpointOracle is a Go wrapper around an on-chain light client checkpoint oracle.
    34  type CheckpointOracle struct {
    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{contract: c}, nil
    45  }
    46  
    47  // Contract returns the underlying contract instance.
    48  func (oracle *CheckpointOracle) Contract() *contract.CheckpointOracle {
    49  	return oracle.contract
    50  }
    51  
    52  // LookupCheckpointEvents searches checkpoint event for specific section in the
    53  // given log batches.
    54  func (oracle *CheckpointOracle) LookupCheckpointEvents(blockLogs [][]*types.Log, section uint64, hash common.Hash) []*contract.CheckpointOracleNewCheckpointVote {
    55  	var votes []*contract.CheckpointOracleNewCheckpointVote
    56  
    57  	for _, logs := range blockLogs {
    58  		for _, log := range logs {
    59  			event, err := oracle.contract.ParseNewCheckpointVote(*log)
    60  			if err != nil {
    61  				continue
    62  			}
    63  			if event.Index == section && common.Hash(event.CheckpointHash) == hash {
    64  				votes = append(votes, event)
    65  			}
    66  		}
    67  	}
    68  	return votes
    69  }
    70  
    71  // RegisterCheckpoint registers the checkpoint with a batch of associated signatures
    72  // that are collected off-chain and sorted by lexicographical order.
    73  //
    74  // Notably all signatures given should be transformed to "ethereum style" which transforms
    75  // v from 0/1 to 27/28 according to the yellow paper.
    76  func (oracle *CheckpointOracle) RegisterCheckpoint(opts *bind.TransactOpts, index uint64, hash []byte, rnum *big.Int, rhash [32]byte, sigs [][]byte) (*types.Transaction, error) {
    77  	var (
    78  		r [][32]byte
    79  		s [][32]byte
    80  		v []uint8
    81  	)
    82  	for i := 0; i < len(sigs); i++ {
    83  		if len(sigs[i]) != 65 {
    84  			return nil, errors.New("invalid signature")
    85  		}
    86  		r = append(r, common.BytesToHash(sigs[i][:32]))
    87  		s = append(s, common.BytesToHash(sigs[i][32:64]))
    88  		v = append(v, sigs[i][64])
    89  	}
    90  	return oracle.contract.SetCheckpoint(opts, rnum, rhash, common.BytesToHash(hash), index, v, r, s)
    91  }