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

Fix Up SSZ Cache Branch Recomputation #4558

Merged
merged 22 commits into from
Jan 16, 2020
Merged

Fix Up SSZ Cache Branch Recomputation #4558

merged 22 commits into from
Jan 16, 2020

Conversation

rauljordan
Copy link
Contributor

@rauljordan rauljordan commented Jan 15, 2020

Part of #4445


Description

Write why you are making the changes in this pull request

Our old SSZ array roots cache was not recomputing branches if indices changed, instead, it would recompute the entire trie.

Write a summary of the changes you are making

This PR fixes up ssz cache, enables it for e2e tests, and adds a feature flag to enable the flag --enable-ssz-cache

Works at runtime and properly recomputes branches if they change:

[2020-01-16 14:24:22]  INFO initial-sync: Processing block 7071/49321 - estimated time remaining 4h2m48s blocksPerSecond=2.9 peers=2/6
Changed 1 indices, BlockRoots
Changed 1 indices, StateRoots
Changed 1 indices, RandaoMixes
[2020-01-16 14:24:22]  INFO initial-sync: Processing block 7072/49321 - estimated time remaining 3h58m41s blocksPerSecond=3.0 peers=2/6
Changed 1 indices, BlockRoots
Changed 1 indices, StateRoots
Changed 1 indices, RandaoMixes

@nisdas
Copy link
Member

nisdas commented Jan 16, 2020

@rauljordan tests failing


state_root_cache_fuzz_test.go:67: Cached HTR panicked on iteration 6
--
  | panic: runtime error: index out of range [1] with length 1 [recovered]
  | panic: runtime error: index out of range [1] with length 1 [recovered]
  | panic: runtime error: index out of range [1] with length 1

return rt, nil
}

hashKey := hashutil.FastSum256(hashKeyElements)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This cache will never get hit. Arrays always typically get one element added each slot.

}

hashKey := hashutil.FastSum256(hashKeyElements)
if hashKey != emptyKey && h.rootsCache != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here

for i := 0; i < len(changedIndices); i++ {
rt, err = recomputeRoot(changedIndices[i], chunks, fieldName)
if err != nil {
return [32]byte{}, err
}
}
lock.Lock()
leavesCache[fieldName] = chunks
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was part of the problem, we had to update some cached values after recomputing the root

@@ -29,21 +37,6 @@ func TestState_FieldCount(t *testing.T) {
}
}

func TestHashTreeRootEquality(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test will never pass due to #4526 (comment). We will need --define ssz=minimal on this test target for it to pass.

@@ -18,6 +18,11 @@ var (
Name: "enable-attestation-cache",
Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106",
}
// EnableSSZCache --
EnableSSZCache = cli.BoolFlag{
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you un-export this and change the name to enableSSZCacheFlag? No need to export it, that's what the config is for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

@@ -8,14 +8,17 @@ import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

fuzzStateRootCache(t, 3, 100000)
}

func TestStateRootCacheFuzz_1000000(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why were the larger cases removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They take way too long to run now. Now, when doing HTR(state.BlockRoots), we pad the slice to params.BeaconConfig().SlotsPerHistoricalRoot, and the fuzz tests don't really make much use of the cache so much because the inputs change on every iteration, so they take almost 10s to run the 10k fuzz test, let alone the 100k one.

Comment on lines +18 to +24
func init() {
config := &featureconfig.Flags{
EnableSSZCache: true,
}
featureconfig.Init(config)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this being used? Hard to tell.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep! It is because it is picked up by the internal stateutil.HashTreeRoot function as being enabled

@rauljordan rauljordan self-assigned this Jan 16, 2020
@rauljordan rauljordan added the Ready For Review A pull request ready for code review label Jan 16, 2020
@@ -82,7 +75,7 @@ func BenchmarkHashTreeRootState_Generic_512(b *testing.B) {
genesisState := setupGenesisState(b, 512)
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := stateutil.HashTreeRootState(genesisState); err != nil {
if _, err := ssz.HashTreeRoot(genesisState); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

These 3 tests here say BenchmarkHashTreeRootState when it's HashTreeRoot being tested instead, can you update the test names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@codecov
Copy link

codecov bot commented Jan 16, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@298b081). Click here to learn what that means.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master    #4558   +/-   ##
=========================================
  Coverage          ?   43.58%           
=========================================
  Files             ?      193           
  Lines             ?    13708           
  Branches          ?        0           
=========================================
  Hits              ?     5975           
  Misses            ?     6729           
  Partials          ?     1004

@prylabs-bulldozer prylabs-bulldozer bot merged commit ed52996 into master Jan 16, 2020
@delete-merged-branch delete-merged-branch bot deleted the e2e-ssz-cache branch January 16, 2020 21:40
cryptomental pushed a commit to cryptomental/prysm that referenced this pull request Feb 24, 2020
* e2e ssz cache busting
* use ssz cache for e2e
* caching ensure
* fix up the cache even more
* gazelle
* formatting
* formatting
* add back cache for val registry
* sync
* fix up commented item
* add attestations
* Merge branch 'master' into e2e-ssz-cache
* Merge branch 'master' into e2e-ssz-cache
* Merge branch 'e2e-ssz-cache' of github.com:prysmaticlabs/prysm into e2e-ssz-cache
* formatting
* gaz
* Merge branch 'master' into e2e-ssz-cache
* resolve comments
* Merge branch 'master' into e2e-ssz-cache
* naming of test
* Merge refs/heads/master into e2e-ssz-cache
* Merge refs/heads/master into e2e-ssz-cache
cryptomental pushed a commit to cryptomental/prysm that referenced this pull request Feb 28, 2020
* e2e ssz cache busting
* use ssz cache for e2e
* caching ensure
* fix up the cache even more
* gazelle
* formatting
* formatting
* add back cache for val registry
* sync
* fix up commented item
* add attestations
* Merge branch 'master' into e2e-ssz-cache
* Merge branch 'master' into e2e-ssz-cache
* Merge branch 'e2e-ssz-cache' of github.com:prysmaticlabs/prysm into e2e-ssz-cache
* formatting
* gaz
* Merge branch 'master' into e2e-ssz-cache
* resolve comments
* Merge branch 'master' into e2e-ssz-cache
* naming of test
* Merge refs/heads/master into e2e-ssz-cache
* Merge refs/heads/master into e2e-ssz-cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For Review A pull request ready for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants