-
Notifications
You must be signed in to change notification settings - Fork 175
/
events.go
115 lines (96 loc) · 2.75 KB
/
events.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
package generator
import (
"fmt"
"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/ccf"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/unittest"
)
type EventGeneratorOption func(*Events)
func WithEncoding(encoding entities.EventEncodingVersion) EventGeneratorOption {
return func(g *Events) {
g.encoding = encoding
}
}
type Events struct {
count uint32
ids *Identifiers
encoding entities.EventEncodingVersion
}
func EventGenerator(opts ...EventGeneratorOption) *Events {
g := &Events{
count: 1,
ids: IdentifierGenerator(),
encoding: entities.EventEncodingVersion_CCF_V0,
}
for _, opt := range opts {
opt(g)
}
return g
}
func (g *Events) New() flow.Event {
address, err := common.BytesToAddress(unittest.RandomAddressFixture().Bytes())
if err != nil {
panic(fmt.Sprintf("unexpected error while creating random address: %s", err))
}
location := common.NewAddressLocation(nil, address, "TestContract")
identifier := fmt.Sprintf("TestContract.FooEvent%d", g.count)
typeID := location.TypeID(nil, identifier)
testEventType := &cadence.EventType{
Location: location,
QualifiedIdentifier: identifier,
Fields: []cadence.Field{
{
Identifier: "a",
Type: cadence.IntType{},
},
{
Identifier: "b",
Type: cadence.StringType{},
},
},
}
fooString, err := cadence.NewString("foo")
if err != nil {
panic(fmt.Sprintf("unexpected error while creating cadence string: %s", err))
}
testEvent := cadence.NewEvent(
[]cadence.Value{
cadence.NewInt(int(g.count)),
fooString,
}).WithType(testEventType)
var payload []byte
switch g.encoding {
case entities.EventEncodingVersion_CCF_V0:
payload, err = ccf.Encode(testEvent)
if err != nil {
panic(fmt.Sprintf("unexpected error while ccf encoding events: %s", err))
}
case entities.EventEncodingVersion_JSON_CDC_V0:
payload, err = jsoncdc.Encode(testEvent)
if err != nil {
panic(fmt.Sprintf("unexpected error while json encoding events: %s", err))
}
}
event := flow.Event{
Type: flow.EventType(typeID),
TransactionID: g.ids.New(),
TransactionIndex: g.count,
EventIndex: g.count,
Payload: payload,
}
g.count++
return event
}
// GetEventsWithEncoding generates a specified number of events with a given encoding version.
func GetEventsWithEncoding(n int, version entities.EventEncodingVersion) []flow.Event {
eventGenerator := EventGenerator(WithEncoding(version))
events := make([]flow.Event, 0, n)
for i := 0; i < n; i++ {
events = append(events, eventGenerator.New())
}
return events
}