create the bucket only once (#8579)

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
This commit is contained in:
Nishant Das 2021-03-09 22:22:52 +08:00 committed by GitHub
parent 363771a5c7
commit 2bb0a602e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -398,6 +398,15 @@ func (s *Store) saveAttestationRecords(ctx context.Context, atts []*AttestationR
ctx, span := trace.StartSpan(ctx, "Validator.saveAttestationRecords")
defer span.End()
return s.update(func(tx *bolt.Tx) error {
// Initialize buckets for the lowest target and source epochs.
lowestSourceBucket, err := tx.CreateBucketIfNotExists(lowestSignedSourceBucket)
if err != nil {
return err
}
lowestTargetBucket, err := tx.CreateBucketIfNotExists(lowestSignedTargetBucket)
if err != nil {
return err
}
bucket := tx.Bucket(pubKeysBucket)
for _, att := range atts {
pkBucket, err := bucket.CreateBucketIfNotExists(att.PubKey[:])
@ -448,16 +457,6 @@ func (s *Store) saveAttestationRecords(ctx context.Context, atts []*AttestationR
return errors.Wrapf(err, "could not save target epoch %d for epoch %d", att.Target, att.Source)
}
// Initialize buckets for the lowest target and source epochs.
lowestSourceBucket, err := tx.CreateBucketIfNotExists(lowestSignedSourceBucket)
if err != nil {
return err
}
lowestTargetBucket, err := tx.CreateBucketIfNotExists(lowestSignedTargetBucket)
if err != nil {
return err
}
// If the incoming source epoch is lower than the lowest signed source epoch, override.
lowestSignedSourceBytes := lowestSourceBucket.Get(att.PubKey[:])
var lowestSignedSourceEpoch types.Epoch