Skip to content

Commit

Permalink
remove salt from transactions, replace with chainID (#356)
Browse files Browse the repository at this point in the history
* remove salt from transactions, replace with chainID

* chain_info RPC endpoint

* unembed and satisfy linter
  • Loading branch information
jchappelow authored and brennanjl committed Feb 26, 2024
1 parent 33b9871 commit 68d7d23
Show file tree
Hide file tree
Showing 40 changed files with 932 additions and 378 deletions.
8 changes: 8 additions & 0 deletions cmd/kwil-admin/nodecfg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
)

type NodeGenerateConfig struct {
ChainID string
// InitialHeight int64 // ?
OutputDir string
JoinExpiry int64
Expand All @@ -37,6 +38,7 @@ type NodeGenerateConfig struct {
}

type TestnetGenerateConfig struct {
ChainID string
// InitialHeight int64
NValidators int
NNonValidators int
Expand Down Expand Up @@ -114,6 +116,9 @@ func GenerateNodeConfig(genCfg *NodeGenerateConfig) error {
}

func (genCfg *NodeGenerateConfig) applyGenesisParams(genesisCfg *config.GenesisConfig) {
if genCfg.ChainID != "" {
genesisCfg.ChainID = genCfg.ChainID
}
genesisCfg.ConsensusParams.Validator.JoinExpiry = genCfg.JoinExpiry
genesisCfg.ConsensusParams.WithoutGasCosts = genCfg.WithoutGasCosts
genesisCfg.ConsensusParams.WithoutNonces = genCfg.WithoutNonces
Expand Down Expand Up @@ -221,6 +226,9 @@ func GenerateTestnetConfig(genCfg *TestnetGenerateConfig) error {
}

func (genCfg *TestnetGenerateConfig) applyGenesisParams(genesisCfg *config.GenesisConfig) {
if genCfg.ChainID != "" {
genesisCfg.ChainID = genCfg.ChainID
}
genesisCfg.ConsensusParams.Validator.JoinExpiry = genCfg.JoinExpiry
genesisCfg.ConsensusParams.WithoutGasCosts = genCfg.WithoutGasCosts
genesisCfg.ConsensusParams.WithoutNonces = genCfg.WithoutNonces
Expand Down
3 changes: 3 additions & 0 deletions cmd/kwil-admin/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (cc *SetupCmd) run(ctx context.Context) error {
switch {
case cc.Init != nil:
genCfg := &nodecfg.NodeGenerateConfig{
ChainID: cc.Init.ChainID,
OutputDir: cc.Init.OutputDir,
JoinExpiry: cc.Init.JoinExpiry,
WithoutGasCosts: true, // gas disabled by setup init
Expand Down Expand Up @@ -58,6 +59,7 @@ func (cc *SetupCmd) run(ctx context.Context) error {
}

type SetupInitCmd struct {
ChainID string `arg:"--chain-id" help:"override the chain ID"`
OutputDir string `arg:"-o,--output-dir" default:".testnet" help:"parent directory for all of generated node folders" placeholder:"DIR"`
JoinExpiry int64 `arg:"--join-expiry" default:"86400" help:"number of blocks before a join request expires"`
WithoutNonces bool `arg:"--without-nonces" help:"disable nonces"`
Expand All @@ -70,6 +72,7 @@ type SetupInitCmd struct {
// SetupTestnetCmd exactly matches nodecfg.TestnetGenerateConfig in field name,
// type, and layout so that it may be converted directly.
type SetupTestnetCmd struct {
ChainID string `arg:"--chain-id" help:"override the chain ID"`
NValidators int `arg:"-v,--validators" default:"4" help:"number of validators" placeholder:"V"`
NNonValidators int `arg:"-n,--non-validators" default:"4" help:"number of non-validators" placeholder:"N"`
ConfigFile string `arg:"--config" help:"template config file to use, default is none" placeholder:"FILE"`
Expand Down
30 changes: 17 additions & 13 deletions cmd/kwil-admin/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ValidatorsCmd struct {
Leave *ValLeaveCmd `arg:"subcommand:leave"`

RPCServer string `arg:"-s,--rpcserver" default:"127.0.0.1:50051" help:"RPC server address"`
ChainID string `arg:"-c,--chain" default:"" help:"The Kwil network's chain ID to use for transactions"`
OutputFormat string `arg:"-o,--output" default:"text" help:"Output format (text|json)"`
}

Expand All @@ -39,7 +40,7 @@ func (vlc *ValListCmd) run(ctx context.Context, a *args) error {
err := func() error {
rpcAddr := a.Vals.RPCServer
options := []client.Option{client.WithTLSCert("")} // TODO: handle cert
clt, err := client.Dial(rpcAddr, options...)
clt, err := client.Dial(ctx, rpcAddr, options...)
if err != nil {
return err
}
Expand All @@ -62,7 +63,7 @@ func (vjsc *ValJoinStatusCmd) run(ctx context.Context, a *args) error {
err := func() error {
rpcAddr := a.Vals.RPCServer
options := []client.Option{client.WithTLSCert("")} // TODO: handle cert
clt, err := client.Dial(rpcAddr, options...)
clt, err := client.Dial(ctx, rpcAddr, options...)
if err != nil {
return err
}
Expand All @@ -82,16 +83,19 @@ func (vjsc *ValJoinStatusCmd) run(ctx context.Context, a *args) error {

// edSigningClient makes a client using the provided private key as an ed25519
// Signer.
func edSigningClient(rpcAddr string, privKey []byte) (*client.Client, error) {
func edSigningClient(ctx context.Context, rpcAddr, chainID string, privKey []byte) (*client.Client, error) {
edPrivKey, err := crypto.Ed25519PrivateKeyFromBytes(privKey)
if err != nil {
return nil, err
}

signer := auth.Ed25519Signer{Ed25519PrivateKey: *edPrivKey}

options := []client.Option{client.WithSigner(&signer), client.WithTLSCert("")}
return client.Dial(rpcAddr, options...)
options := []client.Option{
client.WithSigner(&signer, chainID),
client.WithTLSCert(""),
}
return client.Dial(ctx, rpcAddr, options...)
}

// valSignedCmd is meant to be embedded in commands that want a private key in
Expand All @@ -102,12 +106,12 @@ type valSignedCmd struct {
PrivKeyFile string `arg:"-k,--key-file" help:"File containing the private key of the validator."`
}

func (vsc *valSignedCmd) client(rpcAddr string) (*client.Client, error) {
func (vsc *valSignedCmd) client(ctx context.Context, rpcAddr, chainID string) (*client.Client, error) {
privKey, err := keyFromBytesOrFile(vsc.PrivKey, vsc.PrivKeyFile)
if err != nil {
return nil, err
}
return edSigningClient(rpcAddr, privKey)
return edSigningClient(ctx, rpcAddr, chainID, privKey)
}

type ValJoinCmd struct {
Expand All @@ -119,8 +123,8 @@ var _ runner = (*ValJoinCmd)(nil)
func (vjc *ValJoinCmd) run(ctx context.Context, a *args) error {
var txHash []byte
err := func() error {
rpcAddr := a.Vals.RPCServer
clt, err := vjc.client(rpcAddr)
rpcAddr, chainID := a.Vals.RPCServer, a.Vals.ChainID
clt, err := vjc.client(ctx, rpcAddr, chainID)
if err != nil {
return err
}
Expand All @@ -141,8 +145,8 @@ var _ runner = (*ValApproveCmd)(nil)
func (vac *ValApproveCmd) run(ctx context.Context, a *args) error {
var txHash []byte
err := func() error {
rpcAddr := a.Vals.RPCServer
clt, err := vac.client(rpcAddr)
rpcAddr, chainID := a.Vals.RPCServer, a.Vals.ChainID
clt, err := vac.client(ctx, rpcAddr, chainID)
if err != nil {
return err
}
Expand All @@ -162,8 +166,8 @@ var _ runner = (*ValLeaveCmd)(nil)
func (vjc *ValLeaveCmd) run(ctx context.Context, a *args) error {
var txHash []byte
err := func() error {
rpcAddr := a.Vals.RPCServer
clt, err := vjc.client(rpcAddr)
rpcAddr, chainID := a.Vals.RPCServer, a.Vals.ChainID
clt, err := vjc.client(ctx, rpcAddr, chainID)
if err != nil {
return err
}
Expand Down
15 changes: 3 additions & 12 deletions cmd/kwil-cli/cmds/common/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func DialClient(ctx context.Context, flags uint8, fn RoundTripper) error {
return fmt.Errorf("private key not provided")
}

signer := auth.EthPersonalSigner{Secp256k1PrivateKey: *conf.PrivateKey}
options = append(options, client.WithSigner(&signer))
signer := auth.EthPersonalSigner{Key: *conf.PrivateKey}
options = append(options, client.WithSigner(&signer, conf.ChainID))
}

if conf.GrpcURL == "" {
Expand All @@ -47,20 +47,11 @@ func DialClient(ctx context.Context, flags uint8, fn RoundTripper) error {
return fmt.Errorf("kwil grpc url is required")
}

clt, err := client.Dial(conf.GrpcURL, options...)
clt, err := client.Dial(ctx, conf.GrpcURL, options...)
if err != nil {
return err
}
defer clt.Close()

pong, err := clt.Ping(ctx)
if err != nil {
return fmt.Errorf("failed to ping provider: %w", err)
}

if pong != "pong" {
return fmt.Errorf("unexpected ping response: %s", pong)
}

return fn(ctx, clt, conf)
}
16 changes: 16 additions & 0 deletions cmd/kwil-cli/cmds/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewCmdConfigure() *cobra.Command {

err = runErrs(conf,
promptGRPCURL,
promptChainID,
promptPrivateKey,
promptTLSCertFile,
)
Expand Down Expand Up @@ -64,6 +65,21 @@ func promptGRPCURL(conf *config.KwilCliConfig) error {
return nil
}

func promptChainID(conf *config.KwilCliConfig) error {
prompt := &common.Prompter{
Label: "Kwil Chain ID (leave empty to trust a server-provided value)",
Default: conf.ChainID,
}
res, err := prompt.Run()
if err != nil { // NOTE: empty is valid (no error)
return err
}

conf.ChainID = res

return nil
}

func promptTLSCertFile(conf *config.KwilCliConfig) error {
prompt := &common.Prompter{
Label: "Kwil RPC TLS certificate path",
Expand Down
8 changes: 6 additions & 2 deletions cmd/kwil-cli/cmds/utils/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func getExampleTxQueryResponse() *transactions.TcTxQueryResponse {
PayloadType: rawPayload.Type(),
Fee: big.NewInt(100),
Nonce: 10,
Salt: []byte("salt"),
ChainID: "asdf",
Description: "This is a test transaction for cli",
},
Serialization: transactions.SignedMsgConcat,
Expand Down Expand Up @@ -128,7 +128,7 @@ func Example_respTxQuery_json() {
// "PayloadType": "execute_action",
// "Fee": 100,
// "Nonce": 10,
// "Salt": "c2FsdA=="
// "ChainID": "asdf"
// },
// "Serialization": "concat",
// "Sender": null
Expand All @@ -152,13 +152,15 @@ func Example_respKwilCliConfig_text() {
display.Print(&respKwilCliConfig{
cfg: &config.KwilCliConfig{
PrivateKey: pk,
ChainID: "chainid123",
GrpcURL: "localhost:9090",
TLSCertFile: "",
},
}, nil, "text")
// Output:
// PrivateKey: ***
// GrpcURL: localhost:9090
// ChainID: chainid123
// TLSCertFile:
}

Expand All @@ -171,6 +173,7 @@ func Example_respKwilCliConfig_json() {
display.Print(&respKwilCliConfig{
cfg: &config.KwilCliConfig{
PrivateKey: pk,
ChainID: "chainid123",
GrpcURL: "localhost:9090",
TLSCertFile: "",
},
Expand All @@ -180,6 +183,7 @@ func Example_respKwilCliConfig_json() {
// "result": {
// "private_key": "***",
// "grpc_url": "localhost:9090",
// "chain_id": "chainid123",
// "tls_cert_file": ""
// },
// "error": ""
Expand Down
4 changes: 4 additions & 0 deletions cmd/kwil-cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type KwilCliConfig struct {
PrivateKey *crypto.Secp256k1PrivateKey
GrpcURL string
ChainID string
TLSCertFile string
}

Expand All @@ -27,6 +28,7 @@ func (c *KwilCliConfig) ToPersistedConfig() *kwilCliPersistedConfig {
return &kwilCliPersistedConfig{
PrivateKey: privKeyHex,
GrpcURL: c.GrpcURL,
ChainID: c.ChainID,
TLSCertFile: c.TLSCertFile,
}
}
Expand All @@ -47,12 +49,14 @@ type kwilCliPersistedConfig struct {
// NOTE: `mapstructure` is used by viper, name is same as the viper key name
PrivateKey string `mapstructure:"private_key" json:"private_key"`
GrpcURL string `mapstructure:"grpc_url" json:"grpc_url"`
ChainID string `mapstructure:"chain_id" json:"chain_id"`
TLSCertFile string `mapstructure:"tls_cert_file" json:"tls_cert_file"`
}

func (c *kwilCliPersistedConfig) toKwilCliConfig() (*KwilCliConfig, error) {
kwilConfig := &KwilCliConfig{
GrpcURL: c.GrpcURL,
ChainID: c.ChainID,
TLSCertFile: c.TLSCertFile,
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/kwil-cli/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ const (
// also since the config file is using `grpc_url`, should change too
// TODO: this is a breaking change
globalProviderFlag = "kwil-provider"
globalChainIDFlag = "chain-id"
globalOutputFlag = "output"
globalTlsCertFlag = "tls-cert-file"
// NOTE: viper key name are used for viper related operations
// here they are same `mapstructure` names defined in the config struct
viperPrivateKeyName = "private_key"
viperProviderName = "grpc_url"
viperChainID = "chain_id"
viperTlsCertName = "tls_cert_file"
viperOutputName = "output"
)
Expand Down Expand Up @@ -83,13 +85,15 @@ var outputFormat = DefaultOutputFormat
func BindGlobalFlags(fs *pflag.FlagSet) {
// Bind flags to environment variables
fs.String(globalPrivateKeyFlag, cliCfg.PrivateKey, "The private key of the wallet that will be used for signing")
fs.String(globalProviderFlag, cliCfg.GrpcURL, "The kwil provider endpoint")
fs.String(globalProviderFlag, cliCfg.GrpcURL, "The Kwil provider endpoint")
fs.String(globalChainIDFlag, cliCfg.ChainID, "The expected/intended Kwil Chain ID")
fs.String(globalTlsCertFlag, cliCfg.TLSCertFile, "The path to the TLS certificate, this is required if the kwil provider endpoint is using TLS")
fs.Var(&outputFormat, globalOutputFlag, "the format for command output, either 'text' or 'json'")

// Bind flags to viper, named by the flag name
viper.BindPFlag(viperPrivateKeyName, fs.Lookup(globalPrivateKeyFlag))
viper.BindPFlag(viperProviderName, fs.Lookup(globalProviderFlag))
viper.BindPFlag(viperChainID, fs.Lookup(globalChainIDFlag))
viper.BindPFlag(viperTlsCertName, fs.Lookup(globalTlsCertFlag))
viper.BindPFlag(viperOutputName, fs.Lookup(globalOutputFlag))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kwild/config/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type VersionParams struct {
}

func generateChainID(prefix string) string {
return prefix + random.String(6)
return prefix + random.String(8)
}

// DefaultGenesisConfig returns a new instance of a GenesisConfig with the
Expand Down
15 changes: 9 additions & 6 deletions cmd/kwild/server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func buildServer(d *coreDependencies, closers *closeFuncs) *Server {
cometBftClient := buildCometBftClient(cometBftNode)

// tx service and grpc server
txsvc := buildTxSvc(d, datasetsModule, accs, vstore, &wrappedCometBFTClient{cometBftClient})
txsvc := buildTxSvc(d, datasetsModule, accs, vstore, &wrappedCometBFTClient{cometBftClient}, abciApp)
grpcServer := buildGrpcServer(d, txsvc)

// admin service and server
Expand Down Expand Up @@ -155,23 +155,26 @@ func buildAbci(d *coreDependencies, closer *closeFuncs, accountsModule abci.Acco
sh = snapshotter
}

genesisHash := d.genesisCfg.ComputeGenesisHash()
return abci.NewAbciApp(
cfg := &abci.AbciConfig{
GenesisAppHash: d.genesisCfg.ComputeGenesisHash(),
ChainID: d.genesisCfg.ChainID,
ApplicationVersion: d.genesisCfg.ConsensusParams.Version.App,
}
return abci.NewAbciApp(cfg,
accountsModule,
datasetsModule,
validatorModule,
atomicKv,
atomicCommitter,
sh,
bootstrapper,
genesisHash,
abci.WithLogger(*d.log.Named("abci")),
)
}

func buildTxSvc(d *coreDependencies, txsvc txSvc.EngineReader, accs txSvc.AccountReader,
vstore *vmgr.ValidatorMgr, cometBftClient txSvc.BlockchainTransactor) *txSvc.Service {
return txSvc.NewService(txsvc, accs, vstore, cometBftClient,
vstore *vmgr.ValidatorMgr, cometBftClient txSvc.BlockchainTransactor, nodeApp txSvc.NodeApplication) *txSvc.Service {
return txSvc.NewService(d.genesisCfg.ChainID, txsvc, accs, vstore, cometBftClient, nodeApp,
txSvc.WithLogger(*d.log.Named("tx-service")),
)
}
Expand Down
Loading

0 comments on commit 68d7d23

Please sign in to comment.