-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
84 lines (66 loc) · 2.42 KB
/
utils.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
package mcms
import (
"encoding/json"
"fmt"
"io"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/smartcontractkit/mcms/types"
)
// Applies the EIP191 prefix to the payload and hashes it.
func toEthSignedMessageHash(payload []byte) common.Hash {
// Add the Ethereum signed message prefix
prefix := []byte("\x19Ethereum Signed Message:\n32")
data := append(prefix, payload...)
// Hash the prefixed message
return crypto.Keccak256Hash(data)
}
func generateQueuedProposalStartingOpCounts[T ProposalInterface](predecessorProposals []T) map[types.ChainSelector]uint64 {
// Set the transaction counts for each chain selector
startingOpCounts := make(map[types.ChainSelector]uint64)
for _, pred := range predecessorProposals {
chainMetadata := pred.ChainMetadatas()
for chainSelector, count := range pred.TransactionCounts() {
if _, ok := startingOpCounts[chainSelector]; !ok {
startingOpCounts[chainSelector] = chainMetadata[chainSelector].StartingOpCount
}
startingOpCounts[chainSelector] += count
}
}
return startingOpCounts
}
func decodeAndValidateProposal[T ProposalInterface](reader io.Reader) (T, error) {
// Decode the proposal
var proposal T
if err := json.NewDecoder(reader).Decode(&proposal); err != nil {
return proposal, fmt.Errorf("failed to decode proposal: %w", err)
}
// Validate the proposal
if err := proposal.Validate(); err != nil {
return proposal, fmt.Errorf("failed to validate proposal: %w", err)
}
return proposal, nil
}
func newProposal[T ProposalInterface](r io.Reader, predecessors []io.Reader) (T, error) {
p, err := decodeAndValidateProposal[T](r)
if err != nil {
return p, fmt.Errorf("failed to decode and validate target proposal: %w", err)
}
predecessorProposals := make([]T, len(predecessors))
for i, pred := range predecessors {
predObj, err := decodeAndValidateProposal[T](pred)
if err != nil {
return p, fmt.Errorf("failed to decode and validate predecessor proposal %d: %w", i, err)
}
predecessorProposals[i] = predObj
}
startingOpCounts := generateQueuedProposalStartingOpCounts(predecessorProposals)
// Set the starting op count for each chain selector in the new proposal
for chainSelector, chainMetadata := range p.ChainMetadatas() {
if count, ok := startingOpCounts[chainSelector]; ok {
chainMetadata.StartingOpCount = count
}
p.setChainMetadata(chainSelector, chainMetadata)
}
return p, nil
}