mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-05 09:14:28 +00:00
1ddb19bba6
sharding: create a syncer and a simulator package Former-commit-id: b392885510ba5a96e61278cbbe2c0ec6f9722ee8 [formerly 3a435eaf6805d02beae55656f155b2c3a66ee663] Former-commit-id: 0f6f3f2053ae77711e2072848b727b0dc9b92276
64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package sharding
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
)
|
|
|
|
// 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 {
|
|
Start()
|
|
Close()
|
|
Register(constructor ServiceConstructor) error
|
|
}
|
|
|
|
// Actor refers to either a notary, proposer, or observer in the sharding spec.
|
|
type Actor interface {
|
|
Service
|
|
// TODO: will actors have actor-specific methods? To be decided.
|
|
}
|
|
|
|
// CollationFetcher defines functionality for a struct that is able to extract
|
|
// respond with collation information to the caller. Shard implements this interface.
|
|
type CollationFetcher interface {
|
|
CollationByHeaderHash(headerHash *common.Hash) (*Collation, error)
|
|
}
|
|
|
|
// 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 {
|
|
Services map[reflect.Type]Service // Index of the already constructed services
|
|
}
|
|
|
|
// ServiceConstructor is the function signature of the constructors needed to be
|
|
// registered for service instantiation.
|
|
type ServiceConstructor func(ctx *ServiceContext) (Service, error)
|
|
|
|
// 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.
|
|
type Service interface {
|
|
// Start is called after all services have been constructed to
|
|
// spawn any goroutines required by the service.
|
|
Start()
|
|
// Stop terminates all goroutines belonging to the service,
|
|
// blocking until they are all terminated.
|
|
Stop() error
|
|
}
|
|
|
|
// RetrieveService sets the `service` argument to a currently running service
|
|
// registered of a specific type.
|
|
func (ctx *ServiceContext) RetrieveService(service interface{}) error {
|
|
element := reflect.ValueOf(service).Elem()
|
|
if running, ok := ctx.Services[element.Type()]; ok {
|
|
element.Set(reflect.ValueOf(running))
|
|
return nil
|
|
}
|
|
return fmt.Errorf("unknown service: %T", service)
|
|
}
|