@@ -26,7 +26,7 @@ func TestContract_ChildMergeFunctionality(t *testing.T) {
26
26
err := accounts .Create (nil , address )
27
27
require .NoError (t , err )
28
28
29
- contractHandler := handler .NewContractHandler (accounts , func () bool { return false }, nil , nil )
29
+ contractHandler := handler .NewContractHandler (accounts , func () bool { return false }, nil , nil , nil )
30
30
31
31
// no contract initially
32
32
names , err := contractHandler .GetContractNames (rAdd )
@@ -68,35 +68,141 @@ func TestContract_ChildMergeFunctionality(t *testing.T) {
68
68
cont , err = contractHandler .GetContract (rAdd , "testContract" )
69
69
require .NoError (t , err )
70
70
require .Equal (t , cont , []byte ("ABC" ))
71
+
72
+ // remove
73
+ err = contractHandler .RemoveContract (rAdd , "testContract" , nil )
74
+ require .NoError (t , err )
75
+
76
+ // contract still there because no commit yet
77
+ cont , err = contractHandler .GetContract (rAdd , "testContract" )
78
+ require .NoError (t , err )
79
+ require .Equal (t , cont , []byte ("ABC" ))
80
+
81
+ // commit removal
82
+ _ , err = contractHandler .Commit ()
83
+ require .NoError (t , err )
84
+
85
+ // contract should no longer be there
86
+ cont , err = contractHandler .GetContract (rAdd , "testContract" )
87
+ require .NoError (t , err )
88
+ require .Equal (t , []byte (nil ), cont )
71
89
}
72
90
73
91
func TestContract_AuthorizationFunctionality (t * testing.T ) {
74
92
sth := state .NewStateHolder (state .NewState (utils .NewSimpleView ()))
75
93
accounts := state .NewAccounts (sth )
76
- address := flow .HexToAddress ("01" )
77
- rAdd := runtime .Address (address )
78
- err := accounts .Create (nil , address )
79
- require .NoError (t , err )
80
94
81
- unAuthAdd := flow .HexToAddress ("02 " )
82
- unAuthRAdd := runtime .Address (unAuthAdd )
83
- err = accounts .Create (nil , unAuthAdd )
95
+ authAdd := flow .HexToAddress ("01 " )
96
+ rAdd := runtime .Address (authAdd )
97
+ err : = accounts .Create (nil , authAdd )
84
98
require .NoError (t , err )
85
99
86
- contractHandler := handler . NewContractHandler ( accounts ,
87
- func () bool { return true },
88
- func () []common. Address { return []common. Address { rAdd } },
89
- func ( address runtime. Address , code [] byte ) ( bool , error ) { return false , nil } )
100
+ authRemove := flow . HexToAddress ( "02" )
101
+ rRemove := runtime . Address ( authRemove )
102
+ err = accounts . Create ( nil , authRemove )
103
+ require . NoError ( t , err )
90
104
91
- // try to set contract by an unAuthRAdd
92
- err = contractHandler . SetContract ( rAdd , "testContract1" , [] byte ( "ABC" ), []common. Address { unAuthRAdd } )
93
- require . Error ( t , err )
94
- require .False (t , contractHandler . HasUpdates () )
105
+ authBoth := flow . HexToAddress ( "03" )
106
+ rBoth := runtime . Address ( authBoth )
107
+ err = accounts . Create ( nil , authBoth )
108
+ require .NoError (t , err )
95
109
96
- // set contract by an authorized account
97
- err = contractHandler .SetContract (rAdd , "testContract2" , []byte ("ABC" ), []common.Address {rAdd })
110
+ unAuth := flow .HexToAddress ("04" )
111
+ unAuthR := runtime .Address (unAuth )
112
+ err = accounts .Create (nil , unAuth )
98
113
require .NoError (t , err )
99
- require .True (t , contractHandler .HasUpdates ())
114
+
115
+ makeHandler := func () * handler.ContractHandler {
116
+ return handler .NewContractHandler (accounts ,
117
+ func () bool { return true },
118
+ func () []common.Address { return []common.Address {rAdd , rBoth } },
119
+ func () []common.Address { return []common.Address {rRemove , rBoth } },
120
+ func (address runtime.Address , code []byte ) (bool , error ) { return false , nil })
121
+ }
122
+
123
+ t .Run ("try to set contract with unauthorized account" , func (t * testing.T ) {
124
+ contractHandler := makeHandler ()
125
+
126
+ err = contractHandler .SetContract (rAdd , "testContract1" , []byte ("ABC" ), []common.Address {unAuthR })
127
+ require .Error (t , err )
128
+ require .False (t , contractHandler .HasUpdates ())
129
+ })
130
+
131
+ t .Run ("try to set contract with account only authorized for removal" , func (t * testing.T ) {
132
+ contractHandler := makeHandler ()
133
+
134
+ err = contractHandler .SetContract (rAdd , "testContract1" , []byte ("ABC" ), []common.Address {rRemove })
135
+ require .Error (t , err )
136
+ require .False (t , contractHandler .HasUpdates ())
137
+ })
138
+
139
+ t .Run ("set contract with account authorized for adding" , func (t * testing.T ) {
140
+ contractHandler := makeHandler ()
141
+
142
+ err = contractHandler .SetContract (rAdd , "testContract2" , []byte ("ABC" ), []common.Address {rAdd })
143
+ require .NoError (t , err )
144
+ require .True (t , contractHandler .HasUpdates ())
145
+ })
146
+
147
+ t .Run ("set contract with account authorized for adding and removing" , func (t * testing.T ) {
148
+ contractHandler := makeHandler ()
149
+
150
+ err = contractHandler .SetContract (rAdd , "testContract2" , []byte ("ABC" ), []common.Address {rBoth })
151
+ require .NoError (t , err )
152
+ require .True (t , contractHandler .HasUpdates ())
153
+ })
154
+
155
+ t .Run ("try to remove contract with unauthorized account" , func (t * testing.T ) {
156
+ contractHandler := makeHandler ()
157
+
158
+ err = contractHandler .SetContract (rAdd , "testContract1" , []byte ("ABC" ), []common.Address {rAdd })
159
+ require .NoError (t , err )
160
+ _ , err = contractHandler .Commit ()
161
+ require .NoError (t , err )
162
+
163
+ err = contractHandler .RemoveContract (unAuthR , "testContract2" , []common.Address {unAuthR })
164
+ require .Error (t , err )
165
+ require .False (t , contractHandler .HasUpdates ())
166
+ })
167
+
168
+ t .Run ("remove contract account authorized for removal" , func (t * testing.T ) {
169
+ contractHandler := makeHandler ()
170
+
171
+ err = contractHandler .SetContract (rAdd , "testContract1" , []byte ("ABC" ), []common.Address {rAdd })
172
+ require .NoError (t , err )
173
+ _ , err = contractHandler .Commit ()
174
+ require .NoError (t , err )
175
+
176
+ err = contractHandler .RemoveContract (rRemove , "testContract2" , []common.Address {rRemove })
177
+ require .NoError (t , err )
178
+ require .True (t , contractHandler .HasUpdates ())
179
+ })
180
+
181
+ t .Run ("try to remove contract with account only authorized for adding" , func (t * testing.T ) {
182
+ contractHandler := makeHandler ()
183
+
184
+ err = contractHandler .SetContract (rAdd , "testContract1" , []byte ("ABC" ), []common.Address {rAdd })
185
+ require .NoError (t , err )
186
+ _ , err = contractHandler .Commit ()
187
+ require .NoError (t , err )
188
+
189
+ err = contractHandler .RemoveContract (rAdd , "testContract2" , []common.Address {rAdd })
190
+ require .Error (t , err )
191
+ require .False (t , contractHandler .HasUpdates ())
192
+ })
193
+
194
+ t .Run ("remove contract with account authorized for adding and removing" , func (t * testing.T ) {
195
+ contractHandler := makeHandler ()
196
+
197
+ err = contractHandler .SetContract (rAdd , "testContract1" , []byte ("ABC" ), []common.Address {rAdd })
198
+ require .NoError (t , err )
199
+ _ , err = contractHandler .Commit ()
200
+ require .NoError (t , err )
201
+
202
+ err = contractHandler .RemoveContract (rBoth , "testContract2" , []common.Address {rBoth })
203
+ require .NoError (t , err )
204
+ require .True (t , contractHandler .HasUpdates ())
205
+ })
100
206
}
101
207
102
208
func TestContract_DeploymentVouchers (t * testing.T ) {
@@ -120,6 +226,9 @@ func TestContract_DeploymentVouchers(t *testing.T) {
120
226
func () []common.Address {
121
227
return []common.Address {}
122
228
},
229
+ func () []common.Address {
230
+ return []common.Address {}
231
+ },
123
232
func (address runtime.Address , code []byte ) (bool , error ) {
124
233
if address .String () == addressWithVoucher .String () {
125
234
return true , nil
@@ -170,6 +279,9 @@ func TestContract_ContractUpdate(t *testing.T) {
170
279
func () []common.Address {
171
280
return []common.Address {}
172
281
},
282
+ func () []common.Address {
283
+ return []common.Address {}
284
+ },
173
285
func (address runtime.Address , code []byte ) (bool , error ) {
174
286
// Ensure the voucher check is only called once,
175
287
// for the initial contract deployment,
@@ -234,6 +346,7 @@ func TestContract_DeterministicErrorOnCommit(t *testing.T) {
234
346
func () bool { return false },
235
347
nil ,
236
348
nil ,
349
+ nil ,
237
350
)
238
351
239
352
address1 := runtime .Address (flow .HexToAddress ("0000000000000001" ))
0 commit comments