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

[FVM] reducing register touches #2502

Merged
merged 35 commits into from
Jun 27, 2022
Merged

Conversation

ramtinms
Copy link
Contributor

@ramtinms ramtinms commented May 26, 2022

This PR

  • merges frozen and exists register keys into a new register (AccountStatus)
  • adds a migration for ^ change
  • removes frozen enabling step as it's already covered inside transaction ENV dynamically.
  • moves remaining frozen checks into the tx verifier process (less allocation)
  • optimized getContract method call for happy path, so we don't need to load contract_names registers by default (this saves at least 4-5 register reads per tx)
  • removes old migration codes

@github-actions
Copy link
Contributor

github-actions bot commented May 26, 2022

FVM Benchstat comparison

This branch with compared with the base branch onflow:master commit dc85e89

The command (for i in {1..N}; do go test ./fvm --bench . --tags relic -shuffle=on; done) was used.

Bench tests were run a total of 7 times on each branch.

Collapsed results for better readability

old.txtnew.txt
time/opdelta
RuntimeTransaction/reference_tx-229.8ms ± 0%28.9ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/convert_int_to_string-230.9ms ± 0%29.8ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/convert_int_to_string_and_concatenate_it-232.6ms ± 0%31.9ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_signer_address-230.1ms ± 0%29.0ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_public_account-233.8ms ± 0%31.8ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_account_and_get_balance-2530ms ± 0%482ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_account_and_get_available_balance-2405ms ± 0%386ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_account_and_get_storage_used-242.3ms ± 0%36.5ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_account_and_get_storage_capacity-2375ms ± 0%363ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_signer_vault-239.4ms ± 0%39.1ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_signer_receiver-250.5ms ± 0%49.9ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/transfer_tokens-2185ms ± 0%185ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/load_and_save_empty_string_on_signers_address-236.7ms ± 0%35.8ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/load_and_save_long_string_on_signers_address-277.6ms ± 0%77.9ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/create_new_account-21.11s ± 0%1.10s ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/call_empty_contract_function-233.9ms ± 0%32.3ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/emit_event-247.2ms ± 0%45.9ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/borrow_array_from_storage-2115ms ± 0%114ms ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/copy_array_from_storage-2113ms ± 0%113ms ± 0%~(p=1.000 n=1+1)
 
computationdelta
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-2802 ± 0%802 ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_available_balance-22.40k ± 0%2.40k ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_storage_used-2402 ± 0%402 ± 0%~(all equal)
RuntimeTransaction/get_account_and_get_storage_capacity-21.10k ± 0%1.10k ± 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
RuntimeTransaction/reference_tx-247.8k ± 0%47.4k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/convert_int_to_string-247.8k ± 0%47.4k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/convert_int_to_string_and_concatenate_it-247.8k ± 0%47.4k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_signer_address-247.8k ± 0%47.4k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_public_account-247.8k ± 0%47.4k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_account_and_get_balance-216.8M ± 0%16.7M ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_account_and_get_available_balance-25.33M ± 0%5.28M ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_account_and_get_storage_used-250.6k ± 0%50.2k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_account_and_get_storage_capacity-25.32M ± 0%5.28M ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_signer_vault-249.1k ± 0%48.7k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/get_signer_receiver-249.5k ± 0%49.1k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/transfer_tokens-250.2k ± 0%49.9k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/load_and_save_empty_string_on_signers_address-251.3k ± 0%50.9k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/load_and_save_long_string_on_signers_address-256.3k ± 0%55.9k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/create_new_account-28.47M ± 0%8.40M ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/call_empty_contract_function-248.0k ± 0%47.7k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/emit_event-248.0k ± 0%47.7k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/borrow_array_from_storage-254.2k ± 0%53.8k ± 0%~(p=1.000 n=1+1)
RuntimeTransaction/copy_array_from_storage-254.2k ± 0%53.8k ± 0%~(p=1.000 n=1+1)
 

@@ -34,7 +34,7 @@ type Context struct {
CadenceLoggingEnabled bool
EventCollectionEnabled bool
ServiceEventCollectionEnabled bool
AccountFreezeAvailable bool
AccountFreezeEnabled bool
Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed it to match other ones.

@ramtinms ramtinms marked this pull request as ready for review May 30, 2022 17:20
@ramtinms ramtinms requested a review from SupunS May 30, 2022 17:20
Copy link
Contributor

@janezpodhostnik janezpodhostnik left a comment

Choose a reason for hiding this comment

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

Looks good, I just have some questions.

fvm/state/accounts.go Outdated Show resolved Hide resolved
fvm/context.go Show resolved Hide resolved
// all accounts provides enoguh weights
//
// if KeyWeightThreshold is set to a negative number, signature verification is skipped
type TransactionVerifier struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Whats the benefit of merging all the checks into one processor?

I was thinking that the frozen checker could actually be run for all transactions in a block(chunk?) in parallel before any transaction invocation. This would only change the behaviour of the freeze so that it is valid only from the next block on, which I think would be acceptable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think checking frozen accounts should happen at the time of the register reads, for example, we do have extra checks when a contract is going to load from an account that is not the authorizer of the tx but should still block that call, etc ...

Comment on lines +12 to +13
maskExist byte = 0b0000_0001
maskFrozen byte = 0b1000_0000
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of using enum style I did it mask style, cause if something gets frozen and unfrozen we can maintain future states for accounts, while maintaining frozen state

Comment on lines -32 to -35
func keyPublicKey(index uint64) string {
return fmt.Sprintf("public_key_%d", index)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved it to the end of file

@ramtinms ramtinms requested a review from fxamacker June 9, 2022 21:34
Copy link
Member

@fxamacker fxamacker left a comment

Choose a reason for hiding this comment

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

LGTM! I focused on changes with exists and frozen registers, and have one minor comment.

Comment on lines +254 to +269
for _, authorizer := range tx.Authorizers {
err := accounts.CheckAccountNotFrozen(authorizer)
if err != nil {
return fmt.Errorf("checking frozen account failed: %w", err)
}
}

err := accounts.CheckAccountNotFrozen(tx.ProposalKey.Address)
if err != nil {
return fmt.Errorf("checking frozen account failed: %w", err)
}

err = accounts.CheckAccountNotFrozen(tx.Payer)
if err != nil {
return fmt.Errorf("checking frozen account failed: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if it's feasible, maybe we can batch read account status registers for multiple addresses and then check frozen status for all read registers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice idea, though it requires way more changes as I don't think we can read batch of registers yet from inside the fvm yet. going to create a follow up PR to batch read in places that are possible.

@ramtinms
Copy link
Contributor Author

bors merge

bors bot added a commit that referenced this pull request Jun 22, 2022
2502: [FVM] reducing register touches  r=ramtinms a=ramtinms

This PR
- merges `frozen` and `exists` register keys into a new register (AccountStatus) 
- adds a migration for ^ change 
- removes frozen enabling step as it's already covered inside transaction ENV dynamically. 
- moves remaining frozen checks into the tx verifier process (less allocation) 
- optimized getContract method call for happy path, so we don't need to load `contract_names` registers by default (this saves at least 4-5 register reads per tx) 
- removes old migration codes 

Co-authored-by: ramtinms <ramtin@axiomzen.co>
Co-authored-by: Ramtin M. Seraj <ramtinms@users.noreply.github.com>
@ramtinms
Copy link
Contributor Author

bors cancel

@bors
Copy link
Contributor

bors bot commented Jun 22, 2022

Canceled.

@ramtinms ramtinms merged commit 018bac2 into master Jun 27, 2022
@ramtinms ramtinms deleted the ramtin/2492-revamp-account-registers branch June 27, 2022 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants