forked from onflow/flow-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_test.go
156 lines (122 loc) · 4.56 KB
/
block_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
/*
* Flow Emulator
*
* Copyright 2019-2022 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package emulator_test
import (
"fmt"
"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"
)
func TestCommitBlock(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)
// Add tx1 to pending block
err = b.AddTransaction(*tx1)
assert.NoError(t, err)
tx1Result, err := b.GetTransactionResult(tx1.ID())
assert.NoError(t, err)
assert.Equal(t, flow.TransactionStatusPending, tx1Result.Status)
tx2 := flow.NewTransaction().
SetScript([]byte(`transaction { execute { panic("revert!") } }`)).
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)
// Add tx2 to pending block
err = b.AddTransaction(*tx2)
require.NoError(t, err)
tx2Result, err := b.GetTransactionResult(tx2.ID())
assert.NoError(t, err)
assert.Equal(t, flow.TransactionStatusPending, tx2Result.Status)
// Execute tx1
result, err := b.ExecuteNextTransaction()
assert.NoError(t, err)
assert.True(t, result.Succeeded())
// Execute tx2
result, err = b.ExecuteNextTransaction()
assert.NoError(t, err)
assert.True(t, result.Reverted())
// Commit tx1 and tx2 into new block
_, err = b.CommitBlock()
assert.NoError(t, err)
// tx1 status becomes TransactionStatusSealed
tx1Result, err = b.GetTransactionResult(tx1.ID())
require.NoError(t, err)
assert.Equal(t, flow.TransactionStatusSealed, tx1Result.Status)
// tx2 status also becomes TransactionStatusSealed, even though it is reverted
tx2Result, err = b.GetTransactionResult(tx2.ID())
require.NoError(t, err)
assert.Equal(t, flow.TransactionStatusSealed, tx2Result.Status)
assert.Error(t, tx2Result.Error)
}
func TestBlockView(t *testing.T) {
t.Parallel()
const nBlocks = 3
b, err := emulator.NewBlockchain()
require.NoError(t, err)
t.Run("genesis should have 0 view", func(t *testing.T) {
block, err := b.GetBlockByHeight(0)
require.NoError(t, err)
assert.Equal(t, uint64(0), block.Header.Height)
assert.Equal(t, uint64(0), block.Header.View)
})
addTwoScript, _ := deployAndGenerateAddTwoScript(t, b)
// create a few blocks, each with one transaction
for i := 0; i < nBlocks; i++ {
tx := 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 = tx.SignEnvelope(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().Signer())
assert.NoError(t, err)
// Add tx to pending block
err = b.AddTransaction(*tx)
assert.NoError(t, err)
// execute and commit the block
_, _, err = b.ExecuteAndCommitBlock()
require.NoError(t, err)
}
for height := uint64(1); height <= nBlocks+1; height++ {
block, err := b.GetBlockByHeight(height)
require.NoError(t, err)
maxView := height * emulator.MaxViewIncrease
t.Run(fmt.Sprintf("block %d should have view <%d", height, maxView), func(t *testing.T) {
assert.Equal(t, height, block.Header.Height)
assert.LessOrEqual(t, block.Header.View, maxView)
})
}
}