diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f44eb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +bdls.test +cpu.out diff --git a/cmd/emucon/emucon b/cmd/emucon/emucon index a232151..3753d28 100755 Binary files a/cmd/emucon/emucon and b/cmd/emucon/emucon differ diff --git a/cmd/emucon/quorum.json b/cmd/emucon/quorum.json index c3ec8ce..d5340e2 100644 --- a/cmd/emucon/quorum.json +++ b/cmd/emucon/quorum.json @@ -1,8 +1,8 @@ { "keys": [ - 98219006221833178226499894663828536080581259993811761082380731664557876151926, - 58630051888648454967792790648160553661274118301081136769104558537494738155681, - 100955098131707660017796861077112074015350838580117274026874229793880529586064, - 74259512097651292383407728057818570425528934348167008131707948177877498894584 + 68082493172628484253808951113461196766221768923883438540199548009461479956986, + 44652770827640294682875208048383575561358062645764968117337703282091165609211, + 80512969964988849039583604411558290822829809041684390237207179810031917243659, + 55978351916851767744151875911101025920456547576858680756045508192261620541580 ] } diff --git a/consensus.go b/consensus.go index 74d638d..0fc7f1a 100644 --- a/consensus.go +++ b/consensus.go @@ -10,13 +10,15 @@ import ( "sort" "time" + //"fmt" + "github.com/yonggewang/bdls/crypto/blake2b" proto "github.com/gogo/protobuf/proto" ) const ( - // ProtocolVersion is the current BDLS protocol implementation version, - // version wil be sent along with messages for protocol upgrading. + // the current BDLS protocol version, + // version will be sent along with messages for protocol upgrading. ProtocolVersion = 1 // DefaultConsensusLatency is the default propagation latency setting for // consensus protocol, user can adjust consensus object's latency setting @@ -28,9 +30,10 @@ const ( ) type ( - // State is the data to participant in consensus + // State is the data to participant in consensus. This could be candidate + // blocks in blockchain systems State []byte - // StateHash is a fixed size hash to identify a state + // StateHash = H(State) StateHash [blake2b.Size256]byte ) @@ -38,7 +41,7 @@ type ( func defaultHash(s State) StateHash { return blake2b.Sum256(s) } type ( - // consensusStage defines the status of consensus automate + // consensusStage defines the status of consensus automata consensusStage byte ) @@ -51,8 +54,6 @@ const ( stageLockRelease ) -// messageTuple contains a state hash, a decoded incoming message -// and it's encoded raw message with a signature. type messageTuple struct { StateHash StateHash // computed while adding Message *Message // the decoded message @@ -255,10 +256,10 @@ type Consensus struct { currentRound *consensusRound // current round which has collected >=2t+1 // timeouts in different stage - rcTimeout time.Time // roundchange status timeout - lockTimeout time.Time // lock status timeout - commitTimeout time.Time // commit status timeout - lockReleaseTimeout time.Time // lock-release status timeout + rcTimeout time.Time // roundchange status timeout: Delta_0 + lockTimeout time.Time // lock status timeout: Delta_1 + commitTimeout time.Time // commit status timeout: Delta_2 + lockReleaseTimeout time.Time // lock-release status timeout: Delta_3 // locked states, along with its signatures and hashes in tuple locks []messageTuple @@ -294,7 +295,7 @@ type Consensus struct { participants []Identity // count num of individual identities - numIdentities int + numIdentities int //[YONGGE WANG' comments:] make sure this is synchronized with []Identity // set to true to enable message unicast enableCommitUnicast bool @@ -1243,7 +1244,7 @@ func (c *Consensus) receiveMessage(bts []byte, now time.Time) error { } // for message, we need to find in each round - // to check if this participant has already sent + // to check if this sender has already sent // we only keep the message from the max round. // NOTE: we don't touch current round to prevent removing // valid proofs. @@ -1321,7 +1322,7 @@ func (c *Consensus) receiveMessage(bts []byte, now time.Time) error { } - // for the leader, who's current round has at least 2*t+1 , + // for the leader, whose current round has at least 2*t+1 , // we will track max proposed state for each valid added if round == c.currentRound && round.NumRoundChanges() >= 2*c.t()+1 { leaderKey := c.roundLeader(m.Round) @@ -1478,6 +1479,7 @@ func (c *Consensus) receiveMessage(bts []byte, now time.Time) error { c.rcTimeout = now.Add(c.roundchangeDuration(0)) // we sync our height and broadcast new . c.broadcastRoundChange() + case MessageType_Resync: // push the proofs in loopback device for k := range m.Proof { @@ -1488,6 +1490,7 @@ func (c *Consensus) receiveMessage(bts []byte, now time.Time) error { } c.loopback = append(c.loopback, out) } + default: return ErrMessageUnknownMessageType } diff --git a/consensus_test.go b/consensus_test.go index 13391ea..3313701 100644 --- a/consensus_test.go +++ b/consensus_test.go @@ -50,6 +50,7 @@ func (c *Consensus) AddParticipant(key *ecdsa.PublicKey) { } } c.participants = append(c.participants, coord) + c.numIdentities ++ } // createConsensus creates a valid consensus object with given height & round and random state @@ -230,6 +231,7 @@ func TestLockMessageRoundSwitch(t *testing.T) { bts, err = proto.Marshal(sp) assert.Nil(t, err) + consensus.numIdentities = 20 err = consensus.ReceiveMessage(bts, time.Now()) assert.Nil(t, err) // assert length of locks to 2 @@ -237,6 +239,7 @@ func TestLockMessageRoundSwitch(t *testing.T) { // round switch to 12 with old B', resetting particpants consensus.participants = nil + consensus.numIdentities = 0 _, sp, privateKey, proofKeys = createLockMessageState(t, 20, m.State, 1, 12, 1, 12) consensus.AddParticipant(&privateKey.PublicKey) consensus.SetLeader(&privateKey.PublicKey) @@ -251,6 +254,7 @@ func TestLockMessageRoundSwitch(t *testing.T) { assert.Nil(t, err) // assert length of locks to 2 assert.Equal(t, 2, len(consensus.locks)) + } func TestLockReleaseMessageRoundSwitch(t *testing.T) { @@ -270,6 +274,7 @@ func TestLockReleaseMessageRoundSwitch(t *testing.T) { // round switch to 11, resetting particpants consensus.participants = nil + consensus.numIdentities = 0 _, sp, privateKey, proofKeys = createLockReleaseMessage(t, 20, 1, 11, 1, 11) consensus.AddParticipant(&privateKey.PublicKey) consensus.SetLeader(&privateKey.PublicKey) diff --git a/errors.go b/errors.go index bf9a936..12b487c 100644 --- a/errors.go +++ b/errors.go @@ -74,7 +74,7 @@ var ( ErrCommitStateMismatch = errors.New("the state in message does not match what leader has locked") ErrCommitStateValidation = errors.New("the state data validation failed message") ErrCommitStatus = errors.New("received message in non COMMIT state") - ErrCommitHeightMismatch = errors.New("the messge has another height than expected") + ErrCommitHeightMismatch = errors.New("the message has another height than expected") ErrCommitRoundMismatch = errors.New("the message is from another round") // verification