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

refactor trie root hash computation #6594

Open
wants to merge 18 commits into
base: feat/trie-mutex-refactor
Choose a base branch
from

Conversation

BeniaminDrasovean
Copy link
Contributor

@BeniaminDrasovean BeniaminDrasovean commented Nov 11, 2024

Reasoning behind the pull request

  • There are multiple trie functions that are used to set the hash of a node.

Proposed changes

  • Merge setHash, setHashConcurrent and setRootHash functions from the trie. The resulting function will be able to set the hash for a specific trie node using goroutines. For example, a branch node will set the hashes of it's children on different goroutines, and after that it will compute it's own hash.

Testing procedure

  • System tests when all the PRs are merged in the feat branch

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

…refactor-trie-set-hash

# Conflicts:
#	integrationTests/state/stateTrieSync/stateTrieSync_test.go
#	trie/branchNode.go
#	trie/branchNode_test.go
#	trie/extensionNode_test.go
#	trie/patriciaMerkleTrie.go
@BeniaminDrasovean BeniaminDrasovean changed the base branch from refactor-trie-mutex-usage to trie-concurrency-unit-tests December 4, 2024 10:07
Base automatically changed from trie-concurrency-unit-tests to feat/trie-mutex-refactor December 18, 2024 11:56
@BeniaminDrasovean BeniaminDrasovean marked this pull request as ready for review December 19, 2024 13:36
@ssd04 ssd04 self-requested a review December 23, 2024 13:30
Comment on lines 685 to 686
func (tr *patriciaMerkleTrie) GetProof(key []byte, rootHash []byte) ([][]byte, []byte, error) {
trie, err := tr.Recreate(rootHash)
Copy link
Contributor

Choose a reason for hiding this comment

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

why recreating trie here? to avoid concurrent accesses?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. changed a bit and added a TODO to refactor this func in the future. We can avoid some redundant operations

@@ -1438,7 +1438,7 @@ func (n *Node) getProof(rootHash []byte, key []byte) (*common.GetProofResponse,
return nil, err
}

computedProof, value, err := tr.GetProof(key)
computedProof, value, err := tr.GetProof(key, rootHash)
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to recreate trie in GetProof? AccountsAdapterAPI().GetTrie(rootHash) will also recreate trie based on rootHash, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. Changed inside GetProof and added a TODO there for a future improvement.

}

it, err := NewDFSIterator(newTr)
it, err := NewDFSIterator(tr, rootHash)
Copy link
Contributor

Choose a reason for hiding this comment

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

do we still need critical section here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. The idea is that this func is called on goroutines a lot (on API calls and this is also used when another node requests trie data because it is syncing). This gets a lot of data from the storage. Thus if we do not add a critical section here the processing will have delays because it will compete for storage with this function.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add more iterations in ConcurrencyOperations test; for me with more iterations it's failing with data race

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, it fails sometimes. This is because not all trie funcs have been refactored yet to allow parallel execution. Will do this in a future PR when all trie funcs have been refactored, and then it should not fail anymore.

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