github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/consensus/podc/core/roundstate.go (about) 1 // Copyright 2017 AMIS Technologies 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 "io" 21 "math/big" 22 "sync" 23 24 "github.com/ethereum/go-ethereum/consensus/podc" 25 "github.com/ethereum/go-ethereum/rlp" 26 ) 27 28 func newRoundState(view *podc.View, validatorSet podc.ValidatorSet) *roundState { 29 return &roundState{ 30 round: view.Round, 31 sequence: view.Sequence, 32 Preprepare: nil, 33 Prepares: newMessageSet(validatorSet), 34 Commits: newMessageSet(validatorSet), 35 //Dselects: newMessageSet(validatorSet), 36 //Dcommits: newMessageSet(validatorSet), 37 Checkpoints: newMessageSet(validatorSet), 38 mu: new(sync.RWMutex), 39 } 40 } 41 42 // roundState stores the consensus state 43 type roundState struct { 44 round *big.Int 45 sequence *big.Int 46 47 Preprepare *podc.Preprepare 48 49 Prepares *messageSet 50 Commits *messageSet 51 Checkpoints *messageSet 52 53 mu *sync.RWMutex 54 } 55 56 func (s *roundState) Subject() *podc.Subject { 57 s.mu.RLock() 58 defer s.mu.RUnlock() 59 60 if s.Preprepare == nil { 61 return nil 62 } 63 64 return &podc.Subject{ 65 View: &podc.View{ 66 Round: new(big.Int).Set(s.round), 67 Sequence: new(big.Int).Set(s.sequence), 68 }, 69 Digest: s.Preprepare.Proposal.Hash(), 70 } 71 } 72 73 func (s *roundState) SetPreprepare(preprepare *podc.Preprepare) { 74 s.mu.Lock() 75 defer s.mu.Unlock() 76 77 s.Preprepare = preprepare 78 } 79 80 func (s *roundState) Proposal() podc.Proposal { 81 s.mu.RLock() 82 defer s.mu.RUnlock() 83 84 if s.Preprepare != nil { 85 return s.Preprepare.Proposal //제안할 블럭을 가져온다... 합의가 끝나면,, 체인에 연결할 블럭을 가져온다. 86 } 87 88 return nil 89 } 90 91 func (s *roundState) SetRound(r *big.Int) { 92 s.mu.Lock() 93 defer s.mu.Unlock() 94 95 s.round = new(big.Int).Set(r) 96 } 97 98 func (s *roundState) Round() *big.Int { 99 s.mu.RLock() 100 defer s.mu.RUnlock() 101 102 return s.round 103 } 104 105 func (s *roundState) SetSequence(seq *big.Int) { 106 s.mu.Lock() 107 defer s.mu.Unlock() 108 109 s.sequence = seq 110 } 111 112 func (s *roundState) Sequence() *big.Int { 113 s.mu.RLock() 114 defer s.mu.RUnlock() 115 116 return s.sequence 117 } 118 119 // The DecodeRLP method should read one value from the given 120 // Stream. It is not forbidden to read less or more, but it might 121 // be confusing. 122 func (s *roundState) DecodeRLP(stream *rlp.Stream) error { 123 var ss struct { 124 Round *big.Int 125 Sequence *big.Int 126 Preprepare *podc.Preprepare 127 Prepares *messageSet 128 Commits *messageSet 129 130 131 //Dselects *messageSet 132 //Dcommits *messageSet 133 Checkpoints *messageSet 134 } 135 136 if err := stream.Decode(&ss); err != nil { 137 return err 138 } 139 s.round = ss.Round 140 s.sequence = ss.Sequence 141 s.Preprepare = ss.Preprepare 142 s.Prepares = ss.Prepares 143 s.Commits = ss.Commits 144 145 //s.Dselects = ss.Dselects 146 //s.Dcommits = ss.Dcommits 147 s.Checkpoints = ss.Checkpoints 148 s.mu = new(sync.RWMutex) 149 150 return nil 151 } 152 153 // EncodeRLP should write the RLP encoding of its receiver to w. 154 // If the implementation is a pointer method, it may also be 155 // called for nil pointers. 156 // 157 // Implementations should generate valid RLP. The data written is 158 // not verified at the moment, but a future version might. It is 159 // recommended to write only a single value but writing multiple 160 // values or no value at all is also permitted. 161 func (s *roundState) EncodeRLP(w io.Writer) error { 162 s.mu.RLock() 163 defer s.mu.RUnlock() 164 165 return rlp.Encode(w, []interface{}{ 166 s.round, 167 s.sequence, 168 s.Preprepare, 169 s.Prepares, 170 s.Commits, 171 //s.Dselects, 172 //s.Dcommits, 173 s.Checkpoints, 174 }) 175 }