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

Stateful block to vm #1744

Merged
merged 51 commits into from
Nov 12, 2024
Merged

Stateful block to vm #1744

merged 51 commits into from
Nov 12, 2024

Conversation

aaronbuchwald
Copy link
Collaborator

@aaronbuchwald aaronbuchwald commented Nov 6, 2024

This PR moves StatefulBlock from the chain package to the VM package.

The StatefulBlock type handles the state sync invariants, which is implemented by the syncervm client/server in the VM package, so this should be moved so that the VM package is solely responsible for state sync.

The StatefulBlock type also handles determining whether a block has been processed, when a block should be executed in verify vs deferred to handled on accept, etc.

This PR separates all of that logic from the chain package, which is now solely responsible for handling the state transition function of a HyperVM (VM using actions/auth). This requires that it can:

  • build a block
  • parse a block
  • execute a block given a parent state and return a result and newly computed state
  • pre-execute a transaction before allowing it in the mempool

The end goal for this refactor is to decouple the VM package as well from knowing the block/result/state types required by the chain package (#1278).

@aaronbuchwald aaronbuchwald added the DO NOT MERGE This PR is not meant to be merged in its current state label Nov 6, 2024
@aaronbuchwald aaronbuchwald changed the base branch from main to refactor-chain November 6, 2024 02:32
chain/chain.go Outdated Show resolved Hide resolved
vm/option.go Outdated
Comment on lines 89 to 93
func WithTxRemovedSubscriptions(subscriptions ...event.SubscriptionFactory[TxRemovedEvent]) RegisterFunc {
return func(vm *VM) {
vm.txRemovedSubscriptionFactories = append(vm.txRemovedSubscriptionFactories, subscriptions...)
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can split this out into a separate PR. Given the limited cases where we can send a notification (only if auth fails, which will only happen for an incorrect/malicious client), clients cannot rely on it for any useful information so we may as well remove it entirely.

vm/resolutions.go Outdated Show resolved Hide resolved
vm/resolutions.go Outdated Show resolved Hide resolved
vm/vm.go Outdated
Comment on lines 331 to 334
vm.chain, err = chain.NewChain(vm, vm.config.ExecutionConfig, vm)
if err != nil {
return err
}
Copy link
Collaborator Author

@aaronbuchwald aaronbuchwald Nov 6, 2024

Choose a reason for hiding this comment

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

Will make this pluggable so that we pass the chain implementation into the VM and the VM is not tied to the *chain.XXX block types

vm/vm.go Outdated
Comment on lines 381 to 400
genesisExecutionBlk, err := chain.NewGenesisBlock(root)
if err != nil {
snowCtx.Log.Error("could not create genesis block", zap.Error(err))
return err
}
genesisBlk, err := ParseStatefulBlock(
ctx,
chain.NewGenesisBlock(root),
nil,
genesisExecutionBlk,
true,
vm,
)
if err != nil {
snowCtx.Log.Error("unable to init genesis block", zap.Error(err))
return err
}
// Set executed block, since we will never execute the genesis block
genesisBlk.executedBlock = &chain.ExecutedBlock{
BlockID: genesisBlk.ID(),
Block: genesisExecutionBlk.StatelessBlock,
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO: clean up init genesis

vm/vm.go Show resolved Hide resolved
chain/chain.go Outdated Show resolved Hide resolved
@aaronbuchwald aaronbuchwald changed the base branch from refactor-chain to main November 6, 2024 02:43
Comment on lines -69 to -73
txRemovedSubscription := event.SubscriptionFuncFactory[vm.TxRemovedEvent]{
AcceptF: func(event vm.TxRemovedEvent) error {
return server.RemoveTx(event.TxID, event.Err)
},
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This can be factored out to a separate PR if needed. Removed this as part of this change so that PreExecute can move into the chain/ package and would prefer not to force that function to differentiate between an error that can notify the caller the tx will never go through vs. may go through in the future. This is not currently providing any new information to the caller since it only sends this notification for an invalid signature, which the caller could always check itself.

@aaronbuchwald aaronbuchwald marked this pull request as ready for review November 8, 2024 19:57
@aaronbuchwald aaronbuchwald removed the DO NOT MERGE This PR is not meant to be merged in its current state label Nov 12, 2024
chain/validity_window.go Outdated Show resolved Hide resolved
chain/chain.go Outdated

func NewChain(
tracer trace.Tracer,
gatherer metrics.MultiGatherer,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we using MultiGatherer? I previously thought that MultiGatherer was just exposed so you could register metrics into avalanchego, otherwise it seems like the prometheus.Registerer interface is just simpler to use. Wondering if we should pass in a prometheus registry here and move the setup code upstream into the VM.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a note in #1765 to remove the special case AvalancheGo metric types throughout the rest of the repo

chain/metrics.go Outdated
r := prometheus.NewRegistry()

rootCalculated, err := metric.NewAverager(
"chain_root_calculated",
Copy link
Contributor

Choose a reason for hiding this comment

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

This avalanchego wrapper seems to have a subtle difference where the underlying metric does not have a defined namespace (whereas those created through the prometheus package in the rest of this function do). This might cause issues in datadog queries since these won't map to the same namespace.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Created a separate issue since this PR is moving existing metrics #1765

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed from this PR, but leaving the issue to clean up everywhere else it's used

chain/metrics.go Outdated Show resolved Hide resolved
block.sigJob = sigJob

// Setup signature verification job
_, sigVerifySpan := p.tracer.Start(ctx, "Chain.AsyncVerify.verifySignatures") //nolint:spancheck
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we're doing this nolint override beacause this is async... would it make more sense for us to just have an AuthBatchVerifier that wraps the action-dev defined-one and adds tracing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ya that's a good idea. Separate PR?

Copy link
Contributor

@joshua-kim joshua-kim Nov 12, 2024

Choose a reason for hiding this comment

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

Made an issue: #1766. I saw we're ignoring this linter on main already as well...

vm/resolutions.go Outdated Show resolved Hide resolved
vm/verify_context.go Outdated Show resolved Hide resolved
vm/vm_test.go Outdated Show resolved Hide resolved
@aaronbuchwald aaronbuchwald enabled auto-merge (squash) November 12, 2024 21:46
@aaronbuchwald aaronbuchwald merged commit ee0c4d8 into main Nov 12, 2024
17 checks passed
@aaronbuchwald aaronbuchwald deleted the stateful-block-to-vm branch November 12, 2024 22:02
@aaronbuchwald aaronbuchwald mentioned this pull request Dec 10, 2024
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.

2 participants