Skip to content

Commit

Permalink
feat: implement randomness module
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Dec 4, 2023
1 parent 114b572 commit 4c4a4ab
Show file tree
Hide file tree
Showing 17 changed files with 755 additions and 153 deletions.
11 changes: 8 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
Expand Down Expand Up @@ -302,7 +303,12 @@ func NewApp(
std.RegisterLegacyAminoCodec(legacyAmino)
std.RegisterInterfaces(interfaceRegistry)

// TO-DO
// building BaseApp
nonceMempool := mempool.NewSenderNonceMempool()
mempoolOpt := baseapp.SetMempool(nonceMempool)
baseAppOptions = append(baseAppOptions, mempoolOpt)

bApp := baseapp.NewBaseApp(Name, logger, db, txConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
Expand Down Expand Up @@ -793,7 +799,6 @@ func NewApp(
vestingtypes.ModuleName,
consensusparamtypes.ModuleName,
circuittypes.ModuleName,
wasmstoragetypes.ModuleName,
// ibc modules
capabilitytypes.ModuleName,
ibctransfertypes.ModuleName,
Expand Down Expand Up @@ -862,10 +867,10 @@ func NewApp(

// Pseudorandomness beacon
app.SetPrepareProposal(
randomnesskeeper.PrepareProposalHandler(txConfig, app.RandomnessKeeper),
randomnesskeeper.PrepareProposalHandler(txConfig, app.RandomnessKeeper, app.AccountKeeper, app.StakingKeeper, nonceMempool),
)
app.SetProcessProposal(
randomnesskeeper.ProcessProposalHandler(txConfig, app.RandomnessKeeper),
randomnesskeeper.ProcessProposalHandler(txConfig, app.RandomnessKeeper, app.StakingKeeper),
)

if loadLatest {
Expand Down
17 changes: 16 additions & 1 deletion cmd/seda-chaind/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"

cfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/crypto/secp256k1"
cmtos "github.com/cometbft/cometbft/libs/os"
"github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/types"
Expand Down Expand Up @@ -64,7 +65,21 @@ func readInMnemonic(cmd *cobra.Command) (string, error) {
return mnemonic, nil
}

// If validator key file exists, create and save an empty validator state file.
func generateValidatorWithSecp256k1Key(config *cfg.Config) error {
pvKeyFile := config.PrivValidatorKeyFile()
if err := os.MkdirAll(filepath.Dir(pvKeyFile), 0o777); err != nil {
return fmt.Errorf("could not create directory %q: %w", filepath.Dir(pvKeyFile), err)
}
pvStateFile := config.PrivValidatorStateFile()
if err := os.MkdirAll(filepath.Dir(pvStateFile), 0o777); err != nil {
return fmt.Errorf("could not create directory %q: %w", filepath.Dir(pvStateFile), err)
}

pv := privval.NewFilePV(secp256k1.GenPrivKey(), config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
pv.Save()
return nil
}

func configureValidatorFiles(config *cfg.Config) error {
keyFilePath := config.PrivValidatorKeyFile()
if err := os.MkdirAll(filepath.Dir(keyFilePath), 0o777); err != nil {
Expand Down
24 changes: 23 additions & 1 deletion cmd/seda-chaind/cmd/init_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

cfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/libs/cli"
cmtos "github.com/cometbft/cometbft/libs/os"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -81,6 +82,16 @@ func newNetworkCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Comma
initHeight = 1
}

// Before initializing the node, if a mnemonic is not given and
// the private validator key file does not exist, create a validator
// using a key generated on secp256k1.
if len(mnemonic) == 0 && !cmtos.FileExists(config.PrivValidatorKeyFile()) {
err = generateValidatorWithSecp256k1Key(config)
if err != nil {
return err
}
}

// initialize node
nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic)
if err != nil {
Expand Down Expand Up @@ -179,12 +190,23 @@ $ %s init join moniker --network devnet
return fmt.Errorf("unsupported network type: %s", network)
}

// configure validator files
// TO-DO remove (See: https://github.com/sedaprotocol/seda-chain/pull/76#issuecomment-1762303200)
// If validator key file exists, create and save an empty validator state file.
err = configureValidatorFiles(config)
if err != nil {
return err
}

// Before initializing the node, if a mnemonic is not given and
// the private validator key file does not exist, create a validator
// using a key generated on secp256k1.
if len(mnemonic) == 0 && !cmtos.FileExists(config.PrivValidatorKeyFile()) {
err = generateValidatorWithSecp256k1Key(config)
if err != nil {
return err
}
}

// initialize the node
nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/hyperledger/burrow v0.34.4
github.com/ory/dockertest/v3 v3.10.0
github.com/pkg/errors v0.9.1
github.com/sedaprotocol/vrf-go v0.0.0-20231128010622-13deb847d981
github.com/sedaprotocol/vrf-go v0.0.0-20231202231847-9ca3f58655e6
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -101,7 +101,7 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/emicklei/dot v1.6.0 // indirect
github.com/ethereum/go-ethereum v1.13.4 // indirect
github.com/ethereum/go-ethereum v1.13.5 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ethereum/go-ethereum v1.13.4 h1:25HJnaWVg3q1O7Z62LaaI6S9wVq8QCw3K88g8wEzrcM=
github.com/ethereum/go-ethereum v1.13.4/go.mod h1:I0U5VewuuTzvBtVzKo7b3hJzDhXOUtn9mJW7SsIPB0Q=
github.com/ethereum/go-ethereum v1.13.5 h1:U6TCRciCqZRe4FPXmy1sMGxTfuk8P7u2UoinF3VbaFk=
github.com/ethereum/go-ethereum v1.13.5/go.mod h1:yMTu38GSuyxaYzQMViqNmQ1s3cE84abZexQmTgenWk0=
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
Expand Down Expand Up @@ -1096,6 +1098,8 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg
github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
github.com/sedaprotocol/vrf-go v0.0.0-20231128010622-13deb847d981 h1:YmuOnAwJZSBfBwKJrwisACb9jZ4/us8R6A06PxqbKmQ=
github.com/sedaprotocol/vrf-go v0.0.0-20231128010622-13deb847d981/go.mod h1:cub/hpbOATCymOnKPJVOhuExFhhspWaOJW2nr/yzWUs=
github.com/sedaprotocol/vrf-go v0.0.0-20231202231847-9ca3f58655e6 h1:GqAxxdvT0bteI0b0Sq3iPDMP6BGlymYjPHCOveK9KH4=
github.com/sedaprotocol/vrf-go v0.0.0-20231202231847-9ca3f58655e6/go.mod h1:cub/hpbOATCymOnKPJVOhuExFhhspWaOJW2nr/yzWUs=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
10 changes: 10 additions & 0 deletions proto/sedachain/randomness/v1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
package sedachain.randomness.v1;

import "gogoproto/gogo.proto";

option go_package = "github.com/sedaprotocol/seda-chain/x/randomness/types";

message GenesisState {
string seed = 1;
}
6 changes: 3 additions & 3 deletions proto/sedachain/randomness/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ service Msg {
message MsgNewSeed {
option (cosmos.msg.v1.signer) = "proposer";

string seed = 1;
string pi = 2;
string proposer = 3;
string proposer = 1;
string pi = 2; // VRF proof
string beta = 3; // VRF hash
}

message MsgNewSeedResponse {}
5 changes: 3 additions & 2 deletions scripts/local_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ $BIN init new node0
cat $HOME/.seda-chain/config/genesis.json | jq '.app_state["gov"]["voting_params"]["voting_period"]="30s"' > $HOME/.seda-chain/config/tmp_genesis.json && mv $HOME/.seda-chain/config/tmp_genesis.json $HOME/.seda-chain/config/genesis.json
cat $HOME/.seda-chain/config/genesis.json | jq '.app_state["gov"]["params"]["voting_period"]="30s"' > $HOME/.seda-chain/config/tmp_genesis.json && mv $HOME/.seda-chain/config/tmp_genesis.json $HOME/.seda-chain/config/genesis.json
cat $HOME/.seda-chain/config/genesis.json | jq '.app_state["gov"]["params"]["expedited_voting_period"]="15s"' > $HOME/.seda-chain/config/tmp_genesis.json && mv $HOME/.seda-chain/config/tmp_genesis.json $HOME/.seda-chain/config/genesis.json
cat $HOME/.seda-chain/config/genesis.json | jq '.consensus["params"]["validator"]["pub_key_types"]=["secp256k1"]' > $HOME/.seda-chain/config/tmp_genesis.json && mv $HOME/.seda-chain/config/tmp_genesis.json $HOME/.seda-chain/config/genesis.json

$BIN keys add satoshi --keyring-backend test
ADDR=$($BIN keys show satoshi --keyring-backend test -a)
$BIN add-genesis-account $ADDR 100000000000000000seda --keyring-backend test
$BIN gentx satoshi 10000000000000000seda --keyring-backend test
$BIN gentx satoshi 10000000000000000seda --keyring-backend test --chain-id sedachain

$BIN keys add acc1 --keyring-backend test
ADDR=$($BIN keys show acc1 --keyring-backend test -a)
$BIN add-genesis-account $ADDR 100000000000000000seda --keyring-backend test

$BIN collect-gentxs
$BIN start
$BIN start --log_level debug
21 changes: 21 additions & 0 deletions x/randomness/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package randomness

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sedaprotocol/seda-chain/x/randomness/keeper"
"github.com/sedaprotocol/seda-chain/x/randomness/types"
)

// InitGenesis puts data from genesis state into store.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) {
k.SetSeed(ctx, data.Seed)

}

// ExportGenesis extracts data from store to genesis state.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
return types.GenesisState{
Seed: k.GetSeed(ctx),
}
}
Loading

0 comments on commit 4c4a4ab

Please sign in to comment.