Skip to content

Commit

Permalink
go/epochtime: refactor to ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jul 16, 2019
1 parent 5bb535f commit 689e2cd
Show file tree
Hide file tree
Showing 55 changed files with 1,180 additions and 1,065 deletions.
9 changes: 3 additions & 6 deletions .buildkite/scripts/common_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,11 @@ wait_nodes() {
--nodes $nodes
}

# Set epoch.
# Advances epoch.
#
# Arguments:
# epoch - epoch to set
set_epoch() {
local epoch=$1
advance_epoch() {

${EKIDEN_NODE} debug dummy set-epoch \
${EKIDEN_NODE} debug dummy advance-epoch \
--address unix:${EKIDEN_VALIDATOR_SOCKET} \
--epoch $epoch
}
Expand Down
6 changes: 3 additions & 3 deletions .buildkite/scripts/test_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ scenario_basic() {
wait_nodes 6

# Advance epoch to elect a new committee.
set_epoch 1
advance_epoch
}

scenario_compute_discrepancy() {
Expand All @@ -72,7 +72,7 @@ scenario_compute_discrepancy() {
wait_nodes 6

# Advance epoch to elect a new committee.
set_epoch 1
advance_epoch
}

assert_compute_discrepancy_scenario_works() {
Expand All @@ -98,7 +98,7 @@ scenario_merge_discrepancy() {
wait_nodes 6

# Advance epoch to elect a new committee.
set_epoch 1
advance_epoch
}

assert_merge_discrepancy_scenario_works() {
Expand Down
4 changes: 2 additions & 2 deletions .buildkite/scripts/test_migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test_migration() {
# Wait for all nodes to start: 1 compute + 1 storage + key manager.
wait_nodes 3

set_epoch 1
advance_epoch
sleep 1

# Start client and do the state mutations.
Expand Down Expand Up @@ -96,7 +96,7 @@ test_migration() {
# Wait for all nodes to start: 1 compute + 1 storage + key manager.
wait_nodes 3

set_epoch 2
advance_epoch

# Start client and do state verification, checking that migration succeeded.
${CLIENT} \
Expand Down
6 changes: 3 additions & 3 deletions go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ node, development, and debugging.
All sub-commands have online documentation that can be accessed via the
`--help` parameter, for example:
```
ekiden debug dummy set-epoch --help
ekiden debug dummy advance-epoch --help
```

### `debug dummy` - Control the dummy (centralized) node during tests

The `debug dummy` sub-command provides faclities for controlling the centralized
node during tests.

#### `debug dummy set-epoch` - Set the Oasis epoch
#### `debug dummy advance-epoch` - Set the Oasis epoch

The `dummy set-epoch` sub-command allows the node's Oasis epoch to be
The `dummy advance-epoch` sub-command allows the node's Oasis epoch to be
set to an arbitrary value, provided a compatible epochtime backend is
being used (`mock`, `tendermint_mock`).

Expand Down
4 changes: 2 additions & 2 deletions go/beacon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
"github.com/oasislabs/ekiden/go/beacon/api"
"github.com/oasislabs/ekiden/go/beacon/tendermint"
commonFlags "github.com/oasislabs/ekiden/go/ekiden/cmd/common/flags"
epochtime "github.com/oasislabs/ekiden/go/epochtime/api"
"github.com/oasislabs/ekiden/go/tendermint/service"
ticker "github.com/oasislabs/ekiden/go/ticker/api"
)

const (
cfgDebugDeterministic = "beacon.debug.deterministic"
)

// New constructs a new Backend based on the configuration flags.
func New(ctx context.Context, timeSource epochtime.Backend, tmService service.TendermintService) (api.Backend, error) {
func New(ctx context.Context, timeSource ticker.Backend, tmService service.TendermintService) (api.Backend, error) {
backend := commonFlags.ConsensusBackend()
switch strings.ToLower(backend) {
case tendermint.BackendName:
Expand Down
4 changes: 2 additions & 2 deletions go/beacon/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

"github.com/oasislabs/ekiden/go/beacon/api"
"github.com/oasislabs/ekiden/go/common/logging"
epochtime "github.com/oasislabs/ekiden/go/epochtime/api"
tmapi "github.com/oasislabs/ekiden/go/tendermint/api"
app "github.com/oasislabs/ekiden/go/tendermint/apps/beacon"
"github.com/oasislabs/ekiden/go/tendermint/service"
ticker "github.com/oasislabs/ekiden/go/ticker/api"
)

// BackendName is the name of this implementation.
Expand All @@ -36,7 +36,7 @@ func (t *Backend) GetBeacon(ctx context.Context, height int64) ([]byte, error) {
}

// New constructs a new tendermint backed beacon Backend instance.
func New(ctx context.Context, timeSource epochtime.Backend, service service.TendermintService, cfg *api.Config) (api.Backend, error) {
func New(ctx context.Context, timeSource ticker.Backend, service service.TendermintService, cfg *api.Config) (api.Backend, error) {
if err := service.ForceInitialize(); err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions go/beacon/tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ import (
"github.com/stretchr/testify/require"

"github.com/oasislabs/ekiden/go/beacon/api"
epochtime "github.com/oasislabs/ekiden/go/epochtime/api"
epochtimeTests "github.com/oasislabs/ekiden/go/epochtime/tests"
scheduler "github.com/oasislabs/ekiden/go/scheduler/api"
ticker "github.com/oasislabs/ekiden/go/ticker/api"
tickerTests "github.com/oasislabs/ekiden/go/ticker/tests"
)

// BeaconImplementationTests exercises the basic functionality of a
// beacon backend.
func BeaconImplementationTests(t *testing.T, backend api.Backend, epochtime epochtime.SetableBackend) {
func BeaconImplementationTests(t *testing.T, backend api.Backend, ticker ticker.SetableBackend, scheduler scheduler.Backend) {
require := require.New(t)

beacon, err := backend.GetBeacon(context.Background(), 0)
require.NoError(err, "GetBeacon")
require.Len(beacon, api.BeaconSize, "GetBeacon - length")

_ = epochtimeTests.MustAdvanceEpoch(t, epochtime, 1)
tickerTests.MustAdvanceEpoch(t, ticker, scheduler)

newBeacon, err := backend.GetBeacon(context.Background(), 0)
require.NoError(err, "GetBeacon")
Expand Down
41 changes: 32 additions & 9 deletions go/dummydebug/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,60 @@ import (
"google.golang.org/grpc"

"github.com/oasislabs/ekiden/go/common/logging"
epochtime "github.com/oasislabs/ekiden/go/epochtime/api"
registry "github.com/oasislabs/ekiden/go/registry/api"
scheduler "github.com/oasislabs/ekiden/go/scheduler/api"
ticker "github.com/oasislabs/ekiden/go/ticker/api"

dbgPB "github.com/oasislabs/ekiden/go/grpc/dummydebug"
)

var (
errIncompatibleBackend = errors.New("epochtime/grpc: incompatible backend for call")
errIncompatibleBackend = errors.New("ticker/grpc: incompatible backend for call")

_ dbgPB.DummyDebugServer = (*grpcServer)(nil)
)

type grpcServer struct {
logger *logging.Logger

timeSource epochtime.Backend
timeSource ticker.Backend
registry registry.Backend
scheduler scheduler.Backend
}

func (s *grpcServer) SetEpoch(ctx context.Context, req *dbgPB.SetEpochRequest) (*dbgPB.SetEpochResponse, error) {
mockTS, ok := s.timeSource.(epochtime.SetableBackend)
func (s *grpcServer) AdvanceEpoch(ctx context.Context, req *dbgPB.AdvanceEpochRequest) (*dbgPB.AdvanceEpochResponse, error) {
mockTS, ok := s.timeSource.(ticker.SetableBackend)
if !ok {
return nil, errIncompatibleBackend
}

epoch := epochtime.EpochTime(req.GetEpoch())
err := mockTS.SetEpoch(ctx, epoch)
epoch, err := s.scheduler.GetEpoch(ctx, 0)
if err != nil {
return nil, err
}

return &dbgPB.SetEpochResponse{}, nil
// TODO: make it not get stuck
for {
err := mockTS.DoTick(ctx)
if err != nil {
return nil, err
}

newEpoch, nerr := s.scheduler.GetEpoch(ctx, 0)
if nerr != nil {
return nil, nerr
}
if epoch != newEpoch {
// After epoch changed, do one more tick.
err = mockTS.DoTick(ctx)
if err != nil {
return nil, err
}
break
}
}

return &dbgPB.AdvanceEpochResponse{}, nil
}

func (s *grpcServer) WaitNodes(ctx context.Context, req *dbgPB.WaitNodesRequest) (*dbgPB.WaitNodesResponse, error) {
Expand Down Expand Up @@ -81,11 +103,12 @@ Loop:

// NewGRPCServer initializes and registers a gRPC dummydebug server
// backed by the provided backends.
func NewGRPCServer(srv *grpc.Server, timeSource epochtime.Backend, registry registry.Backend) {
func NewGRPCServer(srv *grpc.Server, timeSource ticker.Backend, registry registry.Backend, scheduler scheduler.Backend) {
s := &grpcServer{
logger: logging.GetLogger("dummydebug/grpc"),
timeSource: timeSource,
registry: registry,
scheduler: scheduler,
}
dbgPB.RegisterDummyDebugServer(srv, s)
}
22 changes: 9 additions & 13 deletions go/ekiden/cmd/debug/dummy/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ import (
)

var (
epoch uint64
nodes uint64

dummyCmd = &cobra.Command{
Use: "dummy",
Short: "control dummy node during tests",
}

dummySetEpochCmd = &cobra.Command{
Use: "set-epoch",
Short: "set mock epochtime",
Run: doSetEpoch,
dummyAdvanceEpochCmd = &cobra.Command{
Use: "advance-epoch",
Short: "advance an epoch",
Run: advanceEpoch,
}

dummyWaitNodesCmd = &cobra.Command{
Expand Down Expand Up @@ -59,18 +58,16 @@ func doConnect(cmd *cobra.Command) (*grpc.ClientConn, dummydebug.DummyDebugClien
return conn, client
}

func doSetEpoch(cmd *cobra.Command, args []string) {
func advanceEpoch(cmd *cobra.Command, args []string) {
conn, client := doConnect(cmd)
defer conn.Close()

logger.Info("setting epoch",
"epoch", epoch,
)
logger.Info("advancing epoch")

// Use background context to block until mock epoch transition is done.
_, err := client.SetEpoch(context.Background(), &dummydebug.SetEpochRequest{Epoch: epoch})
_, err := client.AdvanceEpoch(context.Background(), &dummydebug.AdvanceEpochRequest{})
if err != nil {
logger.Error("failed to set epoch",
logger.Error("failed to do tick",
"err", err,
)
}
Expand Down Expand Up @@ -111,10 +108,9 @@ func doWaitNodes(cmd *cobra.Command, args []string) {
// Register registers the dummy sub-command and all of it's children.
func Register(parentCmd *cobra.Command) {
cmdGrpc.RegisterClientFlags(dummyCmd, true)
dummySetEpochCmd.Flags().Uint64VarP(&epoch, "epoch", "e", 0, "set epoch to given value")
dummyWaitNodesCmd.Flags().Uint64VarP(&nodes, "nodes", "n", 1, "number of nodes to wait for")

dummyCmd.AddCommand(dummySetEpochCmd)
dummyCmd.AddCommand(dummyAdvanceEpochCmd)
dummyCmd.AddCommand(dummyWaitNodesCmd)
parentCmd.AddCommand(dummyCmd)
}
25 changes: 12 additions & 13 deletions go/ekiden/cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/oasislabs/ekiden/go/ekiden/cmd/common/metrics"
"github.com/oasislabs/ekiden/go/ekiden/cmd/common/pprof"
"github.com/oasislabs/ekiden/go/ekiden/cmd/common/tracing"
"github.com/oasislabs/ekiden/go/epochtime"
epochtimeAPI "github.com/oasislabs/ekiden/go/epochtime/api"
"github.com/oasislabs/ekiden/go/genesis"
genesisAPI "github.com/oasislabs/ekiden/go/genesis/api"
"github.com/oasislabs/ekiden/go/ias"
Expand All @@ -47,6 +45,8 @@ import (
storageAPI "github.com/oasislabs/ekiden/go/storage/api"
"github.com/oasislabs/ekiden/go/tendermint"
tmService "github.com/oasislabs/ekiden/go/tendermint/service"
"github.com/oasislabs/ekiden/go/ticker"
tickerAPI "github.com/oasislabs/ekiden/go/ticker/api"
workerCommon "github.com/oasislabs/ekiden/go/worker/common"
"github.com/oasislabs/ekiden/go/worker/common/p2p"
"github.com/oasislabs/ekiden/go/worker/compute"
Expand Down Expand Up @@ -83,7 +83,7 @@ type Node struct {
Genesis genesisAPI.Provider
Identity *identity.Identity
Beacon beaconAPI.Backend
Epochtime epochtimeAPI.Backend
Ticker tickerAPI.Backend
Registry registryAPI.Backend
RootHash roothashAPI.Backend
Scheduler schedulerAPI.Backend
Expand Down Expand Up @@ -126,24 +126,24 @@ func (n *Node) initBackends() error {
var err error

// Initialize the various backends.
if n.Epochtime, err = epochtime.New(n.svcMgr.Ctx, n.svcTmnt); err != nil {
if n.Ticker, err = ticker.New(n.svcMgr.Ctx, n.svcTmnt); err != nil {
return err
}
if n.Beacon, err = beacon.New(n.svcMgr.Ctx, n.Epochtime, n.svcTmnt); err != nil {
if n.Beacon, err = beacon.New(n.svcMgr.Ctx, n.Ticker, n.svcTmnt); err != nil {
return err
}
if n.Staking, err = staking.New(n.svcMgr.Ctx, n.svcTmnt); err != nil {
return err
}
if n.Registry, err = registry.New(n.svcMgr.Ctx, n.Epochtime, n.svcTmnt); err != nil {
if n.Registry, err = registry.New(n.svcMgr.Ctx, n.Ticker, n.svcTmnt); err != nil {
return err
}
n.svcMgr.RegisterCleanupOnly(n.Registry, "registry backend")
if n.KeyManager, err = keymanager.New(n.svcMgr.Ctx, n.Epochtime, n.Registry, n.svcTmnt); err != nil {
if n.KeyManager, err = keymanager.New(n.svcMgr.Ctx, n.Ticker, n.Registry, n.svcTmnt); err != nil {
return err
}
n.svcMgr.RegisterCleanupOnly(n.Staking, "staking backend")
if n.Scheduler, err = scheduler.New(n.svcMgr.Ctx, n.Epochtime, n.Registry, n.Beacon, n.svcTmnt); err != nil {
if n.Scheduler, err = scheduler.New(n.svcMgr.Ctx, n.Ticker, n.Registry, n.Beacon, n.svcTmnt); err != nil {
return err
}
n.svcMgr.RegisterCleanupOnly(n.Scheduler, "scheduler backend")
Expand All @@ -152,7 +152,7 @@ func (n *Node) initBackends() error {
return err
}
n.svcMgr.RegisterCleanupOnly(n.Storage, "storage backend")
if n.RootHash, err = roothash.New(n.svcMgr.Ctx, dataDir, n.Epochtime, n.Scheduler, n.Registry, n.Beacon, n.svcTmnt); err != nil {
if n.RootHash, err = roothash.New(n.svcMgr.Ctx, dataDir, n.Ticker, n.Scheduler, n.Registry, n.Beacon, n.svcTmnt); err != nil {
return err
}
n.svcMgr.RegisterCleanupOnly(n.RootHash, "roothash backend")
Expand All @@ -162,7 +162,7 @@ func (n *Node) initBackends() error {
registry.NewGRPCServer(grpcSrv, n.Registry)
staking.NewGRPCServer(grpcSrv, n.Staking)
storage.NewGRPCServer(grpcSrv, n.Storage)
dummydebug.NewGRPCServer(grpcSrv, n.Epochtime, n.Registry)
dummydebug.NewGRPCServer(grpcSrv, n.Ticker, n.Registry, n.Scheduler)

cmdCommon.Logger().Debug("backends initialized")

Expand Down Expand Up @@ -203,7 +203,7 @@ func (n *Node) initAndStartWorkers(logger *logging.Logger) error {
workerCommonCfg := n.CommonWorker.GetConfig()
n.WorkerRegistration, err = registration.New(
dataDir,
n.Epochtime,
n.Ticker,
n.Registry,
n.Identity,
n.svcTmnt,
Expand Down Expand Up @@ -231,7 +231,6 @@ func (n *Node) initAndStartWorkers(logger *logging.Logger) error {

// Initialize the storage worker.
n.StorageWorker, err = workerStorage.New(
n.Epochtime,
n.Storage,
n.CommonWorker.Grpc,
n.WorkerRegistration,
Expand Down Expand Up @@ -564,7 +563,7 @@ func RegisterFlags(cmd *cobra.Command) {
pprof.RegisterFlags,
genesis.RegisterFlags,
beacon.RegisterFlags,
epochtime.RegisterFlags,
ticker.RegisterFlags,
registry.RegisterFlags,
roothash.RegisterFlags,
scheduler.RegisterFlags,
Expand Down
Loading

0 comments on commit 689e2cd

Please sign in to comment.