Attestation is Decoupled From Proposal (#3800)

* fix

* add correct test

* add helper

* gaz

* add helper

* gaz
This commit is contained in:
Nishant Das 2019-10-22 07:15:32 +08:00 committed by GitHub
parent 97905c3e79
commit c9a7a9c709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 5 deletions

View File

@ -2,10 +2,16 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["slotticker.go"],
srcs = [
"slotticker.go",
"slottime.go",
],
importpath = "github.com/prysmaticlabs/prysm/shared/slotutil",
visibility = ["//visibility:public"],
deps = ["//shared/roughtime:go_default_library"],
deps = [
"//shared/params:go_default_library",
"//shared/roughtime:go_default_library",
],
)
go_test(

View File

@ -0,0 +1,15 @@
package slotutil
import (
"time"
"github.com/prysmaticlabs/prysm/shared/params"
)
// SlotStartTime returns the start time in terms of its unix epoch
// value.
func SlotStartTime(genesis uint64, slot uint64) time.Time {
duration := time.Second * time.Duration(slot*params.BeaconConfig().SecondsPerSlot)
startTime := time.Unix(int64(genesis), 0).Add(duration)
return startTime
}

View File

@ -92,8 +92,8 @@ func run(ctx context.Context, v Validator) {
case pb.ValidatorRole_ATTESTER:
v.AttestToBlockHead(slotCtx, slot, id)
case pb.ValidatorRole_PROPOSER:
go v.AttestToBlockHead(slotCtx, slot, id)
v.ProposeBlock(slotCtx, slot, id)
v.AttestToBlockHead(slotCtx, slot, id)
case pb.ValidatorRole_UNKNOWN:
log.Debug("No active role, doing nothing")
default:

View File

@ -14,6 +14,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/roughtime"
"github.com/prysmaticlabs/prysm/shared/slotutil"
"go.opencensus.io/trace"
)
@ -135,7 +136,7 @@ func (v *validator) waitToSlotMidpoint(ctx context.Context, slot uint64) {
if half == 0 {
delay = 500 * time.Millisecond
}
duration := time.Duration(slot*params.BeaconConfig().SecondsPerSlot) + delay
timeToBroadcast := time.Unix(int64(v.genesisTime), 0).Add(duration)
startTime := slotutil.SlotStartTime(v.genesisTime, slot)
timeToBroadcast := startTime.Add(delay)
time.Sleep(roughtime.Until(timeToBroadcast))
}

View File

@ -14,6 +14,7 @@ import (
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/roughtime"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
@ -300,3 +301,19 @@ func TestAttestToBlockHead_CorrectBitfieldLength(t *testing.T) {
t.Errorf("Wanted length %d, received %d", 2, len(generatedAttestation.AggregationBits))
}
}
func TestWaitForSlotMidPoint_WaitCorrectly(t *testing.T) {
validator, _, finish := setup(t)
defer finish()
currentTime := uint64(time.Now().Unix())
numOfSlots := uint64(4)
validator.genesisTime = currentTime - (numOfSlots * params.BeaconConfig().SecondsPerSlot)
timeToSleep := params.BeaconConfig().SecondsPerSlot / 2
midpointTime := currentTime + timeToSleep
validator.waitToSlotMidpoint(context.Background(), numOfSlots)
currentTime = uint64(time.Now().Unix())
if currentTime != midpointTime {
t.Errorf("Wanted %d time for slot midpoint but got %d", midpointTime, currentTime)
}
}