github.com/ConsenSys/Quorum@v20.10.0+incompatible/consensus/istanbul/core/prepare.go (about) 1 // Copyright 2017 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 core 18 19 import ( 20 "reflect" 21 22 "github.com/ethereum/go-ethereum/consensus/istanbul" 23 ) 24 25 func (c *core) sendPrepare() { 26 logger := c.logger.New("state", c.state) 27 28 sub := c.current.Subject() 29 encodedSubject, err := Encode(sub) 30 if err != nil { 31 logger.Error("Failed to encode", "subject", sub) 32 return 33 } 34 c.broadcast(&message{ 35 Code: msgPrepare, 36 Msg: encodedSubject, 37 }) 38 } 39 40 func (c *core) handlePrepare(msg *message, src istanbul.Validator) error { 41 // Decode PREPARE message 42 var prepare *istanbul.Subject 43 err := msg.Decode(&prepare) 44 if err != nil { 45 return errFailedDecodePrepare 46 } 47 48 if err := c.checkMessage(msgPrepare, prepare.View); err != nil { 49 return err 50 } 51 52 // If it is locked, it can only process on the locked block. 53 // Passing verifyPrepare and checkMessage implies it is processing on the locked block since it was verified in the Preprepared state. 54 if err := c.verifyPrepare(prepare, src); err != nil { 55 return err 56 } 57 58 c.acceptPrepare(msg, src) 59 60 // Change to Prepared state if we've received enough PREPARE messages or it is locked 61 // and we are in earlier state before Prepared state. 62 if ((c.current.IsHashLocked() && prepare.Digest == c.current.GetLockedHash()) || c.current.GetPrepareOrCommitSize() >= c.QuorumSize()) && 63 c.state.Cmp(StatePrepared) < 0 { 64 c.current.LockHash() 65 c.setState(StatePrepared) 66 c.sendCommit() 67 } 68 69 return nil 70 } 71 72 // verifyPrepare verifies if the received PREPARE message is equivalent to our subject 73 func (c *core) verifyPrepare(prepare *istanbul.Subject, src istanbul.Validator) error { 74 logger := c.logger.New("from", src, "state", c.state) 75 76 sub := c.current.Subject() 77 if !reflect.DeepEqual(prepare, sub) { 78 logger.Warn("Inconsistent subjects between PREPARE and proposal", "expected", sub, "got", prepare) 79 return errInconsistentSubject 80 } 81 82 return nil 83 } 84 85 func (c *core) acceptPrepare(msg *message, src istanbul.Validator) error { 86 logger := c.logger.New("from", src, "state", c.state) 87 88 // Add the PREPARE message to current round state 89 if err := c.current.Prepares.Add(msg); err != nil { 90 logger.Error("Failed to add PREPARE message to round state", "msg", msg, "err", err) 91 return err 92 } 93 94 return nil 95 }