github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/consensus/istanbul/ibft/core/commit.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/kisexp/xdchain/common" 23 "github.com/kisexp/xdchain/consensus/istanbul" 24 istanbulcommon "github.com/kisexp/xdchain/consensus/istanbul/common" 25 ibfttypes "github.com/kisexp/xdchain/consensus/istanbul/ibft/types" 26 ) 27 28 func (c *core) sendCommit() { 29 sub := c.current.Subject() 30 c.broadcastCommit(sub) 31 } 32 33 func (c *core) sendCommitForOldBlock(view *istanbul.View, digest common.Hash) { 34 sub := &istanbul.Subject{ 35 View: view, 36 Digest: digest, 37 } 38 c.broadcastCommit(sub) 39 } 40 41 func (c *core) broadcastCommit(sub *istanbul.Subject) { 42 logger := c.logger.New("state", c.state) 43 44 encodedSubject, err := ibfttypes.Encode(sub) 45 if err != nil { 46 logger.Error("Failed to encode", "subject", sub) 47 return 48 } 49 c.broadcast(&ibfttypes.Message{ 50 Code: ibfttypes.MsgCommit, 51 Msg: encodedSubject, 52 }) 53 } 54 55 func (c *core) handleCommit(msg *ibfttypes.Message, src istanbul.Validator) error { 56 // Decode COMMIT message 57 var commit *istanbul.Subject 58 err := msg.Decode(&commit) 59 if err != nil { 60 return istanbulcommon.ErrFailedDecodeCommit 61 } 62 63 if err := c.checkMessage(ibfttypes.MsgCommit, commit.View); err != nil { 64 return err 65 } 66 67 if err := c.verifyCommit(commit, src); err != nil { 68 return err 69 } 70 71 c.acceptCommit(msg, src) 72 73 // Commit the proposal once we have enough COMMIT messages and we are not in the Committed state. 74 // 75 // If we already have a proposal, we may have chance to speed up the consensus process 76 // by committing the proposal without PREPARE messages. 77 if c.current.Commits.Size() >= c.QuorumSize() && c.state.Cmp(ibfttypes.StateCommitted) < 0 { 78 // Still need to call LockHash here since state can skip Prepared state and jump directly to the Committed state. 79 c.current.LockHash() 80 c.commit() 81 } 82 83 return nil 84 } 85 86 // verifyCommit verifies if the received COMMIT message is equivalent to our subject 87 func (c *core) verifyCommit(commit *istanbul.Subject, src istanbul.Validator) error { 88 logger := c.logger.New("from", src, "state", c.state) 89 90 sub := c.current.Subject() 91 if !reflect.DeepEqual(commit, sub) { 92 logger.Warn("Inconsistent subjects between commit and proposal", "expected", sub, "got", commit) 93 return istanbulcommon.ErrInconsistentSubject 94 } 95 96 return nil 97 } 98 99 func (c *core) acceptCommit(msg *ibfttypes.Message, src istanbul.Validator) error { 100 logger := c.logger.New("from", src, "state", c.state) 101 102 // Add the COMMIT message to current round state 103 if err := c.current.Commits.Add(msg); err != nil { 104 logger.Error("Failed to record commit message", "msg", msg, "err", err) 105 return err 106 } 107 108 return nil 109 }