diff options
author | Mission Liao <mission.liao@dexon.org> | 2019-04-03 16:14:13 +0800 |
---|---|---|
committer | Mission Liao <mission.liao@dexon.org> | 2019-04-15 14:41:19 +0800 |
commit | 52bf529c57ec881cb2eb9498e56a09029ac00d67 (patch) | |
tree | e4f3f17954731d63a03b3ef8cab9f47e01e7552a | |
parent | e9a1d3bca8353ee206d262ab1fad2d7e3e0b24a5 (diff) | |
download | dexon-consensus-52bf529c57ec881cb2eb9498e56a09029ac00d67.tar.gz dexon-consensus-52bf529c57ec881cb2eb9498e56a09029ac00d67.tar.zst dexon-consensus-52bf529c57ec881cb2eb9498e56a09029ac00d67.zip |
Add core.agreementEvent
-rw-r--r-- | core/agreement-event.go | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/core/agreement-event.go b/core/agreement-event.go new file mode 100644 index 0000000..de4d808 --- /dev/null +++ b/core/agreement-event.go @@ -0,0 +1,127 @@ +// Copyright 2019 The dexon-consensus Authors +// This file is part of the dexon-consensus library. +// +// The dexon-consensus library is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The dexon-consensus library is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the dexon-consensus library. If not, see +// <http://www.gnu.org/licenses/>. + +package core + +import ( + "encoding/hex" + "fmt" + + "github.com/dexon-foundation/dexon-consensus/common" + "github.com/dexon-foundation/dexon-consensus/core/types" +) + +// agreementEventType is the type of agreemnt event. +type agreementEventType byte + +// agreementEventType enum. +const ( + // agreementEventInvalid is just a type guard, not intend to be used. + agreementEventInvalid agreementEventType = iota + // agreementEventLock is triggered when receiving more than 2t+1 pre-commit + // votes for one value. + agreementEventLock + // agreementEventForward is triggered when receiving more than 2t+1 commit + // votes for different values. + agreementEventForward + // agreementEventDecide is triggered when receiving more than 2t+1 commit + // votes for one value. + agreementEventDecide + // agreementEventFork is triggered when detecting a fork vote. + agreementEventFork + // Do not add any type below maxAgreementEventType. + maxAgreementEventType +) + +func (t agreementEventType) String() string { + switch t { + case agreementEventLock: + return "Lock" + case agreementEventForward: + return "Forward" + case agreementEventDecide: + return "Decide" + case agreementEventFork: + return "Fork" + default: + return fmt.Sprintf("Unknown-%d", t) + } +} + +// agreementEvent represents an agreement event, ex. fast forward, decide, +// fork, ..., which would trigger state change of agreement module. +type agreementEvent struct { + types.AgreementResult + + period uint64 + evtType agreementEventType + voteType types.VoteType +} + +func (e agreementEvent) toAgreementResult() types.AgreementResult { + if e.evtType != agreementEventDecide { + panic(fmt.Errorf("invalid event to extract agreement result: %s", e)) + } + return e.AgreementResult +} + +func (e agreementEvent) String() string { + if len(e.Randomness) > 0 { + return fmt.Sprintf( + "agreementEvent{[%s:%s:%s:%d],Empty:%t,Hash:%s,Rand:%s}", + e.evtType, e.voteType, e.Position, e.period, e.IsEmptyBlock, + e.BlockHash.String()[:6], hex.EncodeToString(e.Randomness)[:6]) + } + return fmt.Sprintf( + "agreementEvent{[%s:%s:%s:%d],Empty:%t,Hash:%s}", + e.evtType, e.voteType, e.Position, e.period, e.IsEmptyBlock, + e.BlockHash.String()[:6]) +} + +// newAgreementEvent constructs an agreementEvent instance. +func newAgreementEvent( + t agreementEventType, votes []types.Vote) *agreementEvent { + return &agreementEvent{ + AgreementResult: types.AgreementResult{ + Position: votes[0].Position, + Votes: votes, + IsEmptyBlock: votes[0].BlockHash == types.NullBlockHash, + BlockHash: votes[0].BlockHash, + }, + period: votes[0].Period, + evtType: t, + voteType: votes[0].Type, + } +} + +func newAgreementEventFromTSIG( + position types.Position, + hash common.Hash, + tSig []byte, + isEmpty bool) *agreementEvent { + return &agreementEvent{ + AgreementResult: types.AgreementResult{ + Position: position, + BlockHash: hash, + IsEmptyBlock: isEmpty, + Randomness: tSig, + }, + period: 0, + evtType: agreementEventDecide, + voteType: types.MaxVoteType, + } +} |