Skip to content
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

[Exec] Use CCF in self-describing mode to encode events (replaces JSON-CDC) #4417

Merged
merged 18 commits into from
Jun 6, 2023

Conversation

fxamacker
Copy link
Member

@fxamacker fxamacker commented Jun 2, 2023

Updates onflow/cadence#2283

This PR uses CCF in fully self-describing mode, so events will encode to about 1/2 the size of JSON-CDC encoding.

Using CCF in partially self-describing mode can encode events to 1/14 the size of JSON-CDC but that requires other changes outside of CCF codec and other resulting work.

Background

For more info about CCF, see:

TODO

✔️ Requires PR #4390 (merged on June 2, 2023).
✔️ Requires PR onflow/cadence#2528
✔️ Requires PR onflow/cadence#2529
✔️ Requires PR onflow/flow-emulator#408

This uses CCF in fully self-describing mode, so events will
encode to about 1/2 the size of JSON-CDC encoding.

Using CCF in partially self-describing mode can encode events
to 1/14 the size of JSON-CDC but that requires other changes
outside of CCF codec.
@fxamacker fxamacker self-assigned this Jun 2, 2023
@fxamacker fxamacker marked this pull request as draft June 2, 2023 15:27
@github-actions
Copy link
Contributor

github-actions bot commented Jun 2, 2023

FVM Benchstat comparison

This branch with compared with the base branch onflow:master commit 189f62e

The command (for i in {1..7}; do go test ./fvm ./engine/execution/computation --bench . --tags relic -shuffle=on --benchmem --run ^$; done) was used.

Collapsed results for better readability

old.txtnew.txt
time/opdelta
pkg:github.com/onflow/flow-go/fvm goos:linux goarch:amd64
RuntimeNFTBatchTransfer-2130ms ± 4%129ms ± 3%~(p=0.445 n=7+6)
RuntimeTransaction/reference_tx-236.0ms ±11%35.1ms ± 9%~(p=0.710 n=7+7)
RuntimeTransaction/convert_int_to_string-239.7ms ±10%37.4ms ± 3%~(p=0.053 n=7+7)
RuntimeTransaction/convert_int_to_string_and_concatenate_it-240.5ms ± 3%39.5ms ± 4%~(p=0.073 n=5+7)
RuntimeTransaction/get_signer_address-236.1ms ± 3%36.5ms ± 4%~(p=0.589 n=6+6)
RuntimeTransaction/get_public_account-238.8ms ± 4%38.1ms ± 3%~(p=0.128 n=7+7)
RuntimeTransaction/get_account_and_get_available_balance-2306ms ±10%293ms ± 5%~(p=0.209 n=7+7)
RuntimeTransaction/get_account_and_get_storage_used-241.2ms ± 6%40.8ms ± 5%~(p=0.710 n=7+7)
RuntimeTransaction/get_account_and_get_storage_capacity-2261ms ± 2%259ms ± 2%~(p=0.259 n=7+7)
RuntimeTransaction/get_signer_vault-242.8ms ±10%43.3ms ± 2%~(p=0.366 n=7+6)
RuntimeTransaction/get_signer_receiver-255.8ms ± 7%56.4ms ± 6%~(p=0.710 n=7+7)
RuntimeTransaction/transfer_tokens-2226ms ± 1%229ms ±14%~(p=0.343 n=5+7)
RuntimeTransaction/load_and_save_empty_string_on_signers_address-242.5ms ± 2%42.6ms ± 4%~(p=0.902 n=7+7)
RuntimeTransaction/load_and_save_long_string_on_signers_address-288.3ms ± 4%88.7ms ± 2%~(p=0.620 n=7+7)
RuntimeTransaction/create_new_account-2916ms ± 5%901ms ± 4%~(p=0.209 n=7+7)
RuntimeTransaction/call_empty_contract_function-239.2ms ± 6%38.4ms ± 7%~(p=0.318 n=7+7)
RuntimeTransaction/emit_event-253.2ms ±11%51.8ms ± 6%~(p=0.456 n=7+7)
RuntimeTransaction/borrow_array_from_storage-2151ms ± 3%150ms ± 4%~(p=0.731 n=6+7)
RuntimeTransaction/copy_array_from_storage-2152ms ± 5%151ms ± 4%~(p=0.805 n=7+7)
pkg:github.com/onflow/flow-go/engine/execution/computation goos:linux goarch:amd64
ComputeBlock/16/cols/128/txes-25.11s ± 4%5.03s ± 2%~(p=0.259 n=7+7)
pkg:github.com/onflow/flow-go/fvm goos:linux goarch:amd64
RuntimeTransaction/get_account_and_get_balance-2306ms ± 3%297ms ± 3%−2.81%(p=0.038 n=7+7)
 
alloc/opdelta
pkg:github.com/onflow/flow-go/fvm goos:linux goarch:amd64
RuntimeNFTBatchTransfer-255.4MB ± 5%54.5MB ± 2%~(p=0.259 n=7+7)
RuntimeTransaction/reference_tx-235.0MB ± 3%34.9MB ± 6%~(p=1.000 n=7+7)
RuntimeTransaction/convert_int_to_string-235.9MB ± 3%35.6MB ± 5%~(p=0.805 n=7+7)
RuntimeTransaction/convert_int_to_string_and_concatenate_it-236.2MB ± 6%36.0MB ± 5%~(p=0.710 n=7+7)
RuntimeTransaction/get_signer_address-235.2MB ± 3%35.5MB ± 3%~(p=0.535 n=7+7)
RuntimeTransaction/get_public_account-236.8MB ± 3%36.4MB ± 5%~(p=0.383 n=7+7)
RuntimeTransaction/get_account_and_get_balance-2122MB ± 2%122MB ± 3%~(p=0.620 n=7+7)
RuntimeTransaction/get_account_and_get_available_balance-2107MB ± 4%108MB ± 3%~(p=0.383 n=7+7)
RuntimeTransaction/get_account_and_get_storage_used-237.4MB ± 4%37.1MB ± 4%~(p=0.805 n=7+7)
RuntimeTransaction/get_account_and_get_storage_capacity-2105MB ± 0%104MB ± 2%~(p=0.639 n=5+7)
RuntimeTransaction/get_signer_vault-236.7MB ± 6%37.0MB ± 4%~(p=0.620 n=7+7)
RuntimeTransaction/get_signer_receiver-241.5MB ± 7%40.7MB ± 6%~(p=0.456 n=7+7)
RuntimeTransaction/load_and_save_empty_string_on_signers_address-236.2MB ± 3%36.1MB ± 5%~(p=0.535 n=7+7)
RuntimeTransaction/load_and_save_long_string_on_signers_address-252.9MB ± 3%54.1MB ± 5%~(p=0.073 n=7+7)
RuntimeTransaction/create_new_account-2186MB ± 3%184MB ± 3%~(p=0.209 n=7+7)
RuntimeTransaction/call_empty_contract_function-236.2MB ± 3%35.6MB ± 2%~(p=0.181 n=7+6)
RuntimeTransaction/emit_event-240.5MB ± 6%39.8MB ± 2%~(p=0.209 n=7+7)
RuntimeTransaction/borrow_array_from_storage-269.1MB ± 5%69.8MB ± 3%~(p=0.805 n=7+7)
RuntimeTransaction/copy_array_from_storage-281.6MB ± 2%82.2MB ± 3%~(p=0.383 n=7+7)
pkg:github.com/onflow/flow-go/engine/execution/computation goos:linux goarch:amd64
ComputeBlock/16/cols/128/txes-21.24GB ± 0%1.23GB ± 1%−0.76%(p=0.048 n=5+7)
pkg:github.com/onflow/flow-go/fvm goos:linux goarch:amd64
RuntimeTransaction/transfer_tokens-284.9MB ± 2%80.7MB ± 4%−4.96%(p=0.005 n=6+7)
 
allocs/opdelta
pkg:github.com/onflow/flow-go/fvm goos:linux goarch:amd64
RuntimeTransaction/emit_event-2139k ± 0%143k ± 0%+2.77%(p=0.001 n=7+7)
RuntimeTransaction/get_account_and_get_balance-21.32M ± 0%1.32M ± 0%−0.01%(p=0.001 n=7+7)
RuntimeTransaction/get_account_and_get_available_balance-21.27M ± 0%1.27M ± 0%−0.01%(p=0.001 n=7+7)
RuntimeTransaction/get_account_and_get_storage_capacity-21.14M ± 0%1.14M ± 0%−0.02%(p=0.000 n=7+6)
RuntimeTransaction/copy_array_from_storage-2296k ± 0%296k ± 0%−0.04%(p=0.003 n=7+5)
RuntimeTransaction/borrow_array_from_storage-2364k ± 0%364k ± 0%−0.04%(p=0.001 n=7+6)
RuntimeTransaction/load_and_save_long_string_on_signers_address-2218k ± 0%218k ± 0%−0.06%(p=0.001 n=7+7)
RuntimeTransaction/get_signer_receiver-2215k ± 0%215k ± 0%−0.07%(p=0.001 n=7+7)
RuntimeTransaction/load_and_save_empty_string_on_signers_address-2135k ± 0%135k ± 0%−0.10%(p=0.001 n=7+7)
RuntimeTransaction/get_signer_vault-2129k ± 0%129k ± 0%−0.11%(p=0.001 n=7+7)
RuntimeTransaction/get_account_and_get_storage_used-2124k ± 0%124k ± 0%−0.11%(p=0.001 n=7+7)
RuntimeTransaction/convert_int_to_string_and_concatenate_it-2108k ± 0%108k ± 0%−0.12%(p=0.001 n=7+7)
RuntimeTransaction/get_public_account-2113k ± 0%113k ± 0%−0.12%(p=0.001 n=7+7)
RuntimeTransaction/call_empty_contract_function-2102k ± 0%102k ± 0%−0.13%(p=0.001 n=7+7)
RuntimeTransaction/convert_int_to_string-296.6k ± 0%96.4k ± 0%−0.14%(p=0.001 n=7+7)
RuntimeTransaction/reference_tx-284.4k ± 0%84.2k ± 0%−0.14%(p=0.001 n=7+7)
RuntimeTransaction/get_signer_address-288.6k ± 0%88.4k ± 0%−0.15%(p=0.001 n=7+7)
pkg:github.com/onflow/flow-go/engine/execution/computation goos:linux goarch:amd64
ComputeBlock/16/cols/128/txes-217.7M ± 0%17.7M ± 0%−0.20%(p=0.001 n=7+7)
pkg:github.com/onflow/flow-go/fvm goos:linux goarch:amd64
RuntimeTransaction/create_new_account-22.35M ± 0%2.35M ± 0%−0.24%(p=0.001 n=7+7)
RuntimeNFTBatchTransfer-2275k ± 0%273k ± 0%−0.43%(p=0.009 n=6+6)
RuntimeTransaction/transfer_tokens-2866k ± 0%858k ± 0%−0.94%(p=0.001 n=7+7)
 
computationdelta
pkg:github.com/onflow/flow-go/fvm goos:linux goarch:amd64
RuntimeTransaction/reference_tx-2202 ± 0%202 ± 0%~(all equal)
RuntimeTransaction/convert_int_to_string-2402 ± 0%402 ± 0%~(all equal)
RuntimeTransaction/convert_int_to_string_and_concatenate_it-2502 ± 0%502 ± 0%~(all equal)
RuntimeTransaction/get_signer_address-2302 ± 0%302 ± 0%~(all equal)
RuntimeTransaction/get_public_account-2402 ± 0%402 ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_balance-21.00k ± 0%1.00k ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_available_balance-23.10k ± 0%3.10k ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_storage_used-2402 ± 0%402 ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_storage_capacity-21.70k ± 0%1.70k ± 0%~(all equal)
RuntimeTransaction/get_signer_vault-2402 ± 0%402 ± 0%~(all equal)
RuntimeTransaction/get_signer_receiver-2602 ± 0%602 ± 0%~(all equal)
RuntimeTransaction/transfer_tokens-23.50k ± 0%3.50k ± 0%~(all equal)
RuntimeTransaction/load_and_save_empty_string_on_signers_address-2602 ± 0%602 ± 0%~(all equal)
RuntimeTransaction/load_and_save_long_string_on_signers_address-2602 ± 0%602 ± 0%~(all equal)
RuntimeTransaction/create_new_account-2202 ± 0%202 ± 0%~(all equal)
RuntimeTransaction/call_empty_contract_function-2402 ± 0%402 ± 0%~(all equal)
RuntimeTransaction/emit_event-2602 ± 0%602 ± 0%~(all equal)
RuntimeTransaction/borrow_array_from_storage-22.60k ± 0%2.60k ± 0%~(all equal)
RuntimeTransaction/copy_array_from_storage-22.60k ± 0%2.60k ± 0%~(all equal)
 
interactionsdelta
pkg:github.com/onflow/flow-go/fvm goos:linux goarch:amd64
RuntimeTransaction/reference_tx-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/convert_int_to_string-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/convert_int_to_string_and_concatenate_it-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/get_signer_address-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/get_public_account-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_balance-246.4k ± 0%46.4k ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_available_balance-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_storage_used-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_storage_capacity-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/get_signer_vault-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/get_signer_receiver-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/transfer_tokens-238.1k ± 0%38.1k ± 0%~(all equal)
RuntimeTransaction/load_and_save_empty_string_on_signers_address-238.3k ± 0%38.3k ± 0%~(all equal)
RuntimeTransaction/load_and_save_long_string_on_signers_address-242.1k ± 0%42.1k ± 0%~(all equal)
RuntimeTransaction/create_new_account-284.3k ± 0%84.3k ± 0%~(all equal)
RuntimeTransaction/call_empty_contract_function-238.3k ± 0%38.3k ± 0%~(all equal)
RuntimeTransaction/emit_event-238.3k ± 0%38.3k ± 0%~(all equal)
RuntimeTransaction/borrow_array_from_storage-243.4k ± 0%43.4k ± 0%~(p=1.000 n=7+7)
RuntimeTransaction/copy_array_from_storage-243.4k ± 0%43.4k ± 0%~(p=0.538 n=7+6)
 
us/txdelta
pkg:github.com/onflow/flow-go/engine/execution/computation goos:linux goarch:amd64
ComputeBlock/16/cols/128/txes-22.49k ± 4%2.46k ± 2%~(p=0.259 n=7+7)
 

@fxamacker fxamacker changed the title WIP: Use CCF codec in self-describing mode to encode events instead of JSON-CDC WIP: Use CCF in self-describing mode to encode events instead of JSON-CDC Jun 2, 2023
@fxamacker fxamacker changed the title WIP: Use CCF in self-describing mode to encode events instead of JSON-CDC [Exec] Use CCF in self-describing mode to encode events (replaces JSON-CDC) Jun 2, 2023
@fxamacker fxamacker added the Execution Cadence Execution Team label Jun 2, 2023
fxamacker added 4 commits June 2, 2023 16:09
Since JSON-CDC encodes less type information than CCF, decoding
JSON-CDC and then encoding it in CCF breaks because required
type information is missing.

Fix this by creating Cadence events and then encoding it in CCF,
without involving JSON-CDC.
@codecov-commenter
Copy link

codecov-commenter commented Jun 3, 2023

Codecov Report

Merging #4417 (fb9f39f) into master (189f62e) will increase coverage by 0.44%.
The diff coverage is 79.12%.

@@            Coverage Diff             @@
##           master    #4417      +/-   ##
==========================================
+ Coverage   53.71%   54.16%   +0.44%     
==========================================
  Files         888      888              
  Lines       82612    83737    +1125     
==========================================
+ Hits        44378    45352     +974     
- Misses      34744    34866     +122     
- Partials     3490     3519      +29     
Flag Coverage Δ
unittests 54.16% <79.12%> (+0.44%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
model/convert/service_event.go 47.59% <45.34%> (+2.99%) ⬆️
utils/unittest/service_events_fixtures.go 84.14% <98.00%> (+84.14%) ⬆️
engine/execution/rpc/engine.go 51.60% <100.00%> (+0.52%) ⬆️
fvm/environment/event_encoder.go 100.00% <100.00%> (ø)

... and 8 files with indirect coverage changes

Currently, JSON-CDC decoded event fields are not explicitly sorted
and field values are obtained by index in some tests.  This isn't
compatible with CCF which explicitly sorts fields for determinism.

Fix fvm tests by obtaining field values by field identifier to
avoid dependency on field order.
Currently, JSON-CDC decoded event fields are not explicitly sorted
and field values are obtained by index in some tests.  This isn't
compatible with CCF which explicitly sorts fields for determinism.

Fix fvm tests by obtaining field values by field identifier to
avoid dependency on field order.
fxamacker added 2 commits June 5, 2023 13:32
Currently, JSON-CDC decoded event fields are not explicitly sorted
and field values are obtained by index in some tests.  This isn't
compatible with CCF which explicitly sorts fields for determinism.

Fix service events by obtaining field values by field identifier to
avoid dependency on field order.
This update gets Cadence PRs 2528 and 2529 to unblock tests.
if !ok {
return nil, invalidCadenceTypeError(
"counter",
cdcEvent.Fields[i],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use f instead of cdcEvent.Fields[i] (same elsewhere)

btw, maybe rename f to field (single letter variables are not easily readable / searchable)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use f instead of cdcEvent.Fields[i] (same elsewhere)

No, not in this case because f is different from cdcEvent.Fields[i].

f is event type's field (type cadence.Field), while cdcEvent.Fields[i] is event value's field (type cadence.Value).


setup := new(flow.EpochSetup)

setup.Counter = uint64(counter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe skip the counter/firstView/finalView temp variable, and just directly assign the values into the setup struct

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not in this case because we need to first test if counter/firstView/finalView are of specific Cadence types and then cast them as Go types.

for i, f := range evt.Fields {
switch f.Identifier {
case "counter":
foundFieldCount++
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can fields potentially have duplicate identifier (e.g., multiple "counter")? if so, then you should track found field differently

Copy link
Member Author

@fxamacker fxamacker Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can fields potentially have duplicate identifier (e.g., multiple "counter")?

No, it would be invalid Cadence event.

model/convert/service_event.go Outdated Show resolved Hide resolved
@fxamacker fxamacker force-pushed the fxamacker/use-ccf-to-encode-event branch from 4995eee to ec49e0c Compare June 6, 2023 15:09
@fxamacker fxamacker force-pushed the fxamacker/use-ccf-to-encode-event branch from 3e692fd to fc5f3c6 Compare June 6, 2023 20:19
@fxamacker fxamacker marked this pull request as ready for review June 6, 2023 22:03
@fxamacker fxamacker requested a review from turbolent June 6, 2023 22:07
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Great to see this finally being rolled out 👏 🎉

@fxamacker
Copy link
Member Author

bors merge

@bors bors bot merged commit 66e59a9 into master Jun 6, 2023
@bors bors bot deleted the fxamacker/use-ccf-to-encode-event branch June 6, 2023 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Execution Cadence Execution Team Feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants