-
Notifications
You must be signed in to change notification settings - Fork 52
Scheduled callbacks #823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Scheduled callbacks #823
Changes from 24 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
3ab3459
add execute and process templates
devbugging bee210b
add callback transactions
devbugging a89b8b8
execution of callbacks
devbugging 2d8c206
add contracts and transactions for scheduler
devbugging f934204
add execution of scheduled callbacks if enabled
devbugging 1c1a3f2
add callbacks logic for parsing events and preparing transactions
devbugging 65a19c2
config wiring
devbugging 5553d6a
licence
devbugging 350b70b
change cancel logic to closure
devbugging 324a73a
remove cancel function
devbugging bc21b1b
update scheduled callback
devbugging 13138f7
rejected callback
devbugging 0afbc9f
update API for scheduling
devbugging 9e78772
add garbage collection of callbacks
devbugging fdefdab
updated contract with singleton pattern
devbugging 3f7a42d
callbacks tests
devbugging c870e31
fix cancel method
devbugging a44bda6
convert uint64
devbugging d48ee51
type id formatting
devbugging ba2fa44
Merge branch 'master' into gregor/schedule-callbacks
nialexsan 645c8be
Update emulator/callbacks.go
nialexsan 66a319e
Update emulator/callbacks_test.go
nialexsan 235470a
Update emulator/callbacks_test.go
nialexsan 9e87889
PR review comments
devbugging 4d5f2e4
update to latest version of FlowCallbackScheduler
joshuahannan d842353
return empty dictionary when there are no low prio callbacks
joshuahannan 65ed6d1
update to latest contract and fix comment
joshuahannan 41a13de
contract 07-31
joshuahannan e0eb1ef
update to latest contract and fix event parameters
joshuahannan 4026a2a
use core contracts version of schedule callbacks cadence code
joshuahannan 67c296d
go mod tidy
joshuahannan f931f3b
update core contracts version
joshuahannan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
devbugging marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Flow Emulator | ||
* | ||
* Copyright Flow Foundation | ||
* | ||
* 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 | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/onflow/cadence" | ||
"github.com/onflow/cadence/common" | ||
jsoncdc "github.com/onflow/cadence/encoding/json" | ||
flowsdk "github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
//go:embed templates/executeCallbackTransaction.cdc | ||
var executeCallbackScript []byte | ||
|
||
//go:embed templates/processCallbackTransaction.cdc | ||
var processCallbackScript []byte | ||
|
||
const ( | ||
contractName = "UnsafeCallbackScheduler" | ||
callbackProcessedEvent = "CallbackProcessed" | ||
) | ||
|
||
// todo: replace all the functions bellow with flow-go implementation once it's done | ||
// issue: https://github.com/onflow/flow-emulator/issues/829 | ||
|
||
func processCallbackTransaction( | ||
serviceAddress flow.Address, | ||
parentID flow.Identifier, | ||
) flow.TransactionBody { | ||
script := replaceSchedulerAddress(processCallbackScript, serviceAddress) | ||
|
||
return *flow.NewTransactionBody(). | ||
SetScript(script). | ||
SetComputeLimit(defaultTransactionMaxGasLimit). | ||
SetPayer(serviceAddress). | ||
SetReferenceBlockID(parentID) | ||
} | ||
|
||
func executeCallbackTransactions( | ||
scheduleEvent []flowsdk.Event, | ||
serviceAddress flow.Address, | ||
parentID flow.Identifier, | ||
) ([]flow.TransactionBody, error) { | ||
var transactions []flow.TransactionBody | ||
script := replaceSchedulerAddress(executeCallbackScript, serviceAddress) | ||
|
||
for _, e := range scheduleEvent { | ||
limit, id, err := parseSchedulerProcessedEvent(e, serviceAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tx := flow.NewTransactionBody(). | ||
SetScript(script). | ||
AddArgument(id). | ||
SetPayer(serviceAddress). | ||
SetReferenceBlockID(parentID). | ||
SetComputeLimit(limit) | ||
|
||
transactions = append(transactions, *tx) | ||
} | ||
|
||
return transactions, nil | ||
} | ||
|
||
// parseSchedulerProcessedEvent parses flow event that is emitted during scheduler | ||
// marking the callback as scheduled. | ||
// Returns: | ||
// - execution effort | ||
// - ID of the event encoded as bytes | ||
// - error in case the event type is not correct | ||
func parseSchedulerProcessedEvent(event flowsdk.Event, serviceAddress flow.Address) (uint64, []byte, error) { | ||
contractLocation := common.AddressLocation{ | ||
Address: common.Address(serviceAddress), | ||
Name: contractName, | ||
} | ||
callbackScheduledEvent := contractLocation.TypeID(nil, fmt.Sprintf("%s.%s", contractName, callbackProcessedEvent)) | ||
|
||
const ( | ||
IDField = "ID" | ||
executionField = "executionEffort" | ||
) | ||
|
||
if event.Type != string(callbackScheduledEvent) { | ||
return 0, nil, fmt.Errorf("invalid event type, got: %s, expected: %s", event.Type, callbackScheduledEvent) | ||
} | ||
|
||
id, ok := event.Value.SearchFieldByName(IDField).(cadence.UInt64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Instead of searching linearly for two fields, maybe use |
||
if !ok { | ||
return 0, nil, fmt.Errorf("invalid ID value type: %v", id) | ||
} | ||
|
||
encodedID, err := jsoncdc.Encode(id) | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
|
||
effort, ok := event.Value.SearchFieldByName(executionField).(cadence.UInt64) | ||
if !ok { | ||
return 0, nil, fmt.Errorf("invalid effort value type: %v", effort) | ||
} | ||
computeLimit := uint64(effort) | ||
|
||
return computeLimit, encodedID, nil | ||
} | ||
|
||
func replaceSchedulerAddress(script []byte, serviceAddress flow.Address) []byte { | ||
devbugging marked this conversation as resolved.
Show resolved
Hide resolved
|
||
s := strings.ReplaceAll( | ||
string(script), | ||
`import "UnsafeCallbackScheduler"`, | ||
fmt.Sprintf("import UnsafeCallbackScheduler from %s", serviceAddress.HexWithPrefix()), | ||
) | ||
|
||
return []byte(s) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.