Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProposerVM Epochs POC #3746

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ var (
CortinaXChainStopVertexID: ids.Empty,
DurangoTime: InitiallyActiveTime,
EtnaTime: InitiallyActiveTime,
FUpgradeTime: UnscheduledActivationTime,
FUpgradeTime: InitiallyActiveTime,
FUpgradeEpochDuration: 30,
}

ErrInvalidUpgradeTimes = errors.New("invalid upgrade configuration")
Expand All @@ -95,6 +96,7 @@ type Config struct {
DurangoTime time.Time `json:"durangoTime"`
EtnaTime time.Time `json:"etnaTime"`
FUpgradeTime time.Time `json:"fUpgradeTime"`
FUpgradeEpochDuration uint64 `json:"fUpgradeEpochDuration"`
}

func (c *Config) Validate() error {
Expand Down
69 changes: 67 additions & 2 deletions vms/proposervm/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Block interface {
buildChild(context.Context) (Block, error)

pChainHeight(context.Context) (uint64, error)
pChainEpochHeight(context.Context) (uint64, error)
}

type PostForkBlock interface {
Expand Down Expand Up @@ -163,7 +164,9 @@ func (p *postForkCommonComponents) Verify(
}

var contextPChainHeight uint64
if p.vm.Upgrades.IsEtnaActivated(childTimestamp) {
if p.vm.Upgrades.IsFUpgradeActivated(childTimestamp) {
contextPChainHeight = child.PChainEpochHeight()
} else if p.vm.Upgrades.IsEtnaActivated(childTimestamp) {
contextPChainHeight = childPChainHeight
} else {
contextPChainHeight = parentPChainHeight
Expand Down Expand Up @@ -203,6 +206,64 @@ func (p *postForkCommonComponents) buildChild(
return nil, err
}

var pChainEpochHeight, pChainEpochHeightParent uint64
if p.vm.Upgrades.IsFUpgradeActivated(newTimestamp) {
parent, err := p.vm.getBlock(ctx, parentID)
if err != nil {
p.vm.ctx.Log.Error("unexpected build block failure",
zap.String("reason", "failed to fetch parent block"),
zap.Stringer("parentID", parentID),
zap.Error(err),
)
return nil, err
}
parent2, err := p.vm.getBlock(ctx, parent.Parent())
if err != nil {
p.vm.ctx.Log.Error("unexpected build block failure",
zap.String("reason", "failed to fetch second parent block"),
zap.Stringer("parentID", parentID),
zap.Error(err),
)
return nil, err
}

if p.vm.GetEpoch(parent.Timestamp()) == p.vm.GetEpoch(parent2.Timestamp()) {
pChainEpochHeightParent, err = parent.pChainEpochHeight(ctx)
if err != nil {
p.vm.ctx.Log.Error("unexpected build block failure",
zap.String("reason", "failed to get parent epoch height"),
zap.Stringer("parentID", parentID),
zap.Error(err),
)
return nil, err
}
pChainEpochHeight = pChainEpochHeightParent

p.vm.ctx.Log.Info(
"continuing epoch",
zap.Uint64("pChainHeight", pChainHeight),
zap.Uint64("pChainEpochHeight", pChainEpochHeight),
zap.Uint64("epoch", p.vm.GetEpoch(newTimestamp)),
)
} else {
pChainEpochHeight, err = parent.pChainHeight(ctx)
if err != nil {
p.vm.ctx.Log.Error("unexpected build block failure",
zap.String("reason", "failed to get parent p-chain height"),
zap.Stringer("parentID", parentID),
zap.Error(err),
)
return nil, err
}
p.vm.ctx.Log.Info(
"epoch has advanced",
zap.Uint64("pChainHeight", pChainHeight),
zap.Uint64("pChainEpochHeight", pChainEpochHeight),
zap.Uint64("epoch", p.vm.GetEpoch(newTimestamp)),
)
}
}

var shouldBuildSignedBlock bool
if p.vm.Upgrades.IsDurangoActivated(parentTimestamp) {
shouldBuildSignedBlock, err = p.shouldBuildSignedBlockPostDurango(
Expand All @@ -226,7 +287,9 @@ func (p *postForkCommonComponents) buildChild(
}

var contextPChainHeight uint64
if p.vm.Upgrades.IsEtnaActivated(newTimestamp) {
if p.vm.Upgrades.IsFUpgradeActivated(newTimestamp) {
contextPChainHeight = pChainEpochHeight
} else if p.vm.Upgrades.IsEtnaActivated(newTimestamp) {
contextPChainHeight = pChainHeight
} else {
contextPChainHeight = parentPChainHeight
Expand All @@ -251,6 +314,7 @@ func (p *postForkCommonComponents) buildChild(
parentID,
newTimestamp,
pChainHeight,
pChainEpochHeight,
p.vm.StakingCertLeaf,
innerBlock.Bytes(),
p.vm.ctx.ChainID,
Expand All @@ -261,6 +325,7 @@ func (p *postForkCommonComponents) buildChild(
parentID,
newTimestamp,
pChainHeight,
pChainEpochHeight,
innerBlock.Bytes(),
)
}
Expand Down
16 changes: 11 additions & 5 deletions vms/proposervm/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type SignedBlock interface {
Block

PChainHeight() uint64
PChainEpochHeight() uint64
Timestamp() time.Time

// Proposer returns the ID of the node that proposed this block. If no node
Expand All @@ -43,11 +44,12 @@ type SignedBlock interface {
}

type statelessUnsignedBlock struct {
ParentID ids.ID `serialize:"true"`
Timestamp int64 `serialize:"true"`
PChainHeight uint64 `serialize:"true"`
Certificate []byte `serialize:"true"`
Block []byte `serialize:"true"`
ParentID ids.ID `serialize:"true"`
Timestamp int64 `serialize:"true"`
PChainHeight uint64 `serialize:"true"`
PChainEpochHeight uint64 `serialize:"true"`
Certificate []byte `serialize:"true"`
Block []byte `serialize:"true"`
}

type statelessBlock struct {
Expand Down Expand Up @@ -127,6 +129,10 @@ func (b *statelessBlock) PChainHeight() uint64 {
return b.StatelessBlock.PChainHeight
}

func (b *statelessBlock) PChainEpochHeight() uint64 {
return b.StatelessBlock.PChainEpochHeight
}

func (b *statelessBlock) Timestamp() time.Time {
return b.timestamp
}
Expand Down
24 changes: 14 additions & 10 deletions vms/proposervm/block/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ func BuildUnsigned(
parentID ids.ID,
timestamp time.Time,
pChainHeight uint64,
pChainEpochHeight uint64,
blockBytes []byte,
) (SignedBlock, error) {
var block SignedBlock = &statelessBlock{
StatelessBlock: statelessUnsignedBlock{
ParentID: parentID,
Timestamp: timestamp.Unix(),
PChainHeight: pChainHeight,
Certificate: nil,
Block: blockBytes,
ParentID: parentID,
Timestamp: timestamp.Unix(),
PChainHeight: pChainHeight,
PChainEpochHeight: pChainEpochHeight,
Certificate: nil,
Block: blockBytes,
},
timestamp: timestamp,
}
Expand All @@ -43,18 +45,20 @@ func Build(
parentID ids.ID,
timestamp time.Time,
pChainHeight uint64,
pChainEpochHeight uint64,
cert *staking.Certificate,
blockBytes []byte,
chainID ids.ID,
key crypto.Signer,
) (SignedBlock, error) {
block := &statelessBlock{
StatelessBlock: statelessUnsignedBlock{
ParentID: parentID,
Timestamp: timestamp.Unix(),
PChainHeight: pChainHeight,
Certificate: cert.Raw,
Block: blockBytes,
ParentID: parentID,
Timestamp: timestamp.Unix(),
PChainHeight: pChainHeight,
PChainEpochHeight: pChainEpochHeight,
Certificate: cert.Raw,
Block: blockBytes,
},
timestamp: timestamp,
cert: cert,
Expand Down
15 changes: 15 additions & 0 deletions vms/proposervm/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vms/proposervm/post_fork_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (b *postForkBlock) pChainHeight(context.Context) (uint64, error) {
return b.PChainHeight(), nil
}

func (b *postForkBlock) pChainEpochHeight(context.Context) (uint64, error) {
return b.PChainEpochHeight(), nil
}

func (b *postForkBlock) getStatelessBlk() block.Block {
return b.SignedBlock
}
8 changes: 8 additions & 0 deletions vms/proposervm/post_fork_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func (b *postForkOption) pChainHeight(ctx context.Context) (uint64, error) {
return parent.pChainHeight(ctx)
}

func (b *postForkOption) pChainEpochHeight(ctx context.Context) (uint64, error) {
parent, err := b.vm.getBlock(ctx, b.ParentID())
if err != nil {
return 0, err
}
return parent.pChainEpochHeight(ctx)
}

func (b *postForkOption) getStatelessBlk() block.Block {
return b.Block
}
5 changes: 5 additions & 0 deletions vms/proposervm/pre_fork_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (b *preForkBlock) buildChild(ctx context.Context) (Block, error) {
parentID,
newTimestamp,
pChainHeight,
pChainHeight,
innerBlock.Bytes(),
)
if err != nil {
Expand Down Expand Up @@ -238,3 +239,7 @@ func (b *preForkBlock) buildChild(ctx context.Context) (Block, error) {
func (*preForkBlock) pChainHeight(context.Context) (uint64, error) {
return 0, nil
}

func (*preForkBlock) pChainEpochHeight(context.Context) (uint64, error) {
return 0, nil
}
9 changes: 9 additions & 0 deletions vms/proposervm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ type VM struct {
acceptedBlocksSlotHistogram prometheus.Histogram
}

// Epochs are numbered starting from 0.
// The 0th epoch is defined as ending at FUpgradeTime + FUpgradeEpochDuration
func (vm *VM) GetEpoch(timestamp time.Time) uint64 {
if timestamp.Before(vm.Upgrades.FUpgradeTime) {
return 0
}
return uint64(timestamp.Sub(vm.Upgrades.FUpgradeTime).Seconds()) / vm.Upgrades.FUpgradeEpochDuration
}

// New performs best when [minBlkDelay] is whole seconds. This is because block
// timestamps are only specific to the second.
func New(
Expand Down
54 changes: 54 additions & 0 deletions vms/proposervm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2530,3 +2530,57 @@ func TestLocalParse(t *testing.T) {
})
}
}

func TestGetEpoch(t *testing.T) {
t0 := time.Now()
vm := New(
&fullVM{},
Config{
Upgrades: upgrade.Config{
FUpgradeTime: t0,
FUpgradeEpochDuration: 10,
},
},
)

testCases := []struct {
timestamp time.Time
epoch uint64
}{
{
timestamp: t0.Add(-10 * time.Second),
epoch: 0,
},
{
timestamp: t0.Add(-1 * time.Second),
epoch: 0,
},
{
timestamp: t0,
epoch: 0,
},
{
timestamp: t0.Add(1 * time.Second),
epoch: 0,
},
{
timestamp: t0.Add(9 * time.Second),
epoch: 0,
},
{
timestamp: t0.Add(10 * time.Second),
epoch: 1,
},
{
timestamp: t0.Add(11 * time.Second),
epoch: 1,
},
{
timestamp: t0.Add(20 * time.Second),
epoch: 2,
},
}
for i := range testCases {
require.Equal(t, vm.GetEpoch(testCases[i].timestamp), testCases[i].epoch)
}
}