@@ -20,6 +20,7 @@ import (
20
20
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
21
21
tm2events "github.com/gnolang/gno/tm2/pkg/events"
22
22
"github.com/gnolang/gno/tm2/pkg/log"
23
+ tm2std "github.com/gnolang/gno/tm2/pkg/std"
23
24
"github.com/stretchr/testify/assert"
24
25
"github.com/stretchr/testify/require"
25
26
)
@@ -203,7 +204,6 @@ func Render(_ string) string { return str }
203
204
},
204
205
}
205
206
206
- // Call NewDevNode with no package should work
207
207
node , emitter := newTestingDevNode (t , & fooPkg )
208
208
assert .Len (t , node .ListPkgs (), 1 )
209
209
@@ -243,6 +243,82 @@ func Render(_ string) string { return str }
243
243
assert .Equal (t , mock .EvtNull , emitter .NextEvent ().Type ())
244
244
}
245
245
246
+ func TestTxGasFailure (t * testing.T ) {
247
+ fooPkg := gnovm.MemPackage {
248
+ Name : "foo" ,
249
+ Path : "gno.land/r/dev/foo" ,
250
+ Files : []* gnovm.MemFile {
251
+ {
252
+ Name : "foo.gno" ,
253
+ Body : `package foo
254
+ import "strconv"
255
+
256
+ var i int
257
+ func Inc() { i++ } // method to increment i
258
+ func Render(_ string) string { return strconv.Itoa(i) }
259
+ ` ,
260
+ },
261
+ },
262
+ }
263
+
264
+ node , emitter := newTestingDevNode (t , & fooPkg )
265
+ assert .Len (t , node .ListPkgs (), 1 )
266
+
267
+ // Test rendering
268
+ render , err := testingRenderRealm (t , node , "gno.land/r/dev/foo" )
269
+ require .NoError (t , err )
270
+ require .Equal (t , "0" , render )
271
+
272
+ // Call `Inc` to update counter
273
+ msg := vm.MsgCall {
274
+ PkgPath : "gno.land/r/dev/foo" ,
275
+ Func : "Inc" ,
276
+ Args : nil ,
277
+ Send : nil ,
278
+ }
279
+
280
+ res , err := testingCallRealm (t , node , msg )
281
+ require .NoError (t , err )
282
+ require .NoError (t , res .CheckTx .Error )
283
+ require .NoError (t , res .DeliverTx .Error )
284
+ assert .Equal (t , emitter .NextEvent ().Type (), events .EvtTxResult )
285
+
286
+ // Check for correct render update
287
+ render , err = testingRenderRealm (t , node , "gno.land/r/dev/foo" )
288
+ require .NoError (t , err )
289
+ require .Equal (t , "1" , render )
290
+
291
+ // Not Enough gas wanted
292
+ callCfg := gnoclient.BaseTxCfg {
293
+ GasFee : ugnot .ValueString (10000 ), // Gas fee
294
+
295
+ // Ensure sufficient gas is provided for the transaction to be committed.
296
+ // However, avoid providing too much gas to allow the
297
+ // transaction to succeed (OutOfGasError).
298
+ GasWanted : 100_000 ,
299
+ }
300
+
301
+ res , err = testingCallRealmWithConfig (t , node , callCfg , msg )
302
+ require .Error (t , err )
303
+ require .ErrorAs (t , err , & tm2std.OutOfGasError {})
304
+
305
+ // Transaction should be committed regardless the error
306
+ require .Equal (t , emitter .NextEvent ().Type (), events .EvtTxResult ,
307
+ "(probably) not enough gas for the transaction to be committed" )
308
+
309
+ // Reload the node
310
+ err = node .Reload (context .Background ())
311
+ require .NoError (t , err )
312
+ assert .Equal (t , events .EvtReload , emitter .NextEvent ().Type ())
313
+
314
+ // Check for correct render update
315
+ render , err = testingRenderRealm (t , node , "gno.land/r/dev/foo" )
316
+ require .NoError (t , err )
317
+
318
+ // Assert that the previous transaction hasn't succeeded during genesis reload
319
+ require .Equal (t , "1" , render )
320
+ }
321
+
246
322
func TestTxTimestampRecover (t * testing.T ) {
247
323
const fooFile = `
248
324
package foo
@@ -455,17 +531,23 @@ func testingRenderRealm(t *testing.T, node *Node, rlmpath string) (string, error
455
531
func testingCallRealm (t * testing.T , node * Node , msgs ... vm.MsgCall ) (* core_types.ResultBroadcastTxCommit , error ) {
456
532
t .Helper ()
457
533
534
+ defaultCfg := gnoclient.BaseTxCfg {
535
+ GasFee : ugnot .ValueString (1000000 ), // Gas fee
536
+ GasWanted : 3_000_000 , // Gas wanted
537
+ }
538
+
539
+ return testingCallRealmWithConfig (t , node , defaultCfg , msgs ... )
540
+ }
541
+
542
+ func testingCallRealmWithConfig (t * testing.T , node * Node , bcfg gnoclient.BaseTxCfg , msgs ... vm.MsgCall ) (* core_types.ResultBroadcastTxCommit , error ) {
543
+ t .Helper ()
544
+
458
545
signer := newInMemorySigner (t , node .Config ().ChainID ())
459
546
cli := gnoclient.Client {
460
547
Signer : signer ,
461
548
RPCClient : node .Client (),
462
549
}
463
550
464
- txcfg := gnoclient.BaseTxCfg {
465
- GasFee : ugnot .ValueString (1000000 ), // Gas fee
466
- GasWanted : 3_000_000 , // Gas wanted
467
- }
468
-
469
551
// Set Caller in the msgs
470
552
caller , err := signer .Info ()
471
553
require .NoError (t , err )
@@ -474,7 +556,7 @@ func testingCallRealm(t *testing.T, node *Node, msgs ...vm.MsgCall) (*core_types
474
556
vmMsgs = append (vmMsgs , vm .NewMsgCall (caller .GetAddress (), msg .Send , msg .PkgPath , msg .Func , msg .Args ))
475
557
}
476
558
477
- return cli .Call (txcfg , vmMsgs ... )
559
+ return cli .Call (bcfg , vmMsgs ... )
478
560
}
479
561
480
562
func newTestingNodeConfig (pkgs ... * gnovm.MemPackage ) * NodeConfig {
0 commit comments