From 01ef2dbfd058edfbf39e513e4e944cf65e07cde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Fri, 4 Aug 2023 16:38:18 +0200 Subject: [PATCH] Improve communication to liquid issuance service (#58) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added OSC response listener with logging added Machine NFT issuance for each machine attestation process added CID and Planetmint Issuer extPublicKey to the issuance process removed type inconsistency * added machine NFT creation criteria * [toml] Parsing does not allow sub-structs * made OSC listener port configurable Signed-off-by: Jürgen Eckel Signed-off-by: Julian Strobl Co-authored-by: Julian Strobl --- app/app.go | 1 + cmd/planetmint-god/cmd/root.go | 2 +- config/config.go | 32 ++++++++----------- go.mod | 1 + go.sum | 2 ++ x/machine/keeper/issue_response.go | 29 +++++++++++++++++ x/machine/keeper/msg_server_attest_machine.go | 30 +++++++++++------ 7 files changed, 68 insertions(+), 29 deletions(-) create mode 100644 x/machine/keeper/issue_response.go diff --git a/app/app.go b/app/app.go index 3f3a4f5..27cfef0 100644 --- a/app/app.go +++ b/app/app.go @@ -537,6 +537,7 @@ func New( app.GetSubspace(machinemoduletypes.ModuleName), ) machineModule := machinemodule.NewAppModule(appCodec, app.MachineKeeper, app.AccountKeeper, app.BankKeeper) + go app.MachineKeeper.IssueResponseHandler(logger) app.AssetKeeper = *assetmodulekeeper.NewKeeper( appCodec, diff --git a/cmd/planetmint-god/cmd/root.go b/cmd/planetmint-god/cmd/root.go index f583571..ebbc481 100644 --- a/cmd/planetmint-god/cmd/root.go +++ b/cmd/planetmint-god/cmd/root.go @@ -238,7 +238,7 @@ func (a appCreator) newApp( // Get [planetmint] section from app.toml plmntConfig := planetmintconfig.GetConfig() - plmntConfig.SetWatchmenConfig(appOpts.Get("planetmint")) + plmntConfig.SetPlanetmintConfig(appOpts.Get("planetmint")) if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) { cache = store.NewCommitKVStoreCacheManager() diff --git a/config/config.go b/config/config.go index 11c04f9..a61beaf 100644 --- a/config/config.go +++ b/config/config.go @@ -2,7 +2,6 @@ package app import ( "encoding/json" - "fmt" "sync" ) @@ -12,19 +11,16 @@ const DefaultConfigTemplate = ` ############################################################################### [planetmint] -watchmen-endpoint = "{{ .PlmntConfig.WatchmenConfig.Endpoint }}" -watchmen-port = {{ .PlmntConfig.WatchmenConfig.Port }} +osc-service-port = {{ .PlmntConfig.OSCServicePort }} +watchmen-endpoint = "{{ .PlmntConfig.WatchmenEndpoint }}" +watchmen-port = {{ .PlmntConfig.WatchmenPort }} ` // Config defines Planetmint's top level configuration type Config struct { - WatchmenConfig WatchmenConfig `mapstructure:"watchmen-config" json:"watchmen-config"` -} - -// WatchmenConfig defines Planetmint's watchmen configuration -type WatchmenConfig struct { - Endpoint string `mapstructure:"watchmen-endpoint" json:"watchmen-endpoint"` - Port int `mapstructure:"watchmen-port" json:"watchmen-port"` + OSCServicePort int `mapstructure:"osc-service-port" json:"osc-service-port"` + WatchmenEndpoint string `mapstructure:"watchmen-endpoint" json:"watchmen-endpoint"` + WatchmenPort int `mapstructure:"watchmen-port" json:"watchmen-port"` } // cosmos-sdk wide global singleton @@ -36,10 +32,9 @@ var ( // DefaultConfig returns planetmint's default configuration. func DefaultConfig() *Config { return &Config{ - WatchmenConfig: WatchmenConfig{ - Endpoint: "localhost", - Port: 7401, - }, + OSCServicePort: 8766, + WatchmenEndpoint: "lab.r3c.network", + WatchmenPort: 7401, } } @@ -51,15 +46,14 @@ func GetConfig() *Config { return plmntConfig } -// SetWatchmenConfig sets Planetmint's watchmen configuration -func (config *Config) SetWatchmenConfig(watchmenConfig interface{}) { - jsonWatchmenConfig, err := json.Marshal(watchmenConfig) +// SetWatchmenConfig sets Planetmint's configuration +func (config *Config) SetPlanetmintConfig(planetmintconfig interface{}) { + jsonConfig, err := json.Marshal(planetmintconfig) if err != nil { panic(err) } - err = json.Unmarshal(jsonWatchmenConfig, &config.WatchmenConfig) + err = json.Unmarshal(jsonConfig, &config) if err != nil { panic(err) } - fmt.Printf("%+v\n", config.WatchmenConfig.Port) } diff --git a/go.mod b/go.mod index bcaba53..0c0c443 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,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/hypebeast/go-osc v0.0.0-20220308234300-cec5a8a1e5f5 github.com/spf13/cast v1.5.0 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 8af7b56..f5641a2 100644 --- a/go.sum +++ b/go.sum @@ -669,6 +669,8 @@ github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0Jr github.com/huandu/skiplist v1.2.0 h1:gox56QD77HzSC0w+Ws3MH3iie755GBJU1OER3h5VsYw= github.com/huandu/skiplist v1.2.0/go.mod h1:7v3iFjLcSAzO4fN5B8dvebvo/qsfumiLiDXMrPiHF9w= github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= +github.com/hypebeast/go-osc v0.0.0-20220308234300-cec5a8a1e5f5 h1:fqwINudmUrvGCuw+e3tedZ2UJ0hklSw6t8UPomctKyQ= +github.com/hypebeast/go-osc v0.0.0-20220308234300-cec5a8a1e5f5/go.mod h1:lqMjoCs0y0GoRRujSPZRBaGb4c5ER6TfkFKSClxkMbY= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/improbable-eng/grpc-web v0.15.0 h1:BN+7z6uNXZ1tQGcNAuaU1YjsLTApzkjt2tzCixLaUPQ= diff --git a/x/machine/keeper/issue_response.go b/x/machine/keeper/issue_response.go new file mode 100644 index 0000000..398c7cd --- /dev/null +++ b/x/machine/keeper/issue_response.go @@ -0,0 +1,29 @@ +package keeper + +import ( + config "planetmint-go/config" + "strconv" + + "github.com/cometbft/cometbft/libs/log" + "github.com/hypebeast/go-osc/osc" +) + +func (k Keeper) IssueResponseHandler(logger log.Logger) { + conf := config.GetConfig() + addr := "0.0.0.0:" + strconv.FormatInt(int64(conf.OSCServicePort), 10) + d := osc.NewStandardDispatcher() + err := d.AddMsgHandler("/rddl/resp", func(msg *osc.Message) { + logger.Info("Issue Response: " + msg.String()) + }) + if err != nil { + logger.Error("Unable to add handler to OSC service.") + } + server := &osc.Server{ + Addr: addr, + Dispatcher: d, + } + err = server.ListenAndServe() + if err != nil { + logger.Error("Unable to start the OSC service.") + } +} diff --git a/x/machine/keeper/msg_server_attest_machine.go b/x/machine/keeper/msg_server_attest_machine.go index e809894..a8ed24d 100644 --- a/x/machine/keeper/msg_server_attest_machine.go +++ b/x/machine/keeper/msg_server_attest_machine.go @@ -3,6 +3,7 @@ package keeper import ( "context" "errors" + "strconv" config "planetmint-go/config" "planetmint-go/x/machine/types" @@ -14,6 +15,12 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +func (k msgServer) isNFTCreationRequest(machine *types.Machine) bool { + if !machine.GetReissue() && machine.GetAmount() == 1 && machine.GetPrecision() == 8 { + return true + } + return false +} func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMachine) (*types.MsgAttestMachineResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) @@ -21,11 +28,10 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach if !isValidIssuerLiquid { return nil, errors.New("invalid liquid key") } - - if msg.Machine.Reissue { - err := k.reissueMachine(msg.Machine) + if k.isNFTCreationRequest(msg.Machine) { + err := k.issueMachineNFT(msg.Machine) if err != nil { - return nil, errors.New("an error occured while reissuning the machine") + return nil, errors.New("an error occurred while issuing the machine NFT") } } @@ -44,16 +50,22 @@ func validateIssuerLiquid(issuerLiquid string) bool { return isValidLiquidKey } -func (k msgServer) reissueMachine(machine *types.Machine) error { +func (k msgServer) issueMachineNFT(machine *types.Machine) error { conf := config.GetConfig() - client := osc.NewClient(conf.WatchmenConfig.Endpoint, conf.WatchmenConfig.Port) - msg := osc.NewMessage("/rddl/*") + client := osc.NewClient(conf.WatchmenEndpoint, conf.WatchmenPort) + machine_precision := strconv.FormatInt(int64(machine.Precision), 10) + machine_amount := strconv.FormatInt(int64(machine.Amount), 10) + + msg := osc.NewMessage("/rddl/issue") msg.Append(machine.Name) msg.Append(machine.Ticker) msg.Append(machine.Domain) - msg.Append(int32(machine.Amount)) + msg.Append(machine_amount) msg.Append("1") - msg.Append(int32(machine.Precision)) + msg.Append(machine_precision) + msg.Append(machine.Metadata.GetAdditionalDataCID()) + msg.Append(machine.GetIssuerPlanetmint()) err := client.Send(msg) + return err }