github.com/ConsenSys/Quorum@v20.10.0+incompatible/les/checkpointoracle.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 les 18 19 import ( 20 "encoding/binary" 21 "sync/atomic" 22 23 "github.com/ethereum/go-ethereum/accounts/abi/bind" 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/contracts/checkpointoracle" 26 "github.com/ethereum/go-ethereum/crypto" 27 "github.com/ethereum/go-ethereum/log" 28 "github.com/ethereum/go-ethereum/params" 29 ) 30 31 // checkpointOracle is responsible for offering the latest stable checkpoint 32 // generated and announced by the contract admins on-chain. The checkpoint is 33 // verified by clients locally during the checkpoint syncing. 34 type checkpointOracle struct { 35 config *params.CheckpointOracleConfig 36 contract *checkpointoracle.CheckpointOracle 37 38 running int32 // Flag whether the contract backend is set or not 39 getLocal func(uint64) params.TrustedCheckpoint // Function used to retrieve local checkpoint 40 } 41 42 // newCheckpointOracle returns a checkpoint registrar handler. 43 func newCheckpointOracle(config *params.CheckpointOracleConfig, getLocal func(uint64) params.TrustedCheckpoint) *checkpointOracle { 44 if config == nil { 45 log.Info("Checkpoint registrar is not enabled") 46 return nil 47 } 48 if config.Address == (common.Address{}) || uint64(len(config.Signers)) < config.Threshold { 49 log.Warn("Invalid checkpoint registrar config") 50 return nil 51 } 52 log.Info("Configured checkpoint registrar", "address", config.Address, "signers", len(config.Signers), "threshold", config.Threshold) 53 54 return &checkpointOracle{ 55 config: config, 56 getLocal: getLocal, 57 } 58 } 59 60 // start binds the registrar contract and start listening to the 61 // newCheckpointEvent for the server side. 62 func (reg *checkpointOracle) start(backend bind.ContractBackend) { 63 contract, err := checkpointoracle.NewCheckpointOracle(reg.config.Address, backend) 64 if err != nil { 65 log.Error("Oracle contract binding failed", "err", err) 66 return 67 } 68 if !atomic.CompareAndSwapInt32(®.running, 0, 1) { 69 log.Error("Already bound and listening to registrar") 70 return 71 } 72 reg.contract = contract 73 } 74 75 // isRunning returns an indicator whether the registrar is running. 76 func (reg *checkpointOracle) isRunning() bool { 77 return atomic.LoadInt32(®.running) == 1 78 } 79 80 // stableCheckpoint returns the stable checkpoint which was generated by local 81 // indexers and announced by trusted signers. 82 func (reg *checkpointOracle) stableCheckpoint() (*params.TrustedCheckpoint, uint64) { 83 // Retrieve the latest checkpoint from the contract, abort if empty 84 latest, hash, height, err := reg.contract.Contract().GetLatestCheckpoint(nil) 85 if err != nil || (latest == 0 && hash == [32]byte{}) { 86 return nil, 0 87 } 88 local := reg.getLocal(latest) 89 90 // The following scenarios may occur: 91 // 92 // * local node is out of sync so that it doesn't have the 93 // checkpoint which registered in the contract. 94 // * local checkpoint doesn't match with the registered one. 95 // 96 // In both cases, server won't send the **stable** checkpoint 97 // to the client(no worry, client can use hardcoded one instead). 98 if local.HashEqual(common.Hash(hash)) { 99 return &local, height.Uint64() 100 } 101 return nil, 0 102 } 103 104 // verifySigners recovers the signer addresses according to the signature and 105 // checks whether there are enough approvals to finalize the checkpoint. 106 func (reg *checkpointOracle) verifySigners(index uint64, hash [32]byte, signatures [][]byte) (bool, []common.Address) { 107 // Short circuit if the given signatures doesn't reach the threshold. 108 if len(signatures) < int(reg.config.Threshold) { 109 return false, nil 110 } 111 var ( 112 signers []common.Address 113 checked = make(map[common.Address]struct{}) 114 ) 115 for i := 0; i < len(signatures); i++ { 116 if len(signatures[i]) != 65 { 117 continue 118 } 119 // EIP 191 style signatures 120 // 121 // Arguments when calculating hash to validate 122 // 1: byte(0x19) - the initial 0x19 byte 123 // 2: byte(0) - the version byte (data with intended validator) 124 // 3: this - the validator address 125 // -- Application specific data 126 // 4 : checkpoint section_index (uint64) 127 // 5 : checkpoint hash (bytes32) 128 // hash = keccak256(checkpoint_index, section_head, cht_root, bloom_root) 129 buf := make([]byte, 8) 130 binary.BigEndian.PutUint64(buf, index) 131 data := append([]byte{0x19, 0x00}, append(reg.config.Address.Bytes(), append(buf, hash[:]...)...)...) 132 signatures[i][64] -= 27 // Transform V from 27/28 to 0/1 according to the yellow paper for verification. 133 pubkey, err := crypto.Ecrecover(crypto.Keccak256(data), signatures[i]) 134 if err != nil { 135 return false, nil 136 } 137 var signer common.Address 138 copy(signer[:], crypto.Keccak256(pubkey[1:])[12:]) 139 if _, exist := checked[signer]; exist { 140 continue 141 } 142 for _, s := range reg.config.Signers { 143 if s == signer { 144 signers = append(signers, signer) 145 checked[signer] = struct{}{} 146 } 147 } 148 } 149 threshold := reg.config.Threshold 150 if uint64(len(signers)) < threshold { 151 log.Warn("Not enough signers to approve checkpoint", "signers", len(signers), "threshold", threshold) 152 return false, nil 153 } 154 return true, signers 155 }