mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00
Improve communication to liquid issuance service (#58)
* 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 <juergen@riddleandcode.com> Signed-off-by: Julian Strobl <jmastr@mailbox.org> Co-authored-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
parent
70b61372d2
commit
01ef2dbfd0
@ -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,
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
1
go.mod
1
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
|
||||
|
2
go.sum
2
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=
|
||||
|
29
x/machine/keeper/issue_response.go
Normal file
29
x/machine/keeper/issue_response.go
Normal file
@ -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.")
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user