Fix Bolt Fatal Crash (#3320)

* add fix and reg test

* nogo

* nogo
This commit is contained in:
Nishant Das 2019-08-26 19:30:40 +05:30 committed by Raul Jordan
parent 9ad00ffafb
commit 5828278807
2 changed files with 73 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package kv
import (
"context"
"sync"
"testing"
"github.com/gogo/protobuf/proto"
@ -56,6 +57,70 @@ func TestStore_AttestationCRUD(t *testing.T) {
}
}
func TestStore_BoltDontPanic(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
var wg sync.WaitGroup
for i := 0; i <= 100; i++ {
att := &ethpb.Attestation{
Data: &ethpb.AttestationData{
Crosslink: &ethpb.Crosslink{
Shard: 5,
ParentRoot: []byte("parent"),
StartEpoch: uint64(i + 1),
EndEpoch: 2,
},
},
}
ctx := context.Background()
attDataRoot, err := ssz.HashTreeRoot(att.Data)
if err != nil {
t.Fatal(err)
}
retrievedAtt, err := db.Attestation(ctx, attDataRoot)
if err != nil {
t.Fatal(err)
}
if retrievedAtt != nil {
t.Errorf("Expected nil attestation, received %v", retrievedAtt)
}
if err := db.SaveAttestation(ctx, att); err != nil {
t.Fatal(err)
}
}
// if indices are improperly deleted this test will then panic.
for i := 0; i <= 100; i++ {
startEpoch := i + 1
wg.Add(1)
go func() {
att := &ethpb.Attestation{
Data: &ethpb.AttestationData{
Crosslink: &ethpb.Crosslink{
Shard: 5,
ParentRoot: []byte("parent"),
StartEpoch: uint64(startEpoch),
EndEpoch: 2,
},
},
}
ctx := context.Background()
attDataRoot, err := ssz.HashTreeRoot(att.Data)
if err != nil {
t.Fatal(err)
}
if err := db.DeleteAttestation(ctx, attDataRoot); err != nil {
t.Fatal(err)
}
if db.HasAttestation(ctx, attDataRoot) {
t.Error("Expected attestation to have been deleted from the db")
}
wg.Done()
}()
}
wg.Wait()
}
func TestStore_Attestations_FiltersCorrectly(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)

View File

@ -61,7 +61,14 @@ func deleteValueForIndices(indicesByBucket map[string][]byte, root []byte, tx *b
// We clear out the root from the values at index slice. For example,
// If we had [0x32, 0x33, 0x45] and we wanted to clear out 0x33, the code below
// updates the slice to [0x32, 0x45].
valuesAtIndex = append(valuesAtIndex[:start], valuesAtIndex[start+len(root):]...)
valuesStart := make([]byte, len(valuesAtIndex[:start]))
copy(valuesStart, valuesAtIndex[:start])
valuesEnd := make([]byte, len(valuesAtIndex[start+len(root):]))
copy(valuesEnd, valuesAtIndex[start+len(root):])
valuesAtIndex = append(valuesStart, valuesEnd...)
if err := bkt.Put(idx, valuesAtIndex); err != nil {
return err
}