mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-30 18:02:30 +00:00
447 add validatoraddress getter to config (#449)
* feat: add GetValidatorAddress() to config * feat: add GetDefaultValidatorRecord() to libConfig --------- Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
72df3de139
commit
2a5b7c49c8
@ -2,7 +2,12 @@ package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/planetmint/planetmint-go/lib"
|
||||
"github.com/planetmint/planetmint-go/lib/trustwallet"
|
||||
"github.com/rddl-network/go-utils/logger"
|
||||
)
|
||||
|
||||
const DefaultConfigTemplate = `
|
||||
@ -11,7 +16,6 @@ const DefaultConfigTemplate = `
|
||||
###############################################################################
|
||||
|
||||
[planetmint]
|
||||
validator-address = "{{ .PlmntConfig.ValidatorAddress }}"
|
||||
mqtt-domain = "{{ .PlmntConfig.MqttDomain }}"
|
||||
mqtt-port = {{ .PlmntConfig.MqttPort }}
|
||||
mqtt-user = "{{ .PlmntConfig.MqttUser }}"
|
||||
@ -22,17 +26,19 @@ issuer-host = "{{ .PlmntConfig.IssuerHost }}"
|
||||
certs-path = "{{ .PlmntConfig.CertsPath }}"
|
||||
`
|
||||
|
||||
// ValAddr to be reomved see: https://github.com/planetmint/planetmint-go/issues/454
|
||||
const ValAddr = "VALIDATOR_ADDRESS"
|
||||
|
||||
// Config defines Planetmint's top level configuration
|
||||
type Config struct {
|
||||
ValidatorAddress string `json:"validator-address" mapstructure:"validator-address"`
|
||||
MqttDomain string `json:"mqtt-domain" mapstructure:"mqtt-domain"`
|
||||
MqttPort int `json:"mqtt-port" mapstructure:"mqtt-port"`
|
||||
MqttUser string `json:"mqtt-user" mapstructure:"mqtt-user"`
|
||||
MqttPassword string `json:"mqtt-password" mapstructure:"mqtt-password"`
|
||||
ClaimHost string `json:"claim-host" mapstructure:"claim-host"`
|
||||
MqttTLS bool `json:"mqtt-tls" mapstructure:"mqtt-tls"`
|
||||
IssuerHost string `json:"issuer-host" mapstructure:"issuer-host"`
|
||||
CertsPath string `json:"certs-path" mapstructure:"certs-path"`
|
||||
MqttDomain string `json:"mqtt-domain" mapstructure:"mqtt-domain"`
|
||||
MqttPort int `json:"mqtt-port" mapstructure:"mqtt-port"`
|
||||
MqttUser string `json:"mqtt-user" mapstructure:"mqtt-user"`
|
||||
MqttPassword string `json:"mqtt-password" mapstructure:"mqtt-password"`
|
||||
ClaimHost string `json:"claim-host" mapstructure:"claim-host"`
|
||||
MqttTLS bool `json:"mqtt-tls" mapstructure:"mqtt-tls"`
|
||||
IssuerHost string `json:"issuer-host" mapstructure:"issuer-host"`
|
||||
CertsPath string `json:"certs-path" mapstructure:"certs-path"`
|
||||
}
|
||||
|
||||
// cosmos-sdk wide global singleton
|
||||
@ -44,15 +50,14 @@ var (
|
||||
// DefaultConfig returns planetmint's default configuration.
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
ValidatorAddress: "plmnt1w5dww335zhh98pzv783hqre355ck3u4w4hjxcx",
|
||||
MqttDomain: "testnet-mqtt.rddl.io",
|
||||
MqttPort: 1886,
|
||||
MqttUser: "user",
|
||||
MqttPassword: "password",
|
||||
ClaimHost: "https://testnet-p2r.rddl.io",
|
||||
MqttTLS: true,
|
||||
IssuerHost: "https://testnet-issuer.rddl.io",
|
||||
CertsPath: "./certs/",
|
||||
MqttDomain: "testnet-mqtt.rddl.io",
|
||||
MqttPort: 1886,
|
||||
MqttUser: "user",
|
||||
MqttPassword: "password",
|
||||
ClaimHost: "https://testnet-p2r.rddl.io",
|
||||
MqttTLS: true,
|
||||
IssuerHost: "https://testnet-issuer.rddl.io",
|
||||
CertsPath: "./certs/",
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,3 +80,43 @@ func (config *Config) SetPlanetmintConfig(planetmintconfig interface{}) {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (config *Config) GetValidatorAddress() string {
|
||||
// Case: testing
|
||||
if os.Getenv(ValAddr) != "" {
|
||||
return os.Getenv(ValAddr)
|
||||
}
|
||||
|
||||
libConfig := lib.GetConfig()
|
||||
|
||||
// Case: No Trust Wallet connected
|
||||
if libConfig.GetSerialPort() == "" {
|
||||
defaultRecord, err := libConfig.GetDefaultValidatorRecord()
|
||||
if err != nil {
|
||||
logger.GetLogger(logger.ERROR).Error("msg", err.Error())
|
||||
return ""
|
||||
}
|
||||
addr, err := defaultRecord.GetAddress()
|
||||
if err != nil {
|
||||
logger.GetLogger(logger.ERROR).Error("msg", err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
return addr.String()
|
||||
}
|
||||
|
||||
// Case: Trust Wallet connected
|
||||
connector, err := trustwallet.NewTrustWalletConnector(libConfig.GetSerialPort())
|
||||
if err != nil {
|
||||
logger.GetLogger(logger.ERROR).Error("msg", err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
keys, err := connector.GetPlanetmintKeys()
|
||||
if err != nil {
|
||||
logger.GetLogger(logger.ERROR).Error("msg", err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
return keys.PlanetmintAddress
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/planetmint/planetmint-go/lib/params"
|
||||
)
|
||||
@ -125,3 +127,25 @@ func (config *Config) SetSerialPort(port string) *Config {
|
||||
config.serialPort = port
|
||||
return config
|
||||
}
|
||||
|
||||
func (config *Config) GetSerialPort() string {
|
||||
return config.serialPort
|
||||
}
|
||||
|
||||
func (config *Config) getLibKeyring() (keyring.Keyring, error) {
|
||||
return keyring.New("lib", keyring.BackendTest, config.rootDir, os.Stdin, config.encodingConfig.Marshaler, []keyring.Option{}...)
|
||||
}
|
||||
|
||||
func (config *Config) GetDefaultValidatorRecord() (*keyring.Record, error) {
|
||||
keyring, err := config.getLibKeyring()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
records, err := keyring.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return records[0], nil
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func getClientContext(fromAddress sdk.AccAddress) (clientCtx client.Context, err
|
||||
codec := encodingConfig.Marshaler
|
||||
keyringOptions := []keyring.Option{}
|
||||
|
||||
keyring, err := keyring.New("lib", keyring.BackendTest, rootDir, input, codec, keyringOptions...)
|
||||
keyring, err := GetConfig().getLibKeyring()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ func (mms *MqttMonitor) setNumDBElements(numElements int64) {
|
||||
|
||||
func getClientID() string {
|
||||
conf := config.GetConfig()
|
||||
return "monitor-" + conf.ValidatorAddress
|
||||
return "monitor-" + conf.GetValidatorAddress()
|
||||
}
|
||||
|
||||
func (mms *MqttMonitor) lazyLoadMonitorMQTTClient() util.MQTTClientI {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package monitor_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -15,6 +16,7 @@ import (
|
||||
func init() {
|
||||
// Use MQTT mock client
|
||||
monitor.MonitorMQTTClient = &mocks.MockMQTTClient{}
|
||||
os.Setenv(config.ValAddr, "plmnt10mq5nj8jhh27z7ejnz2ql3nh0qhzjnfvy50877")
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -595,8 +595,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
|
||||
}
|
||||
l.Log("started validator", idx)
|
||||
if idx == 0 {
|
||||
conf := config.GetConfig()
|
||||
conf.ValidatorAddress = network.Validators[0].Address.String()
|
||||
os.Setenv(config.ValAddr, network.Validators[0].Address.String())
|
||||
// set missing validator client context values for sending txs
|
||||
var output bytes.Buffer
|
||||
network.Validators[0].ClientCtx.BroadcastMode = "sync"
|
||||
|
@ -51,7 +51,7 @@ func buildSignBroadcastTx(goCtx context.Context, loggingContext string, sendingV
|
||||
|
||||
func SendInitReissuance(goCtx context.Context, proposerAddress string, txUnsigned string, blockHeight int64,
|
||||
firstIncludedPop int64, lastIncludedPop int64) {
|
||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
||||
sendingValidatorAddress := config.GetConfig().GetValidatorAddress()
|
||||
msg := daotypes.NewMsgReissueRDDLProposal(sendingValidatorAddress, proposerAddress, txUnsigned, blockHeight,
|
||||
firstIncludedPop, lastIncludedPop)
|
||||
loggingContext := "reissuance proposal"
|
||||
@ -59,14 +59,14 @@ func SendInitReissuance(goCtx context.Context, proposerAddress string, txUnsigne
|
||||
}
|
||||
|
||||
func SendReissuanceResult(goCtx context.Context, proposerAddress string, txID string, blockHeight int64) {
|
||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
||||
sendingValidatorAddress := config.GetConfig().GetValidatorAddress()
|
||||
msg := daotypes.NewMsgReissueRDDLResult(sendingValidatorAddress, proposerAddress, txID, blockHeight)
|
||||
loggingContext := "reissuance result"
|
||||
buildSignBroadcastTx(goCtx, loggingContext, sendingValidatorAddress, msg)
|
||||
}
|
||||
|
||||
func SendDistributionRequest(goCtx context.Context, distribution daotypes.DistributionOrder) {
|
||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
||||
sendingValidatorAddress := config.GetConfig().GetValidatorAddress()
|
||||
msg := daotypes.NewMsgDistributionRequest(sendingValidatorAddress, &distribution)
|
||||
loggingContext := "distribution request"
|
||||
buildSignBroadcastTx(goCtx, loggingContext, sendingValidatorAddress, msg)
|
||||
@ -74,35 +74,35 @@ func SendDistributionRequest(goCtx context.Context, distribution daotypes.Distri
|
||||
|
||||
func SendDistributionResult(goCtx context.Context, lastPoP int64, daoTxID string, invTxID string,
|
||||
popTxID string, earlyInvestorTxID string, strategicTxID string) {
|
||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
||||
sendingValidatorAddress := config.GetConfig().GetValidatorAddress()
|
||||
msg := daotypes.NewMsgDistributionResult(sendingValidatorAddress, lastPoP, daoTxID, invTxID, popTxID, earlyInvestorTxID, strategicTxID)
|
||||
loggingContext := "distribution result"
|
||||
buildSignBroadcastTx(goCtx, loggingContext, sendingValidatorAddress, msg)
|
||||
}
|
||||
|
||||
func SendLiquidAssetRegistration(goCtx context.Context, notarizedAsset machinetypes.LiquidAsset) {
|
||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
||||
sendingValidatorAddress := config.GetConfig().GetValidatorAddress()
|
||||
msg := machinetypes.NewMsgNotarizeLiquidAsset(sendingValidatorAddress, ¬arizedAsset)
|
||||
loggingContext := "notarize liquid asset"
|
||||
buildSignBroadcastTx(goCtx, loggingContext, sendingValidatorAddress, msg)
|
||||
}
|
||||
|
||||
func SendInitPoP(goCtx context.Context, challenger string, challengee string, blockHeight int64) {
|
||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
||||
sendingValidatorAddress := config.GetConfig().GetValidatorAddress()
|
||||
msg := daotypes.NewMsgInitPop(sendingValidatorAddress, sendingValidatorAddress, challenger, challengee, blockHeight)
|
||||
loggingContext := "PoP"
|
||||
buildSignBroadcastTx(goCtx, loggingContext, sendingValidatorAddress, msg)
|
||||
}
|
||||
|
||||
func SendUpdateRedeemClaim(goCtx context.Context, beneficiary string, id uint64, txID string) {
|
||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
||||
sendingValidatorAddress := config.GetConfig().GetValidatorAddress()
|
||||
msg := daotypes.NewMsgUpdateRedeemClaim(sendingValidatorAddress, beneficiary, txID, id)
|
||||
loggingContext := "redeem claim"
|
||||
buildSignBroadcastTx(goCtx, loggingContext, sendingValidatorAddress, msg)
|
||||
}
|
||||
|
||||
func SendTokens(goCtx context.Context, beneficiary sdk.AccAddress, amount uint64, denominator string) {
|
||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
||||
sendingValidatorAddress := config.GetConfig().GetValidatorAddress()
|
||||
|
||||
coin := sdk.NewCoin(denominator, sdk.NewIntFromUint64(amount))
|
||||
coins := sdk.NewCoins(coin)
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/planetmint/planetmint-go/clients"
|
||||
"github.com/planetmint/planetmint-go/config"
|
||||
"github.com/planetmint/planetmint-go/testutil/keeper"
|
||||
clientmocks "github.com/planetmint/planetmint-go/testutil/mocks"
|
||||
"github.com/planetmint/planetmint-go/testutil/moduleobject"
|
||||
@ -50,6 +51,7 @@ func TestRegisterNFT(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMachineNFTIssuance(t *testing.T) {
|
||||
t.Setenv(config.ValAddr, "plmnt10mq5nj8jhh27z7ejnz2ql3nh0qhzjnfvy50877")
|
||||
ctrl := gomock.NewController(t)
|
||||
elements.Client = &elementsmocks.MockClient{}
|
||||
shamirMock := clientmocks.NewMockIShamirCoordinatorClient(ctrl)
|
||||
|
@ -49,7 +49,7 @@ func LazyLoadMQTTClient() {
|
||||
}
|
||||
|
||||
opts := mqtt.NewClientOptions().AddBroker(uri)
|
||||
opts.SetClientID(conf.ValidatorAddress)
|
||||
opts.SetClientID(conf.GetValidatorAddress())
|
||||
opts.SetUsername(conf.MqttUser)
|
||||
opts.SetPassword(conf.MqttPassword)
|
||||
if conf.MqttTLS {
|
||||
|
Loading…
x
Reference in New Issue
Block a user