Skip to content

Improve account block validation performance#44

Open
vilkris4 wants to merge 5 commits into
zenon-network:masterfrom
vilkris4:feature/pr_add_chain_cache
Open

Improve account block validation performance#44
vilkris4 wants to merge 5 commits into
zenon-network:masterfrom
vilkris4:feature/pr_add_chain_cache

Conversation

@vilkris4

@vilkris4 vilkris4 commented Aug 7, 2024

Copy link
Copy Markdown
Collaborator

Significant performance issues start appearing when the node validates an account block that uses an old momentum acknowledgement height.

The reason for the slow processing time of such an account block is that in order to verify the account block, the node must rollback the chain database's state to the block's momentum acknowledgement height. The rollback operation is done in the Get function in versioned_db.go.

In order to prevent the need for an expensive rollback operation, this PR introduces an additional chain cache database that stores the necessary state information in a way that can be accessed quickly.

To validate a user account block, the following stateful information is needed:

  1. The amount of plasma fused to the sender's address at momentum acknowledgement height.
  2. The amount of total chain plasma committed by the sender at momentum acknowledgement height.
  3. What sporks are active at momentum acknowledgement height.

These three data items are stored in the cache using a momentum height composite key, allowing the key-value pairs to be iterated over in order to get the value at a specific height. Using the cache removes the need to rollback the momentum store to the momentum acknowledgment height, when validating a user account block.

To validate an embedded account block, various stateful data items are needed, since processing an embedded account block executes contract functionality. This means that the validation of an embedded account block still rolls back the momentum store to the momentum acknowledgement height. An embedded account block uses the send block's confirmation height as its acknowledgement height, which means that the amount of momentums needed to be rolled back will always be small, minimizing potential issues related to performance.

Note: An account block is not allowed to reference an older momentum acknowledgement height than what the previous account block has used. Because of this, the old states related to plasma are pruned. This significantly reduces the cache's size.

Benchmarks

Benchmarking the time it takes to process the troublesome momentum 6,606,105 produces the following results:

Version Processing time
Current (v0.0.7) 10 min 21.6126 s
New 11.6853 ms

Testing

A full resync of the node has been tested with these changes.

Significant performance issues start appearing when the node validates an account block that uses an old momentum acknowledgement height.

The reason for the slow processing time of such an account block is that in order to verify the account block, the node must rollback the chain database's state to the block's momentum acknowledgement height. The rollback operation is done in the `Get` function in `versioned_db.go`.

In order to prevent the need for an expensive rollback operation, this commit introduces an additional chain cache database that stores the necessary state information in a way that can be accessed quickly.

To validate a *user* account block, the following stateful information is needed:

1. The amount of plasma fused to the sender's address at momentum acknowledgement height.
2. The amount of total chain plasma committed by the sender at momentum acknowledgement height.
3. What sporks are active at momentum acknowledgement height.

These three data items are stored in the cache using a momentum height composite key, allowing the key-value pairs to be iterated over in order to get the value at a specific height. Using the cache removes the need to rollback the momentum store to the momentum acknowledgment height, when validating a user account block.

To validate an *embedded* account block, various stateful data items are needed, since processing an embedded account block executes contract functionality. This means that the validation of an embedded account block still rolls back the momentum store to the momentum acknowledgement height. An embedded account block uses the send block's confirmation height as its acknowledgement height, which means that the amount of momentums needed to be rolled back will always be small, minimizing potential issues related to performance.

**Note:** An account block is not allowed to reference an older momentum acknowledgement height than what the previous account block has used. Because of this, the old states related to plasma are pruned. This significantly reduces the cache's size.
@georgezgeorgez georgezgeorgez self-requested a review August 11, 2024 13:12
@0x3639

0x3639 commented Aug 23, 2024

Copy link
Copy Markdown
Collaborator

Here is a code review between George and Vilkris if you are interested in learning more about this PR.

https://www.youtube.com/watch?v=Pb87qKrtszs&feature=youtu.be

vilkris4 added a commit to hypercore-one/hyperqube_z that referenced this pull request Jan 11, 2025
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