Optimized THE CAPLIN a lil bit (#7687)

This commit is contained in:
Giulio rebuffo 2023-06-08 15:52:09 +02:00 committed by GitHub
parent 1cb1c303d4
commit 8f8da14814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 15 deletions

View File

@ -1,18 +1,30 @@
package cache package cache
import ( import (
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/cltypes/solid" "github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/phase1/core/state/lru" "github.com/ledgerwatch/erigon/cl/phase1/core/state/lru"
"github.com/ledgerwatch/erigon/cl/utils"
) )
var attestationIndiciesCache *lru.Cache[*solid.AttestationData, []uint64] var attestationIndiciesCache *lru.Cache[common.Hash, []uint64]
const attestationIndiciesCacheSize = 256 const attestationIndiciesCacheSize = 256
func LoadAttestatingIndicies(attestation *solid.AttestationData) ([]uint64, bool) { func LoadAttestatingIndicies(attestation *solid.AttestationData, aggregationBits []byte) ([]uint64, bool) {
return attestationIndiciesCache.Get(attestation) bitsHash := utils.Keccak256(aggregationBits)
hash, err := attestation.HashSSZ()
if err != nil {
return nil, false
}
return attestationIndiciesCache.Get(utils.Keccak256(hash[:], bitsHash[:]))
} }
func StoreAttestation(attestation *solid.AttestationData, indicies []uint64) { func StoreAttestation(attestation *solid.AttestationData, aggregationBits []byte, indicies []uint64) {
attestationIndiciesCache.Add(attestation, indicies) bitsHash := utils.Keccak256(aggregationBits)
hash, err := attestation.HashSSZ()
if err != nil {
return
}
attestationIndiciesCache.Add(utils.Keccak256(hash[:], bitsHash[:]), indicies)
} }

View File

@ -10,9 +10,9 @@ import (
func TestAttestationsCache(t *testing.T) { func TestAttestationsCache(t *testing.T) {
input := []uint64{1} input := []uint64{1}
a := &solid.AttestationData{} a := solid.NewAttestationData()
cache.StoreAttestation(a, []uint64{1}) cache.StoreAttestation(&a, []byte{2}, []uint64{1})
output, valid := cache.LoadAttestatingIndicies(a) output, valid := cache.LoadAttestatingIndicies(&a, []byte{2})
require.True(t, valid) require.True(t, valid)
require.Equal(t, input, output) require.Equal(t, input, output)
} }

View File

@ -1,13 +1,13 @@
package cache package cache
import ( import (
"github.com/ledgerwatch/erigon/cl/cltypes/solid" "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/phase1/core/state/lru" "github.com/ledgerwatch/erigon/cl/phase1/core/state/lru"
) )
func init() { func init() {
var err error var err error
if attestationIndiciesCache, err = lru.New[*solid.AttestationData, []uint64]("attestationIndiciesCacheSize", attestationIndiciesCacheSize); err != nil { if attestationIndiciesCache, err = lru.New[common.Hash, []uint64]("attestationIndiciesCacheSize", attestationIndiciesCacheSize); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -244,7 +244,7 @@ func (b *BeaconState) ComputeNextSyncCommittee() (*solid.SyncCommittee, error) {
// GetAttestingIndicies retrieves attesting indicies for a specific attestation. however some tests will not expect the aggregation bits check. // GetAttestingIndicies retrieves attesting indicies for a specific attestation. however some tests will not expect the aggregation bits check.
// thus, it is a flag now. // thus, it is a flag now.
func (b *BeaconState) GetAttestingIndicies(attestation solid.AttestationData, aggregationBits []byte, checkBitsLength bool) ([]uint64, error) { func (b *BeaconState) GetAttestingIndicies(attestation solid.AttestationData, aggregationBits []byte, checkBitsLength bool) ([]uint64, error) {
if cached, ok := cache.LoadAttestatingIndicies(&attestation); ok { if cached, ok := cache.LoadAttestatingIndicies(&attestation, aggregationBits); ok {
return cached, nil return cached, nil
} }
committee, err := b.GetBeaconCommitee(attestation.Slot(), attestation.ValidatorIndex()) committee, err := b.GetBeaconCommitee(attestation.Slot(), attestation.ValidatorIndex())
@ -267,7 +267,7 @@ func (b *BeaconState) GetAttestingIndicies(attestation solid.AttestationData, ag
attestingIndices = append(attestingIndices, member) attestingIndices = append(attestingIndices, member)
} }
} }
cache.StoreAttestation(&attestation, attestingIndices) cache.StoreAttestation(&attestation, aggregationBits, attestingIndices)
return attestingIndices, nil return attestingIndices, nil
} }

View File

@ -153,8 +153,8 @@ func (f *ForkGraph) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock, full
// Add block to list of invalid blocks // Add block to list of invalid blocks
log.Debug("Invalid beacon block", "reason", invalidBlockErr) log.Debug("Invalid beacon block", "reason", invalidBlockErr)
f.badBlocks[blockRoot] = struct{}{} f.badBlocks[blockRoot] = struct{}{}
f.currentReferenceState.CopyInto(f.currentState) f.nextReferenceState.CopyInto(f.currentState)
f.currentStateBlockRoot, err = f.currentReferenceState.BlockRoot() f.currentStateBlockRoot, err = f.nextReferenceState.BlockRoot()
if err != nil { if err != nil {
log.Error("[Caplin] Could not recover from invalid block") log.Error("[Caplin] Could not recover from invalid block")
} }

View File

@ -19,7 +19,7 @@ func (f *ForkChoiceStore) OnAttestation(attestation *solid.Attestation, fromBloc
return err return err
} }
target := data.Target() target := data.Target()
if cachedIndicies, ok := cache.LoadAttestatingIndicies(&data); ok { if cachedIndicies, ok := cache.LoadAttestatingIndicies(&data, attestation.AggregationBits()); ok {
f.processAttestingIndicies(attestation, cachedIndicies) f.processAttestingIndicies(attestation, cachedIndicies)
return nil return nil
} }