Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/oasis-node/cmd/debug/consim: Initial import #2784

Merged
merged 5 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .buildkite/code.pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ steps:
plugins:
<<: *docker_plugin_sgx

###############
# Consensus simulator tests
###############
- label: Consensus simulator tests
timeout_in_minutes: 3
command:
- .buildkite/scripts/download_e2e_test_artifacts.sh
- .buildkite/scripts/test_consim.sh
artifact_paths:
- /tmp/consim-datadir/*.json
- /tmp/consim-datadir/consim.log
env:
TEST_BASE_DIR: /tmp
agents:
buildkite_agent_size: large
plugins:
<<: *docker_plugin

####################################
# Rust coverage job (only on master)
####################################
Expand Down
36 changes: 36 additions & 0 deletions .buildkite/scripts/test_consim.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

############################################################
# This script tests the Oasis Core project with the consensus
# simulator.
#
# Usage:
# test_consim.sh
############################################################

# Helpful tips on writing build scripts:
# https://buildkite.com/docs/pipelines/writing-build-scripts
set -euxo pipefail

# Working directory.
WORKDIR=$PWD

#################
# Run test suite.
#################

echo ${WORKDIR}

# Run the consensusm simulator.
${WORKDIR}/go/oasis-node/oasis-node \
debug consim \
--datadir /tmp/consim-datadir \
--log.level DEBUG \
--log.file /tmp/consim-datadir/consim.log \
-g ${WORKDIR}/tests/fixture-data/consim/genesis.json \
--debug.dont_blame_oasis \
--debug.allow_test_keys \
--consim.workload xfer \
--consim.workload.xfer.iterations 10000 \
--consim.num_kept 1 \
--consim.memdb
4 changes: 4 additions & 0 deletions .changelog/2784.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go/oasis-node/cmd/debug/consim: Initial import

Add the ability to exercise some backends without tendermint, while
attempting to preserve some of the semantics.
3 changes: 3 additions & 0 deletions go/consensus/tendermint/abci/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type ApplicationConfig struct {

// OwnTxSigner is the transaction signer identity of the local node.
OwnTxSigner signature.PublicKey

// TestingMemDB forces the MemDB to be used for the state storage.
TestingMemDB bool
}

// TransactionAuthHandler is the interface for ABCI applications that handle
Expand Down
14 changes: 12 additions & 2 deletions go/consensus/tendermint/abci/mux_mock.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// +build gofuzz

package abci

import (
"context"

epochtime "github.com/oasislabs/oasis-core/go/epochtime/api"
upgrade "github.com/oasislabs/oasis-core/go/upgrade/api"
)

Expand All @@ -18,6 +17,17 @@ func (mux *MockABCIMux) MockRegisterApp(app Application) error {
return mux.doRegister(app)
}

// MockSetEpochtime sets the timesource used by this muxer when testing.
func (mux *MockABCIMux) MockSetEpochtime(epochTime epochtime.Backend) {
mux.state.timeSource = epochTime
}

// MockSetTransactionAuthHandler sets the transaction auth hander used by
// this muxer when testing.
func (mux *MockABCIMux) MockSetTransactionAuthHandler(handler TransactionAuthHandler) {
mux.state.txAuthHandler = handler
}

// MockClose cleans up the muxer's state; it must be called once the muxer is no longer needed.
func (mux *MockABCIMux) MockClose() {
mux.doCleanup()
Expand Down
9 changes: 8 additions & 1 deletion go/consensus/tendermint/abci/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,15 @@ func (s *applicationState) metricsWorker() {
}
}

func newStateDB(cfg *ApplicationConfig) (dbm.DB, error) {
if cfg.TestingMemDB {
return dbm.NewMemDB(), nil
}
return db.New(filepath.Join(cfg.DataDir, "abci-mux-state"), false)
}

func newApplicationState(ctx context.Context, cfg *ApplicationConfig) (*applicationState, error) {
db, err := db.New(filepath.Join(cfg.DataDir, "abci-mux-state"), false)
db, err := newStateDB(cfg)
if err != nil {
return nil, err
}
Expand Down
257 changes: 257 additions & 0 deletions go/oasis-node/cmd/debug/consim/consim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
// Package consim implements the mock consensus simulator.
package consim

import (
"context"
"crypto"
"encoding/hex"
"fmt"
"math/rand"
"path/filepath"
"time"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/oasislabs/oasis-core/go/common/crypto/drbg"
"github.com/oasislabs/oasis-core/go/common/crypto/mathrand"
"github.com/oasislabs/oasis-core/go/common/logging"
"github.com/oasislabs/oasis-core/go/consensus/tendermint/abci"
registryApp "github.com/oasislabs/oasis-core/go/consensus/tendermint/apps/registry"
stakingApp "github.com/oasislabs/oasis-core/go/consensus/tendermint/apps/staking"
genesisFile "github.com/oasislabs/oasis-core/go/genesis/file"
cmdCommon "github.com/oasislabs/oasis-core/go/oasis-node/cmd/common"
cmdFlags "github.com/oasislabs/oasis-core/go/oasis-node/cmd/common/flags"
)

const (
cfgNumKept = "consim.num_kept"
cfgMemDB = "consim.memdb"
cfgWorkload = "consim.workload"
cfgWorkloadSeed = "consim.workload.seed"
)

var (
logger = logging.GetLogger("cmd/consim")

flagsConsim = flag.NewFlagSet("", flag.ContinueOnError)

conSimCmd = &cobra.Command{
Use: "consim",
Short: "mock consensus simulator",
RunE: doRun,
}
)

func doRun(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

if err := cmdCommon.Init(); err != nil {
cmdCommon.EarlyLogAndExit(err)
}

dataDir := cmdCommon.DataDir()
if dataDir == "" {
return fmt.Errorf("datadir is mandatory")
}

ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()

// Load the genesis document.
genesisProvider, err := genesisFile.DefaultFileProvider()
if err != nil {
logger.Error("failed to initialize genesis provider",
"err", err,
)
return err
}
genesisDoc, err := genesisProvider.GetGenesisDocument()
if err != nil {
logger.Error("failed to get genesis document",
"err", err,
)
return err
}
genesisDoc.SetChainContext()
tmChainID := genesisDoc.ChainContext()[:tmtypes.MaxChainIDLen]

// Initialize the DRBG and workload.
rngSrc, err := drbg.New(crypto.SHA512, []byte(viper.GetString(cfgWorkloadSeed)), nil, []byte("consim workload generator"))
if err != nil {
logger.Error("failed to initialize DRBG",
"err", err,
)
return err
}

workload, err := newWorkload(rand.New(mathrand.New(rngSrc)))
if err != nil {
logger.Error("failed to create workload",
"err", err,
)
return err
}
defer workload.Cleanup()

if err = workload.Init(genesisDoc); err != nil {
logger.Error("failed to initialize workload",
"err", err,
)
return err
}

// Initialize the mock chain backend.
txAuthApp := stakingApp.New()
cfg := &mockChainCfg{
dataDir: dataDir,
apps: []abci.Application{
registryApp.New(),
txAuthApp, // This is the staking app.
},
genesisDoc: genesisDoc,
tmChainID: tmChainID,
txAuthHandler: txAuthApp.(abci.TransactionAuthHandler),
numVersions: viper.GetInt64(cfgNumKept),
memDB: viper.GetBool(cfgMemDB),
}
mockChain, err := initMockChain(ctx, cfg)
if err != nil {
logger.Error("failed to initialize mock chain backend",
"err", err,
)
return err
}
defer mockChain.close()
chainState, err := mockChain.stateToGenesis(ctx)
if err != nil {
logger.Error("failed to obtain chain state",
"err", err,
)
return err
}

// Start the workload.
cancelCh, errCh := make(chan struct{}), make(chan error)
defer close(cancelCh)
txVecCh, err := workload.Start(chainState, cancelCh, errCh)
if err != nil {
logger.Error("failed to start workload",
"err", err,
)
return err
}

var checkedTxes, deliveredTxes, numBlocks uint64
start := time.Now()

// Emulate the tendermint block generation loop.
txLoop:
for {
var (
txVec []BlockTx
ok bool
)
select {
case err = <-errCh:
logger.Error("workload error",
"err", err,
)
return err
case txVec, ok = <-txVecCh:
if !ok {
break txLoop
}
}

mockChain.beginBlock()

// CheckTx all the pending transactions for this block.
var toDeliver []BlockTx
for _, v := range txVec {
txCode := mockChain.checkTx(v.Tx)
if txCode != v.Code {
logger.Error("CheckTx response code mismatch",
"tx", hex.EncodeToString(v.Tx),
"code", txCode,
)
return fmt.Errorf("consim: CheckTx response code mismatch")
} else if v.Code == types.CodeTypeOK {
toDeliver = append(toDeliver, v)
}

checkedTxes++
}

// DeliverTx all the pending transactions for this block.
for _, v := range toDeliver {
txCode := mockChain.deliverTx(v.Tx)
if txCode != types.CodeTypeOK {
logger.Error("DeliverTx failed",
"tx", hex.EncodeToString(v.Tx),
"code", txCode,
)
return fmt.Errorf("consim: DeliverTx response code mismatch")
}

deliveredTxes++
}

mockChain.endBlock()

numBlocks++
}

elapsed := time.Since(start)
logger.Info("transaction processing complete",
"time", elapsed,
"check_tx_per_sec", float64(checkedTxes)/elapsed.Seconds(),
"deliver_tx_per_sec", float64(deliveredTxes)/elapsed.Seconds(),
"blocks_per_sec", float64(numBlocks)/elapsed.Seconds(),
)

// Dump the final state to a JSON document.
finalGenesis, err := mockChain.stateToGenesis(ctx)
if err != nil {
logger.Error("failed to obtain state dump",
"err", err,
)
return err
}

if err = workload.Finalize(finalGenesis); err != nil {
logger.Error("failed to finalize workload",
"err", err,
)
return err
}

if err = finalGenesis.WriteFileJSON(filepath.Join(dataDir, "dump.json")); err != nil {
logger.Error("failed to write state dump",
"err", err,
)
return err
}

return nil
}

// Register registers the consim sub-command.
func Register(parentCmd *cobra.Command) {
conSimCmd.Flags().AddFlagSet(cmdFlags.GenesisFileFlags)
conSimCmd.Flags().AddFlagSet(flagsConsim)
conSimCmd.Flags().AddFlagSet(fileTxsFlag)
conSimCmd.Flags().AddFlagSet(xferFlags)
parentCmd.AddCommand(conSimCmd)
}

func init() {
flagsConsim.Int64(cfgNumKept, 0, "number of versions kept (0 = all)")
flagsConsim.Bool(cfgMemDB, false, "use memory to store state")
flagsConsim.String(cfgWorkload, fileWorkloadName, "workload to execute")
flagsConsim.String(cfgWorkloadSeed, "seeeeeeeeeeeeeeeeeeeeeeeeeeeeeed", "DRBG seed for workloads")
_ = viper.BindPFlags(flagsConsim)
}
Loading