From f061d3d784f40f59e924f6448c8bf867c8065547 Mon Sep 17 00:00:00 2001 From: Tarak Ben Youssef Date: Fri, 4 Aug 2023 17:09:57 -0600 Subject: [PATCH] add tx test with unsafeRandom --- emulator/transaction_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/emulator/transaction_test.go b/emulator/transaction_test.go index 669027ae..f37e8946 100644 --- a/emulator/transaction_test.go +++ b/emulator/transaction_test.go @@ -2116,3 +2116,37 @@ func TestRollbackTransaction(t *testing.T) { IncrementHelper(t, b, adapter, counterAddress, addTwoScript, 2) } + +// TestTransactionWithCadenceRandom checks Cadence's random function works +func TestTransactionWithCadenceRandom(t *testing.T) { + b, adapter := setupTransactionTests(t) + + code := ` + transaction { + prepare() { + assert(unsafeRandom() >= 0) + } + } + ` + callRandomTx := flowsdk.NewTransaction(). + SetGasLimit(flowgo.DefaultMaxTransactionGasLimit). + SetScript([]byte(code)). + SetProposalKey(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().SequenceNumber). + SetPayer(b.ServiceKey().Address) + + signer, err := b.ServiceKey().Signer() + require.NoError(t, err) + + err = callRandomTx.SignEnvelope(b.ServiceKey().Address, b.ServiceKey().Index, signer) + require.NoError(t, err) + + err = adapter.SendTransaction(context.Background(), *callRandomTx) + assert.NoError(t, err) + + result, err := b.ExecuteNextTransaction() + assert.NoError(t, err) + AssertTransactionSucceeded(t, result) + + _, err = b.CommitBlock() + assert.NoError(t, err) +}