-
Notifications
You must be signed in to change notification settings - Fork 178
/
staking_signer.go
95 lines (78 loc) · 2.69 KB
/
staking_signer.go
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
90
91
92
93
94
95
package verification
import (
"fmt"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/model/encoding"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
)
// StakingSigner creates votes for the collector clusters consensus.
// When a participant votes for a block, it _always_ provide the staking signature
// as part of their vote. StakingSigner is responsible for creating correctly
// signed proposals and votes.
type StakingSigner struct {
me module.Local
stakingHasher hash.Hasher
signerID flow.Identifier
}
// NewStakingSigner instantiates a StakingSigner, which signs votes and
// proposals with the staking key. The generated signatures are aggregatable.
func NewStakingSigner(
me module.Local,
) *StakingSigner {
sc := &StakingSigner{
me: me,
stakingHasher: crypto.NewBLSKMAC(encoding.CollectorVoteTag),
signerID: me.NodeID(),
}
return sc
}
// CreateProposal will create a proposal with a staking signature for the given block.
func (c *StakingSigner) CreateProposal(block *model.Block) (*model.Proposal, error) {
// check that the block is created by us
if block.ProposerID != c.signerID {
return nil, fmt.Errorf("can't create proposal for someone else's block")
}
// create the signature data
sigData, err := c.genSigData(block)
if err != nil {
return nil, fmt.Errorf("signing my proposal failed: %w", err)
}
// create the proposal
proposal := &model.Proposal{
Block: block,
SigData: sigData,
}
return proposal, nil
}
// CreateVote will create a vote with a staking signature for the given block.
func (c *StakingSigner) CreateVote(block *model.Block) (*model.Vote, error) {
// create the signature data
sigData, err := c.genSigData(block)
if err != nil {
return nil, fmt.Errorf("could not create signature: %w", err)
}
// create the vote
vote := &model.Vote{
View: block.View,
BlockID: block.BlockID,
SignerID: c.signerID,
SigData: sigData,
}
return vote, nil
}
// genSigData generates the signature data for our local node for the given block.
// It returns:
// - (stakingSig, nil) signature signed with staking key. The sig is 48 bytes long
// - (nil, error) if there is any exception
func (c *StakingSigner) genSigData(block *model.Block) ([]byte, error) {
// create the message to be signed and generate signatures
msg := MakeVoteMessage(block.View, block.BlockID)
stakingSig, err := c.me.Sign(msg, c.stakingHasher)
if err != nil {
return nil, fmt.Errorf("could not generate staking signature for block (%v) at view %v: %w", block.BlockID, block.View, err)
}
return stakingSig, nil
}