Skip to content

Commit

Permalink
P-chain validator management refactor (ava-labs#1284)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
abi87 and StephenButtolph authored May 18, 2023
1 parent b25c22b commit 49b71b4
Show file tree
Hide file tree
Showing 12 changed files with 829 additions and 769 deletions.
14 changes: 14 additions & 0 deletions snow/validators/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,17 @@ func Contains(m Manager, subnetID ids.ID, nodeID ids.NodeID) bool {
}
return vdrs.Contains(nodeID)
}

func NodeIDs(m Manager, subnetID ids.ID) ([]ids.NodeID, error) {
vdrs, exist := m.Get(subnetID)
if !exist {
return nil, fmt.Errorf("%w: %s", errMissingValidators, subnetID)
}

vdrsList := vdrs.List()
nodeIDs := make([]ids.NodeID, len(vdrsList))
for i, vdr := range vdrsList {
nodeIDs[i] = vdr.NodeID
}
return nodeIDs, nil
}
21 changes: 5 additions & 16 deletions vms/platformvm/blocks/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/utils/window"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms/components/avax"
Expand All @@ -53,13 +52,10 @@ import (
blockexecutor "github.com/ava-labs/avalanchego/vms/platformvm/blocks/executor"
txbuilder "github.com/ava-labs/avalanchego/vms/platformvm/txs/builder"
txexecutor "github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
pvalidators "github.com/ava-labs/avalanchego/vms/platformvm/validators"
)

const (
defaultWeight = 10000
maxRecentlyAcceptedWindowSize = 256
recentlyAcceptedWindowTTL = 5 * time.Minute
)
const defaultWeight = 10000

var (
defaultMinStakingDuration = 24 * time.Hour
Expand Down Expand Up @@ -154,13 +150,6 @@ func newEnvironment(t *testing.T) *environment {
}

registerer := prometheus.NewRegistry()
window := window.New[ids.ID](
window.Config{
Clock: res.clk,
MaxSize: maxRecentlyAcceptedWindowSize,
TTL: recentlyAcceptedWindowTTL,
},
)
res.sender = &common.SenderTest{T: t}

metrics, err := metrics.New("", registerer, res.config.TrackedSubnets)
Expand All @@ -177,7 +166,7 @@ func newEnvironment(t *testing.T) *environment {
metrics,
res.state,
&res.backend,
window,
pvalidators.TestManager,
)

res.Builder = New(
Expand Down Expand Up @@ -331,9 +320,9 @@ func defaultConfig() *config.Config {

func defaultClock() *mockable.Clock {
// set time after Banff fork (and before default nextStakerTime)
clk := mockable.Clock{}
clk := &mockable.Clock{}
clk.Set(defaultGenesisTime)
return &clk
return clk
}

type fxVMInt struct {
Expand Down
11 changes: 5 additions & 6 deletions vms/platformvm/blocks/executor/acceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (

"go.uber.org/zap"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/window"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"github.com/ava-labs/avalanchego/vms/platformvm/metrics"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/validators"
)

var (
Expand All @@ -29,9 +28,9 @@ var (
// being shutdown.
type acceptor struct {
*backend
metrics metrics.Metrics
recentlyAccepted window.Window[ids.ID]
bootstrapped *utils.Atomic[bool]
metrics metrics.Metrics
validators validators.Manager
bootstrapped *utils.Atomic[bool]
}

func (a *acceptor) BanffAbortBlock(b *blocks.BanffAbortBlock) error {
Expand Down Expand Up @@ -316,6 +315,6 @@ func (a *acceptor) commonAccept(b blocks.Block) error {
a.state.SetLastAccepted(blkID)
a.state.SetHeight(b.Height())
a.state.AddStatelessBlock(b, choices.Accepted)
a.recentlyAccepted.Add(blkID)
a.validators.OnAcceptedBlockID(blkID)
return nil
}
39 changes: 11 additions & 28 deletions vms/platformvm/blocks/executor/acceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package executor

import (
"testing"
"time"

"github.com/golang/mock/gomock"

Expand All @@ -19,12 +18,12 @@ import (
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/window"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"github.com/ava-labs/avalanchego/vms/platformvm/metrics"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/validators"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

Expand Down Expand Up @@ -61,8 +60,8 @@ func TestAcceptorVisitProposalBlock(t *testing.T) {
},
state: s,
},
metrics: metrics.Noop,
recentlyAccepted: nil,
metrics: metrics.Noop,
validators: validators.TestManager,
}

err = acceptor.ApricotProposalBlock(blk)
Expand Down Expand Up @@ -98,12 +97,8 @@ func TestAcceptorVisitAtomicBlock(t *testing.T) {
SharedMemory: sharedMemory,
},
},
metrics: metrics.Noop,
recentlyAccepted: window.New[ids.ID](window.Config{
Clock: &mockable.Clock{},
MaxSize: 1,
TTL: time.Hour,
}),
metrics: metrics.Noop,
validators: validators.TestManager,
}

blk, err := blocks.NewApricotAtomicBlock(
Expand Down Expand Up @@ -183,12 +178,8 @@ func TestAcceptorVisitStandardBlock(t *testing.T) {
SharedMemory: sharedMemory,
},
},
metrics: metrics.Noop,
recentlyAccepted: window.New[ids.ID](window.Config{
Clock: clk,
MaxSize: 1,
TTL: time.Hour,
}),
metrics: metrics.Noop,
validators: validators.TestManager,
}

blk, err := blocks.NewBanffStandardBlock(
Expand Down Expand Up @@ -278,12 +269,8 @@ func TestAcceptorVisitCommitBlock(t *testing.T) {
SharedMemory: sharedMemory,
},
},
metrics: metrics.Noop,
recentlyAccepted: window.New[ids.ID](window.Config{
Clock: &mockable.Clock{},
MaxSize: 1,
TTL: time.Hour,
}),
metrics: metrics.Noop,
validators: validators.TestManager,
bootstrapped: &utils.Atomic[bool]{},
}

Expand Down Expand Up @@ -374,12 +361,8 @@ func TestAcceptorVisitAbortBlock(t *testing.T) {
SharedMemory: sharedMemory,
},
},
metrics: metrics.Noop,
recentlyAccepted: window.New[ids.ID](window.Config{
Clock: &mockable.Clock{},
MaxSize: 1,
TTL: time.Hour,
}),
metrics: metrics.Noop,
validators: validators.TestManager,
bootstrapped: &utils.Atomic[bool]{},
}

Expand Down
21 changes: 6 additions & 15 deletions vms/platformvm/blocks/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/utils/window"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms/components/avax"
Expand All @@ -54,15 +53,14 @@ import (

db_manager "github.com/ava-labs/avalanchego/database/manager"
p_tx_builder "github.com/ava-labs/avalanchego/vms/platformvm/txs/builder"
pvalidators "github.com/ava-labs/avalanchego/vms/platformvm/validators"
)

const (
pending stakerStatus = iota
current

defaultWeight = 10000
maxRecentlyAcceptedWindowSize = 256
recentlyAcceptedWindowTTL = 5 * time.Minute
defaultWeight = 10000
)

var (
Expand Down Expand Up @@ -189,13 +187,6 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
}

registerer := prometheus.NewRegistry()
window := window.New[ids.ID](
window.Config{
Clock: res.clk,
MaxSize: maxRecentlyAcceptedWindowSize,
TTL: recentlyAcceptedWindowTTL,
},
)
res.sender = &common.SenderTest{T: t}

metrics := metrics.Noop
Expand All @@ -212,7 +203,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
metrics,
res.state,
res.backend,
window,
pvalidators.TestManager,
)
addSubnet(res)
} else {
Expand All @@ -221,7 +212,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
metrics,
res.mockedState,
res.backend,
window,
pvalidators.TestManager,
)
// we do not add any subnet to state, since we can mock
// whatever we need
Expand Down Expand Up @@ -362,9 +353,9 @@ func defaultConfig() *config.Config {
}

func defaultClock() *mockable.Clock {
clk := mockable.Clock{}
clk := &mockable.Clock{}
clk.Set(defaultGenesisTime)
return &clk
return clk
}

type fxVMInt struct {
Expand Down
12 changes: 6 additions & 6 deletions vms/platformvm/blocks/executor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package executor
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/utils/window"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"github.com/ava-labs/avalanchego/vms/platformvm/metrics"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"
"github.com/ava-labs/avalanchego/vms/platformvm/validators"
)

var _ Manager = (*manager)(nil)
Expand All @@ -31,7 +31,7 @@ func NewManager(
metrics metrics.Metrics,
s state.State,
txExecutorBackend *executor.Backend,
recentlyAccepted window.Window[ids.ID],
validatorManager validators.Manager,
) Manager {
backend := &backend{
Mempool: mempool,
Expand All @@ -48,10 +48,10 @@ func NewManager(
txExecutorBackend: txExecutorBackend,
},
acceptor: &acceptor{
backend: backend,
metrics: metrics,
recentlyAccepted: recentlyAccepted,
bootstrapped: txExecutorBackend.Bootstrapped,
backend: backend,
metrics: metrics,
validators: validatorManager,
bootstrapped: txExecutorBackend.Bootstrapped,
},
rejector: &rejector{backend: backend},
}
Expand Down
14 changes: 7 additions & 7 deletions vms/platformvm/txs/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ func newEnvironment(postBanff, postCortina bool) *environment {
baseDB := versiondb.New(baseDBManager.Current().Database)
ctx, msm := defaultCtx(baseDB)

fx := defaultFx(&clk, ctx.Log, isBootstrapped.Get())
fx := defaultFx(clk, ctx.Log, isBootstrapped.Get())

rewards := reward.NewCalculator(config.RewardConfig)
baseState := defaultState(&config, ctx, baseDB, rewards)

atomicUTXOs := avax.NewAtomicUTXOManager(ctx.SharedMemory, txs.Codec)
uptimes := uptime.NewManager(baseState)
utxoHandler := utxo.NewHandler(ctx, &clk, fx)
utxoHandler := utxo.NewHandler(ctx, clk, fx)

txBuilder := builder.New(
ctx,
&config,
&clk,
clk,
fx,
baseState,
atomicUTXOs,
Expand All @@ -142,7 +142,7 @@ func newEnvironment(postBanff, postCortina bool) *environment {
backend := Backend{
Config: &config,
Ctx: ctx,
Clk: &clk,
Clk: clk,
Bootstrapped: &isBootstrapped,
Fx: fx,
FlowChecker: utxoHandler,
Expand All @@ -153,7 +153,7 @@ func newEnvironment(postBanff, postCortina bool) *environment {
env := &environment{
isBootstrapped: &isBootstrapped,
config: &config,
clk: &clk,
clk: clk,
baseDB: baseDB,
ctx: ctx,
msm: msm,
Expand Down Expand Up @@ -318,13 +318,13 @@ func defaultConfig(postBanff, postCortina bool) config.Config {
}
}

func defaultClock(postFork bool) mockable.Clock {
func defaultClock(postFork bool) *mockable.Clock {
now := defaultGenesisTime
if postFork {
// 1 second after Banff fork
now = defaultValidateEndTime.Add(-2 * time.Second)
}
clk := mockable.Clock{}
clk := &mockable.Clock{}
clk.Set(now)
return clk
}
Expand Down
Loading

0 comments on commit 49b71b4

Please sign in to comment.