forked from onflow/flow-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_test.go
177 lines (130 loc) · 4.57 KB
/
init_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package emulator_test
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/templates"
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"
"github.com/onflow/flow-emulator/storage/badger"
)
func TestInitialization(t *testing.T) {
t.Parallel()
t.Run("should inject initial state when initialized with empty store", func(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "badger-test")
require.Nil(t, err)
defer os.RemoveAll(dir)
store, err := badger.New(badger.WithPath(dir))
require.Nil(t, err)
defer store.Close()
b, _ := emulator.NewBlockchain(emulator.WithStore(store))
serviceAcct, err := b.GetAccount(flow.ServiceAddress(flow.Emulator))
require.NoError(t, err)
assert.NotNil(t, serviceAcct)
latestBlock, err := b.GetLatestBlock()
require.NoError(t, err)
assert.EqualValues(t, 0, latestBlock.Header.Height)
assert.Equal(t,
flowgo.Genesis(flowgo.Emulator).ID(),
latestBlock.ID(),
)
})
t.Run("should restore state when initialized with non-empty store", func(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "badger-test")
require.Nil(t, err)
defer os.RemoveAll(dir)
store, err := badger.New(badger.WithPath(dir))
require.Nil(t, err)
defer store.Close()
b, _ := emulator.NewBlockchain(emulator.WithStore(store), emulator.WithStorageLimitEnabled(false))
contracts := []templates.Contract{
{
Name: "Counting",
Source: counterScript,
},
}
counterAddress, err := b.CreateAccount(nil, contracts)
require.NoError(t, err)
// Submit a transaction adds some ledger state and event state
script := fmt.Sprintf(
`
import 0x%s
transaction {
prepare(acct: AuthAccount) {
let counter <- Counting.createCounter()
counter.add(1)
acct.save(<-counter, to: /storage/counter)
acct.link<&Counting.Counter>(/public/counter, target: /storage/counter)
}
}
`,
counterAddress,
)
tx := flow.NewTransaction().
SetScript([]byte(script)).
SetGasLimit(flowgo.DefaultMaxTransactionGasLimit).
SetProposalKey(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().SequenceNumber).
SetPayer(b.ServiceKey().Address).
AddAuthorizer(b.ServiceKey().Address)
err = tx.SignEnvelope(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().Signer())
require.NoError(t, err)
err = b.AddTransaction(*tx)
require.NoError(t, err)
result, err := b.ExecuteNextTransaction()
assert.NoError(t, err)
require.True(t, result.Succeeded())
block, err := b.CommitBlock()
assert.NoError(t, err)
require.NotNil(t, block)
minedTx, err := b.GetTransaction(tx.ID())
require.NoError(t, err)
minedEvents, err := b.GetEventsByHeight(block.Header.Height, "")
require.NoError(t, err)
// Create a new blockchain with the same store
b, _ = emulator.NewBlockchain(emulator.WithStore(store))
t.Run("should be able to read blocks", func(t *testing.T) {
latestBlock, err := b.GetLatestBlock()
require.NoError(t, err)
assert.Equal(t, block.ID(), latestBlock.ID())
blockByHeight, err := b.GetBlockByHeight(block.Header.Height)
require.NoError(t, err)
assert.Equal(t, block.ID(), blockByHeight.ID())
blockByHash, err := b.GetBlockByID(convert.FlowIdentifierToSDK(block.ID()))
require.NoError(t, err)
assert.Equal(t, block.ID(), blockByHash.ID())
})
t.Run("should be able to read transactions", func(t *testing.T) {
txByHash, err := b.GetTransaction(tx.ID())
require.NoError(t, err)
assert.Equal(t, minedTx, txByHash)
})
t.Run("should be able to read events", func(t *testing.T) {
gotEvents, err := b.GetEventsByHeight(block.Header.Height, "")
require.NoError(t, err)
assert.Equal(t, minedEvents, gotEvents)
})
t.Run("should be able to read ledger state", func(t *testing.T) {
readScript := fmt.Sprintf(
`
import 0x%s
pub fun main(): Int {
return getAccount(0x%s).getCapability(/public/counter)!.borrow<&Counting.Counter>()?.count ?? 0
}
`,
counterAddress,
b.ServiceKey().Address,
)
result, err := b.ExecuteScript([]byte(readScript), nil)
require.NoError(t, err)
assert.Equal(t, cadence.NewInt(1), result.Value)
})
})
}