-
Notifications
You must be signed in to change notification settings - Fork 997
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
Optimize Insertion in Deposit Trie #4299
Conversation
…into optimizeDepositLogs
…into optimizeDepositLogs
enc := [32]byte{} | ||
binary.LittleEndian.PutUint64(enc[:], uint64(len(m.originalItems))) | ||
return hashutil.Hash(append(m.branches[len(m.branches)-1][0], enc[:]...)) | ||
} | ||
|
||
// InsertIntoTrie inserts an item(deposit hash) into the trie. | ||
func (m *SparseMerkleTrie) InsertIntoTrie(item []byte, index int) { | ||
for index >= len(m.branches[0]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this is a sparse merkle trie, sometimes we may be attempting to insert something at index 15 when only 5 original leaves exist in the trie, so we have to expand it on-demand
} | ||
parentIdx := currentIndex / 2 | ||
// Update the cached layers at the parent index. | ||
if len(m.branches[i+1]) == 0 || parentIdx >= len(m.branches[i+1]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need some special handling here since the trie is sparse and there may not be an actual object at the index we're trying to modify
Codecov Report
@@ Coverage Diff @@
## master #4299 +/- ##
=========================================
Coverage ? 35.68%
=========================================
Files ? 9
Lines ? 524
Branches ? 0
=========================================
Hits ? 187
Misses ? 332
Partials ? 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go imports and a naming feedback
shared/trieutil/sparse_merkle.go
Outdated
enc := [32]byte{} | ||
binary.LittleEndian.PutUint64(enc[:], uint64(len(m.originalItems))) | ||
return hashutil.Hash(append(m.branches[len(m.branches)-1][0], enc[:]...)) | ||
} | ||
|
||
// InsertIntoTrie inserts an item(deposit hash) into the trie. | ||
func (m *SparseMerkleTrie) InsertIntoTrie(item []byte, index int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would Insert
be more clear? Given the receiver is already SparseMerkleTrie
@@ -43,7 +44,7 @@ func TestDepositTrieRoot_OK(t *testing.T) { | |||
|
|||
testAcc.TxOpts.Value = depositcontract.Amount32Eth() | |||
|
|||
for i := 0; i < 100; i++ { | |||
for i := 0; i < 5; i++ { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why lower this?
@@ -32,7 +33,7 @@ func TestDepositTrieRoot_OK(t *testing.T) { | |||
t.Errorf("Local deposit trie root and contract deposit trie root are not equal. Expected %#x , Got %#x", depRoot, localTrie.Root()) | |||
} | |||
|
|||
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 101) | |||
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
@@ -6,6 +6,7 @@ import ( | |||
|
|||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | |||
"github.com/prysmaticlabs/go-ssz" | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert new line please
…into optimizeDepositLogs
* current changes * change algorithm for tree insert * almost done with getting this to pass * unit test passes * tests now pass * fix in repo * Merge branch 'master' into optimizeDepositLogs * fix build * Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs * remove tautology * fix tautology * fix up sparsity * Merge branch 'master' into optimizeDepositLogs * further fixes * Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs * Update shared/trieutil/sparse_merkle.go * comments * Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs * add bench for optimized * gaz * Merge refs/heads/master into optimizeDepositLogs
* current changes * change algorithm for tree insert * almost done with getting this to pass * unit test passes * tests now pass * fix in repo * Merge branch 'master' into optimizeDepositLogs * fix build * Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs * remove tautology * fix tautology * fix up sparsity * Merge branch 'master' into optimizeDepositLogs * further fixes * Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs * Update shared/trieutil/sparse_merkle.go * comments * Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs * add bench for optimized * gaz * Merge refs/heads/master into optimizeDepositLogs
Resolves #[PR]
Background
Our current workflow for computing the Merkle trie of eth1 deposits is as follows:
This means every new deposit takes up O(n) in runtime, which makes it difficult to process a large number of deposit logs. This PR brings insertion down to O(log n), as instead we can just recompute the changed branch when a new deposit is seen. This will be a huge improvement to the initial deposit verification flow upon launching the beacon node.
Benchmark Results