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] refactor4 - separate script and transaction environments #939

Merged
merged 18 commits into from
Jul 26, 2021

Conversation

ramtinms
Copy link
Member

@ramtinms ramtinms commented Jul 9, 2021

This PR

  • looks big but it does not include many changes (the best way to review it would be to compare the transactionEnv and scriptEnv with the previous env file content.)
  • continues the plan for refactoring fvm
  • defines Environment as an interface
  • separate out the script and transaction environment, which removes the need for extra branching for interface method calls during tx execution and it makes the logic easier to test and review.
  • should not change any behaviour

In the follow-up PRs we are going to make environments even lighter and move the remaining common operations into handlers, basically environments would be a collection of pointers until we just directly inject handlers to the cadence runtime according to the long term plan.

@ramtinms ramtinms requested review from m4ksio and turbolent July 9, 2021 03:10
@ramtinms ramtinms changed the title [WIP] FVM refactor4 - separate script and transaction environments FVM refactor4 - separate script and transaction environments Jul 9, 2021
@codecov-commenter
Copy link

codecov-commenter commented Jul 9, 2021

Codecov Report

Merging #939 (c5a5dce) into master (9ce70b2) will decrease coverage by 0.60%.
The diff coverage is 41.74%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #939      +/-   ##
==========================================
- Coverage   54.76%   54.16%   -0.61%     
==========================================
  Files         286      287       +1     
  Lines       18913    19213     +300     
==========================================
+ Hits        10357    10406      +49     
- Misses       7157     7388     +231     
- Partials     1399     1419      +20     
Flag Coverage Δ
unittests 54.16% <41.74%> (-0.61%) ⬇️

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

Impacted Files Coverage Δ
fvm/scriptEnv.go 35.81% <35.81%> (ø)
fvm/transactionEnv.go 44.00% <44.00%> (ø)
fvm/transactionContractFunctionInvocator.go 80.64% <60.00%> (+0.64%) ⬆️
fvm/account.go 83.33% <100.00%> (ø)
fvm/blocks.go 60.60% <100.00%> (+7.03%) ⬆️
fvm/script.go 94.59% <100.00%> (ø)
fvm/transactionInvocator.go 79.80% <100.00%> (-0.20%) ⬇️
fvm/transactionStorageLimiter.go 65.51% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9ce70b2...c5a5dce. Read the comment docs.

Comment on lines -709 to -711
if e.transactionEnv == nil {
return errors.NewOperationNotSupportedError("AddEncodedAccountKey")
}
Copy link
Member Author

Choose a reason for hiding this comment

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

we got rid of this type of conditioning in several places.

Comment on lines -1107 to -1109
func (e *transactionEnv) RemoveAccountKey(address runtime.Address, keyIndex int) (encodedPublicKey []byte, err error) {
return e.accountKeys.RemoveAccountKey(address, keyIndex)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Methods like this are merged into the main function with the same name so we don't do branching based on the condition transactionEnv != nil

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 to me, but I am slightly worried about the amount of code duplication between scriptEnv and transactionEnv, could we push the common methods into a commonEnv or something simmilar?

@ramtinms
Copy link
Member Author

ramtinms commented Jul 13, 2021

I see your point but, I believe in the next PR we can optimize the scriptEnv and get rid of a lot of functions (I didn't want to change any behaviour in this PR), so in the end, might not be that much operations shared between transaction and scripts. The long-term plan is to get rid of something like env and just inject handlers directly to the cadence runtime (we would have readOnlyAccountHander and ... so we are going to reuse the common functionality there)

@ramtinms ramtinms requested a review from Kay-Zee July 13, 2021 16:41
@ramtinms ramtinms changed the title FVM refactor4 - separate script and transaction environments [FVM] refactor4 - separate script and transaction environments Jul 14, 2021

// TODO set the flags on context
eventHandler := handler.NewEventHandler(ctx.Chain,
ctx.EventCollectionEnabled,
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, instead of controlling behaviour with switches it would be better to have different implementations here - if collection is not enabled, something like NoopEventsCollector etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly, that is what is in the list of future PRs, I didn't want to change the logic that much in this PR.

traceSpan opentracing.Span
}

func NewScriptEnvironment(ctx Context, vm *VirtualMachine, sth *state.StateHolder, programs *programs.Programs) *ScriptEnv {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a copy of TransactionEnvironment. Maybe we should have a one common base for both?

Copy link
Member Author

Choose a reason for hiding this comment

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

it looks the same now, but won't be the same soon, when we switch the handlers with readonly version of them.

@ramtinms ramtinms merged commit 2ff4570 into master Jul 26, 2021
@ramtinms ramtinms deleted the ramtin/fvm-separate-environments branch July 26, 2021 16:35
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.

None yet

4 participants