oracle.go 3.3 KB

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