/
main.go
145 lines (134 loc) · 4.68 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// gen_vectors generates test vectors for the governance transactions.
package main
import (
"encoding/json"
"fmt"
"math"
"os"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
"github.com/oasisprotocol/oasis-core/go/common/version"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction/testvectors"
governance "github.com/oasisprotocol/oasis-core/go/governance/api"
upgrade "github.com/oasisprotocol/oasis-core/go/upgrade/api"
)
func valideSubmitProposal(v uint16, epoch uint64, handler upgrade.HandlerName, target version.ProtocolVersions) bool {
if v < upgrade.MinDescriptorVersion || v > upgrade.MaxDescriptorVersion ||
epoch < uint64(upgrade.MinUpgradeEpoch) || epoch > uint64(upgrade.MaxUpgradeEpoch) ||
len(handler) < upgrade.MinUpgradeHandlerLength || len(handler) > upgrade.MaxUpgradeHandlerLength {
return false
}
if err := target.ValidateBasic(); err != nil {
return false
}
return true
}
func valideCastVote(vote governance.Vote) bool {
for _, v := range []governance.Vote{governance.VoteAbstain, governance.VoteYes, governance.VoteNo} {
if vote == v {
return true
}
}
return false
}
func main() {
// Configure chain context for all signatures using chain domain separation.
var chainContext hash.Hash
chainContext.FromBytes([]byte("governance test vectors"))
signature.SetChainContext(chainContext.String())
var vectors []testvectors.TestVector
// Generate different gas fees.
for _, fee := range []*transaction.Fee{
{},
{Amount: *quantity.NewFromUint64(100000000), Gas: 1000},
{Amount: *quantity.NewFromUint64(0), Gas: 1000},
{Amount: *quantity.NewFromUint64(4242), Gas: 1000},
} {
// Generate different nonces.
for _, nonce := range []uint64{0, 1, 10, 42, 1000, 1_000_000, 10_000_000, math.MaxUint64} {
// Generate upgrade proposal transactions.
for _, v := range []uint16{0, upgrade.LatestDescriptorVersion} {
for _, epoch := range []uint64{0, 1000, 10_000_000, math.MaxUint64 - 1, math.MaxUint64} {
for _, handler := range []upgrade.HandlerName{"", "descriptor-handler", "tooooooo-long-33-char-description"} {
for _, target := range []version.ProtocolVersions{
{},
{ConsensusProtocol: version.Version{Major: 1, Minor: 2, Patch: 3}},
{
ConsensusProtocol: version.Version{
Major: 0,
Minor: 12,
Patch: 1,
},
RuntimeCommitteeProtocol: version.Version{
Major: 42,
Minor: 0,
Patch: 1,
},
RuntimeHostProtocol: version.Version{
Major: 1,
Minor: 2,
Patch: 3,
},
},
version.Versions,
} {
for _, tx := range []*transaction.Transaction{
governance.NewSubmitProposalTx(nonce, fee, &governance.ProposalContent{
Upgrade: &governance.UpgradeProposal{
Descriptor: upgrade.Descriptor{
Versioned: cbor.NewVersioned(v),
Handler: handler,
Target: target,
Epoch: beacon.EpochTime(epoch),
},
},
}),
} {
valid := valideSubmitProposal(v, epoch, handler, target)
vectors = append(vectors, testvectors.MakeTestVector("SubmitProposal", tx, valid))
}
}
}
}
}
// Generate cancel upgrade proposal transactions.
for _, id := range []uint64{0, 1000, 10_000_000, math.MaxUint64} {
for _, tx := range []*transaction.Transaction{
governance.NewSubmitProposalTx(nonce, fee, &governance.ProposalContent{
CancelUpgrade: &governance.CancelUpgradeProposal{
ProposalID: id,
},
}),
} {
vectors = append(vectors, testvectors.MakeTestVector("SubmitProposal", tx, true))
}
}
// Generate cast vote transactions.
for _, id := range []uint64{0, 1000, 10_000_000, math.MaxUint64} {
for _, vote := range []governance.Vote{
governance.VoteAbstain, governance.VoteYes, governance.VoteNo,
} {
for _, tx := range []*transaction.Transaction{
governance.NewCastVoteTx(nonce, fee, &governance.ProposalVote{
ID: id,
Vote: vote,
}),
} {
valid := valideCastVote(vote)
vectors = append(vectors, testvectors.MakeTestVector("SubmitProposal", tx, valid))
}
}
}
}
}
// Generate output.
jsonOut, err := json.MarshalIndent(&vectors, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error encoding test vectors: %v\n", err)
}
fmt.Printf("%s", jsonOut)
}