2021-03-17 19:49:49 +00:00
|
|
|
package stateV0
|
2019-12-04 19:20:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2021-04-12 14:23:55 +00:00
|
|
|
"fmt"
|
2019-12-04 19:20:33 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-03-18 23:29:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2021-04-12 14:23:55 +00:00
|
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-03-31 18:57:19 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2020-06-18 02:15:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/htrutils"
|
2019-12-04 19:20:33 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
2021-04-12 14:23:55 +00:00
|
|
|
// PreviousEpochAttestations corresponding to blocks on the beacon chain.
|
|
|
|
func (b *BeaconState) PreviousEpochAttestations() ([]*pbp2p.PendingAttestation, error) {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if b.state.PreviousEpochAttestations == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.previousEpochAttestations(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// previousEpochAttestations corresponding to blocks on the beacon chain.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) previousEpochAttestations() []*pbp2p.PendingAttestation {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.safeCopyPendingAttestationSlice(b.state.PreviousEpochAttestations)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentEpochAttestations corresponding to blocks on the beacon chain.
|
|
|
|
func (b *BeaconState) CurrentEpochAttestations() ([]*pbp2p.PendingAttestation, error) {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if b.state.CurrentEpochAttestations == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.currentEpochAttestations(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// currentEpochAttestations corresponding to blocks on the beacon chain.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) currentEpochAttestations() []*pbp2p.PendingAttestation {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.safeCopyPendingAttestationSlice(b.state.CurrentEpochAttestations)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *stateRootHasher) epochAttestationsRoot(atts []*pbp2p.PendingAttestation) ([32]byte, error) {
|
|
|
|
max := uint64(params.BeaconConfig().SlotsPerEpoch) * params.BeaconConfig().MaxAttestations
|
|
|
|
if uint64(len(atts)) > max {
|
|
|
|
return [32]byte{}, fmt.Errorf("epoch attestation exceeds max length %d", max)
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:14:26 +00:00
|
|
|
hasher := hashutil.CustomSHA256Hasher()
|
|
|
|
roots := make([][]byte, len(atts))
|
|
|
|
for i := 0; i < len(atts); i++ {
|
|
|
|
pendingRoot, err := h.pendingAttestationRoot(hasher, atts[i])
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, errors.Wrap(err, "could not attestation merkleization")
|
|
|
|
}
|
|
|
|
roots[i] = pendingRoot[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
attsRootsRoot, err := htrutils.BitwiseMerkleize(
|
|
|
|
hasher,
|
|
|
|
roots,
|
|
|
|
uint64(len(roots)),
|
|
|
|
uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, errors.Wrap(err, "could not compute epoch attestations merkleization")
|
|
|
|
}
|
|
|
|
attsLenBuf := new(bytes.Buffer)
|
|
|
|
if err := binary.Write(attsLenBuf, binary.LittleEndian, uint64(len(atts))); err != nil {
|
|
|
|
return [32]byte{}, errors.Wrap(err, "could not marshal epoch attestations length")
|
|
|
|
}
|
|
|
|
// We need to mix in the length of the slice.
|
|
|
|
attsLenRoot := make([]byte, 32)
|
|
|
|
copy(attsLenRoot, attsLenBuf.Bytes())
|
|
|
|
res := htrutils.MixInLength(attsRootsRoot, attsLenRoot)
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 14:23:55 +00:00
|
|
|
func (h *stateRootHasher) pendingAttestationRoot(hasher htrutils.HashFn, att *pbp2p.PendingAttestation) ([32]byte, error) {
|
2021-03-18 23:29:06 +00:00
|
|
|
if att == nil {
|
|
|
|
return [32]byte{}, errors.New("nil pending attestation")
|
|
|
|
}
|
2021-03-16 18:14:26 +00:00
|
|
|
// Marshal attestation to determine if it exists in the cache.
|
2021-03-18 23:29:06 +00:00
|
|
|
enc := stateutil.PendingAttEncKey(att)
|
2021-03-16 18:14:26 +00:00
|
|
|
|
2021-03-18 23:29:06 +00:00
|
|
|
// Check if it exists in cache:
|
|
|
|
if h.rootsCache != nil {
|
|
|
|
if found, ok := h.rootsCache.Get(string(enc)); found != nil && ok {
|
|
|
|
return found.([32]byte), nil
|
2020-03-17 19:25:17 +00:00
|
|
|
}
|
2021-03-16 18:14:26 +00:00
|
|
|
}
|
2020-03-17 19:25:17 +00:00
|
|
|
|
2021-03-18 23:29:06 +00:00
|
|
|
res, err := stateutil.PendingAttRootWithHasher(hasher, att)
|
2021-03-16 18:14:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
2020-03-17 19:25:17 +00:00
|
|
|
}
|
2021-03-16 18:14:26 +00:00
|
|
|
if h.rootsCache != nil {
|
|
|
|
h.rootsCache.Set(string(enc), res, 32)
|
|
|
|
}
|
|
|
|
return res, nil
|
2020-03-17 19:25:17 +00:00
|
|
|
}
|