prysm-pulse/beacon-chain/blockchain/receive_attestation.go

121 lines
4.4 KiB
Go
Raw Normal View History

Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
package blockchain
import (
"context"
"encoding/hex"
"time"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)
// AttestationReceiver interface defines the methods of chain service receive and processing new attestations.
type AttestationReceiver interface {
ReceiveAttestation(ctx context.Context, att *ethpb.Attestation) error
ReceiveAttestationNoPubsub(ctx context.Context, att *ethpb.Attestation) error
}
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
// ReceiveAttestation is a function that defines the operations that are preformed on
// attestation that is received from regular sync. The operations consist of:
// 1. Gossip attestation to other peers
// 2. Validate attestation, update validator's latest vote
// 3. Apply fork choice to the processed attestation
// 4. Save latest head info
func (c *ChainService) ReceiveAttestation(ctx context.Context, att *ethpb.Attestation) error {
ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveAttestation")
defer span.End()
// Broadcast the new attestation to the network.
if err := c.p2p.Broadcast(ctx, att); err != nil {
return errors.Wrap(err, "could not broadcast attestation")
}
log.WithFields(logrus.Fields{
"attDataRoot": hex.EncodeToString(att.Data.BeaconBlockRoot),
2019-08-24 13:26:25 +00:00
}).Debug("Broadcasting attestation")
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
if err := c.ReceiveAttestationNoPubsub(ctx, att); err != nil {
return err
}
processedAtt.Inc()
return nil
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
}
// ReceiveAttestationNoPubsub is a function that defines the operations that are preformed on
// attestation that is received from regular sync. The operations consist of:
// 1. Validate attestation, update validator's latest vote
// 2. Apply fork choice to the processed attestation
// 3. Save latest head info
func (c *ChainService) ReceiveAttestationNoPubsub(ctx context.Context, att *ethpb.Attestation) error {
ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveAttestationNoPubsub")
defer span.End()
// Delay attestation inclusion until the attested slot is in the past.
slot, err := c.waitForAttInclDelay(ctx, att)
if err != nil {
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
return errors.Wrap(err, "could not delay attestation inclusion")
}
// Update forkchoice store for the new attestation
if err := c.forkChoiceStore.OnAttestation(ctx, att); err != nil {
return errors.Wrap(err, "could not process block from fork choice service")
}
log.WithFields(logrus.Fields{
"attSlot": slot,
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
"attDataRoot": hex.EncodeToString(att.Data.BeaconBlockRoot),
2019-08-24 13:26:25 +00:00
}).Debug("Finished updating fork choice store for attestation")
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
// Run fork choice for head block after updating fork choice store.
headRoot, err := c.forkChoiceStore.Head(ctx)
if err != nil {
return errors.Wrap(err, "could not get head from fork choice service")
}
headBlk, err := c.beaconDB.Block(ctx, bytesutil.ToBytes32(headRoot))
if err != nil {
return errors.Wrap(err, "could not compute state from block head")
}
log.WithFields(logrus.Fields{
"headSlot": headBlk.Slot,
"headRoot": hex.EncodeToString(headRoot),
2019-08-24 13:26:25 +00:00
}).Debug("Finished applying fork choice")
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
2019-08-23 23:23:19 +00:00
isCompetingAtts(att.Data.BeaconBlockRoot[:], headRoot)
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
// Save head info after running fork choice.
if err := c.saveHead(ctx, headBlk, bytesutil.ToBytes32(headRoot)); err != nil {
return errors.Wrap(err, "could not save head")
}
processedAttNoPubsub.Inc()
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
return nil
}
// waitForAttInclDelay waits until the next slot because attestation can only affect
// fork choice of subsequent slot. This is to delay attestation inclusion for fork choice
// until the attested slot is in the past.
func (c *ChainService) waitForAttInclDelay(ctx context.Context, a *ethpb.Attestation) (uint64, error) {
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
ctx, span := trace.StartSpan(ctx, "beacon-chain.forkchoice.waitForAttInclDelay")
defer span.End()
s, err := c.beaconDB.State(ctx, bytesutil.ToBytes32(a.Data.Target.Root))
if err != nil {
return 0, errors.Wrap(err, "could not get state")
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
}
slot, err := helpers.AttestationDataSlot(s, a.Data)
if err != nil {
return 0, errors.Wrap(err, "could not get attestation slot")
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
}
nextSlot := slot + 1
duration := time.Duration(nextSlot*params.BeaconConfig().SecondsPerSlot) * time.Second
timeToInclude := time.Unix(int64(s.GenesisTime), 0).Add(duration)
time.Sleep(time.Until(timeToInclude))
return slot, nil
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
}