-
Notifications
You must be signed in to change notification settings - Fork 179
/
protocol_state.go
102 lines (90 loc) · 2.82 KB
/
protocol_state.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
package unittest
import (
"context"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
mockprotocol "github.com/onflow/flow-go/state/protocol/mock"
)
// FinalizedProtocolStateWithParticipants returns a protocol state with finalized participants
func FinalizedProtocolStateWithParticipants(participants flow.IdentityList) (
*flow.Block, *mockprotocol.Snapshot, *mockprotocol.State, *mockprotocol.Snapshot) {
sealed := BlockFixture()
block := BlockWithParentFixture(sealed.Header)
head := block.Header
// set up protocol snapshot mock
snapshot := &mockprotocol.Snapshot{}
snapshot.On("Identities", mock.Anything).Return(
func(filter flow.IdentityFilter) flow.IdentityList {
return participants.Filter(filter)
},
nil,
)
snapshot.On("Identity", mock.Anything).Return(func(id flow.Identifier) *flow.Identity {
for _, n := range participants {
if n.ID() == id {
return n
}
}
return nil
}, nil)
snapshot.On("Head").Return(
func() *flow.Header {
return head
},
nil,
)
sealedSnapshot := &mockprotocol.Snapshot{}
sealedSnapshot.On("Head").Return(
func() *flow.Header {
return sealed.Header
},
nil,
)
// set up protocol state mock
state := &mockprotocol.State{}
state.On("Final").Return(
func() protocol.Snapshot {
return snapshot
},
)
state.On("Sealed").Return(func() protocol.Snapshot {
return sealedSnapshot
},
)
state.On("AtBlockID", mock.Anything).Return(
func(blockID flow.Identifier) protocol.Snapshot {
return snapshot
},
)
return block, snapshot, state, sealedSnapshot
}
// SealBlock seals a block B by building two blocks on it, the first containing
// a receipt for the block (BR), the second (BS) containing a seal for the block.
// B <- BR(Result_B) <- BS(Seal_B)
// Returns the two generated blocks.
func SealBlock(t *testing.T, st protocol.ParticipantState, block *flow.Block, receipt *flow.ExecutionReceipt, seal *flow.Seal) (br *flow.Header, bs *flow.Header) {
block2 := BlockWithParentFixture(block.Header)
block2.SetPayload(flow.Payload{
Receipts: []*flow.ExecutionReceiptMeta{receipt.Meta()},
Results: []*flow.ExecutionResult{&receipt.ExecutionResult},
})
err := st.Extend(context.Background(), block2)
require.NoError(t, err)
block3 := BlockWithParentFixture(block2.Header)
block3.SetPayload(flow.Payload{
Seals: []*flow.Seal{seal},
})
err = st.Extend(context.Background(), block3)
require.NoError(t, err)
return block2.Header, block3.Header
}
// InsertAndFinalize inserts, then finalizes, the input block.
func InsertAndFinalize(t *testing.T, st protocol.ParticipantState, block *flow.Block) {
err := st.Extend(context.Background(), block)
require.NoError(t, err)
err = st.Finalize(context.Background(), block.ID())
require.NoError(t, err)
}