mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 05:47:59 -05:00
Added roughtime to IsSlotValid and fixed test TestIsValidBlock_InvalidSlot (#3186)
* add roughtime to IsSlotValid * gazelle * gofmt -s
This commit is contained in:
committed by
Preston Van Loon
parent
a3ac250ac1
commit
4e886a84f9
@@ -20,6 +20,7 @@ go_library(
|
||||
"//shared/bytesutil:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"//shared/params:go_default_library",
|
||||
"//shared/roughtime:go_default_library",
|
||||
"//shared/sliceutil:go_default_library",
|
||||
"//shared/trieutil:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//common:go_default_library",
|
||||
@@ -52,6 +53,7 @@ go_test(
|
||||
"//shared/bytesutil:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"//shared/params:go_default_library",
|
||||
"//shared/roughtime:go_default_library",
|
||||
"//shared/testutil:go_default_library",
|
||||
"//shared/trieutil:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//common:go_default_library",
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/prysmaticlabs/prysm/shared/roughtime"
|
||||
)
|
||||
|
||||
// IsValidBlock ensures that the block is compliant with the block processing validity conditions.
|
||||
@@ -56,9 +57,9 @@ func IsValidBlock(
|
||||
|
||||
// IsSlotValid compares the slot to the system clock to determine if the block is valid.
|
||||
func IsSlotValid(slot uint64, genesisTime time.Time) bool {
|
||||
secondsPerSlot := time.Duration((slot)*params.BeaconConfig().SecondsPerSlot) * time.Second
|
||||
validTimeThreshold := genesisTime.Add(secondsPerSlot)
|
||||
now := time.Now()
|
||||
secondsSinceGenesis := time.Duration(slot*params.BeaconConfig().SecondsPerSlot) * time.Second
|
||||
validTimeThreshold := genesisTime.Add(secondsSinceGenesis)
|
||||
now := roughtime.Now()
|
||||
isValid := now.After(validTimeThreshold)
|
||||
|
||||
return isValid
|
||||
|
||||
@@ -2,6 +2,7 @@ package blocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -9,6 +10,8 @@ import (
|
||||
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/prysmaticlabs/prysm/shared/roughtime"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -60,32 +63,32 @@ func TestIsValidBlock_NoParent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIsValidBlock_InvalidSlot(t *testing.T) {
|
||||
beaconState := &pb.BeaconState{}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
db := &mockDB{}
|
||||
powClient := &mockPOWClient{}
|
||||
|
||||
beaconState.Slot = 3
|
||||
|
||||
beaconState := &pb.BeaconState{
|
||||
Slot: 3,
|
||||
Eth1Data: ðpb.Eth1Data{
|
||||
DepositRoot: []byte{2},
|
||||
BlockHash: []byte{3},
|
||||
},
|
||||
}
|
||||
db := &mockDB{
|
||||
hasBlock: true,
|
||||
}
|
||||
powClient := &mockPOWClient{
|
||||
blockExists: true,
|
||||
}
|
||||
block := ðpb.BeaconBlock{
|
||||
Slot: 4,
|
||||
}
|
||||
genesisTime := time.Now()
|
||||
|
||||
genesisTime := time.Unix(0, 0)
|
||||
|
||||
block.Slot = 3
|
||||
db.hasBlock = true
|
||||
|
||||
beaconState.Eth1Data = ðpb.Eth1Data{
|
||||
DepositRoot: []byte{2},
|
||||
BlockHash: []byte{3},
|
||||
}
|
||||
if err := IsValidBlock(ctx, beaconState, block,
|
||||
db.HasBlock, powClient.BlockByHash, genesisTime); err == nil {
|
||||
err := IsValidBlock(ctx, beaconState, block, db.HasBlock, powClient.BlockByHash, genesisTime)
|
||||
if err == nil {
|
||||
t.Fatalf("block is valid despite having an invalid slot %d", block.Slot)
|
||||
}
|
||||
if !strings.HasPrefix(err.Error(), "slot of block is too high: ") {
|
||||
t.Fatalf("expected the error about too high slot, but got an error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidBlock_InvalidPoWReference(t *testing.T) {
|
||||
@@ -176,3 +179,31 @@ func TestIsValidBlock_GoodBlock(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSlotValid(t *testing.T) {
|
||||
type testCaseStruct struct {
|
||||
slot uint64
|
||||
genesisTime time.Time
|
||||
result bool
|
||||
}
|
||||
|
||||
testCases := []testCaseStruct{
|
||||
{
|
||||
slot: 5,
|
||||
genesisTime: roughtime.Now(),
|
||||
result: false,
|
||||
},
|
||||
{
|
||||
slot: 5,
|
||||
genesisTime: roughtime.Now().Add(
|
||||
-time.Duration(params.BeaconConfig().SecondsPerSlot*5) * time.Second,
|
||||
),
|
||||
result: true,
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
if testCase.result != IsSlotValid(testCase.slot, testCase.genesisTime) {
|
||||
t.Fatalf("invalid IsSlotValid result for %v", testCase)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,10 +77,15 @@ func init() {
|
||||
|
||||
// Since returns the duration since t, based on the roughtime response
|
||||
func Since(t time.Time) time.Duration {
|
||||
return time.Now().Add(offset).Sub(t)
|
||||
return Now().Sub(t)
|
||||
}
|
||||
|
||||
// Until returns the duration until t, based on the roughtime response
|
||||
func Until(t time.Time) time.Duration {
|
||||
return t.Sub(time.Now().Add(offset))
|
||||
return t.Sub(Now())
|
||||
}
|
||||
|
||||
// Now returns the current local time given the roughtime offset.
|
||||
func Now() time.Time {
|
||||
return time.Now().Add(offset)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user