Skip to content

Commit

Permalink
(BIDS-2182) miss att in orph slots (#2435)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauserBitfly authored Aug 8, 2023
1 parent 46fb5a0 commit 27c5566
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
50 changes: 41 additions & 9 deletions db/bigtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,16 @@ func (bigtable *Bigtable) GetValidatorAttestationHistory(validators []uint64, st
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute*5))
defer cancel()

slots := []uint64{}

for slot := startEpoch * utils.Config.Chain.Config.SlotsPerEpoch; slot < (endEpoch+1)*utils.Config.Chain.Config.SlotsPerEpoch; slot++ {
slots = append(slots, slot)
}
orphanedSlotsMap, err := GetOrphanedSlotsMap(slots)
if err != nil {
return nil, err
}

ranges := bigtable.getSlotRanges(startEpoch, endEpoch)
res := make(map[uint64][]*types.ValidatorAttestation, len(validators))

Expand Down Expand Up @@ -847,7 +857,7 @@ func (bigtable *Bigtable) GetValidatorAttestationHistory(validators []uint64, st
gcp_bigtable.LatestNFilter(1),
)
}
err := bigtable.tableBeaconchain.ReadRows(ctx, ranges, func(r gcp_bigtable.Row) bool {
err = bigtable.tableBeaconchain.ReadRows(ctx, ranges, func(r gcp_bigtable.Row) bool {
keySplit := strings.Split(r.Key(), ":")

attesterSlot, err := strconv.ParseUint(keySplit[4], 10, 64)
Expand All @@ -863,6 +873,8 @@ func (bigtable *Bigtable) GetValidatorAttestationHistory(validators []uint64, st
if inclusionSlot == max_block_number {
inclusionSlot = 0
status = 0
} else if orphanedSlotsMap[inclusionSlot] {
status = 0
}

validator, err := strconv.ParseUint(strings.TrimPrefix(ri.Column, ATTESTATIONS_FAMILY+":"), 10, 64)
Expand All @@ -876,9 +888,12 @@ func (bigtable *Bigtable) GetValidatorAttestationHistory(validators []uint64, st
}

if len(res[validator]) > 0 && res[validator][len(res[validator])-1].AttesterSlot == attesterSlot {
res[validator][len(res[validator])-1].InclusionSlot = inclusionSlot
res[validator][len(res[validator])-1].Status = status
res[validator][len(res[validator])-1].Delay = int64(inclusionSlot - attesterSlot)
// don't override successful attestion, that was included in a different slot
if status == 1 || res[validator][len(res[validator])-1].Status != 1 {
res[validator][len(res[validator])-1].InclusionSlot = inclusionSlot
res[validator][len(res[validator])-1].Status = status
res[validator][len(res[validator])-1].Delay = int64(inclusionSlot - attesterSlot)
}
} else {
res[validator] = append(res[validator], &types.ValidatorAttestation{
Index: validator,
Expand Down Expand Up @@ -963,9 +978,20 @@ func (bigtable *Bigtable) GetValidatorMissedAttestationHistory(validators []uint
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute*20))
defer cancel()

slots := []uint64{}

for slot := startEpoch * utils.Config.Chain.Config.SlotsPerEpoch; slot < (endEpoch+1)*utils.Config.Chain.Config.SlotsPerEpoch; slot++ {
slots = append(slots, slot)
}
orphanedSlotsMap, err := GetOrphanedSlotsMap(slots)
if err != nil {
return nil, err
}

ranges := bigtable.getSlotRanges(startEpoch, endEpoch)

res := make(map[uint64]map[uint64]bool)
foundValid := make(map[uint64]map[uint64]bool)

columnFilters := []gcp_bigtable.Filter{}
if valLen < 1000 {
Expand Down Expand Up @@ -995,7 +1021,7 @@ func (bigtable *Bigtable) GetValidatorMissedAttestationHistory(validators []uint
)
}

err := bigtable.tableBeaconchain.ReadRows(ctx, ranges, func(r gcp_bigtable.Row) bool {
err = bigtable.tableBeaconchain.ReadRows(ctx, ranges, func(r gcp_bigtable.Row) bool {
keySplit := strings.Split(r.Key(), ":")

attesterSlot, err := strconv.ParseUint(keySplit[4], 10, 64)
Expand All @@ -1010,7 +1036,6 @@ func (bigtable *Bigtable) GetValidatorMissedAttestationHistory(validators []uint

status := uint64(1)
if inclusionSlot == max_block_number {
inclusionSlot = 0
status = 0
}

Expand All @@ -1020,13 +1045,20 @@ func (bigtable *Bigtable) GetValidatorMissedAttestationHistory(validators []uint
return false
}

if status == 0 {
// only if the attestation was not included in another slot we count it as missed
if (status == 0 || orphanedSlotsMap[inclusionSlot]) && (foundValid[validator] == nil || !foundValid[validator][attesterSlot]) {
if res[validator] == nil {
res[validator] = make(map[uint64]bool, 0)
}
res[validator][attesterSlot] = true
} else if res[validator] != nil && res[validator][attesterSlot] {
delete(res[validator], attesterSlot)
} else {
if res[validator] != nil && res[validator][attesterSlot] {
delete(res[validator], attesterSlot)
}
if foundValid[validator] == nil {
foundValid[validator] = make(map[uint64]bool, 0)
}
foundValid[validator][attesterSlot] = true
}
}
return true
Expand Down
12 changes: 12 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3192,3 +3192,15 @@ func GetOrphanedSlots(slots []uint64) ([]uint64, error) {

return orphaned, err
}

func GetOrphanedSlotsMap(slots []uint64) (map[uint64]bool, error) {
orphanedSlots, err := GetOrphanedSlots(slots)
if err != nil {
return nil, err
}
orphanedSlotsMap := make(map[uint64]bool)
for _, slot := range orphanedSlots {
orphanedSlotsMap[slot] = true
}
return orphanedSlotsMap, nil
}

0 comments on commit 27c5566

Please sign in to comment.