Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to store assets on the chain as json objects to enable rich queries. #31

Open
Nova38 opened this issue Apr 12, 2023 · 7 comments

Comments

@Nova38
Copy link

Nova38 commented Apr 12, 2023

Is there a way to set the router so that using the state mappings I could have the object defined in the .proto stored on the chain as a Json object/struct instead of a binary protobuf? I have already set the router option WithSerializer(serialize.PreferJSONSerializer). The reason this would be useful is it would enable the ability to use CouchDB to be used in order to use rich queries.

@Nova38
Copy link
Author

Nova38 commented Apr 12, 2023

I have also tried useing both defparm and parma.struct for the arguments to be defined

@Nova38
Copy link
Author

Nova38 commented Apr 13, 2023

I have created a middleware function to set the states to use the serialize.PreferJSONSerializer.

func SetStateJSON(next router.HandlerFunc, pos ...int) router.HandlerFunc {
	return func(c router.Context) (interface{}, error) {
		c.State().UseSerializer(serialize.PreferJSONSerializer)
		return next(c)
	}
}

It is working for submitting new assets and retrieving them one at a time, but the get list keeps getting a panic with the error `"reflect: call of unknown method on zero Value"

"reflect: call of unknown method on zero Value"
Stack:
	 2  0x000000000071174f in reflect.flag.mustBe
	     at C:/Users/devcs/scoop/apps/go/current/src/reflect/value.go:221
	 3  0x000000000071f277 in reflect.Append
	     at C:/Users/devcs/scoop/apps/go/current/src/reflect/value.go:2736
	 4  0x0000000000e587fd in github.com/hyperledger-labs/cckit/state.(*StateList).Get
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/state/state_list.go:61
	 5  0x0000000000e57f9e in github.com/hyperledger-labs/cckit/state.(*StateList).Fill
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/state/state_list.go:50
	 6  0x0000000000e50ea5 in github.com/hyperledger-labs/cckit/state.(*Impl).List
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/state/state.go:233
	 7  0x0000000000e6541e in github.com/hyperledger-labs/cckit/state/mapping.(*Impl).List
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/state/mapping/state.go:185
	 8  0x0000000000e7bc74 in github.com/nova38/biochain/fabric/biochain/cc_func.QueryUsers
	     at D:/projects/BioChain/fabric/biochain/cc_func/fn_user.go:15
	 9  0x0000000000e5ef76 in github.com/hyperledger-labs/cckit/router.(*Group).addHandler.func1
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:208
	10  0x0000000000e7d0f8 in github.com/nova38/biochain/fabric/biochain.SetStateJSON.func1
	     at D:/projects/BioChain/fabric/biochain/chaincode.go:17
	11  0x0000000000e62f7e in github.com/hyperledger-labs/cckit/state/mapping.MapEvents.func1.1
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/state/mapping/middleware.go:20
	12  0x0000000000e62c3e in github.com/hyperledger-labs/cckit/state/mapping.MapStates.func1.1
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/state/mapping/middleware.go:11
	13  0x0000000000e5dcb1 in github.com/hyperledger-labs/cckit/router.(*Group).handleContext.func2
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:138
	14  0x0000000000e5d59a in github.com/hyperledger-labs/cckit/router.(*Group).handleContext
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:141
	15  0x0000000000e5f92d in github.com/hyperledger-labs/cckit/router.(*Group).handleContext-fm
	     at <autogenerated>:1
	16  0x0000000000e5c60e in github.com/hyperledger-labs/cckit/router.(*Group).buildHandler.func1
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:80
	17  0x0000000000e5cbd7 in github.com/hyperledger-labs/cckit/router.(*Group).Handle
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:102
	18  0x0000000000e59605 in github.com/hyperledger-labs/cckit/router.(*Chaincode).Invoke
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/chaincode.go:29
	19  0x0000000000d12f02 in github.com/hyperledger/fabric-chaincode-go/shim.(*Handler).handleTransaction
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger/[email protected]/shim/handler.go:209
	20  0x0000000000d28605 in github.com/hyperledger/fabric-chaincode-go/shim.(*Handler).handleTransaction-fm
	     at <autogenerated>:1
	21  0x0000000000d11d9c in github.com/hyperledger/fabric-chaincode-go/shim.(*Handler).handleStubInteraction
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger/[email protected]/shim/handler.go:159
	22  0x0000000000d1d0b5 in github.com/hyperledger/fabric-chaincode-go/shim.(*Handler).handleReady.func2
	     at C:/Users/devcs/go/pkg/mod/github.com/hyperledger/[email protected]/shim/handler.go:620

@Nova38
Copy link
Author

Nova38 commented Apr 13, 2023

It seems like the problem is a arising here in the first if statment in the state_list.go get function.
if _, isListProto := sl.listTarget.(proto.Message); isListProto {

I got it to not error out when i removed the list maping (m.List(&schema.UserList{}), ) from my state mappings. That let me access the else statement which didn't run into the same issue.

@vitiko
Copy link
Contributor

vitiko commented Apr 19, 2023

@Nova38
Copy link
Author

Nova38 commented Apr 23, 2023

When I try to get the list of the users using that method I ended up only being able to populate the list as the IDs otherwise it errors out.

Works:

func QueryUsers(c router.Context) (interface{}, error) {
	return c.State().List(&schema.UserId{})
}

Errors out

func QueryUsers(c router.Context) (interface{}, error) {
	return c.State().List(&schema.User{})
}
Traceback
github.com/nova38/biochain/fabric/biochain/cc_func.QueryUsers({0x10bbfc0, 0xc0000e4af0})
	D:/projects/BioChain/fabric/biochain/cc_func/users.go:66 +0xf4
github.com/hyperledger-labs/cckit/router.(*Group).addHandler.func1({0x10bbfc0, 0xc0000e4af0})
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:208 +0x156
github.com/hyperledger-labs/cckit/state/mapping.MapEvents.func1.1({0x10bbfc0, 0xc0000e4af0})
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/state/mapping/middleware.go:20 +0xde
github.com/hyperledger-labs/cckit/state/mapping.MapStates.func1.1({0x10bbfc0, 0xc0000e4af0})
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/state/mapping/middleware.go:11 +0xde
github.com/hyperledger-labs/cckit/router.(*Group).handleContext.func2({0x10bbfc0, 0xc0000e4af0})
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:138 +0x231
github.com/hyperledger-labs/cckit/router.(*Group).handleContext(0xc000000000, {0x10bbfc0, 0xc0000e4af0})
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:141 +0x93a
github.com/hyperledger-labs/cckit/router.(*Group).buildHandler.func1({0x10bbfc0, 0xc0000e4af0})
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:80 +0x1ae
github.com/hyperledger-labs/cckit/router.(*Group).Handle(0xc000000000, {0x10bd8e8, 0xc0002d5550})
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/router.go:102 +0x197
github.com/hyperledger-labs/cckit/router.(*Chaincode).Invoke(0xc00000a680, {0x10bd8e8, 0xc0002d5550})
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger-labs/[email protected]/router/chaincode.go:29 +0x85
github.com/hyperledger/fabric-chaincode-go/shim.(*Handler).handleTransaction(0xc0005983c0, 0xc0005a4a80)
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger/[email protected]/shim/handler.go:209 +0x4a2
github.com/hyperledger/fabric-chaincode-go/shim.(*Handler).handleStubInteraction(0xc0005983c0, 0xc0004982a0, 0xc0005a4a80, 0xc000594960)
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger/[email protected]/shim/handler.go:159 +0x5c
created by github.com/hyperledger/fabric-chaincode-go/shim.(*Handler).handleReady
	C:/Users/devcs/go/pkg/mod/github.com/hyperledger/[email protected]/shim/handler.go:620 +0x37f
message User {
  string msp_id = 1;
  string id = 3;
  string name = 2;
  string affiliation = 4;
  repeated Membership memberships = 5;
}
message UserId {
  string msp_id = 1;
  string id = 2;
}
message UserList {
  repeated User users = 1;
}
{
  "items": [
    {
      "@type": "type.googleapis.com/schema.UserId",
      "msp_id": "Org1MSP",
      "id": "org-admin"
    },
    {
      "@type": "type.googleapis.com/schema.UserId",
      "msp_id": "Org1MSP",
      "id": "user1"
    }
  ]
}

@Nova38
Copy link
Author

Nova38 commented Apr 23, 2023

I am able to get the users full value by using the state().get() instead of having to declare an additional unique key set.

@Nova38
Copy link
Author

Nova38 commented Apr 23, 2023

It doesn't hit that error if I leave off the mapping.List(&schema.UserList{}) from the statemapping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants