forked from onflow/flow-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_info_test.go
102 lines (82 loc) · 3.05 KB
/
block_info_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
/*
* 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 TestBlockInfo(t *testing.T) {
t.Parallel()
b, err := emulator.NewBlockchain()
require.NoError(t, err)
block1, err := b.CommitBlock()
require.NoError(t, err)
block2, err := b.CommitBlock()
require.NoError(t, err)
t.Run("works as transaction", func(t *testing.T) {
tx := flow.NewTransaction().
SetScript([]byte(`
transaction {
execute {
let block = getCurrentBlock()
log(block)
let lastBlock = getBlock(at: block.height - UInt64(1))
log(lastBlock)
}
}
`)).
SetGasLimit(flowgo.DefaultMaxTransactionGasLimit).
SetProposalKey(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().SequenceNumber).
SetPayer(b.ServiceKey().Address)
err = tx.SignEnvelope(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().Signer())
assert.NoError(t, err)
err = b.AddTransaction(*tx)
require.NoError(t, err)
result, err := b.ExecuteNextTransaction()
assert.NoError(t, err)
assertTransactionSucceeded(t, result)
require.Len(t, result.Logs, 2)
assert.Equal(t, fmt.Sprintf("Block(height: %v, view: %v, id: 0x%x, timestamp: %.8f)", block2.Header.Height+1,
b.PendingBlockView(), b.PendingBlockID(), float64(b.PendingBlockTimestamp().Unix())), result.Logs[0])
assert.Equal(t, fmt.Sprintf("Block(height: %v, view: %v, id: 0x%x, timestamp: %.8f)", block2.Header.Height,
block2.Header.View, block2.ID(), float64(block2.Header.Timestamp.Unix())), result.Logs[1])
})
t.Run("works as script", func(t *testing.T) {
script := []byte(`
pub fun main() {
let block = getCurrentBlock()
log(block)
let lastBlock = getBlock(at: block.height - UInt64(1))
log(lastBlock)
}
`)
result, err := b.ExecuteScript(script, nil)
assert.NoError(t, err)
assert.True(t, result.Succeeded())
require.Len(t, result.Logs, 2)
assert.Equal(t, fmt.Sprintf("Block(height: %v, view: %v, id: 0x%x, timestamp: %.8f)", block2.Header.Height,
block2.Header.View, block2.ID(), float64(block2.Header.Timestamp.Unix())), result.Logs[0])
assert.Equal(t, fmt.Sprintf("Block(height: %v, view: %v, id: 0x%x, timestamp: %.8f)", block1.Header.Height,
block1.Header.View, block1.ID(), float64(block1.Header.Timestamp.Unix())), result.Logs[1])
})
}