prysm-pulse/validator/db/kv/graffiti.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

40 lines
1.2 KiB
Go

package kv
import (
"bytes"
"context"
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
bolt "go.etcd.io/bbolt"
)
// SaveGraffitiOrderedIndex writes the current graffiti index to the db
func (s *Store) SaveGraffitiOrderedIndex(_ context.Context, index uint64) error {
return s.db.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket(graffitiBucket)
indexBytes := bytesutil.Uint64ToBytesBigEndian(index)
return bkt.Put(graffitiOrderedIndexKey, indexBytes)
})
}
// GraffitiOrderedIndex fetches the ordered index, resetting if the file hash changed
func (s *Store) GraffitiOrderedIndex(_ context.Context, fileHash [32]byte) (uint64, error) {
orderedIndex := uint64(0)
err := s.db.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket(graffitiBucket)
dbFileHash := bkt.Get(graffitiFileHashKey)
if bytes.Equal(dbFileHash, fileHash[:]) {
indexBytes := bkt.Get(graffitiOrderedIndexKey)
orderedIndex = bytesutil.BytesToUint64BigEndian(indexBytes)
} else {
indexBytes := bytesutil.Uint64ToBytesBigEndian(0)
if err := bkt.Put(graffitiOrderedIndexKey, indexBytes); err != nil {
return err
}
return bkt.Put(graffitiFileHashKey, fileHash[:])
}
return nil
})
return orderedIndex, err
}