Skip to content

Commit

Permalink
feat: implemented ability to create a tendermint node initialized wit…
Browse files Browse the repository at this point in the history
…h celestia-app
  • Loading branch information
evan-forbes committed Sep 22, 2022
1 parent d53f66b commit 5db77f3
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cmd/celestia-appd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig encoding.Config) {
config.Cmd(),
)

server.AddCommands(rootCmd, app.DefaultNodeHome, newApp, createAppAndExport, addModuleInitFlags)
server.AddCommands(rootCmd, app.DefaultNodeHome, NewAppServer, createAppAndExport, addModuleInitFlags)

// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
Expand Down Expand Up @@ -201,7 +201,7 @@ func txCommand() *cobra.Command {
return cmd
}

func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
func NewAppServer(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
var cache sdk.MultiStorePersistentCache

if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) {
Expand Down
27 changes: 25 additions & 2 deletions pkg/inclusion/nmt_caching_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package inclusion

import (
"bytes"
"crypto/rand"
"sort"
"testing"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/celestiaorg/celestia-app/testutil/coretestutil"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/rsmt2d"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -114,7 +116,7 @@ func TestWalkCachedSubTreeRoot(t *testing.T) {

func TestEDSSubRootCacher(t *testing.T) {
oss := uint64(8)
d := coretestutil.GenerateRandNamespacedRawData(uint32(oss*oss), appconsts.NamespaceSize, appconsts.ShareSize-appconsts.NamespaceSize)
d := generateRandNamespacedRawData(uint32(oss*oss), appconsts.NamespaceSize, appconsts.ShareSize-appconsts.NamespaceSize)
stc := NewSubtreeCacher(oss)

eds, err := rsmt2d.ComputeExtendedDataSquare(d, appconsts.DefaultCodec(), stc.Constructor)
Expand Down Expand Up @@ -182,3 +184,24 @@ func chunkSlice(slice [][]byte, chunkSize int) [][][]byte {

return chunks
}

func generateRandNamespacedRawData(total, nidSize, leafSize uint32) [][]byte {
data := make([][]byte, total)
for i := uint32(0); i < total; i++ {
nid := make([]byte, nidSize)
rand.Read(nid)
data[i] = nid
}
sortByteArrays(data)
for i := uint32(0); i < total; i++ {
d := make([]byte, leafSize)
rand.Read(d)
data[i] = append(data[i], d...)
}

return data
}

func sortByteArrays(src [][]byte) {
sort.Slice(src, func(i, j int) bool { return bytes.Compare(src[i], src[j]) < 0 })
}
28 changes: 0 additions & 28 deletions testutil/coretestutil/core.go

This file was deleted.

81 changes: 81 additions & 0 deletions testutil/coretestutil/full_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package coretestutil

import (
"os"
"testing"

"github.com/celestiaorg/celestia-app/cmd/celestia-appd/cmd"
"github.com/tendermint/tendermint/config"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/proxy"
"github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
)

func NewTendermintNode(t *testing.T, cfg *config.Config, supressLog bool) (*node.Node, error) {
var logger log.Logger
if supressLog {
logger = log.NewNopLogger()
} else {
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger = log.NewFilter(logger, log.AllowError())
}

cfg.Genesis = "./testdata/genesis.json"
cfg.RootDir = t.TempDir()

pv := loadPV()

// papp := proxy.NewLocalClientCreator(app)
nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
panic(err)
}

db := dbm.NewMemDB()

return node.NewNode(
cfg,
pv,
nodeKey,
proxy.NewLocalClientCreator(cmd.NewAppServer(logger, db, nil, emptyAppOptions{})),
localGenesisCreator,
node.DefaultDBProvider,
node.DefaultMetricsProvider(cfg.Instrumentation),
logger,
)
}

func loadPV() *privval.FilePV {
pvKey := privval.FilePVKey{}
err := tmjson.Unmarshal([]byte(privVal), &pvKey)
if err != nil {
panic("Error reading PrivValidator key from")
}

// overwrite pubkey and address for convenience
pvKey.PubKey = pvKey.PrivKey.PubKey()
pvKey.Address = pvKey.PubKey.Address()

pvState := privval.FilePVLastSignState{}

return &privval.FilePV{
Key: pvKey,
LastSignState: pvState,
}
}

func localGenesisCreator() (*types.GenesisDoc, error) {
return types.GenesisDocFromJSON([]byte(genesis))
}

type emptyAppOptions struct{}

// Get implements AppOptions
func (ao emptyAppOptions) Get(o string) interface{} {
return nil
}
Loading

0 comments on commit 5db77f3

Please sign in to comment.