mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-06 22:26:43 +00:00
hotfix: inconsistent logging of block-proposer results on the chain. … (#153)
* hotfix: inconsistent logging of block-proposer results on the chain. These results need to be consensed via an explicit message instead of an error output. Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
126d59cff9
commit
c6909a3e4a
@ -3,12 +3,8 @@ package keeper
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
config "github.com/planetmint/planetmint-go/config"
|
||||
@ -50,10 +46,11 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach
|
||||
}
|
||||
|
||||
if k.isNFTCreationRequest(msg.Machine) && util.IsValidatorBlockProposer(ctx, ctx.BlockHeader().ProposerAddress) {
|
||||
err := k.issueMachineNFT(msg.Machine)
|
||||
if err != nil {
|
||||
return nil, types.ErrNFTIssuanceFailed
|
||||
}
|
||||
_ = k.issueMachineNFT(msg.Machine)
|
||||
//TODO create NFTCreationMessage to be stored by all nodes
|
||||
// if err != nil {
|
||||
// return nil, types.ErrNFTIssuanceFailed
|
||||
// }
|
||||
}
|
||||
|
||||
if msg.Machine.GetType() == 0 { // 0 == RDDL_MACHINE_UNDEFINED
|
||||
@ -98,7 +95,7 @@ func (k msgServer) issueNFTAsset(name string, machine_address string) (asset_id
|
||||
// Execute the command
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
log.Fatalf("cmd.Run() failed with %s\n", err)
|
||||
log.Printf("cmd.Run() failed with %s\n", err)
|
||||
err = errorsmod.Wrap(types.ErrMachineNFTIssuance, stderr.String())
|
||||
} else {
|
||||
lines := strings.Split(stdout.String(), "\n")
|
||||
@ -111,58 +108,14 @@ func (k msgServer) issueNFTAsset(name string, machine_address string) (asset_id
|
||||
}
|
||||
return asset_id, contract, err
|
||||
}
|
||||
func (k msgServer) registerAsset(asset_id string, contract string) error {
|
||||
|
||||
conf := config.GetConfig()
|
||||
|
||||
// Create your request payload
|
||||
data := map[string]interface{}{
|
||||
"asset_id": asset_id,
|
||||
"contract": contract,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return errorsmod.Wrap(types.ErrAssetRegistryReqFailure, "Marshall "+err.Error())
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", conf.AssetRegistryEndpoint, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return errorsmod.Wrap(types.ErrAssetRegistryReqFailure, "Request creation: "+err.Error())
|
||||
}
|
||||
|
||||
// Set headers
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("accept", "application/json")
|
||||
|
||||
// Send request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return errorsmod.Wrap(types.ErrAssetRegistryReqSending, err.Error())
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Read response
|
||||
if resp.StatusCode > 299 {
|
||||
return errorsmod.Wrap(types.ErrAssetRegistryRepsonse, "Error reading response body:"+strconv.Itoa(resp.StatusCode))
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errorsmod.Wrap(types.ErrAssetRegistryRepsonse, "Error reading response body:"+err.Error())
|
||||
}
|
||||
result_obj := string(body)
|
||||
if strings.Contains(result_obj, asset_id) {
|
||||
return nil
|
||||
} else {
|
||||
return errorsmod.Wrap(types.ErrAssetRegistryRepsonse, "does not confirm asset registration")
|
||||
}
|
||||
}
|
||||
|
||||
func (k msgServer) issueMachineNFT(machine *types.Machine) error {
|
||||
asset_id, contract, err := k.issueNFTAsset(machine.Name, machine.Address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return k.registerAsset(asset_id, contract)
|
||||
_, _, err := k.issueNFTAsset(machine.Name, machine.Address)
|
||||
return err
|
||||
// asset registration is not performed in case of NFT issuance for machines
|
||||
//asset_id, contract, err := k.issueNFTAsset(machine.Name, machine.Address)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//return k.registerAsset(asset_id, contract)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user