1
1
package mcms_test
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
- "fmt"
6
6
"testing"
7
- "time"
8
7
9
8
"github.com/ethereum/go-ethereum/common"
10
9
"github.com/go-playground/validator/v10"
@@ -13,19 +12,27 @@ import (
13
12
14
13
"github.com/smartcontractkit/mcms"
15
14
"github.com/smartcontractkit/mcms/internal/testutils/chaintest"
16
- "github.com/smartcontractkit/mcms/internal/utils/safecast"
17
15
"github.com/smartcontractkit/mcms/types"
18
16
)
19
17
20
18
func TestProposalBuilder (t * testing.T ) {
21
19
t .Parallel ()
22
20
23
21
// Define a fixed validUntil timestamp for consistency
24
- fixedValidUntil := int64 (1893456000 ) // January 1, 2030
25
- fixedValidUntilCasted , err := safecast .Int64ToUint32 (fixedValidUntil )
26
- require .NoError (t , err )
27
- pastValidUntilCast , err := safecast .Int64ToUint32 (time .Now ().Add (- 24 * time .Hour ).Unix ())
28
- require .NoError (t , err )
22
+ validUntil := uint32 (1893456000 ) // January 1, 2030
23
+
24
+ tx1 := types.Transaction {
25
+ To : "0x123" ,
26
+ Data : []byte {0x01 },
27
+ AdditionalFields : json .RawMessage (`{"value": 0}` ),
28
+ }
29
+
30
+ tx2 := types.Transaction {
31
+ To : "0x456" ,
32
+ Data : []byte {0x02 },
33
+ AdditionalFields : json .RawMessage (`{"value": 0}` ),
34
+ }
35
+
29
36
tests := []struct {
30
37
name string
31
38
setup func (* mcms.ProposalBuilder )
@@ -36,20 +43,20 @@ func TestProposalBuilder(t *testing.T) {
36
43
name : "valid Proposal" ,
37
44
setup : func (b * mcms.ProposalBuilder ) {
38
45
b .SetVersion ("v1" ).
39
- SetValidUntil (fixedValidUntilCasted ).
46
+ SetValidUntil (validUntil ).
40
47
SetDescription ("Valid Proposal" ).
41
48
SetOverridePreviousRoot (false ).
42
49
AddChainMetadata (chaintest .Chain2Selector , types.ChainMetadata {StartingOpCount : 0 }).
43
50
AddOperation (types.Operation {
44
51
ChainSelector : chaintest .Chain2Selector ,
45
- Transaction : types. Transaction { Data : [] byte { 0x01 }} ,
52
+ Transaction : tx1 ,
46
53
})
47
54
},
48
55
want : & mcms.Proposal {
49
56
BaseProposal : mcms.BaseProposal {
50
57
Version : "v1" ,
51
58
Kind : types .KindProposal ,
52
- ValidUntil : fixedValidUntilCasted ,
59
+ ValidUntil : validUntil ,
53
60
Description : "Valid Proposal" ,
54
61
OverridePreviousRoot : false ,
55
62
ChainMetadata : map [types.ChainSelector ]types.ChainMetadata {
@@ -59,36 +66,36 @@ func TestProposalBuilder(t *testing.T) {
59
66
Operations : []types.Operation {
60
67
{
61
68
ChainSelector : chaintest .Chain2Selector ,
62
- Transaction : types. Transaction { Data : [] byte { 0x01 }} ,
69
+ Transaction : tx1 ,
63
70
},
64
71
},
65
72
},
66
73
wantErrs : nil ,
67
74
},
68
75
{
69
- name : "valid Proposal using SetTransactions " ,
76
+ name : "valid Proposal using SetOperations " ,
70
77
setup : func (b * mcms.ProposalBuilder ) {
71
78
b .SetVersion ("v1" ).
72
- SetValidUntil (fixedValidUntilCasted ).
79
+ SetValidUntil (validUntil ).
73
80
SetDescription ("Valid Proposal" ).
74
81
SetOverridePreviousRoot (false ).
75
82
AddChainMetadata (chaintest .Chain2Selector , types.ChainMetadata {StartingOpCount : 0 }).
76
83
SetOperations ([]types.Operation {
77
84
{
78
85
ChainSelector : chaintest .Chain2Selector ,
79
- Transaction : types. Transaction { Data : [] byte { 0x01 }} ,
86
+ Transaction : tx1 ,
80
87
},
81
88
{
82
89
ChainSelector : chaintest .Chain2Selector ,
83
- Transaction : types. Transaction { Data : [] byte { 0x02 }} ,
90
+ Transaction : tx2 ,
84
91
},
85
92
})
86
93
},
87
94
want : & mcms.Proposal {
88
95
BaseProposal : mcms.BaseProposal {
89
96
Version : "v1" ,
90
97
Kind : types .KindProposal ,
91
- ValidUntil : fixedValidUntilCasted ,
98
+ ValidUntil : validUntil ,
92
99
Description : "Valid Proposal" ,
93
100
OverridePreviousRoot : false ,
94
101
ChainMetadata : map [types.ChainSelector ]types.ChainMetadata {
@@ -98,11 +105,11 @@ func TestProposalBuilder(t *testing.T) {
98
105
Operations : []types.Operation {
99
106
{
100
107
ChainSelector : chaintest .Chain2Selector ,
101
- Transaction : types. Transaction { Data : [] byte { 0x01 }} ,
108
+ Transaction : tx1 ,
102
109
},
103
110
{
104
111
ChainSelector : chaintest .Chain2Selector ,
105
- Transaction : types. Transaction { Data : [] byte { 0x02 }} ,
112
+ Transaction : tx2 ,
106
113
},
107
114
},
108
115
},
@@ -112,7 +119,7 @@ func TestProposalBuilder(t *testing.T) {
112
119
name : "valid Proposal with signature and set chain metadata" ,
113
120
setup : func (b * mcms.ProposalBuilder ) {
114
121
b .SetVersion ("v1" ).
115
- SetValidUntil (fixedValidUntilCasted ).
122
+ SetValidUntil (validUntil ).
116
123
SetDescription ("Valid Proposal" ).
117
124
SetOverridePreviousRoot (false ).
118
125
SetChainMetadata (map [types.ChainSelector ]types.ChainMetadata {
@@ -121,11 +128,11 @@ func TestProposalBuilder(t *testing.T) {
121
128
SetOperations ([]types.Operation {
122
129
{
123
130
ChainSelector : chaintest .Chain2Selector ,
124
- Transaction : types. Transaction { Data : [] byte { 0x01 }} ,
131
+ Transaction : tx1 ,
125
132
},
126
133
{
127
134
ChainSelector : chaintest .Chain2Selector ,
128
- Transaction : types. Transaction { Data : [] byte { 0x02 }} ,
135
+ Transaction : tx2 ,
129
136
},
130
137
}).
131
138
AddSignature (types.Signature {
@@ -138,7 +145,7 @@ func TestProposalBuilder(t *testing.T) {
138
145
BaseProposal : mcms.BaseProposal {
139
146
Version : "v1" ,
140
147
Kind : types .KindProposal ,
141
- ValidUntil : fixedValidUntilCasted ,
148
+ ValidUntil : validUntil ,
142
149
Description : "Valid Proposal" ,
143
150
OverridePreviousRoot : false ,
144
151
Signatures : []types.Signature {{
@@ -153,84 +160,33 @@ func TestProposalBuilder(t *testing.T) {
153
160
Operations : []types.Operation {
154
161
{
155
162
ChainSelector : chaintest .Chain2Selector ,
156
- Transaction : types. Transaction { Data : [] byte { 0x01 }} ,
163
+ Transaction : tx1 ,
157
164
},
158
165
{
159
166
ChainSelector : chaintest .Chain2Selector ,
160
- Transaction : types. Transaction { Data : [] byte { 0x02 }} ,
167
+ Transaction : tx2 ,
161
168
},
162
169
},
163
170
},
164
171
wantErrs : nil ,
165
172
},
166
173
{
167
- name : "Missing Version " ,
174
+ name : "validation error " ,
168
175
setup : func (b * mcms.ProposalBuilder ) {
169
- b .SetValidUntil (fixedValidUntilCasted ).
170
- SetDescription ("Missing Version " ).
176
+ b .SetValidUntil (validUntil ).
177
+ SetDescription ("Valid Proposal " ).
171
178
SetOverridePreviousRoot (false ).
172
179
AddChainMetadata (chaintest .Chain2Selector , types.ChainMetadata {StartingOpCount : 0 }).
173
180
AddOperation (types.Operation {
174
181
ChainSelector : chaintest .Chain2Selector ,
175
- Transaction : types. Transaction { Data : [] byte { 0x01 }} ,
182
+ Transaction : tx1 ,
176
183
})
177
184
},
178
185
want : nil ,
179
186
wantErrs : []string {
180
187
"Key: 'Proposal.BaseProposal.Version' Error:Field validation for 'Version' failed on the 'required' tag" ,
181
188
},
182
189
},
183
- {
184
- name : "ValidUntil in Past" ,
185
- setup : func (b * mcms.ProposalBuilder ) {
186
- b .SetVersion ("v1" ).
187
- SetValidUntil (pastValidUntilCast ).
188
- SetDescription ("ValidUntil in Past" ).
189
- SetOverridePreviousRoot (false ).
190
- AddChainMetadata (chaintest .Chain2Selector , types.ChainMetadata {StartingOpCount : 0 }).
191
- AddOperation (types.Operation {
192
- ChainSelector : chaintest .Chain2Selector ,
193
- Transaction : types.Transaction {Data : []byte {0x01 }},
194
- })
195
- },
196
- want : nil ,
197
- wantErrs : []string {
198
- fmt .Sprintf ("invalid valid until: %d" , pastValidUntilCast ),
199
- },
200
- },
201
- {
202
- name : "No Transactions" ,
203
- setup : func (b * mcms.ProposalBuilder ) {
204
- b .SetVersion ("v1" ).
205
- SetValidUntil (fixedValidUntilCasted ).
206
- SetDescription ("No Transactions" ).
207
- SetOverridePreviousRoot (false ).
208
- AddChainMetadata (chaintest .Chain2Selector , types.ChainMetadata {StartingOpCount : 0 })
209
- // No transactions added
210
- },
211
- want : nil ,
212
- wantErrs : []string {
213
- "Key: 'Proposal.Operations' Error:Field validation for 'Operations' failed on the 'min' tag" ,
214
- },
215
- },
216
- {
217
- name : "Missing ChainMetadata" ,
218
- setup : func (b * mcms.ProposalBuilder ) {
219
- b .SetVersion ("v1" ).
220
- SetValidUntil (fixedValidUntilCasted ).
221
- SetDescription ("Missing ChainMetadata" ).
222
- SetOverridePreviousRoot (false ).
223
- // ChainMetadata is not added
224
- AddOperation (types.Operation {
225
- ChainSelector : chaintest .Chain2Selector ,
226
- Transaction : types.Transaction {Data : []byte {0x01 }},
227
- })
228
- },
229
- want : nil ,
230
- wantErrs : []string {
231
- "Key: 'Proposal.BaseProposal.ChainMetadata' Error:Field validation for 'ChainMetadata' failed on the 'min' tag" ,
232
- },
233
- },
234
190
}
235
191
236
192
for _ , tt := range tests {
0 commit comments