func (s *State) applyVote(vote AggregatedVote, config *config.Config) error {
	voteHash, err := ssz.HashTreeRoot(vote.Data)
	if err != nil {
		return err
	}

	for i, proposal := range s.Proposals {
		proposalHash, err := ssz.HashTreeRoot(proposal.Data)
		if err != nil {
			return err
		}

		// proposal is already active
		if bytes.Equal(proposalHash[:], voteHash[:]) {
			// ignore if already queued
			if proposal.Queued {
				return nil
			}

			err := s.validateParticipationSignature(voteHash, vote.Participation, vote.Signature)
			if err != nil {
				return err
			}

			needed := len(vote.Participation) - len(s.Proposals[i].Participation)
			if needed > 0 {
				s.Proposals[i].Participation = append(s.Proposals[i].Participation, make([]uint8, needed)...)
			}

			// update the proposal
			for j := range vote.Participation {
				s.Proposals[i].Participation[j] |= vote.Participation[j]
			}

			return nil
		}
	}

	proposerBitSet := vote.Participation[vote.Data.Proposer/8] & (1 << uint(vote.Data.Proposer%8))

	if proposerBitSet == 0 {
		return errors.New("could not process vote with proposer bit not set")
	}

	if vote.Data.Type == Cancel {
		foundProposalToCancel := false

		for _, proposal := range s.Proposals {
			proposalHash, err := ssz.HashTreeRoot(proposal.Data)
			if err != nil {
				return err
			}

			if bytes.Equal(vote.Data.ActionHash[:], proposalHash[:]) {
				foundProposalToCancel = true
			}
		}

		if !foundProposalToCancel {
			return errors.New("could not find proposal to cancel")
		}
	}

	err = s.validateParticipationSignature(voteHash, vote.Participation, vote.Signature)
	if err != nil {
		return err
	}

	s.ValidatorBalances[vote.Data.Proposer] -= config.ProposalCost

	s.Proposals = append(s.Proposals, ActiveProposal{
		Data:          vote.Data,
		Participation: vote.Participation,
		StartEpoch:    s.EpochIndex,
		Queued:        false,
	})

	return nil
}
// applyVote validates a vote and adds it to pending votes.
applyVote validates a vote and adds it to pending votes.
// applyVote validates a vote and adds it to pending votes .
func (s *State) applyVote(vote AggregatedVote, config *config.Config) error {
	voteHash, err := ssz.HashTreeRoot(vote.Data)
	if err != nil {
		return err
	}
	for i, proposal := range s.Proposals {
		proposalHash, err := ssz.HashTreeRoot(proposal.Data)
		if err != nil {
			return err
		}
		if bytes.Equal(proposalHash[:], voteHash[:]) {
			if proposal.Queued {
				return nil
			}
			err := s.validateParticipationSignature(voteHash, vote.Participation, vote.Signature)
			if err != nil {
				return err
			}
			needed := len(vote.Participation) - len(s.Proposals[i].Participation)
			if needed > 0 {
				s.Proposals[i].Participation = append(s.Proposals[i].Participation, make([]uint8, needed)...)
			}
			for j := range vote.Participation {
				s.Proposals[i].Participation[j] |= vote.Participation[j]
			}
			return nil
		}
	}
	proposerBitSet := vote.Participation[vote.Data.Proposer/8] & (1 << uint(vote.Data.Proposer%8))
	if proposerBitSet == 0 {
		return errors.New("could not process vote with proposer bit not set")
	}
	if vote.Data.Type == Cancel {
		foundProposalToCancel := false
		for _, proposal := range s.Proposals {
			proposalHash, err := ssz.HashTreeRoot(proposal.Data)
			if err != nil {
				return err
			}
			if bytes.Equal(vote.Data.ActionHash[:], proposalHash[:]) {
				foundProposalToCancel = true
			}
		}
		if !foundProposalToCancel {
			return errors.New("could not find proposal to cancel")
		}
	}
	err = s.validateParticipationSignature(voteHash, vote.Participation, vote.Signature)
	if err != nil {
		return err
	}
	s.ValidatorBalances[vote.Data.Proposer] -= config.ProposalCost
	s.Proposals = append(s.Proposals, ActiveProposal{
		Data:          vote.Data,
		Participation: vote.Participation,
		StartEpoch:    s.EpochIndex,
		Queued:        false,
	})
	return nil
}
func ( s * State ) applyVote ( vote AggregatedVote , config * config . Config ) error { voteHash , err := ssz . HashTreeRoot ( vote . Data ) if err != nil { return err } for i , proposal := range s . Proposals { proposalHash , err := ssz . HashTreeRoot ( proposal . Data ) if err != nil { return err } if bytes . Equal ( proposalHash [ : ] , voteHash [ : ] ) { if proposal . Queued { return nil } err := s . validateParticipationSignature ( voteHash , vote . Participation , vote . Signature ) if err != nil { return err } needed := len ( vote . Participation ) - len ( s . Proposals [ i ] . Participation ) if needed > 0 { s . Proposals [ i ] . Participation = append ( s . Proposals [ i ] . Participation , make ( [ ] uint8 , needed ) ... ) } for j := range vote . Participation { s . Proposals [ i ] . Participation [ j ] |= vote . Participation [ j ] } return nil } } proposerBitSet := vote . Participation [ vote . Data . Proposer / 8 ] & ( 1 << uint ( vote . Data . Proposer % 8 ) ) if proposerBitSet == 0 { return errors . New ( " " ) } if vote . Data . Type == Cancel { foundProposalToCancel := false for _ , proposal := range s . Proposals { proposalHash , err := ssz . HashTreeRoot ( proposal . Data ) if err != nil { return err } if bytes . Equal ( vote . Data . ActionHash [ : ] , proposalHash [ : ] ) { foundProposalToCancel = true } } if ! foundProposalToCancel { return errors . New ( " " ) } } err = s . validateParticipationSignature ( voteHash , vote . Participation , vote . Signature ) if err != nil { return err } s . ValidatorBalances [ vote . Data . Proposer ] -= config . ProposalCost s . Proposals = append ( s . Proposals , ActiveProposal { Data : vote . Data , Participation : vote . Participation , StartEpoch : s . EpochIndex , Queued : false , } ) return nil }
applyVote validates a vote and adds it to pending votes.
applyVote validates a vote and adds it to pending votes .
// proposal is already active // ignore if already queued // update the proposal