2018-05-22 11:16:57 +00:00
|
|
|
package sharding
|
|
|
|
|
2018-06-02 22:29:35 +00:00
|
|
|
import (
|
2018-06-04 19:58:02 +00:00
|
|
|
"fmt"
|
2018-06-04 19:31:42 +00:00
|
|
|
"reflect"
|
2018-06-02 22:29:35 +00:00
|
|
|
)
|
|
|
|
|
2018-06-04 19:31:42 +00:00
|
|
|
// Node defines a a sharding-enabled Ethereum instance that provides
|
|
|
|
// full control and shared access of necessary components and services
|
|
|
|
// for a sharded Ethereum blockchain.
|
|
|
|
type Node interface {
|
2018-06-06 18:46:26 +00:00
|
|
|
Start()
|
2018-06-06 18:04:00 +00:00
|
|
|
Close()
|
2018-06-04 19:58:02 +00:00
|
|
|
Register(constructor ServiceConstructor) error
|
2018-06-02 22:29:35 +00:00
|
|
|
}
|
2018-05-22 11:16:57 +00:00
|
|
|
|
2018-06-04 21:19:16 +00:00
|
|
|
// ShardP2P defines an interface for a peer-to-peer service in a
|
2018-06-04 21:10:59 +00:00
|
|
|
// sharded Ethereum blockchain.
|
|
|
|
type ShardP2P interface{}
|
|
|
|
|
2018-06-04 21:19:16 +00:00
|
|
|
// TXPool defines an interface for a transaction pool service that handles
|
2018-06-04 21:10:59 +00:00
|
|
|
// incoming shard transactions in the network.
|
|
|
|
type TXPool interface{}
|
|
|
|
|
2018-06-05 21:28:57 +00:00
|
|
|
// Actor refers to either a notary, proposer, or observer in the sharding spec.
|
2018-06-04 19:31:42 +00:00
|
|
|
type Actor interface {
|
2018-06-06 14:30:43 +00:00
|
|
|
Service
|
|
|
|
// TODO: will actors have actor-specific methods? To be decided.
|
2018-06-04 19:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceContext is a collection of service independent options inherited from
|
|
|
|
// the protocol stack, that is passed to all constructors to be optionally used;
|
|
|
|
// as well as utility methods to operate on the service environment.
|
|
|
|
type ServiceContext struct {
|
2018-06-04 21:19:16 +00:00
|
|
|
Services map[reflect.Type]Service // Index of the already constructed services
|
2018-06-04 19:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceConstructor is the function signature of the constructors needed to be
|
|
|
|
// registered for service instantiation.
|
|
|
|
type ServiceConstructor func(ctx *ServiceContext) (Service, error)
|
|
|
|
|
2018-06-05 21:28:57 +00:00
|
|
|
// Service is an individual protocol that can be registered into a node. Having a sharding
|
|
|
|
// node maintain a service registry allows for easy, shared-dependencies. For example,
|
|
|
|
// a proposer service might depend on a p2p server, a txpool, an smc client, etc.
|
2018-06-04 19:31:42 +00:00
|
|
|
type Service interface {
|
|
|
|
// Start is called after all services have been constructed to
|
|
|
|
// spawn any goroutines required by the service.
|
2018-06-11 22:21:24 +00:00
|
|
|
Start()
|
2018-06-04 19:31:42 +00:00
|
|
|
// Stop terminates all goroutines belonging to the service,
|
|
|
|
// blocking until they are all terminated.
|
2018-05-22 11:16:57 +00:00
|
|
|
Stop() error
|
|
|
|
}
|
2018-06-04 19:58:02 +00:00
|
|
|
|
2018-06-06 15:12:03 +00:00
|
|
|
// RetrieveService sets the `service` argument to a currently running service
|
|
|
|
// registered of a specific type.
|
2018-06-06 17:22:21 +00:00
|
|
|
func (ctx *ServiceContext) RetrieveService(service interface{}) error {
|
2018-06-04 19:58:02 +00:00
|
|
|
element := reflect.ValueOf(service).Elem()
|
2018-06-04 21:19:16 +00:00
|
|
|
if running, ok := ctx.Services[element.Type()]; ok {
|
2018-06-04 19:58:02 +00:00
|
|
|
element.Set(reflect.ValueOf(running))
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-06 14:30:43 +00:00
|
|
|
return fmt.Errorf("unknown service: %T", service)
|
2018-06-04 19:58:02 +00:00
|
|
|
}
|