oracle.go 3.5 KB

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