Skip to content

Commit

Permalink
Add proposerMinBlockDelay to subnet config (ava-labs#2202)
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 authored Nov 8, 2022
1 parent 538bc90 commit c2bbcf9
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 17 deletions.
10 changes: 10 additions & 0 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,20 @@ func (m *manager) createSnowmanChain(
return nil, fmt.Errorf("error while fetching chain config: %w", err)
}

minBlockDelay := proposervm.DefaultMinBlockDelay
if subnetCfg, ok := m.SubnetConfigs[ctx.SubnetID]; ok {
minBlockDelay = subnetCfg.ProposerMinBlockDelay
}
m.Log.Info("creating proposervm wrapper",
zap.Time("activationTime", m.ApricotPhase4Time),
zap.Uint64("minPChainHeight", m.ApricotPhase4MinPChainHeight),
zap.Duration("minBlockDelay", minBlockDelay),
)
vm = proposervm.New(
vm,
m.ApricotPhase4Time,
m.ApricotPhase4MinPChainHeight,
minBlockDelay,
)

if m.MeterVMEnabled {
Expand Down
6 changes: 6 additions & 0 deletions chains/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package chains

import (
"sync"
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
Expand All @@ -31,6 +32,11 @@ type SubnetConfig struct {
// ValidatorOnly indicates that this Subnet's Chains are available to only subnet validators.
ValidatorOnly bool `json:"validatorOnly" yaml:"validatorOnly"`
ConsensusParameters avalanche.Parameters `json:"consensusParameters" yaml:"consensusParameters"`

// ProposerMinBlockDelay is the minimum delay this node will enforce when
// building a snowman++ block.
// TODO: Remove this flag once all VMs throttle their own block production.
ProposerMinBlockDelay time.Duration `json:"proposerMinBlockDelay" yaml:"proposerMinBlockDelay"`
}

type subnet struct {
Expand Down
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/proposervm"
)

const (
Expand Down Expand Up @@ -1145,9 +1146,10 @@ func parseSubnetConfigs(data []byte, defaultSubnetConfig chains.SubnetConfig) (c

func getDefaultSubnetConfig(v *viper.Viper) chains.SubnetConfig {
return chains.SubnetConfig{
ConsensusParameters: getConsensusConfig(v),
ValidatorOnly: false,
GossipConfig: getGossipConfig(v),
ConsensusParameters: getConsensusConfig(v),
ValidatorOnly: false,
GossipConfig: getGossipConfig(v),
ProposerMinBlockDelay: proposervm.DefaultMinBlockDelay,
}
}

Expand Down
2 changes: 1 addition & 1 deletion vms/proposervm/batched_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ func initTestRemoteProposerVM(
}
}

proVM := New(coreVM, proBlkStartTime, 0)
proVM := New(coreVM, proBlkStartTime, 0, DefaultMinBlockDelay)

valState := &validators.TestState{
T: t,
Expand Down
2 changes: 1 addition & 1 deletion vms/proposervm/post_fork_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ func TestOptionTimestampValidity(t *testing.T) {
// Restart the node.

ctx := proVM.ctx
proVM = New(coreVM, time.Time{}, 0)
proVM = New(coreVM, time.Time{}, 0, DefaultMinBlockDelay)

coreVM.InitializeF = func(
*snow.Context,
Expand Down
2 changes: 1 addition & 1 deletion vms/proposervm/state_syncable_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func helperBuildStateSyncTestObjects(t *testing.T) (*fullVM, *VM) {
innerVM.GetBlockF = func(i ids.ID) (snowman.Block, error) { return innerGenesisBlk, nil }

// createVM
vm := New(innerVM, time.Time{}, 0)
vm := New(innerVM, time.Time{}, 0, DefaultMinBlockDelay)

ctx := snow.DefaultContextTest()
ctx.NodeID = ids.NodeIDFromCert(pTestCert.Leaf)
Expand Down
23 changes: 18 additions & 5 deletions vms/proposervm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ import (
)

const (
// minBlockDelay should be kept as whole seconds because block timestamps
// are only specific to the second.
minBlockDelay = time.Second
// DefaultMinBlockDelay should be kept as whole seconds because block
// timestamps are only specific to the second.
DefaultMinBlockDelay = time.Second

checkIndexedFrequency = 10 * time.Second
innerBlkCacheSize = 512
)
Expand All @@ -62,6 +63,7 @@ type VM struct {

activationTime time.Time
minimumPChainHeight uint64
minBlkDelay time.Duration

state.State
hIndexer indexer.HeightIndexer
Expand Down Expand Up @@ -99,10 +101,13 @@ type VM struct {
lastAcceptedHeight uint64
}

// New performs best when [minBlkDelay] is whole seconds. This is because block
// timestamps are only specific to the second.
func New(
vm block.ChainVM,
activationTime time.Time,
minimumPChainHeight uint64,
minBlkDelay time.Duration,
) *VM {
bVM, _ := vm.(block.BatchedChainVM)
hVM, _ := vm.(block.HeightIndexedChainVM)
Expand All @@ -115,6 +120,7 @@ func New(

activationTime: activationTime,
minimumPChainHeight: minimumPChainHeight,
minBlkDelay: minBlkDelay,
}
}

Expand Down Expand Up @@ -284,8 +290,15 @@ func (vm *VM) SetPreference(preferred ids.ID) error {
// until the P-chain's height has advanced.
return nil
}
if minDelay < minBlockDelay {
minDelay = minBlockDelay

// Note: The P-chain does not currently try to target any block time. It
// notifies the consensus engine as soon as a new block may be built. To
// avoid fast runs of blocks there is an additional minimum delay that
// validators can specify. This delay may be an issue for high performance,
// custom VMs. Until the P-chain is modified to target a specific block
// time, ProposerMinBlockDelay can be configured in the subnet config.
if minDelay < vm.minBlkDelay {
minDelay = vm.minBlkDelay
}

preferredTime := blk.Timestamp()
Expand Down
13 changes: 7 additions & 6 deletions vms/proposervm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func initTestProposerVM(
}
}

proVM := New(coreVM, proBlkStartTime, minPChainHeight)
proVM := New(coreVM, proBlkStartTime, minPChainHeight, DefaultMinBlockDelay)

valState := &validators.TestState{
T: t,
Expand Down Expand Up @@ -866,7 +866,7 @@ func TestExpiredBuildBlock(t *testing.T) {
}
}

proVM := New(coreVM, time.Time{}, 0)
proVM := New(coreVM, time.Time{}, 0, DefaultMinBlockDelay)

valState := &validators.TestState{
T: t,
Expand Down Expand Up @@ -1196,7 +1196,7 @@ func TestInnerVMRollback(t *testing.T) {

dbManager := manager.NewMemDB(version.Semantic1_0_0)

proVM := New(coreVM, time.Time{}, 0)
proVM := New(coreVM, time.Time{}, 0, DefaultMinBlockDelay)

if err := proVM.Initialize(ctx, dbManager, nil, nil, nil, nil, nil, nil); err != nil {
t.Fatalf("failed to initialize proposerVM with %s", err)
Expand Down Expand Up @@ -1287,7 +1287,7 @@ func TestInnerVMRollback(t *testing.T) {

coreBlk.StatusV = choices.Processing

proVM = New(coreVM, time.Time{}, 0)
proVM = New(coreVM, time.Time{}, 0, DefaultMinBlockDelay)

if err := proVM.Initialize(ctx, dbManager, nil, nil, nil, nil, nil, nil); err != nil {
t.Fatalf("failed to initialize proposerVM with %s", err)
Expand Down Expand Up @@ -1813,7 +1813,7 @@ func TestRejectedHeightNotIndexed(t *testing.T) {
}
}

proVM := New(coreVM, time.Time{}, 0)
proVM := New(coreVM, time.Time{}, 0, DefaultMinBlockDelay)

valState := &validators.TestState{
T: t,
Expand Down Expand Up @@ -1991,7 +1991,7 @@ func TestRejectedOptionHeightNotIndexed(t *testing.T) {
}
}

proVM := New(coreVM, time.Time{}, 0)
proVM := New(coreVM, time.Time{}, 0, DefaultMinBlockDelay)

valState := &validators.TestState{
T: t,
Expand Down Expand Up @@ -2128,6 +2128,7 @@ func TestVMInnerBlkCache(t *testing.T) {
innerVM,
time.Time{}, // fork is active
0, // minimum P-Chain height
DefaultMinBlockDelay,
)

dummyDBManager := manager.NewMemDB(version.Semantic1_0_0)
Expand Down

0 comments on commit c2bbcf9

Please sign in to comment.