aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2019-01-16 15:59:56 +0800
committerMission Liao <mission.liao@dexon.org>2019-01-28 16:19:51 +0800
commit67e39e4c6218d595436b5efe33541e3ddca0f525 (patch)
treeb547c1dcd8fe864cf8682745f2b66bbfdd186504
parent42fbab7d463689ca7a4f8a6faf00406fe9f29359 (diff)
downloaddexon-consensus-67e39e4c6218d595436b5efe33541e3ddca0f525.tar.gz
dexon-consensus-67e39e4c6218d595436b5efe33541e3ddca0f525.tar.zst
dexon-consensus-67e39e4c6218d595436b5efe33541e3ddca0f525.zip
Add agreement.Signal
-rw-r--r--core/agreement/signal.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/core/agreement/signal.go b/core/agreement/signal.go
new file mode 100644
index 0000000..5fc96e8
--- /dev/null
+++ b/core/agreement/signal.go
@@ -0,0 +1,86 @@
+// Copyright 2018 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 agreement
+
+import (
+ "fmt"
+
+ "github.com/dexon-foundation/dexon-consensus/core/types"
+ "github.com/ethereum/go-ethereum/common"
+)
+
+// SignalType is the type of agreemnt signal.
+type SignalType byte
+
+const (
+ // SignalInvalid is just a type guard, not intend to be used.
+ SignalInvalid SignalType = iota
+ // SignalLock is triggered when receiving more than 2t+1 pre-commit votes
+ // for one value.
+ SignalLock
+ // SignalForward is triggered when receiving more than 2t+1 commit votes for
+ // different values.
+ SignalForward
+ // SignalDecide is triggered when receiving more than 2t+1 commit
+ // votes for one value.
+ SignalDecide
+ // SignalFork is triggered when detecting a fork vote.
+ SignalFork
+)
+
+func (t SignalType) String() string {
+ switch t {
+ case SignalLock:
+ return "Lock"
+ case SignalForward:
+ return "Forward"
+ case SignalDecide:
+ return "Decide"
+ case SignalFork:
+ return "Fork"
+ }
+ panic(fmt.Errorf("attempting to dump unknown agreement signal type: %d", t))
+}
+
+// Signal represents an agreement signal, ex. fast forward, decide,
+// fork, ..., which would trigger state change of agreement module.
+type Signal struct {
+ Position types.Position
+ Period uint64
+ Votes []types.Vote
+ Type SignalType
+ VType types.VoteType
+ BlockHash common.Hash
+}
+
+func (s *Signal) String() string {
+ return fmt.Sprintf(
+ "AgreementSignal{Type:%s,vType:%s,Pos:%s,Period:%d,Hash:%s",
+ s.Type, s.VType, &s.Position, s.Period, s.BlockHash.String()[:6])
+}
+
+// NewSignal constructs an Signal instance.
+func NewSignal(t SignalType, votes []types.Vote) *Signal {
+ return &Signal{
+ Position: votes[0].Position,
+ Period: votes[0].Period,
+ Type: t,
+ VType: votes[0].Type,
+ Votes: votes,
+ }
+}