@@ -23,157 +23,48 @@ import (
2323 "fmt"
2424
2525 "github.com/onflow/cadence"
26- "github.com/onflow/cadence/common"
27- jsoncdc "github.com/onflow/cadence/encoding/json"
26+ "github.com/onflow/cadence/encoding/ccf"
2827 "github.com/onflow/flow-core-contracts/lib/go/templates"
29- flowsdk "github.com/onflow/flow-go-sdk "
28+ "github.com/onflow/flow-go/fvm/blueprints "
3029 flowgo "github.com/onflow/flow-go/model/flow"
3130)
3231
33- const (
34- contractName = "FlowTransactionScheduler"
35- pendingExecutionEventName = "PendingExecution"
36- )
37-
38- // filterPendingExecutionEvents filters events to only include PendingExecution events
39- func filterPendingExecutionEvents (events []flowsdk.Event , serviceAddress flowgo.Address ) []flowsdk.Event {
40- var filteredEvents []flowsdk.Event
32+ // parseScheduledIDs parses ID of the scheduled transactions from the events
33+ func parseScheduledIDs (env templates.Environment , events flowgo.EventsList ) ([]string , error ) {
34+ const (
35+ processedCallbackIDFieldName = "id"
36+ processedCallbackEffortFieldName = "executionEffort"
37+ )
4138
42- contractLocation := common.AddressLocation {
43- Address : common .Address (serviceAddress ),
44- Name : contractName ,
45- }
46- expectedEventType := string (contractLocation .TypeID (nil , fmt .Sprintf ("%s.%s" , contractName , pendingExecutionEventName )))
39+ ids := make ([]string , 0 )
4740
4841 for _ , event := range events {
49- if event . Type == expectedEventType {
50- filteredEvents = append ( filteredEvents , event )
42+ if blueprints . PendingExecutionEventType ( env ) != event . Type {
43+ continue
5144 }
52- }
53-
54- return filteredEvents
55- }
56-
57- // todo: replace all the functions bellow with flow-go implementation once it's done
58- // issue: https://github.com/onflow/flow-emulator/issues/829
59-
60- func processScheduledTransaction (
61- serviceAddress flowgo.Address ,
62- parentID flowgo.Identifier ,
63- ) flowgo.TransactionBody {
64- env := templates.Environment {
65- FlowTransactionSchedulerAddress : serviceAddress .HexWithPrefix (),
66- }
67-
68- script := templates .GenerateProcessTransactionScript (env )
69-
70- txBuilder := flowgo .NewTransactionBodyBuilder ().
71- SetScript (script ).
72- SetComputeLimit (defaultTransactionMaxGasLimit ).
73- SetPayer (serviceAddress ).
74- AddAuthorizer (serviceAddress ).
75- SetReferenceBlockID (parentID )
76-
77- tx , err := txBuilder .Build ()
78- if err != nil {
79- panic (err )
80- }
81-
82- return * tx
83- }
84-
85- func executeScheduledTransactions (
86- pendingExecutionEvents []flowsdk.Event ,
87- serviceAddress flowgo.Address ,
88- parentID flowgo.Identifier ,
89- ) (transactions []flowgo.TransactionBody , scheduledIDs []string , err error ) {
90- transactions = make ([]flowgo.TransactionBody , 0 )
91- scheduledIDs = make ([]string , 0 )
92-
93- env := templates.Environment {
94- FlowTransactionSchedulerAddress : serviceAddress .HexWithPrefix (),
95- }
96-
97- script := templates .GenerateExecuteTransactionScript (env )
9845
99- for _ , e := range pendingExecutionEvents {
100- id , _ , limit , _ , err := parseSchedulerPendingExecutionEvent (e , serviceAddress )
46+ eventData , err := ccf .Decode (nil , event .Payload )
10147 if err != nil {
102- return nil , nil , err
48+ return nil , fmt . Errorf ( "failed to decode event: %w" , err )
10349 }
10450
105- txBuilder := flowgo .NewTransactionBodyBuilder ().
106- SetScript (script ).
107- AddArgument (id ).
108- SetPayer (serviceAddress ).
109- AddAuthorizer (serviceAddress ).
110- SetReferenceBlockID (parentID ).
111- SetComputeLimit (limit )
112-
113- tx , err := txBuilder .Build ()
114- if err != nil {
115- return nil , nil , err
51+ cadenceEvent , ok := eventData .(cadence.Event )
52+ if ! ok {
53+ return nil , fmt .Errorf ("event data is not a cadence event" )
11654 }
11755
118- transactions = append (transactions , * tx )
119- scheduledIDs = append (scheduledIDs , string (id ))
120- }
121-
122- return transactions , scheduledIDs , nil
123- }
124-
125- // parseSchedulerPendingExecutionEvent parses flow event that is emitted during scheduler
126- // marking the transaction as pending execution.
127- // Returns:
128- // - ID of the transaction encoded as bytes
129- // - The priority of the transaction
130- // - execution effort
131- // - The address of the account that owns the transaction
132- // - error in case the event type is not correct
133- func parseSchedulerPendingExecutionEvent (event flowsdk.Event , serviceAddress flowgo.Address ) ([]byte , uint8 , uint64 , cadence.Address , error ) {
134- contractLocation := common.AddressLocation {
135- Address : common .Address (serviceAddress ),
136- Name : contractName ,
137- }
138- transactionPendingExecutionEvent := contractLocation .TypeID (nil , fmt .Sprintf ("%s.%s" , contractName , pendingExecutionEventName ))
139-
140- const (
141- IDField = "id"
142- priorityField = "priority"
143- executionField = "executionEffort"
144- ownerField = "transactionHandlerOwner"
145- )
146-
147- if event .Type != string (transactionPendingExecutionEvent ) {
148- return nil , 0 , 0 , cadence .BytesToAddress ([]byte {}), fmt .Errorf ("invalid event type, got: %s, expected: %s" , event .Type , transactionPendingExecutionEvent )
149- }
150-
151- id , ok := event .Value .SearchFieldByName (IDField ).(cadence.UInt64 )
152- if ! ok {
153- return nil , 0 , 0 , cadence .BytesToAddress ([]byte {}), fmt .Errorf ("invalid ID value type: %v" , id )
154- }
155-
156- encodedID , err := jsoncdc .Encode (id )
157- if err != nil {
158- return nil , 0 , 0 , cadence .BytesToAddress ([]byte {}), err
159- }
160-
161- priorityRaw , ok := event .Value .SearchFieldByName (priorityField ).(cadence.UInt8 )
162- if ! ok {
163- return nil , 0 , 0 , cadence .BytesToAddress ([]byte {}), fmt .Errorf ("invalid priority value type: %v" , priorityRaw )
164- }
165- priority := uint8 (priorityRaw )
56+ idValue := cadence .SearchFieldByName (
57+ cadenceEvent ,
58+ processedCallbackIDFieldName ,
59+ )
16660
167- effortRaw , ok := event .Value .SearchFieldByName (executionField ).(cadence.UInt64 )
168- if ! ok {
169- return nil , 0 , 0 , cadence .BytesToAddress ([]byte {}), fmt .Errorf ("invalid effort value type: %v" , effortRaw )
170- }
171- executionEffort := uint64 (effortRaw )
61+ id , ok := idValue .(cadence.UInt64 )
62+ if ! ok {
63+ return nil , fmt .Errorf ("id is not uint64" )
64+ }
17265
173- owner , ok := event .Value .SearchFieldByName (ownerField ).(cadence.Address )
174- if ! ok {
175- return nil , 0 , 0 , cadence .BytesToAddress ([]byte {}), fmt .Errorf ("invalid owner value type: %v" , owner )
66+ ids = append (ids , fmt .Sprintf ("%d" , id ))
17667 }
17768
178- return encodedID , priority , executionEffort , owner , nil
69+ return ids , nil
17970}
0 commit comments