Skip to content

Commit

Permalink
go: add byzantine subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Aug 1, 2019
1 parent e556adb commit 5f810cd
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
44 changes: 44 additions & 0 deletions go/ekiden/cmd/debug/byzantine/byzantine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package byzantine

import (
"github.com/spf13/cobra"

"github.com/oasislabs/ekiden/go/common/logging"
"github.com/oasislabs/ekiden/go/ekiden/cmd/common"
"github.com/oasislabs/ekiden/go/genesis"
"github.com/oasislabs/ekiden/go/tendermint"
)

var (
logger = logging.GetLogger("cmd/byzantine")
byzantineCmd = &cobra.Command{
Use: "byzantine",
Short: "run some node behaviors for testing, often not honest",
}
computeHonestCmd = &cobra.Command{
Use: "compute-honest",
Short: "act as an honest compute worker",
Run: doComputeHonest,
}
)

func doComputeHonest(cmd *cobra.Command, args []string) {
if err := common.Init(); err != nil {
common.EarlyLogAndExit(err)
}

startHonestTendermint()
defer stopHonestTendermint()
logger.Debug("compute honest: ok %%%")
}

// Register registers the byzantine sub-command and all of its children.
func Register(parentCmd *cobra.Command) {
byzantineCmd.AddCommand(computeHonestCmd)
parentCmd.AddCommand(byzantineCmd)
}

func init() {
computeHonestCmd.Flags().AddFlagSet(genesis.Flags)
computeHonestCmd.Flags().AddFlagSet(tendermint.Flags)
}
133 changes: 133 additions & 0 deletions go/ekiden/cmd/debug/byzantine/steps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package byzantine

import (
"context"
"time"

beacon "github.com/oasislabs/ekiden/go/beacon/api"
"github.com/oasislabs/ekiden/go/common/crypto/signature"
fileSigner "github.com/oasislabs/ekiden/go/common/crypto/signature/signers/file"
"github.com/oasislabs/ekiden/go/common/identity"
"github.com/oasislabs/ekiden/go/common/pubsub"
"github.com/oasislabs/ekiden/go/ekiden/cmd/common"
"github.com/oasislabs/ekiden/go/epochtime/api"
"github.com/oasislabs/ekiden/go/genesis"
registry "github.com/oasislabs/ekiden/go/registry/api"
scheduler "github.com/oasislabs/ekiden/go/scheduler/api"
"github.com/oasislabs/ekiden/go/tendermint"
beaconapp "github.com/oasislabs/ekiden/go/tendermint/apps/beacon"
keymanagerapp "github.com/oasislabs/ekiden/go/tendermint/apps/keymanager"
registryapp "github.com/oasislabs/ekiden/go/tendermint/apps/registry"
roothashapp "github.com/oasislabs/ekiden/go/tendermint/apps/roothash"
schedulerapp "github.com/oasislabs/ekiden/go/tendermint/apps/scheduler"
stakingapp "github.com/oasislabs/ekiden/go/tendermint/apps/staking"
"github.com/oasislabs/ekiden/go/tendermint/service"
)

var (
honestTendermint service.TendermintService

_ api.Backend = (*fakeTimeBackend)(nil)
)

// fakeTimeBackend is like TendermintBackend (of epochtime), but without
// any workers.
// TODO: come up with an acceptable way to do this
type fakeTimeBackend struct{}

// GetEpoch implements epochtime Backend.
func (*fakeTimeBackend) GetEpoch(ctx context.Context, height int64) (api.EpochTime, error) {
if height == 0 {
panic("0 height not supported")
}
return api.EpochTime(height / 30), nil
}

// GetEpochBlock implements epochtime Backend.
func (*fakeTimeBackend) GetEpochBlock(ctx context.Context, epoch api.EpochTime) (int64, error) {
panic("GetEpochBlock not supported")
// return int64(epoch) * 30, nil
}

// WatchEpochs implements epochtime Backend.
func (*fakeTimeBackend) WatchEpochs() (<-chan api.EpochTime, *pubsub.Subscription) {
panic("WatchEpochs not supported")
}

func startHonestTendermint() {
if honestTendermint != nil {
panic("honest Tendermint service already started")
}

dataDir := common.DataDir()
signerFactory := fileSigner.NewFactory(dataDir, signature.SignerNode, signature.SignerP2P, signature.SignerEntity)
identity, err := identity.LoadOrGenerate(dataDir, signerFactory)
if err != nil {
panic("identity.LoadOrGenerate: " + err.Error())
}
genesis, err := genesis.New(identity)
if err != nil {
panic("genesis.New: " + err.Error())
}
honestTendermint = tendermint.New(context.Background(), dataDir, identity, genesis)
logger.Debug("honest Tendermint service instantiated %%%", "honestTendermint", honestTendermint)

if err := honestTendermint.ForceInitialize(); err != nil {
panic("honestTendermint.ForceInitialize: " + err.Error())
}
logger.Debug("honest Tendermint service force initialized %%%")

// Register honest mux apps.
// This isn't very flexible. It's configured to match what we use in end-to-end tests.
// And we do that mostly by hardcoding options. We could make this more flexible with command
// line flags in future work.
timeSource := &fakeTimeBackend{}
// Tendermint epochtime has no registration
if err := honestTendermint.RegisterApplication(beaconapp.New(timeSource, &beacon.Config{
DebugDeterministic: true,
})); err != nil {
panic("honestTendermint.RegisterApplication beacon: " + err.Error())
}
if err := honestTendermint.RegisterApplication(stakingapp.New(nil)); err != nil {
panic("honestTendermint.RegisterApplication staking: " + err.Error())
}
if err := honestTendermint.RegisterApplication(registryapp.New(timeSource, &registry.Config{
DebugAllowRuntimeRegistration: false,
DebugBypassStake: false,
})); err != nil {
panic("honestTendermint.RegisterApplication registry: " + err.Error())
}
if err := honestTendermint.RegisterApplication(keymanagerapp.New(timeSource)); err != nil {
panic("honestTendermint.RegisterApplication keymanager: " + err.Error())
}
if err := honestTendermint.RegisterApplication(schedulerapp.New(timeSource, &scheduler.Config{
DebugBypassStake: false,
})); err != nil {
panic("honestTendermint.RegisterApplication scheduler: " + err.Error())
}
// storage has no registration
if err := honestTendermint.RegisterApplication(roothashapp.New(context.Background(), timeSource, nil, 10*time.Second)); err != nil {
panic("honestTendermint.RegisterApplication roothash: " + err.Error())
}

if err := honestTendermint.Start(); err != nil {
panic("honestTendermint.Start: " + err.Error())
}
logger.Debug("honest Tendermint service start returned %%%")
<-honestTendermint.Started()
logger.Debug("honest Tendermint service start channel closed %%%")
<-honestTendermint.Synced()
logger.Debug("honest Tendermint service sync channel closed %%%")
}

func stopHonestTendermint() {
if honestTendermint == nil {
panic("honest Tendermint service not started")
}

honestTendermint.Stop()
logger.Debug("honest Tendermint service stop returned %%%")
<-honestTendermint.Quit()
logger.Debug("honest Tendermint service quit channel closed %%%")
honestTendermint = nil
}
2 changes: 2 additions & 0 deletions go/ekiden/cmd/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"

"github.com/oasislabs/ekiden/go/ekiden/cmd/debug/bootstrap"
"github.com/oasislabs/ekiden/go/ekiden/cmd/debug/byzantine"
"github.com/oasislabs/ekiden/go/ekiden/cmd/debug/client"
"github.com/oasislabs/ekiden/go/ekiden/cmd/debug/dummy"
"github.com/oasislabs/ekiden/go/ekiden/cmd/debug/roothash"
Expand All @@ -23,6 +24,7 @@ func Register(parentCmd *cobra.Command) {
roothash.Register(debugCmd)
tendermint.Register(debugCmd)
bootstrap.Register(debugCmd)
byzantine.Register(debugCmd)

parentCmd.AddCommand(debugCmd)
}
2 changes: 1 addition & 1 deletion go/ekiden/cmd/debug/dummy/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func doWaitNodes(cmd *cobra.Command, args []string) {
logger.Info("enough nodes have been registered")
}

// Register registers the dummy sub-command and all of it's children.
// Register registers the dummy sub-command and all of its children.
func Register(parentCmd *cobra.Command) {
cmdGrpc.RegisterClientFlags(dummyCmd, true)
dummySetEpochCmd.Flags().Uint64VarP(&epoch, "epoch", "e", 0, "set epoch to given value")
Expand Down

0 comments on commit 5f810cd

Please sign in to comment.