forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
103 lines (90 loc) · 3.58 KB
/
util_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
package protocol_test
import (
"errors"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
storagemock "github.com/onflow/flow-go/storage/mock"
"github.com/onflow/flow-go/utils/unittest"
)
func TestIsSporkRootSnapshot(t *testing.T) {
t.Run("spork root", func(t *testing.T) {
snapshot := unittest.RootSnapshotFixture(unittest.IdentityListFixture(10, unittest.WithAllRoles()))
isSporkRoot, err := protocol.IsSporkRootSnapshot(snapshot)
require.NoError(t, err)
assert.True(t, isSporkRoot)
})
t.Run("other snapshot", func(t *testing.T) {
snapshot := unittest.RootSnapshotFixture(unittest.IdentityListFixture(10, unittest.WithAllRoles()))
snapshot.Encodable().Head.Height += 1 // modify head height to break equivalence with spork root block height
isSporkRoot, err := protocol.IsSporkRootSnapshot(snapshot)
require.NoError(t, err)
assert.False(t, isSporkRoot)
})
}
// TestOrderedSeals tests that protocol.OrderedSeals returns a list of ordered seals for a payload.
func TestOrderedSeals(t *testing.T) {
t.Run("empty payload", func(t *testing.T) {
payload := flow.EmptyPayload()
headers := storagemock.NewHeaders(t)
ordered, err := protocol.OrderedSeals(&payload, headers)
require.NoError(t, err)
require.Empty(t, ordered)
})
t.Run("block not found error", func(t *testing.T) {
headers := storagemock.NewHeaders(t)
seals := unittest.Seal.Fixtures(10)
payload := unittest.PayloadFixture(unittest.WithSeals(seals...))
headers.On("ByBlockID", mock.Anything).Return(nil, storage.ErrNotFound)
ordered, err := protocol.OrderedSeals(&payload, headers)
require.ErrorIs(t, err, storage.ErrNotFound)
require.Empty(t, ordered)
})
t.Run("unexpected error", func(t *testing.T) {
headers := storagemock.NewHeaders(t)
seals := unittest.Seal.Fixtures(10)
payload := unittest.PayloadFixture(unittest.WithSeals(seals...))
exception := errors.New("exception")
headers.On("ByBlockID", mock.Anything).Return(nil, exception)
ordered, err := protocol.OrderedSeals(&payload, headers)
require.ErrorIs(t, err, exception)
require.Empty(t, ordered)
})
t.Run("already ordered", func(t *testing.T) {
headers := storagemock.NewHeaders(t)
blocks := unittest.ChainFixtureFrom(10, unittest.BlockHeaderFixture())
seals := unittest.Seal.Fixtures(10)
for i, seal := range seals {
seal.BlockID = blocks[i].ID()
headers.On("ByBlockID", seal.BlockID).Return(blocks[i].Header, nil)
}
payload := unittest.PayloadFixture(unittest.WithSeals(seals...))
ordered, err := protocol.OrderedSeals(&payload, headers)
require.NoError(t, err)
require.Equal(t, seals, ordered)
})
t.Run("unordered", func(t *testing.T) {
headers := storagemock.NewHeaders(t)
blocks := unittest.ChainFixtureFrom(10, flow.Genesis(flow.Localnet).Header)
orderedSeals := unittest.Seal.Fixtures(len(blocks))
for i, seal := range orderedSeals {
seal.BlockID = blocks[i].ID()
headers.On("ByBlockID", seal.BlockID).Return(blocks[i].Header, nil)
}
unorderedSeals := make([]*flow.Seal, len(orderedSeals))
copy(unorderedSeals, orderedSeals)
// randomly re-order seals
rand.Shuffle(len(unorderedSeals), func(i, j int) {
unorderedSeals[i], unorderedSeals[j] = unorderedSeals[j], unorderedSeals[i]
})
payload := unittest.PayloadFixture(unittest.WithSeals(unorderedSeals...))
ordered, err := protocol.OrderedSeals(&payload, headers)
require.NoError(t, err)
require.Equal(t, orderedSeals, ordered)
})
}