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
pmante "github.com/planetmint/planetmint-go/app/ante"
appparams "github.com/planetmint/planetmint-go/app/params"
"github.com/planetmint/planetmint-go/docs"
appparams "github.com/planetmint/planetmint-go/lib/params"
)
const (

View File

@ -6,7 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/std"
"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.

View File

@ -40,13 +40,18 @@ import (
// this line is used by starport scaffolding # root/moduleImport
"github.com/planetmint/planetmint-go/app"
appparams "github.com/planetmint/planetmint-go/app/params"
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
func NewRootCmd() (*cobra.Command, appparams.EncodingConfig) {
encodingConfig := app.MakeEncodingConfig()
// Initialize library
libConfig := lib.GetConfig()
libConfig.SetEncodingConfig(encodingConfig)
libConfig.SetBech32PrefixForAccount("plmnt")
initClientCtx := client.Context{}.
WithCodec(encodingConfig.Marshaler).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).

3
go.mod
View File

@ -20,6 +20,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
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/cobra v1.6.1
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/planetmint/planetmint-go/lib => ./lib

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
@ -20,7 +21,6 @@ import (
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/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.
@ -117,7 +117,11 @@ func getAccountNumberAndSequence(address sdk.AccAddress) (accountNumber, sequenc
// BuildAndSignTx constructs the transaction from address' private key and messages.
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()
err = txBuilder.SetMsgs(msgs...)

View File

@ -1,58 +1,63 @@
package util
import (
"os/exec"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"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
logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Proposal")
cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-proposal",
"--from", sendingValidatorAddress, "-y",
proposerAddress, txUnsigned, strconv.FormatInt(blockHeight, 10))
logger.Debug("REISSUE: create Proposal")
return cmd.Start()
logger.Debug("REISSUE: create Reissuance Proposal")
msg := daotypes.NewMsgReissueRDDLProposal(sendingValidatorAddress, proposerAddress, txUnsigned, blockHeight)
err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg)
return
}
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()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Result")
cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-result",
"--from", sendingValidatorAddress, "-y",
proposerAddress, txID, strconv.FormatInt(blockHeight, 10))
logger.Debug("REISSUE: create Result")
return cmd.Start()
logger.Debug("REISSUE: create Reissuance Result")
msg := daotypes.NewMsgReissueRDDLResult(sendingValidatorAddress, proposerAddress, txID, blockHeight)
err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg)
return
}
func SendRDDLDistributionRequest(ctx sdk.Context, distribution types.DistributionOrder) error {
func SendRDDLDistributionRequest(ctx sdk.Context, distribution daotypes.DistributionOrder) (err error) {
logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress
cmd := exec.Command("planetmint-god", "tx", "dao", "distribution-request",
"--from", sendingValidatorAddress, "-y", "'"+distribution.String()+"'")
logger.Debug("REISSUE: create Result")
return cmd.Start()
logger.Debug("REISSUE: create Distribution Request")
msg := daotypes.NewMsgDistributionRequest(sendingValidatorAddress, &distribution)
err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg)
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()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Result")
cmd := exec.Command("planetmint-god", "tx", "dao", "distribution-result",
"--from", sendingValidatorAddress, "-y",
lastPoP, daoTxid, invTxid, popTxid)
logger.Debug("REISSUE: create Result")
return cmd.Start()
logger.Debug("REISSUE: create Distribution Result")
iLastPoP, err := strconv.ParseInt(lastPoP, 10, 64)
if err != nil {
return
}
msg := daotypes.NewMsgDistributionResult(sendingValidatorAddress, iLastPoP, daoTxID, invTxID, popTxID)
err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg)
return
}