github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/v1/setters_attestation.go (about) 1 package v1 2 3 import ( 4 "fmt" 5 6 "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" 7 pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" 8 "github.com/prysmaticlabs/prysm/shared/params" 9 ) 10 11 // RotateAttestations sets the previous epoch attestations to the current epoch attestations and 12 // then clears the current epoch attestations. 13 func (b *BeaconState) RotateAttestations() error { 14 if !b.hasInnerState() { 15 return ErrNilInnerState 16 } 17 b.lock.Lock() 18 defer b.lock.Unlock() 19 20 b.setPreviousEpochAttestations(b.currentEpochAttestations()) 21 b.setCurrentEpochAttestations([]*pbp2p.PendingAttestation{}) 22 return nil 23 } 24 25 func (b *BeaconState) setPreviousEpochAttestations(val []*pbp2p.PendingAttestation) { 26 b.sharedFieldReferences[previousEpochAttestations].MinusRef() 27 b.sharedFieldReferences[previousEpochAttestations] = stateutil.NewRef(1) 28 29 b.state.PreviousEpochAttestations = val 30 b.markFieldAsDirty(previousEpochAttestations) 31 b.rebuildTrie[previousEpochAttestations] = true 32 } 33 34 func (b *BeaconState) setCurrentEpochAttestations(val []*pbp2p.PendingAttestation) { 35 b.sharedFieldReferences[currentEpochAttestations].MinusRef() 36 b.sharedFieldReferences[currentEpochAttestations] = stateutil.NewRef(1) 37 38 b.state.CurrentEpochAttestations = val 39 b.markFieldAsDirty(currentEpochAttestations) 40 b.rebuildTrie[currentEpochAttestations] = true 41 } 42 43 // AppendCurrentEpochAttestations for the beacon state. Appends the new value 44 // to the the end of list. 45 func (b *BeaconState) AppendCurrentEpochAttestations(val *pbp2p.PendingAttestation) error { 46 if !b.hasInnerState() { 47 return ErrNilInnerState 48 } 49 b.lock.Lock() 50 defer b.lock.Unlock() 51 52 atts := b.state.CurrentEpochAttestations 53 max := uint64(params.BeaconConfig().SlotsPerEpoch) * params.BeaconConfig().MaxAttestations 54 if uint64(len(atts)) >= max { 55 return fmt.Errorf("current pending attestation exceeds max length %d", max) 56 } 57 58 if b.sharedFieldReferences[currentEpochAttestations].Refs() > 1 { 59 // Copy elements in underlying array by reference. 60 atts = make([]*pbp2p.PendingAttestation, len(b.state.CurrentEpochAttestations)) 61 copy(atts, b.state.CurrentEpochAttestations) 62 b.sharedFieldReferences[currentEpochAttestations].MinusRef() 63 b.sharedFieldReferences[currentEpochAttestations] = stateutil.NewRef(1) 64 } 65 66 b.state.CurrentEpochAttestations = append(atts, val) 67 b.markFieldAsDirty(currentEpochAttestations) 68 b.addDirtyIndices(currentEpochAttestations, []uint64{uint64(len(b.state.CurrentEpochAttestations) - 1)}) 69 return nil 70 } 71 72 // AppendPreviousEpochAttestations for the beacon state. Appends the new value 73 // to the the end of list. 74 func (b *BeaconState) AppendPreviousEpochAttestations(val *pbp2p.PendingAttestation) error { 75 if !b.hasInnerState() { 76 return ErrNilInnerState 77 } 78 b.lock.Lock() 79 defer b.lock.Unlock() 80 81 atts := b.state.PreviousEpochAttestations 82 max := uint64(params.BeaconConfig().SlotsPerEpoch) * params.BeaconConfig().MaxAttestations 83 if uint64(len(atts)) >= max { 84 return fmt.Errorf("previous pending attestation exceeds max length %d", max) 85 } 86 87 if b.sharedFieldReferences[previousEpochAttestations].Refs() > 1 { 88 atts = make([]*pbp2p.PendingAttestation, len(b.state.PreviousEpochAttestations)) 89 copy(atts, b.state.PreviousEpochAttestations) 90 b.sharedFieldReferences[previousEpochAttestations].MinusRef() 91 b.sharedFieldReferences[previousEpochAttestations] = stateutil.NewRef(1) 92 } 93 94 b.state.PreviousEpochAttestations = append(atts, val) 95 b.markFieldAsDirty(previousEpochAttestations) 96 b.addDirtyIndices(previousEpochAttestations, []uint64{uint64(len(b.state.PreviousEpochAttestations) - 1)}) 97 return nil 98 }