2021-06-30 15:06:19 +00:00
|
|
|
package v1
|
2021-04-12 14:23:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2021-12-13 09:16:23 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
2021-07-29 21:45:17 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-04-12 14:23:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RotateAttestations sets the previous epoch attestations to the current epoch attestations and
|
|
|
|
// then clears the current epoch attestations.
|
|
|
|
func (b *BeaconState) RotateAttestations() error {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return ErrNilInnerState
|
|
|
|
}
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
b.setPreviousEpochAttestations(b.currentEpochAttestations())
|
2021-07-29 21:45:17 +00:00
|
|
|
b.setCurrentEpochAttestations([]*ethpb.PendingAttestation{})
|
2021-04-12 14:23:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:45:17 +00:00
|
|
|
func (b *BeaconState) setPreviousEpochAttestations(val []*ethpb.PendingAttestation) {
|
2021-04-12 14:23:55 +00:00
|
|
|
b.sharedFieldReferences[previousEpochAttestations].MinusRef()
|
|
|
|
b.sharedFieldReferences[previousEpochAttestations] = stateutil.NewRef(1)
|
|
|
|
|
|
|
|
b.state.PreviousEpochAttestations = val
|
|
|
|
b.markFieldAsDirty(previousEpochAttestations)
|
|
|
|
b.rebuildTrie[previousEpochAttestations] = true
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:45:17 +00:00
|
|
|
func (b *BeaconState) setCurrentEpochAttestations(val []*ethpb.PendingAttestation) {
|
2021-04-12 14:23:55 +00:00
|
|
|
b.sharedFieldReferences[currentEpochAttestations].MinusRef()
|
|
|
|
b.sharedFieldReferences[currentEpochAttestations] = stateutil.NewRef(1)
|
|
|
|
|
|
|
|
b.state.CurrentEpochAttestations = val
|
|
|
|
b.markFieldAsDirty(currentEpochAttestations)
|
|
|
|
b.rebuildTrie[currentEpochAttestations] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendCurrentEpochAttestations for the beacon state. Appends the new value
|
|
|
|
// to the the end of list.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (b *BeaconState) AppendCurrentEpochAttestations(val *ethpb.PendingAttestation) error {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
|
|
|
return ErrNilInnerState
|
|
|
|
}
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
atts := b.state.CurrentEpochAttestations
|
2021-12-13 09:16:23 +00:00
|
|
|
max := uint64(fieldparams.CurrentEpochAttestationsLength)
|
2021-04-12 14:23:55 +00:00
|
|
|
if uint64(len(atts)) >= max {
|
|
|
|
return fmt.Errorf("current pending attestation exceeds max length %d", max)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.sharedFieldReferences[currentEpochAttestations].Refs() > 1 {
|
|
|
|
// Copy elements in underlying array by reference.
|
2021-07-29 21:45:17 +00:00
|
|
|
atts = make([]*ethpb.PendingAttestation, len(b.state.CurrentEpochAttestations))
|
2021-04-12 14:23:55 +00:00
|
|
|
copy(atts, b.state.CurrentEpochAttestations)
|
|
|
|
b.sharedFieldReferences[currentEpochAttestations].MinusRef()
|
|
|
|
b.sharedFieldReferences[currentEpochAttestations] = stateutil.NewRef(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.state.CurrentEpochAttestations = append(atts, val)
|
|
|
|
b.markFieldAsDirty(currentEpochAttestations)
|
2021-04-13 19:12:56 +00:00
|
|
|
b.addDirtyIndices(currentEpochAttestations, []uint64{uint64(len(b.state.CurrentEpochAttestations) - 1)})
|
2021-04-12 14:23:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendPreviousEpochAttestations for the beacon state. Appends the new value
|
|
|
|
// to the the end of list.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (b *BeaconState) AppendPreviousEpochAttestations(val *ethpb.PendingAttestation) error {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
|
|
|
return ErrNilInnerState
|
|
|
|
}
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
atts := b.state.PreviousEpochAttestations
|
2021-12-13 09:16:23 +00:00
|
|
|
max := uint64(fieldparams.PreviousEpochAttestationsLength)
|
2021-04-12 14:23:55 +00:00
|
|
|
if uint64(len(atts)) >= max {
|
|
|
|
return fmt.Errorf("previous pending attestation exceeds max length %d", max)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.sharedFieldReferences[previousEpochAttestations].Refs() > 1 {
|
2021-07-29 21:45:17 +00:00
|
|
|
atts = make([]*ethpb.PendingAttestation, len(b.state.PreviousEpochAttestations))
|
2021-04-12 14:23:55 +00:00
|
|
|
copy(atts, b.state.PreviousEpochAttestations)
|
|
|
|
b.sharedFieldReferences[previousEpochAttestations].MinusRef()
|
|
|
|
b.sharedFieldReferences[previousEpochAttestations] = stateutil.NewRef(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.state.PreviousEpochAttestations = append(atts, val)
|
|
|
|
b.markFieldAsDirty(previousEpochAttestations)
|
|
|
|
b.addDirtyIndices(previousEpochAttestations, []uint64{uint64(len(b.state.PreviousEpochAttestations) - 1)})
|
|
|
|
return nil
|
|
|
|
}
|