Create Bucket If Not Exists When Fetching Proposal History (#8011)

* create bucket if not exist when fetching proposal history

* adding unit test
This commit is contained in:
Raul Jordan 2020-12-01 08:00:03 -06:00 committed by GitHub
parent b150acffca
commit fc7c6776f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View File

@ -51,11 +51,11 @@ func (store *Store) ProposalHistoryForSlot(ctx context.Context, publicKey [48]by
var err error
var proposalExists bool
signingRoot := [32]byte{}
err = store.view(func(tx *bolt.Tx) error {
err = store.update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(newHistoricProposalsBucket)
valBucket := bucket.Bucket(publicKey[:])
if valBucket == nil {
return fmt.Errorf("validator history empty for public key: %#x", publicKey)
valBucket, err := bucket.CreateBucketIfNotExists(publicKey[:])
if err != nil {
return fmt.Errorf("could not create bucket for public key %#x", publicKey[:])
}
signingRootBytes := valBucket.Get(bytesutil.Uint64ToBytesBigEndian(slot))
if signingRootBytes == nil {

View File

@ -22,12 +22,13 @@ func TestProposalHistoryForSlot_InitializesNewPubKeys(t *testing.T) {
}
}
func TestNewProposalHistoryForSlot_NilDB(t *testing.T) {
func TestNewProposalHistoryForSlot_ReturnsNilIfNoHistory(t *testing.T) {
valPubkey := [48]byte{1, 2, 3}
db := setupDB(t, [][48]byte{})
_, _, err := db.ProposalHistoryForSlot(context.Background(), valPubkey, 0)
require.ErrorContains(t, "validator history empty for public key", err, "Unexpected error for nil DB")
_, proposalExists, err := db.ProposalHistoryForSlot(context.Background(), valPubkey, 0)
require.NoError(t, err)
assert.Equal(t, false, proposalExists)
}
func TestSaveProposalHistoryForSlot_OK(t *testing.T) {