mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 11:41:19 +00:00
Optimized THE CAPLIN a lil bit (#7687)
This commit is contained in:
parent
1cb1c303d4
commit
8f8da14814
22
cl/phase1/cache/attestation_indicies_cache.go
vendored
22
cl/phase1/cache/attestation_indicies_cache.go
vendored
@ -1,18 +1,30 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"github.com/ledgerwatch/erigon-lib/common"
|
||||
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
|
||||
"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
|
||||
|
||||
func LoadAttestatingIndicies(attestation *solid.AttestationData) ([]uint64, bool) {
|
||||
return attestationIndiciesCache.Get(attestation)
|
||||
func LoadAttestatingIndicies(attestation *solid.AttestationData, aggregationBits []byte) ([]uint64, bool) {
|
||||
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) {
|
||||
attestationIndiciesCache.Add(attestation, indicies)
|
||||
func StoreAttestation(attestation *solid.AttestationData, aggregationBits []byte, indicies []uint64) {
|
||||
bitsHash := utils.Keccak256(aggregationBits)
|
||||
hash, err := attestation.HashSSZ()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
attestationIndiciesCache.Add(utils.Keccak256(hash[:], bitsHash[:]), indicies)
|
||||
}
|
||||
|
6
cl/phase1/cache/cache_test.go
vendored
6
cl/phase1/cache/cache_test.go
vendored
@ -10,9 +10,9 @@ import (
|
||||
|
||||
func TestAttestationsCache(t *testing.T) {
|
||||
input := []uint64{1}
|
||||
a := &solid.AttestationData{}
|
||||
cache.StoreAttestation(a, []uint64{1})
|
||||
output, valid := cache.LoadAttestatingIndicies(a)
|
||||
a := solid.NewAttestationData()
|
||||
cache.StoreAttestation(&a, []byte{2}, []uint64{1})
|
||||
output, valid := cache.LoadAttestatingIndicies(&a, []byte{2})
|
||||
require.True(t, valid)
|
||||
require.Equal(t, input, output)
|
||||
}
|
||||
|
4
cl/phase1/cache/init.go
vendored
4
cl/phase1/cache/init.go
vendored
@ -1,13 +1,13 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
|
||||
"github.com/ledgerwatch/erigon-lib/common"
|
||||
"github.com/ledgerwatch/erigon/cl/phase1/core/state/lru"
|
||||
)
|
||||
|
||||
func init() {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
// thus, it is a flag now.
|
||||
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
|
||||
}
|
||||
committee, err := b.GetBeaconCommitee(attestation.Slot(), attestation.ValidatorIndex())
|
||||
@ -267,7 +267,7 @@ func (b *BeaconState) GetAttestingIndicies(attestation solid.AttestationData, ag
|
||||
attestingIndices = append(attestingIndices, member)
|
||||
}
|
||||
}
|
||||
cache.StoreAttestation(&attestation, attestingIndices)
|
||||
cache.StoreAttestation(&attestation, aggregationBits, attestingIndices)
|
||||
return attestingIndices, nil
|
||||
}
|
||||
|
||||
|
@ -153,8 +153,8 @@ func (f *ForkGraph) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock, full
|
||||
// Add block to list of invalid blocks
|
||||
log.Debug("Invalid beacon block", "reason", invalidBlockErr)
|
||||
f.badBlocks[blockRoot] = struct{}{}
|
||||
f.currentReferenceState.CopyInto(f.currentState)
|
||||
f.currentStateBlockRoot, err = f.currentReferenceState.BlockRoot()
|
||||
f.nextReferenceState.CopyInto(f.currentState)
|
||||
f.currentStateBlockRoot, err = f.nextReferenceState.BlockRoot()
|
||||
if err != nil {
|
||||
log.Error("[Caplin] Could not recover from invalid block")
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ func (f *ForkChoiceStore) OnAttestation(attestation *solid.Attestation, fromBloc
|
||||
return err
|
||||
}
|
||||
target := data.Target()
|
||||
if cachedIndicies, ok := cache.LoadAttestatingIndicies(&data); ok {
|
||||
if cachedIndicies, ok := cache.LoadAttestatingIndicies(&data, attestation.AggregationBits()); ok {
|
||||
f.processAttestingIndicies(attestation, cachedIndicies)
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user