Skip to content

Commit

Permalink
change RegisterChain structure; move GetVM to common.Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine committed Apr 9, 2021
1 parent 9d57662 commit e1c5a3e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 54 deletions.
90 changes: 46 additions & 44 deletions api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,53 +110,55 @@ func (s *Server) DispatchTLS(certFile, keyFile string) error {
// and at the same time the server's lock is held due to an API call and is trying
// to grab the P-Chain's lock.
func (s *Server) RegisterChain(chainName string, ctx *snow.Context, engineIntf interface{}) {
go func() {
var (
handlers map[string]*common.HTTPHandler
err error
)

switch engine := engineIntf.(type) {
case snowman.Engine:
ctx.Lock.Lock()
handlers, err = engine.GetVM().CreateHandlers()
ctx.Lock.Unlock()
case avalanche.Engine:
ctx.Lock.Lock()
handlers, err = engine.GetVM().CreateHandlers()
ctx.Lock.Unlock()
default:
s.log.Error("engine has unexpected type %T", engineIntf)
return
}
if err != nil {
s.log.Error("failed to create %s handlers: %s", chainName, err)
return
}
go s.registerChain(chainName, ctx, engineIntf)
}

httpLogger, err := s.factory.MakeChain(chainName, "http")
if err != nil {
s.log.Error("failed to create new http logger: %s", err)
}
func (s *Server) registerChain(chainName string, ctx *snow.Context, engineIntf interface{}) {
var (
handlers map[string]*common.HTTPHandler
err error
)

switch engine := engineIntf.(type) {
case snowman.Engine:
ctx.Lock.Lock()
handlers, err = engine.GetVM().CreateHandlers()
ctx.Lock.Unlock()
case avalanche.Engine:
ctx.Lock.Lock()
handlers, err = engine.GetVM().CreateHandlers()
ctx.Lock.Unlock()
default:
s.log.Error("engine has unexpected type %T", engineIntf)
return
}
if err != nil {
s.log.Error("failed to create %s handlers: %s", chainName, err)
return
}

httpLogger, err := s.factory.MakeChain(chainName, "http")
if err != nil {
s.log.Error("failed to create new http logger: %s", err)
}

s.log.Verbo("About to add API endpoints for chain with ID %s", ctx.ChainID)
// all subroutes to a chain begin with "bc/<the chain's ID>"
defaultEndpoint := "bc/" + ctx.ChainID.String()

// Register each endpoint
for extension, handler := range handlers {
// Validate that the route being added is valid
// e.g. "/foo" and "" are ok but "\n" is not
_, err := url.ParseRequestURI(extension)
if extension != "" && err != nil {
s.log.Error("could not add route to chain's API handler because route is malformed: %s", err)
continue
}
if err := s.AddChainRoute(handler, ctx, defaultEndpoint, extension, httpLogger); err != nil {
s.log.Error("error adding route: %s", err)
}
s.log.Verbo("About to add API endpoints for chain with ID %s", ctx.ChainID)
// all subroutes to a chain begin with "bc/<the chain's ID>"
defaultEndpoint := "bc/" + ctx.ChainID.String()

// Register each endpoint
for extension, handler := range handlers {
// Validate that the route being added is valid
// e.g. "/foo" and "" are ok but "\n" is not
_, err := url.ParseRequestURI(extension)
if extension != "" && err != nil {
s.log.Error("could not add route to chain's API handler because route is malformed: %s", err)
continue
}
if err := s.AddChainRoute(handler, ctx, defaultEndpoint, extension, httpLogger); err != nil {
s.log.Error("error adding route: %s", err)
}
}()
}
}

// AddChainRoute registers a route to a chain's handler
Expand Down
4 changes: 0 additions & 4 deletions snow/engine/avalanche/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package avalanche
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
"github.com/ava-labs/avalanchego/snow/engine/common"
)

Expand All @@ -20,7 +19,4 @@ type Engine interface {
// GetVtx returns a vertex by its ID.
// Returns an error if unknown.
GetVtx(vtxID ids.ID) (avalanche.Vertex, error)

// GetVM returns this engine's VM
GetVM() vertex.DAGVM
}
2 changes: 1 addition & 1 deletion snow/engine/avalanche/transitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,6 @@ func (t *Transitive) GetVtx(vtxID ids.ID) (avalanche.Vertex, error) {
return t.Manager.Get(vtxID)
}

func (t *Transitive) GetVM() vertex.DAGVM {
func (t *Transitive) GetVM() common.VM {
return t.VM
}
3 changes: 3 additions & 0 deletions snow/engine/common/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Engine interface {
// Returns nil if the engine is healthy.
// Periodically called and reported through the health API
health.Checkable

// GetVM returns this engine's VM
GetVM() VM
}

// Handler defines the functions that are acted on the node
Expand Down
14 changes: 13 additions & 1 deletion snow/engine/common/test_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type EngineTest struct {

CantHealth,

CantGetVtx bool
CantGetVtx, CantGetVM bool

IsBootstrappedF func() bool
ContextF func() *snow.Context
Expand All @@ -65,6 +65,7 @@ type EngineTest struct {
ConnectedF, DisconnectedF func(validatorID ids.ShortID) error
HealthF func() (interface{}, error)
GetVtxF func() (avalanche.Vertex, error)
GetVMF func() VM
}

var _ Engine = &EngineTest{}
Expand Down Expand Up @@ -107,6 +108,7 @@ func (e *EngineTest) Default(cant bool) {
e.CantHealth = cant

e.CantGetVtx = cant
e.CantGetVM = cant
}

func (e *EngineTest) Context() *snow.Context {
Expand Down Expand Up @@ -435,3 +437,13 @@ func (e *EngineTest) GetVtx() (avalanche.Vertex, error) {
}
return nil, errors.New("unexpectedly called GetVtx")
}

func (e *EngineTest) GetVM() VM {
if e.GetVMF != nil {
return e.GetVMF()
}
if e.CantGetVM && e.T != nil {
e.T.Fatalf("Unexpectedly called GetVM")
}
return nil
}
4 changes: 0 additions & 4 deletions snow/engine/snowman/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
)

// Engine describes the events that can occur to a Snowman instance.
Expand All @@ -27,7 +26,4 @@ type Engine interface {
// GetBlock returns a block by its ID.
// Returns an error if unknown.
GetBlock(blkID ids.ID) (snowman.Block, error)

// GetVM returns this engine's VM
GetVM() block.ChainVM
}

0 comments on commit e1c5a3e

Please sign in to comment.