regen-network / regen-ledger

:seedling: Blockchain for planetary regeneration
https://docs.regen.network
Other
214 stars 103 forks source link

Simplified content hashes JSON for register resolver command #1276

Open ryanchristo opened 2 years ago

ryanchristo commented 2 years ago

Summary

When registering a resolver with the command-line, the content hashes JSON input is currently:

{
  "content_hashes": [
    {
      "graph": {
        "hash": "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY=",
        "digest_algorithm": "DIGEST_ALGORITHM_BLAKE2B_256",
        "canonicalization_algorithm": "GRAPH_CANONICALIZATION_ALGORITHM_URDNA2015",
        "merkle_tree": "GRAPH_MERKLE_TREE_NONE_UNSPECIFIED"
      }
    }
  ]
}

Problem Definition

The content hashes JSON input could be simplified.

Proposal

[
  {
    "graph": {
      "hash": "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY=",
      "digest_algorithm": "DIGEST_ALGORITHM_BLAKE2B_256",
      "canonicalization_algorithm": "GRAPH_CANONICALIZATION_ALGORITHM_URDNA2015",
      "merkle_tree": "GRAPH_MERKLE_TREE_NONE_UNSPECIFIED"
    }
  }
]

For Admin Use

wgwz commented 1 year ago

hey @ryanchristo 👋 i'm interested in working on this one, i'm going to dig in a bit and i will probably reach out with some q's next week

wgwz commented 1 year ago

hey @ryanchristo i could use some guidance on this task when you have some time. here's how i'm trying to solve it so far:

diff --git a/x/data/client/tx.go b/x/data/client/tx.go
index 306cda0b..ce8ca222 100644
--- a/x/data/client/tx.go
+++ b/x/data/client/tx.go
@@ -225,8 +225,6 @@ Flags:
 }

 func parseContentHashes(clientCtx client.Context, filePath string) ([]*data.ContentHash, error) {
-   contentHashes := data.ContentHashes{}
-
    if filePath == "" {
        return nil, fmt.Errorf("file path is empty")
    }
@@ -236,9 +234,11 @@ func parseContentHashes(clientCtx client.Context, filePath string) ([]*data.Cont
        return nil, err
    }

-   if err := clientCtx.Codec.UnmarshalJSON(bz, &contentHashes); err != nil {
+   var hashes []*data.ContentHash
+   if err := clientCtx.Codec.UnmarshalJSON(bz, &hashes); err != nil {
        return nil, err
    }
+   contentHashes := data.ContentHashes{hashes}

    return contentHashes.ContentHashes, nil
 }

i'm attempting to unmarshal the input data into a list of ContentHash's. And then pass these into the ContentHashes struct in order to minimize change. But I'm not sure it's heading in the right direction. I'm seeing this error when compiling (which I have not been able to make sense out):

$ make install                                                                                                                              
Verifying go version...
go install -mod=readonly -tags 'netgo ledger goleveldb' -ldflags '-X github.com/cosmos/cosmos-sdk/version.Name=regen -X github.com/cosmos/cosmos-sdk/version.AppName=regen -X github.com/cosmos/cosmos-sdk/version.Version=master-425690c5f70a0559d1740f1a532854ff21d35cb0 -X github.com/cosmos/cosmos-sdk/version.Commit=425690c5f70a0559d1740f1a532854ff21d35cb0 -X github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,ledger,goleveldb -X github.com/cosmos/cosmos-sdk/types.DBBackend=goleveldb -X github.com/tendermint/tendermint/version.TMCoreSemVer=v0.34.20' /Users/kyle/regen/regen-ledger/app/regen
# github.com/regen-network/regen-ledger/x/data/client
x/data/client/tx.go:238:46: cannot use &hashes (value of type *[]*data.ContentHash) as type "github.com/gogo/protobuf/proto".Message in argument to clientCtx.Codec.UnmarshalJSON:
        *[]*data.ContentHash does not implement "github.com/gogo/protobuf/proto".Message (missing ProtoMessage method)
make: *** [install] Error 2

I also tried using the json.Unmarshal instead of clientCtx.Codec.UnmarshalJSON just for the sake of trying a different approach:

diff --git a/x/data/client/tx.go b/x/data/client/tx.go
index 306cda0b..088aa663 100644
--- a/x/data/client/tx.go
+++ b/x/data/client/tx.go
@@ -1,6 +1,7 @@
 package client

 import (
+   "encoding/json"
    "fmt"
    "io/ioutil"
    "net/url"
@@ -225,8 +226,6 @@ Flags:
 }

 func parseContentHashes(clientCtx client.Context, filePath string) ([]*data.ContentHash, error) {
-   contentHashes := data.ContentHashes{}
-
    if filePath == "" {
        return nil, fmt.Errorf("file path is empty")
    }
@@ -236,9 +235,11 @@ func parseContentHashes(clientCtx client.Context, filePath string) ([]*data.Cont
        return nil, err
    }

-   if err := clientCtx.Codec.UnmarshalJSON(bz, &contentHashes); err != nil {
+   var hashes []*data.ContentHash
+   if err := json.Unmarshal(bz, &hashes); err != nil {
        return nil, err
    }
+   contentHashes := data.ContentHashes{hashes}

    return contentHashes.ContentHashes, nil
 }

Using json.Unmarshal actually compiles but i am getting this error at run time:

$ regen tx data register-resolver 1 content1.json --from regen1xj24leylcxkcnrkh5ztfg0etuakwgjs504esst --keyring-backend test --chain-id test
Error: json: cannot unmarshal string into Go struct field ContentHash_Graph.graph.digest_algorithm of type data.DigestAlgorithm

where content1.json is the simpler version:

 [
  {
    "graph": {
      "hash": "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY=",
      "digest_algorithm": "DIGEST_ALGORITHM_BLAKE2B_256",
      "canonicalization_algorithm": "GRAPH_CANONICALIZATION_ALGORITHM_URDNA2015",
      "merkle_tree": "GRAPH_MERKLE_TREE_NONE_UNSPECIFIED"
    }
  }
]

I'm probably doing something dumb, if you think it'll be easier to pair on this for a short time, that'd be great. Thanks!