forked from onflow/flow-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_test.go
90 lines (66 loc) · 2.34 KB
/
collection_test.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
package emulator_test
import (
"testing"
"github.com/onflow/flow-go-sdk"
flowgo "github.com/onflow/flow-go/model/flow"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
emulator "github.com/onflow/flow-emulator"
convert "github.com/onflow/flow-emulator/convert/sdk"
)
func TestCollections(t *testing.T) {
t.Parallel()
t.Run("Empty block", func(t *testing.T) {
t.Parallel()
b, err := emulator.NewBlockchain()
require.NoError(t, err)
block, err := b.CommitBlock()
require.NoError(t, err)
// block should not contain any collections
assert.Empty(t, block.Payload.Guarantees)
})
t.Run("Non-empty block", func(t *testing.T) {
t.Parallel()
b, err := emulator.NewBlockchain(
emulator.WithStorageLimitEnabled(false),
)
require.NoError(t, err)
addTwoScript, _ := deployAndGenerateAddTwoScript(t, b)
tx1 := flow.NewTransaction().
SetScript([]byte(addTwoScript)).
SetGasLimit(flowgo.DefaultMaxTransactionGasLimit).
SetProposalKey(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().SequenceNumber).
SetPayer(b.ServiceKey().Address).
AddAuthorizer(b.ServiceKey().Address)
err = tx1.SignEnvelope(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().Signer())
assert.NoError(t, err)
tx2 := flow.NewTransaction().
SetScript([]byte(addTwoScript)).
SetGasLimit(flowgo.DefaultMaxTransactionGasLimit).
SetProposalKey(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().SequenceNumber).
SetPayer(b.ServiceKey().Address).
AddAuthorizer(b.ServiceKey().Address)
err = tx2.SignEnvelope(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().Signer())
assert.NoError(t, err)
// generate a list of transactions
transactions := []*flow.Transaction{tx1, tx2}
// add all transactions to block
for _, tx := range transactions {
err = b.AddTransaction(*tx)
require.NoError(t, err)
}
block, _, err := b.ExecuteAndCommitBlock()
require.NoError(t, err)
// block should contain at least one collection
assert.NotEmpty(t, block.Payload.Guarantees)
i := 0
for _, guarantee := range block.Payload.Guarantees {
collection, err := b.GetCollection(convert.FlowIdentifierToSDK(guarantee.ID()))
require.NoError(t, err)
for _, txID := range collection.TransactionIDs {
assert.Equal(t, transactions[i].ID(), txID)
i++
}
}
})
}