Use RPC instead of exec.Command(...) (#176)

* Move `encoding.go` to lib to avoid import cycle

* [lib] Add `SetEncodingConfig` to avoid import cycle

```
package github.com/planetmint/planetmint-go/app
        imports github.com/planetmint/planetmint-go/app/ante
        imports github.com/planetmint/planetmint-go/x/dao
        imports github.com/planetmint/planetmint-go/util
        imports github.com/planetmint/planetmint-go/app: import cycle not allowed
```

* Use RPC instead of `exec.Command(...)`

// Closes #152

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
Julian Strobl 2023-11-15 14:16:21 +01:00 committed by GitHub
parent 682796b3c8
commit 5bc0097ae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 74 additions and 44 deletions

View File

@ -124,8 +124,8 @@ import (
// this line is used by starport scaffolding # stargate/app/moduleImport // this line is used by starport scaffolding # stargate/app/moduleImport
pmante "github.com/planetmint/planetmint-go/app/ante" pmante "github.com/planetmint/planetmint-go/app/ante"
appparams "github.com/planetmint/planetmint-go/app/params"
"github.com/planetmint/planetmint-go/docs" "github.com/planetmint/planetmint-go/docs"
appparams "github.com/planetmint/planetmint-go/lib/params"
) )
const ( const (

View File

@ -6,7 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/std" "github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/x/auth/tx" "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/planetmint/planetmint-go/app/params" "github.com/planetmint/planetmint-go/lib/params"
) )
// makeEncodingConfig creates an EncodingConfig for an amino based test configuration. // makeEncodingConfig creates an EncodingConfig for an amino based test configuration.

View File

@ -40,13 +40,18 @@ import (
// this line is used by starport scaffolding # root/moduleImport // this line is used by starport scaffolding # root/moduleImport
"github.com/planetmint/planetmint-go/app" "github.com/planetmint/planetmint-go/app"
appparams "github.com/planetmint/planetmint-go/app/params"
planetmintconfig "github.com/planetmint/planetmint-go/config" planetmintconfig "github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/lib"
appparams "github.com/planetmint/planetmint-go/lib/params"
) )
// NewRootCmd creates a new root command for a Cosmos SDK application // NewRootCmd creates a new root command for a Cosmos SDK application
func NewRootCmd() (*cobra.Command, appparams.EncodingConfig) { func NewRootCmd() (*cobra.Command, appparams.EncodingConfig) {
encodingConfig := app.MakeEncodingConfig() encodingConfig := app.MakeEncodingConfig()
// Initialize library
libConfig := lib.GetConfig()
libConfig.SetEncodingConfig(encodingConfig)
libConfig.SetBech32PrefixForAccount("plmnt")
initClientCtx := client.Context{}. initClientCtx := client.Context{}.
WithCodec(encodingConfig.Marshaler). WithCodec(encodingConfig.Marshaler).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry). WithInterfaceRegistry(encodingConfig.InterfaceRegistry).

3
go.mod
View File

@ -20,6 +20,7 @@ require (
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2
github.com/planetmint/planetmint-go/lib v0.0.0-00010101000000-000000000000
github.com/spf13/cast v1.5.0 github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1 github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
@ -180,3 +181,5 @@ require (
) )
replace github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 replace github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
replace github.com/planetmint/planetmint-go/lib => ./lib

View File

@ -18,12 +18,16 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/planetmint/planetmint-go/app"
"github.com/planetmint/planetmint-go/lib" "github.com/planetmint/planetmint-go/lib"
) )
func main() { func main() {
encodingConfig := app.MakeEncodingConfig()
libConfig := lib.GetConfig() libConfig := lib.GetConfig()
libConfig.SetBech32PrefixForAccount("plmnt") libConfig.SetBech32PrefixForAccount("plmnt")
libConfig.SetEncodingConfig(encodingConfig)
libConfig.SetRPCEndpoint("https://testnet-api.rddl.io") libConfig.SetRPCEndpoint("https://testnet-api.rddl.io")
addr0 := sdk.MustAccAddressFromBech32("plmnt168z8fyyzap0nw75d4atv9ucr2ye60d57dzlzaf") addr0 := sdk.MustAccAddressFromBech32("plmnt168z8fyyzap0nw75d4atv9ucr2ye60d57dzlzaf")

View File

@ -4,13 +4,15 @@ import (
"sync" "sync"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/lib/params"
) )
// Config defines library top level configuration. // Config defines library top level configuration.
type Config struct { type Config struct {
ChainID string `mapstructure:"chain-id" json:"chain-id"` ChainID string `mapstructure:"chain-id" json:"chain-id"`
RootDir string `mapstructure:"root-dir" json:"root-dir"` EncodingConfig params.EncodingConfig `mapstructure:"encoding-config" json:"encoding-config""`
RPCEndpoint string `mapstructure:"rpc-endpoint" json:"rpc-endpoint"` RootDir string `mapstructure:"root-dir" json:"root-dir"`
RPCEndpoint string `mapstructure:"rpc-endpoint" json:"rpc-endpoint"`
} }
// lib wide global singleton // lib wide global singleton
@ -23,9 +25,10 @@ var (
// DefaultConfig returns library default configuration. // DefaultConfig returns library default configuration.
func DefaultConfig() *Config { func DefaultConfig() *Config {
return &Config{ return &Config{
ChainID: "planetmint-testnet-1", ChainID: "planetmint-testnet-1",
RootDir: "~/.planetmint-go/", EncodingConfig: params.EncodingConfig{},
RPCEndpoint: "http://127.0.0.1:1317", RootDir: "~/.planetmint-go/",
RPCEndpoint: "http://127.0.0.1:1317",
} }
} }
@ -44,6 +47,12 @@ func (config *Config) SetBech32PrefixForAccount(bech32Prefix string) *Config {
return config return config
} }
// SetEncodingConfig sets the encoding config and must not be nil.
func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *Config {
config.EncodingConfig = encodingConfig
return config
}
// SetChainID sets the chain ID parameter. // SetChainID sets the chain ID parameter.
func (config *Config) SetChainID(chainID string) *Config { func (config *Config) SetChainID(chainID string) *Config {
config.ChainID = chainID config.ChainID = chainID

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -20,7 +21,6 @@ import (
sdktx "github.com/cosmos/cosmos-sdk/types/tx" sdktx "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/types/tx/signing"
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/planetmint/planetmint-go/app"
) )
// KeyPair defines a public/private key pair to e.g. sign a transaction. // KeyPair defines a public/private key pair to e.g. sign a transaction.
@ -117,7 +117,11 @@ func getAccountNumberAndSequence(address sdk.AccAddress) (accountNumber, sequenc
// BuildAndSignTx constructs the transaction from address' private key and messages. // BuildAndSignTx constructs the transaction from address' private key and messages.
func BuildAndSignTx(address sdk.AccAddress, msgs ...sdk.Msg) (txBytes []byte, txJSON string, err error) { func BuildAndSignTx(address sdk.AccAddress, msgs ...sdk.Msg) (txBytes []byte, txJSON string, err error) {
encodingConfig := app.MakeEncodingConfig() encodingConfig := GetConfig().EncodingConfig
if encodingConfig.TxConfig == nil {
err = errors.New("encoding config must not be nil")
return
}
txBuilder := encodingConfig.TxConfig.NewTxBuilder() txBuilder := encodingConfig.TxConfig.NewTxBuilder()
err = txBuilder.SetMsgs(msgs...) err = txBuilder.SetMsgs(msgs...)

View File

@ -1,58 +1,63 @@
package util package util
import ( import (
"os/exec"
"strconv" "strconv"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/config" "github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/x/dao/types" "github.com/planetmint/planetmint-go/lib"
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
) )
func InitRDDLReissuanceProcess(ctx sdk.Context, proposerAddress string, txUnsigned string, blockHeight int64) error { func buildSignBroadcastTx(ctx sdk.Context, sendingValidatorAddress string, msg sdk.Msg) (err error) {
logger := ctx.Logger()
addr := sdk.MustAccAddressFromBech32(sendingValidatorAddress)
txBytes, txJSON, err := lib.BuildAndSignTx(addr, msg)
if err != nil {
return
}
logger.Debug("REISSUE: tx: " + txJSON)
_, err = lib.BroadcastTx(txBytes)
return
}
func InitRDDLReissuanceProcess(ctx sdk.Context, proposerAddress string, txUnsigned string, blockHeight int64) (err error) {
//get_last_PoPBlockHeight() // TODO: to be read form the upcoming PoP-store //get_last_PoPBlockHeight() // TODO: to be read form the upcoming PoP-store
logger := ctx.Logger() logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Proposal") logger.Debug("REISSUE: create Reissuance Proposal")
cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-proposal", msg := daotypes.NewMsgReissueRDDLProposal(sendingValidatorAddress, proposerAddress, txUnsigned, blockHeight)
"--from", sendingValidatorAddress, "-y", err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg)
proposerAddress, txUnsigned, strconv.FormatInt(blockHeight, 10)) return
logger.Debug("REISSUE: create Proposal")
return cmd.Start()
} }
func SendRDDLReissuanceResult(ctx sdk.Context, proposerAddress string, txID string, blockHeight int64) error { func SendRDDLReissuanceResult(ctx sdk.Context, proposerAddress string, txID string, blockHeight int64) (err error) {
logger := ctx.Logger() logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Result") logger.Debug("REISSUE: create Reissuance Result")
cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-result", msg := daotypes.NewMsgReissueRDDLResult(sendingValidatorAddress, proposerAddress, txID, blockHeight)
"--from", sendingValidatorAddress, "-y", err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg)
proposerAddress, txID, strconv.FormatInt(blockHeight, 10)) return
logger.Debug("REISSUE: create Result")
return cmd.Start()
} }
func SendRDDLDistributionRequest(ctx sdk.Context, distribution types.DistributionOrder) error { func SendRDDLDistributionRequest(ctx sdk.Context, distribution daotypes.DistributionOrder) (err error) {
logger := ctx.Logger() logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress sendingValidatorAddress := config.GetConfig().ValidatorAddress
cmd := exec.Command("planetmint-god", "tx", "dao", "distribution-request", logger.Debug("REISSUE: create Distribution Request")
"--from", sendingValidatorAddress, "-y", "'"+distribution.String()+"'") msg := daotypes.NewMsgDistributionRequest(sendingValidatorAddress, &distribution)
logger.Debug("REISSUE: create Result") err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg)
return cmd.Start() return
} }
func SendRDDLDistributionResult(ctx sdk.Context, lastPoP string, daoTxid string, invTxid string, popTxid string) error { func SendRDDLDistributionResult(ctx sdk.Context, lastPoP string, daoTxID string, invTxID string, popTxID string) (err error) {
logger := ctx.Logger() logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Result") logger.Debug("REISSUE: create Distribution Result")
cmd := exec.Command("planetmint-god", "tx", "dao", "distribution-result", iLastPoP, err := strconv.ParseInt(lastPoP, 10, 64)
"--from", sendingValidatorAddress, "-y", if err != nil {
lastPoP, daoTxid, invTxid, popTxid) return
logger.Debug("REISSUE: create Result") }
return cmd.Start() msg := daotypes.NewMsgDistributionResult(sendingValidatorAddress, iLastPoP, daoTxID, invTxID, popTxID)
err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg)
return
} }