2020-11-26 16:50:55 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// The size of each data entry in bytes for the source epoch (8 bytes) and signing root (32 bytes).
|
|
|
|
uint64Size = 8
|
|
|
|
latestEpochWrittenSize = uint64Size
|
|
|
|
targetSize = uint64Size
|
|
|
|
sourceSize = uint64Size
|
|
|
|
signingRootSize = 32
|
|
|
|
historySize = targetSize + sourceSize + signingRootSize
|
|
|
|
minimalSize = latestEpochWrittenSize
|
|
|
|
)
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
// deprecatedHistoryData stores the needed data to confirm if an attestation is slashable
|
2020-11-26 16:50:55 +00:00
|
|
|
// or repeated.
|
2021-01-12 15:32:13 +00:00
|
|
|
type deprecatedHistoryData struct {
|
2020-11-26 16:50:55 +00:00
|
|
|
Source uint64
|
|
|
|
SigningRoot []byte
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
// deprecatedEncodedAttestingHistory encapsulated history data.
|
|
|
|
type deprecatedEncodedAttestingHistory []byte
|
2020-11-26 16:50:55 +00:00
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
func (hd deprecatedEncodedAttestingHistory) assertSize() error {
|
2020-11-26 16:50:55 +00:00
|
|
|
if hd == nil || len(hd) < minimalSize {
|
|
|
|
return fmt.Errorf("encapsulated data size: %d is smaller then minimal size: %d", len(hd), minimalSize)
|
|
|
|
}
|
|
|
|
if (len(hd)-minimalSize)%historySize != 0 {
|
|
|
|
return fmt.Errorf("encapsulated data size: %d is not a multiple of entry size: %d", len(hd), historySize)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
func (h *deprecatedHistoryData) isEmpty() bool {
|
|
|
|
if h == (*deprecatedHistoryData)(nil) {
|
2020-11-26 16:50:55 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if h.Source == params.BeaconConfig().FarFutureEpoch {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
func emptyHistoryData() *deprecatedHistoryData {
|
|
|
|
h := &deprecatedHistoryData{Source: params.BeaconConfig().FarFutureEpoch, SigningRoot: bytesutil.PadTo([]byte{}, 32)}
|
2020-11-26 16:50:55 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
// newDeprecatedAttestingHistory creates a new encapsulated attestation history byte array
|
2020-11-26 16:50:55 +00:00
|
|
|
// sized by the latest epoch written.
|
2021-01-12 15:32:13 +00:00
|
|
|
func newDeprecatedAttestingHistory(target uint64) deprecatedEncodedAttestingHistory {
|
2020-11-26 16:50:55 +00:00
|
|
|
relativeTarget := target % params.BeaconConfig().WeakSubjectivityPeriod
|
|
|
|
historyDataSize := (relativeTarget + 1) * historySize
|
|
|
|
arraySize := latestEpochWrittenSize + historyDataSize
|
2021-01-12 15:32:13 +00:00
|
|
|
en := make(deprecatedEncodedAttestingHistory, arraySize)
|
2020-11-26 16:50:55 +00:00
|
|
|
enc := en
|
|
|
|
ctx := context.Background()
|
|
|
|
var err error
|
|
|
|
for i := uint64(0); i <= target%params.BeaconConfig().WeakSubjectivityPeriod; i++ {
|
2021-01-12 15:32:13 +00:00
|
|
|
enc, err = enc.setTargetData(ctx, i, emptyHistoryData())
|
2020-11-26 16:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to set empty target data")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enc
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
func (hd deprecatedEncodedAttestingHistory) getLatestEpochWritten(ctx context.Context) (uint64, error) {
|
2020-11-26 16:50:55 +00:00
|
|
|
if err := hd.assertSize(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return bytesutil.FromBytes8(hd[:latestEpochWrittenSize]), nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
func (hd deprecatedEncodedAttestingHistory) setLatestEpochWritten(ctx context.Context, latestEpochWritten uint64) (deprecatedEncodedAttestingHistory, error) {
|
2020-11-26 16:50:55 +00:00
|
|
|
if err := hd.assertSize(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(hd[:latestEpochWrittenSize], bytesutil.Uint64ToBytesLittleEndian(latestEpochWritten))
|
|
|
|
return hd, nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
func (hd deprecatedEncodedAttestingHistory) getTargetData(ctx context.Context, target uint64) (*deprecatedHistoryData, error) {
|
2020-11-26 16:50:55 +00:00
|
|
|
if err := hd.assertSize(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Cursor for the location to read target epoch from.
|
|
|
|
// Modulus of target epoch X weak subjectivity period in order to have maximum size to the encapsulated data array.
|
|
|
|
cursor := (target%params.BeaconConfig().WeakSubjectivityPeriod)*historySize + latestEpochWrittenSize
|
|
|
|
if uint64(len(hd)) < cursor+historySize {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-01-12 15:32:13 +00:00
|
|
|
history := &deprecatedHistoryData{}
|
2020-11-26 16:50:55 +00:00
|
|
|
history.Source = bytesutil.FromBytes8(hd[cursor : cursor+sourceSize])
|
|
|
|
sr := make([]byte, 32)
|
|
|
|
copy(sr, hd[cursor+sourceSize:cursor+historySize])
|
|
|
|
history.SigningRoot = sr
|
|
|
|
return history, nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:32:13 +00:00
|
|
|
func (hd deprecatedEncodedAttestingHistory) setTargetData(ctx context.Context, target uint64, historyData *deprecatedHistoryData) (deprecatedEncodedAttestingHistory, error) {
|
2020-11-26 16:50:55 +00:00
|
|
|
if err := hd.assertSize(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Cursor for the location to write target epoch to.
|
|
|
|
// Modulus of target epoch X weak subjectivity period in order to have maximum size to the encapsulated data array.
|
|
|
|
cursor := latestEpochWrittenSize + (target%params.BeaconConfig().WeakSubjectivityPeriod)*historySize
|
|
|
|
|
|
|
|
if uint64(len(hd)) < cursor+historySize {
|
|
|
|
ext := make([]byte, cursor+historySize-uint64(len(hd)))
|
|
|
|
hd = append(hd, ext...)
|
|
|
|
}
|
|
|
|
copy(hd[cursor:cursor+sourceSize], bytesutil.Uint64ToBytesLittleEndian(historyData.Source))
|
|
|
|
copy(hd[cursor+sourceSize:cursor+sourceSize+signingRootSize], historyData.SigningRoot)
|
|
|
|
|
|
|
|
return hd, nil
|
|
|
|
}
|