Skip to content

Commit

Permalink
Add engine type to consensus context (ava-labs#2485)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Jan 17, 2023
1 parent eca193c commit de547d3
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 90 deletions.
2 changes: 1 addition & 1 deletion api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func lockMiddleware(
// not done state-syncing/bootstrapping, writes back an error.
func rejectMiddleware(handler http.Handler, ctx *snow.ConsensusContext) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // If chain isn't done bootstrapping, ignore API calls
if ctx.State.Get() != snow.NormalOp {
if ctx.State.Get().State != snow.NormalOp {
w.WriteHeader(http.StatusServiceUnavailable)
// Doesn't matter if there's an error while writing. They'll get the StatusServiceUnavailable code.
_, _ = w.Write([]byte("API call rejected because chain is not done bootstrapping"))
Expand Down
4 changes: 1 addition & 3 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@ func (m *manager) createAvalancheChain(
msgChan,
sb.afterBootstrapped(),
m.ConsensusGossipFrequency,
p2p.EngineType_ENGINE_TYPE_AVALANCHE,
m.ResourceTracker,
validators.UnhandledSubnetConnector, // avalanche chains don't use subnet connector
)
Expand Down Expand Up @@ -966,7 +965,6 @@ func (m *manager) createSnowmanChain(
msgChan,
sb.afterBootstrapped(),
m.ConsensusGossipFrequency,
p2p.EngineType_ENGINE_TYPE_SNOWMAN,
m.ResourceTracker,
subnetConnector,
)
Expand Down Expand Up @@ -1093,7 +1091,7 @@ func (m *manager) IsBootstrapped(id ids.ID) bool {
return false
}

return chain.Context().State.Get() == snow.NormalOp
return chain.Context().State.Get().State == snow.NormalOp
}

func (m *manager) subnetsNotBootstrapped() []ids.ID {
Expand Down
2 changes: 1 addition & 1 deletion snow/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type ConsensusContext struct {
ConsensusAcceptor Acceptor

// State indicates the current state of this consensus instance.
State utils.Atomic[State]
State utils.Atomic[EngineState]

// True iff this chain is executing transactions as part of bootstrapping.
Executing utils.Atomic[bool]
Expand Down
6 changes: 5 additions & 1 deletion snow/engine/avalanche/bootstrap/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/proto/pb/p2p"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
Expand Down Expand Up @@ -322,7 +323,10 @@ func (*bootstrapper) Notify(context.Context, common.Message) error {
func (b *bootstrapper) Start(ctx context.Context, startReqID uint32) error {
b.Ctx.Log.Info("starting bootstrap")

b.Ctx.State.Set(snow.Bootstrapping)
b.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.Bootstrapping,
})
if err := b.VM.SetState(ctx, snow.Bootstrapping); err != nil {
return fmt.Errorf("failed to notify VM that bootstrapping has started: %w",
err)
Expand Down
59 changes: 42 additions & 17 deletions snow/engine/avalanche/bootstrap/bootstrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/proto/pb/p2p"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
Expand Down Expand Up @@ -146,7 +147,10 @@ func TestBootstrapperSingleFrontier(t *testing.T) {
context.Background(),
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.NormalOp)
config.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
return nil
},
)
Expand Down Expand Up @@ -193,7 +197,7 @@ func TestBootstrapperSingleFrontier(t *testing.T) {
}

switch {
case config.Ctx.State.Get() != snow.NormalOp:
case config.Ctx.State.Get().State != snow.NormalOp:
t.Fatalf("Bootstrapping should have finished")
case vtx0.Status() != choices.Accepted:
t.Fatalf("Vertex should be accepted")
Expand Down Expand Up @@ -249,7 +253,10 @@ func TestBootstrapperByzantineResponses(t *testing.T) {
context.Background(),
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.NormalOp)
config.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
return nil
},
)
Expand Down Expand Up @@ -343,7 +350,7 @@ func TestBootstrapperByzantineResponses(t *testing.T) {
switch {
case *requestID != oldReqID:
t.Fatal("should not have issued new request")
case config.Ctx.State.Get() != snow.NormalOp:
case config.Ctx.State.Get().State != snow.NormalOp:
t.Fatalf("Bootstrapping should have finished")
case vtx0.Status() != choices.Accepted:
t.Fatalf("Vertex should be accepted")
Expand Down Expand Up @@ -427,7 +434,10 @@ func TestBootstrapperTxDependencies(t *testing.T) {
context.Background(),
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.NormalOp)
config.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
return nil
},
)
Expand Down Expand Up @@ -498,7 +508,7 @@ func TestBootstrapperTxDependencies(t *testing.T) {
t.Fatal(err)
}

if config.Ctx.State.Get() != snow.NormalOp {
if config.Ctx.State.Get().State != snow.NormalOp {
t.Fatalf("Should have finished bootstrapping")
}
if tx0.Status() != choices.Accepted {
Expand Down Expand Up @@ -571,7 +581,10 @@ func TestBootstrapperMissingTxDependency(t *testing.T) {
context.Background(),
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.NormalOp)
config.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
return nil
},
)
Expand Down Expand Up @@ -631,7 +644,7 @@ func TestBootstrapperMissingTxDependency(t *testing.T) {
t.Fatal(err)
}

if config.Ctx.State.Get() != snow.NormalOp {
if config.Ctx.State.Get().State != snow.NormalOp {
t.Fatalf("Bootstrapping should have finished")
}
if tx0.Status() != choices.Unknown { // never saw this tx
Expand Down Expand Up @@ -692,7 +705,10 @@ func TestBootstrapperIncompleteAncestors(t *testing.T) {
context.Background(),
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.NormalOp)
config.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
return nil
},
)
Expand Down Expand Up @@ -760,7 +776,7 @@ func TestBootstrapperIncompleteAncestors(t *testing.T) {
switch {
case err != nil: // Provide vtx1; should request vtx0
t.Fatal(err)
case bs.Context().State.Get() == snow.NormalOp:
case bs.Context().State.Get().State == snow.NormalOp:
t.Fatalf("should not have finished")
case requested != vtxID0:
t.Fatal("should hae requested vtx0")
Expand All @@ -770,7 +786,7 @@ func TestBootstrapperIncompleteAncestors(t *testing.T) {
switch {
case err != nil: // Provide vtx0; can finish now
t.Fatal(err)
case bs.Context().State.Get() != snow.NormalOp:
case bs.Context().State.Get().State != snow.NormalOp:
t.Fatal("should have finished")
case vtx0.Status() != choices.Accepted:
t.Fatal("should be accepted")
Expand Down Expand Up @@ -812,7 +828,10 @@ func TestBootstrapperFinalized(t *testing.T) {
context.Background(),
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.NormalOp)
config.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
return nil
},
)
Expand Down Expand Up @@ -891,7 +910,7 @@ func TestBootstrapperFinalized(t *testing.T) {
switch {
case err != nil:
t.Fatal(err)
case config.Ctx.State.Get() != snow.NormalOp:
case config.Ctx.State.Get().State != snow.NormalOp:
t.Fatalf("Bootstrapping should have finished")
case vtx0.Status() != choices.Accepted:
t.Fatalf("Vertex should be accepted")
Expand Down Expand Up @@ -943,7 +962,10 @@ func TestBootstrapperAcceptsAncestorsParents(t *testing.T) {
context.Background(),
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.NormalOp)
config.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
return nil
},
)
Expand Down Expand Up @@ -1024,7 +1046,7 @@ func TestBootstrapperAcceptsAncestorsParents(t *testing.T) {
}

switch {
case config.Ctx.State.Get() != snow.NormalOp:
case config.Ctx.State.Get().State != snow.NormalOp:
t.Fatalf("Bootstrapping should have finished")
case vtx0.Status() != choices.Accepted:
t.Fatalf("Vertex should be accepted")
Expand Down Expand Up @@ -1110,7 +1132,10 @@ func TestRestartBootstrapping(t *testing.T) {
context.Background(),
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.NormalOp)
config.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
return nil
},
)
Expand Down Expand Up @@ -1272,7 +1297,7 @@ func TestRestartBootstrapping(t *testing.T) {
}

switch {
case config.Ctx.State.Get() != snow.NormalOp:
case config.Ctx.State.Get().State != snow.NormalOp:
t.Fatalf("Bootstrapping should have finished")
case vtx0.Status() != choices.Accepted:
t.Fatalf("Vertex should be accepted")
Expand Down
6 changes: 5 additions & 1 deletion snow/engine/avalanche/transitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/proto/pb/p2p"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche/poll"
Expand Down Expand Up @@ -376,7 +377,10 @@ func (t *Transitive) Start(ctx context.Context, startReqID uint32) error {
)
t.metrics.bootstrapFinished.Set(1)

t.Ctx.State.Set(snow.NormalOp)
t.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_AVALANCHE,
State: snow.NormalOp,
})
if err := t.VM.SetState(ctx, snow.NormalOp); err != nil {
return fmt.Errorf("failed to notify VM that consensus has started: %w",
err)
Expand Down
2 changes: 1 addition & 1 deletion snow/engine/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *Config) Context() *snow.ConsensusContext {

// IsBootstrapped returns true iff this chain is done bootstrapping
func (c *Config) IsBootstrapped() bool {
return c.Ctx.State.Get() == snow.NormalOp
return c.Ctx.State.Get().State == snow.NormalOp
}

// Shared among common.bootstrapper and snowman/avalanche bootstrapper
Expand Down
6 changes: 5 additions & 1 deletion snow/engine/snowman/bootstrap/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/proto/pb/p2p"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
Expand Down Expand Up @@ -121,7 +122,10 @@ func New(ctx context.Context, config Config, onFinished func(ctx context.Context
func (b *bootstrapper) Start(ctx context.Context, startReqID uint32) error {
b.Ctx.Log.Info("starting bootstrapper")

b.Ctx.State.Set(snow.Bootstrapping)
b.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_SNOWMAN,
State: snow.Bootstrapping,
})
if err := b.VM.SetState(ctx, snow.Bootstrapping); err != nil {
return fmt.Errorf("failed to notify VM that bootstrapping has started: %w",
err)
Expand Down
Loading

0 comments on commit de547d3

Please sign in to comment.