oracle.go 3.7 KB

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