Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slasher batch db read and a write #5534

Merged
merged 24 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
90189d8
batch db read
shayzluf Apr 17, 2020
ad59b5e
fix test
shayzluf Apr 17, 2020
10e7c6b
debug
shayzluf Apr 18, 2020
6af01bf
write function and test
shayzluf Apr 20, 2020
af4edd7
test rename
shayzluf Apr 20, 2020
0846487
Merge branch 'master' of github.com:prysmaticlabs/prysm into slasher_…
shayzluf Apr 20, 2020
e3bc086
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
e35c6e9
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
5298f9d
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
9d1ea5a
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
3da4fb1
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
4828475
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
e0d056a
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
e20ee67
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
63bf760
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
0fa5035
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
47cdc0e
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 20, 2020
1be0fb5
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 21, 2020
0a57d2f
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 21, 2020
398bda2
add to interface
shayzluf Apr 21, 2020
c151ea8
Merge branch 'slasher_batch_db_read' of github.com:prysmaticlabs/prys…
shayzluf Apr 21, 2020
2087193
change order
shayzluf Apr 21, 2020
da2344f
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 21, 2020
de0d753
Merge refs/heads/master into slasher_batch_db_read
prylabs-bulldozer[bot] Apr 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions slasher/db/iface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ReadOnlyDatabase interface {
// MinMaxSpan related methods.
EpochSpansMap(ctx context.Context, epoch uint64) (map[uint64]detectionTypes.Span, error)
EpochSpanByValidatorIndex(ctx context.Context, validatorIdx uint64, epoch uint64) (detectionTypes.Span, error)
EpochsSpanByValidatorsIndices(ctx context.Context, validatorIndices []uint64, maxEpoch uint64) (map[uint64]map[uint64]detectionTypes.Span, error)

// ProposerSlashing related methods.
ProposalSlashingsByStatus(ctx context.Context, status types.SlashingStatus) ([]*ethpb.ProposerSlashing, error)
Expand Down Expand Up @@ -64,6 +65,7 @@ type WriteAccessDatabase interface {
SaveEpochSpansMap(ctx context.Context, epoch uint64, spanMap map[uint64]detectionTypes.Span) error
SaveValidatorEpochSpan(ctx context.Context, validatorIdx uint64, epoch uint64, spans detectionTypes.Span) error
SaveCachedSpansMaps(ctx context.Context) error
SaveEpochsSpanByValidatorsIndices(ctx context.Context, epochsSpans map[uint64]map[uint64]detectionTypes.Span) error
DeleteEpochSpans(ctx context.Context, validatorIdx uint64) error
DeleteValidatorSpanByEpoch(ctx context.Context, validatorIdx uint64, epoch uint64) error

Expand Down
62 changes: 62 additions & 0 deletions slasher/db/kv/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,68 @@ func (db *Store) EpochSpanByValidatorIndex(ctx context.Context, validatorIdx uin
return spans, err
}

// EpochsSpanByValidatorsIndices accepts validator indices and epoch and
// returns all their previous corresponding spans for slashing detection epoch=> validator index => spammap.
// Returns empty map if no values exists and error on db error.
func (db *Store) EpochsSpanByValidatorsIndices(ctx context.Context, validatorIndices []uint64, maxEpoch uint64) (map[uint64]map[uint64]types.Span, error) {
ctx, span := trace.StartSpan(ctx, "slasherDB.EpochsSpanByValidatorsIndices")
defer span.End()

var err error
epochsSpanMap := make(map[uint64]map[uint64]types.Span)
err = db.view(func(tx *bolt.Tx) error {
b := tx.Bucket(validatorsMinMaxSpanBucket)
epoch := maxEpoch
epochBucket := b.Bucket(bytesutil.Bytes8(epoch))

for epochBucket != nil {
valSpans := make(map[uint64]types.Span, len(validatorIndices))
for _, v := range validatorIndices {
enc := epochBucket.Get(bytesutil.Bytes8(v))
value, err := unmarshalSpan(ctx, enc)
if err != nil {
return err
}
valSpans[v] = value
}
epochsSpanMap[epoch] = valSpans
if epoch == 0 {
break
}
epoch--
epochBucket = b.Bucket(bytesutil.Bytes8(epoch))
}
return nil
})
return epochsSpanMap, err
}

// SaveEpochsSpanByValidatorsIndices accepts epochs span maps by validator indices and
// writes them to db.
// Returns error on db write error.
func (db *Store) SaveEpochsSpanByValidatorsIndices(ctx context.Context, epochsSpans map[uint64]map[uint64]types.Span) error {
ctx, span := trace.StartSpan(ctx, "slasherDB.SaveEpochsSpanByValidatorsIndices")
defer span.End()

err := db.update(func(tx *bolt.Tx) error {
b := tx.Bucket(validatorsMinMaxSpanBucket)
for epoch, indicesSpanMaps := range epochsSpans {
epochBucket, err := b.CreateBucketIfNotExists(bytesutil.Bytes8(epoch))
if err != nil {
return err
}
for idx, v := range indicesSpanMaps {
enc := marshalSpan(v)
if err := epochBucket.Put(bytesutil.Bytes8(idx), enc); err != nil {
return err
}
}
}
return nil
})
return err
}

// SaveValidatorEpochSpan accepts validator index epoch and spans returns.
// it reads the epoch spans from cache, updates it and save it back to cache
// if caching is enabled.
Expand Down
45 changes: 45 additions & 0 deletions slasher/db/kv/spanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,48 @@ func TestValidatorSpanMap_SaveCachedSpansMaps(t *testing.T) {
}
}
}

func TestStore_ReadWriteEpochsSpanByValidatorsIndices(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(&app, set, nil))
defer teardownDB(t, db)
ctx := context.Background()

for _, tt := range spanTests {
err := db.SaveEpochSpansMap(ctx, tt.epoch, tt.spanMap)
if err != nil {
t.Fatalf("Save validator span map failed: %v", err)
}
}
res, err := db.EpochsSpanByValidatorsIndices(ctx, []uint64{1, 2, 3}, 3)
if err != nil {
t.Fatal(err)
}
if len(res) != len(spanTests) {
t.Errorf("Wanted map of %d elemets, received map of %d elements", len(spanTests), len(res))
}
for _, tt := range spanTests {
if !reflect.DeepEqual(res[tt.epoch], tt.spanMap) {
t.Errorf("Wanted span map to be equal to: %v , received span map: %v ", spanTests[0].spanMap, res[1])
}
}
teardownDB(t, db)
db = setupDB(t, cli.NewContext(&app, set, nil))
if err := db.SaveEpochsSpanByValidatorsIndices(ctx, res); err != nil {
t.Fatal(err)
}
res, err = db.EpochsSpanByValidatorsIndices(ctx, []uint64{1, 2, 3}, 3)
if err != nil {
t.Fatal(err)
}
if len(res) != len(spanTests) {
t.Errorf("Wanted map of %d elemets, received map of %d elements", len(spanTests), len(res))
}
for _, tt := range spanTests {
if !reflect.DeepEqual(res[tt.epoch], tt.spanMap) {
t.Errorf("Wanted span map to be equal to: %v , received span map: %v ", spanTests[0].spanMap, res[1])
}
}

}