aboutsummaryrefslogtreecommitdiffstats
path: root/core/agreement/signal.go
blob: 0042cd1a545424297608b6ebd013d818d1278ba4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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 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

// SignalType enum.
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
    // Do not add any type below MaxSignalType.
    maxSignalType
)

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,
    }
}