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