2020-07-06 22:27:42 +00:00
|
|
|
package blocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-09-01 01:29:27 +00:00
|
|
|
"context"
|
2020-07-06 22:27:42 +00:00
|
|
|
"errors"
|
|
|
|
|
2021-03-08 22:37:33 +00:00
|
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
2021-06-02 23:49:52 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2021-05-26 18:33:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/copyutil"
|
2020-07-06 22:27:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProcessEth1DataInBlock is an operation performed on each
|
|
|
|
// beacon block to ensure the ETH1 data votes are processed
|
|
|
|
// into the beacon state.
|
|
|
|
//
|
|
|
|
// Official spec definition:
|
|
|
|
// def process_eth1_data(state: BeaconState, body: BeaconBlockBody) -> None:
|
|
|
|
// state.eth1_data_votes.append(body.eth1_data)
|
|
|
|
// if state.eth1_data_votes.count(body.eth1_data) * 2 > EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH:
|
2021-04-13 05:35:22 +00:00
|
|
|
// state.eth1_data = body.eth1_data
|
2021-04-15 13:58:54 +00:00
|
|
|
func ProcessEth1DataInBlock(_ context.Context, beaconState iface.BeaconState, eth1Data *ethpb.Eth1Data) (iface.BeaconState, error) {
|
2021-05-24 04:55:42 +00:00
|
|
|
if beaconState == nil || beaconState.IsNil() {
|
2020-07-06 22:27:42 +00:00
|
|
|
return nil, errors.New("nil state")
|
|
|
|
}
|
2021-04-15 13:58:54 +00:00
|
|
|
if err := beaconState.AppendEth1DataVotes(eth1Data); err != nil {
|
2020-07-06 22:27:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-15 13:58:54 +00:00
|
|
|
hasSupport, err := Eth1DataHasEnoughSupport(beaconState, eth1Data)
|
2020-07-06 22:27:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if hasSupport {
|
2021-04-15 13:58:54 +00:00
|
|
|
if err := beaconState.SetEth1Data(eth1Data); err != nil {
|
2020-07-06 22:27:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return beaconState, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AreEth1DataEqual checks equality between two eth1 data objects.
|
|
|
|
func AreEth1DataEqual(a, b *ethpb.Eth1Data) bool {
|
|
|
|
if a == nil && b == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return a.DepositCount == b.DepositCount &&
|
|
|
|
bytes.Equal(a.BlockHash, b.BlockHash) &&
|
|
|
|
bytes.Equal(a.DepositRoot, b.DepositRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eth1DataHasEnoughSupport returns true when the given eth1data has more than 50% votes in the
|
|
|
|
// eth1 voting period. A vote is cast by including eth1data in a block and part of state processing
|
|
|
|
// appends eth1data to the state in the Eth1DataVotes list. Iterating through this list checks the
|
|
|
|
// votes to see if they match the eth1data.
|
2021-03-08 22:37:33 +00:00
|
|
|
func Eth1DataHasEnoughSupport(beaconState iface.ReadOnlyBeaconState, data *ethpb.Eth1Data) (bool, error) {
|
2020-07-06 22:27:42 +00:00
|
|
|
voteCount := uint64(0)
|
2021-05-26 18:33:46 +00:00
|
|
|
data = copyutil.CopyETH1Data(data)
|
2020-07-06 22:27:42 +00:00
|
|
|
|
|
|
|
for _, vote := range beaconState.Eth1DataVotes() {
|
|
|
|
if AreEth1DataEqual(vote, data) {
|
|
|
|
voteCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If 50+% majority converged on the same eth1data, then it has enough support to update the
|
|
|
|
// state.
|
2021-02-16 07:45:34 +00:00
|
|
|
support := params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod))
|
2021-02-09 10:05:22 +00:00
|
|
|
return voteCount*2 > uint64(support), nil
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|