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

move node types and handlings to separate packages #729

Open
wants to merge 10 commits into
base: move-atomic-sync
Choose a base branch
from

Conversation

ceyonur
Copy link
Collaborator

@ceyonur ceyonur commented Jan 3, 2025

Why this should be merged

Moves declaration of message.NodeType to separate pkgs

How this works

This pull request includes several changes to the plugin/evm package, primarily focusing on the atomic trie synchronization and request handling.

Atomic Trie Synchronization:

Request Handling:

  • plugin/evm/message/handler.go: Simplified the RequestHandler interface by merging HandleStateTrieLeafsRequest and HandleAtomicTrieLeafsRequest into a single HandleLeafsRequest method. [1] [2]
  • plugin/evm/message/leafs_request.go: Updated LeafsRequest to use a single NodeType field and removed the distinction between state and atomic trie nodes. [1] [2]
  • plugin/evm/vm.go: Introduced RegisterLeafRequestHandler that can register different message.NodeTypes and passes configs to networkHandler to handle registered NodeTypes accordingly by creating leaf request handlers for each type.

Test Updates:

Additional Changes:

These changes collectively simplify the codebase and improve the maintainability of the atomic trie synchronization and request handling logic.

How this was tested

Existing UTs

Need to be documented?

No

Need to update RELEASES.md?

No

return nil
}

func (vm *VM) RegisterLeafRequestHandler(nodeType message.NodeType, metricName string, trieDB *triedb.Database, trieKeyLen int, useSnapshot bool) error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm I wonder if we should register an interface rather than nodeType directly. I feel like there is definitely room to improve this PR in general, but did not want to break it anything further.

@ceyonur ceyonur marked this pull request as ready for review January 6, 2025 15:09
@ceyonur ceyonur requested review from darioush and a team as code owners January 6, 2025 15:09
@ceyonur ceyonur self-assigned this Jan 6, 2025
plugin/evm/vm.go Outdated
@@ -1218,15 +1226,41 @@ func (vm *VM) setAppRequestHandlers() {
},
},
)
if err := vm.RegisterLeafRequestHandler(message.StateTrieNode, "sync_state_trie_leaves", evmTrieDB, message.StateTrieKeyLength, true); err != nil {
return err
Copy link
Collaborator

@qdm12 qdm12 Jan 7, 2025

Choose a reason for hiding this comment

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

nit

Suggested change
return err
return fmt.Errorf("failed registering state trie database: %s", err)

maybe %w is needed though

plugin/evm/vm.go Outdated
}
// Register atomic trieDB for serving atomic leafs requests.
if err := vm.RegisterLeafRequestHandler(atomic.AtomicTrieNode, "sync_atomic_trie_leaves", vm.atomicTrie.TrieDB(), atomic.AtomicTrieKeyLength, false); err != nil {
return err
Copy link
Collaborator

@qdm12 qdm12 Jan 7, 2025

Choose a reason for hiding this comment

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

nit

Suggested change
return err
return fmt.Errorf("failed registering atomic trie database: %s", err)

maybe %w is needed though

sync/README.md Outdated
@@ -34,7 +34,7 @@ State sync code is structured as follows:
- `plugin/evm/`: The engine expects the VM to implement `StateSyncableVM` interface,
- `StateSyncServer`: Contains methods executed on nodes _serving_ state sync requests.
- `StateSyncClient`: Contains methods executed on nodes joining the network via state sync, and orchestrates the top level steps of the sync.
- `peer`: Contains abstractions used by `sync/statesync` to send requests to peers (`AppRequest`) and receive responses from peers (`AppResponse`).
- `peer`: Contains abstractions used by `sync/statesync` to send requests to peers (`AppRequest`) and receive responses from peers (`AppResponse`).
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit revert this, the trailing space is a bit out of scope 😄 Although we probably want to setup CI markdown checks at some point to have consistent markdown.

Comment on lines 87 to 88
if len(leafsRequest.Start) != 0 && len(leafsRequest.Start) != lrh.trieKeyLength ||
len(leafsRequest.End) != 0 && len(leafsRequest.End) != lrh.trieKeyLength {
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldnt' we use parentheses in those conditions?? 🤔

StateTrieNode NodeType = iota + 1
// AtomicTrieNode represents a leaf node that belongs to the coreth evm.AtomicTrie
AtomicTrieNode
StateTrieNode = NodeType(0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we keep this as 1 for compatibility?

warpBackend warp.Backend,
networkCodec codec.Manager,
leafRequesTypeConfigs map[message.NodeType]LeafRequestTypeConfig,
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we just take map[message.NodeType]*syncHandlers.LeafsRequestHandler here possibly it can be shortened using a type declaration like type LeafHandlers map[message.NodeType]*syncHandlers.LeafsRequestHandler

vm.warpBackend,
vm.networkCodec,
vm.leafRequestTypeConfigs,
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we return the map from RegisterLeafRequestHandler and perhaps rename it to makeLeafRequestHandlers to avoid a one-time use field on vm? seems like it doesn't need to be defined on 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.

This was in fact to use it from outside this pkg and let an external wrapping VM to register it's own handler.

func (n networkHandler) HandleLeafsRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, leafsRequest message.LeafsRequest) ([]byte, error) {
handler, ok := n.leafRequestHandlers[leafsRequest.NodeType]
if !ok {
return nil, fmt.Errorf("unknown node type %d", leafsRequest.NodeType)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this error will be fatal so we should log debug and return nil as before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants