From e5bc29f888b42ab36b703de8c37b6ed20012293b Mon Sep 17 00:00:00 2001 From: david Date: Thu, 1 Feb 2024 14:09:42 +0800 Subject: [PATCH] move legacy to classic package --- cid/cid.go | 19 +++++++------------ cid/cid_test.go | 23 +++++++++++++++++++++++ mock.go => classic/args.go | 2 +- classic.go => classic/response.go | 12 +++++++++++- struct_test.go => classic/struct_test.go | 4 ++-- fabric.go | 3 ++- fabric_test.go | 12 +++++++----- go.mod | 5 +++-- go.sum | 12 +++++++----- shim.go | 4 ++-- struct.go | 10 ---------- 11 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 cid/cid_test.go rename mock.go => classic/args.go (95%) rename classic.go => classic/response.go (63%) rename struct_test.go => classic/struct_test.go (79%) diff --git a/cid/cid.go b/cid/cid.go index a1c23cc..2542b12 100644 --- a/cid/cid.go +++ b/cid/cid.go @@ -3,25 +3,20 @@ package cid import ( "crypto/x509" "fmt" + "github.com/davidkhala/fabric-common/golang/format" . "github.com/davidkhala/goutils" - . "github.com/davidkhala/goutils/crypto" + "github.com/davidkhala/goutils/crypto" "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric-chaincode-go/pkg/attrmgr" "github.com/hyperledger/fabric-chaincode-go/shim" "github.com/hyperledger/fabric-protos-go/msp" ) -// MSPID Hyperledger Fabric Member Service Provider ID -type MSPID = string - -// CertificateID a unique ID associated with the x509 certificate identity -type CertificateID = string - // ClientIdentity alternative of creator starting from 1.1 type ClientIdentity struct { - MspID MSPID + MspID format.MSPID CertificatePem string - Attrs map[string]string `json:"attrs"` + Attrs map[string]string `json:"Attrs,omitempty"` } func NewClientIdentity(stub shim.ChaincodeStubInterface) (c ClientIdentity) { @@ -47,15 +42,15 @@ func (c ClientIdentity) GetAttributeValue(attrName string) string { } // GetID returns a unique ID associated with the invoking identity. -func (c ClientIdentity) GetID() CertificateID { +func (c ClientIdentity) GetID() format.CertificateID { // The leading "x509::" distinguishes this as an X509 certificate, and // the subject and issuer DNs uniquely identify the X509 certificate. // The resulting ID will remain the same if the certificate is renewed. var certificate = c.GetCertificate() - id := fmt.Sprintf("x509::%s::%s", GetDN(certificate.Subject), GetDN(certificate.Issuer)) + id := fmt.Sprintf("x509::%s::%s", crypto.GetDN(certificate.Subject), crypto.GetDN(certificate.Issuer)) return id } func (c ClientIdentity) GetCertificate() *x509.Certificate { - return ParseCertPemOrPanic([]byte(c.CertificatePem)) + return crypto.ParseCertPemOrPanic([]byte(c.CertificatePem)) } diff --git a/cid/cid_test.go b/cid/cid_test.go new file mode 100644 index 0000000..c9d517e --- /dev/null +++ b/cid/cid_test.go @@ -0,0 +1,23 @@ +package cid + +import ( + "github.com/davidkhala/goutils" + "github.com/stretchr/testify/assert" + "strings" + "testing" +) + +func TestSyntax(t *testing.T) { + t.Run("cid to json", func(t *testing.T) { + var id_nil = ClientIdentity{ + MspID: "msp", + CertificatePem: "cert", + Attrs: nil, + } + assert.False(t, strings.Contains(string(goutils.ToJson(id_nil)), "Attrs")) + id_nil.Attrs = map[string]string{} + assert.False(t, strings.Contains(string(goutils.ToJson(id_nil)), "Attrs")) + id_nil.Attrs["foo"] = "bar" + assert.True(t, strings.Contains(string(goutils.ToJson(id_nil)), "Attrs")) + }) +} diff --git a/mock.go b/classic/args.go similarity index 95% rename from mock.go rename to classic/args.go index 45b7946..79a8163 100644 --- a/mock.go +++ b/classic/args.go @@ -1,4 +1,4 @@ -package golang +package classic type Args struct { buff [][]byte diff --git a/classic.go b/classic/response.go similarity index 63% rename from classic.go rename to classic/response.go index 773b3ff..6f62827 100644 --- a/classic.go +++ b/classic/response.go @@ -1,4 +1,4 @@ -package golang +package classic import ( "fmt" @@ -28,3 +28,13 @@ var DeferHandlerPeerResponse = func(errString string, params ...interface{}) boo debug.PrintStack() return true } + +// PeerResponse a readable structure of peer.response +type PeerResponse struct { + // A status code that should follow the HTTP status codes. + Status int32 `json:"status,omitempty"` + // A message associated with the response code. + Message string `json:"message,omitempty"` + // A payload that can be used to include metadata with this response. + Payload string `json:"payload,omitempty"` +} diff --git a/struct_test.go b/classic/struct_test.go similarity index 79% rename from struct_test.go rename to classic/struct_test.go index d785b92..b85d136 100644 --- a/struct_test.go +++ b/classic/struct_test.go @@ -1,11 +1,11 @@ -package golang +package classic import ( "fmt" "testing" ) -func TestArgsBuilder_Constructor(t *testing.T) { +func TestArgsBuilder(t *testing.T) { var args = ArgsBuilder("abc") fmt.Println("args", args) args.AppendArg("cde") diff --git a/fabric.go b/fabric.go index 24039da..5641fc6 100644 --- a/fabric.go +++ b/fabric.go @@ -1,6 +1,7 @@ package golang import ( + "github.com/davidkhala/fabric-common-chaincode-golang/classic" . "github.com/davidkhala/goutils" "github.com/golang/protobuf/ptypes/timestamp" "github.com/hyperledger/fabric-chaincode-go/shim" @@ -9,7 +10,7 @@ import ( func (cc CommonChaincode) InvokeChaincode(chaincodeName string, args [][]byte, channel string) peer.Response { var resp = cc.CCAPI.InvokeChaincode(chaincodeName, args, channel) - PanicPeerResponse(resp) + classic.PanicPeerResponse(resp) return resp } diff --git a/fabric_test.go b/fabric_test.go index 6f8b54c..8bdaaff 100644 --- a/fabric_test.go +++ b/fabric_test.go @@ -2,6 +2,7 @@ package golang import ( "github.com/davidkhala/fabric-common-chaincode-golang/cid" + "github.com/davidkhala/fabric-common-chaincode-golang/classic" . "github.com/davidkhala/goutils" "github.com/hyperledger/fabric-chaincode-go/shim" "github.com/hyperledger/fabric-chaincode-go/shimtest" @@ -28,7 +29,7 @@ func (t *TestChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response { // Transaction makes payment of X units from A to B func (t *TestChaincode) Invoke(stub shim.ChaincodeStubInterface) (response peer.Response) { logger.Info("Invoke") - defer Deferred(DeferHandlerPeerResponse, &response) + defer Deferred(classic.DeferHandlerPeerResponse, &response) var fcn, _ = stub.GetFunctionAndParameters() var responseBytes []byte switch fcn { @@ -43,13 +44,13 @@ func (t *TestChaincode) Invoke(stub shim.ChaincodeStubInterface) (response peer. var cc = new(TestChaincode) var mock = shimtest.NewMockStub(name, cc) -//initialize mocker +// initialize mocker func TestCommonChaincode_Prepare(t *testing.T) { cc.Prepare(mock) } func TestTestChaincode_Init(t *testing.T) { - var args = ArgsBuilder("Initfcn") + var args = classic.ArgsBuilder("Initfcn") var TxID = "ob" var response = mock.MockInit(TxID, args.Get()) @@ -58,7 +59,7 @@ func TestTestChaincode_Init(t *testing.T) { } func TestTestChaincode_Invoke(t *testing.T) { - var args = ArgsBuilder("Invokefcn") + var args = classic.ArgsBuilder("Invokefcn") var TxID = "oa" var response = mock.MockInvoke(TxID, args.Get()) @@ -83,7 +84,8 @@ func TestCreateCompositeKey(t *testing.T) { mock.MockTransactionEnd(TxID) } -/** +/* +* 2018-07-09 12:46:27.277 HKT [mock] HasNext -> ERRO 001 HasNext() couldn't get Current mockstub.go line 410: mockLogger.Error("HasNext() couldn't get Current") */ diff --git a/go.mod b/go.mod index 5fbfbe5..b2ffec5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/davidkhala/fabric-common-chaincode-golang go 1.20 require ( + github.com/davidkhala/fabric-common/golang v0.0.10 github.com/davidkhala/goutils v1.6.0 github.com/golang/protobuf v1.5.3 github.com/hyperledger/fabric-chaincode-go v0.0.0-20240124143825-7dec3c7e7d45 @@ -19,8 +20,8 @@ require ( golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect - google.golang.org/grpc v1.59.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect + google.golang.org/grpc v1.61.0 // indirect google.golang.org/protobuf v1.32.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 93ce97b..a78a963 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davidkhala/fabric-common/golang v0.0.10 h1:S6l2ZgIaTHWA25MKe8oNUAC5gUa0GPMdDBFPL02cS+o= +github.com/davidkhala/fabric-common/golang v0.0.10/go.mod h1:hgDOy75CL/vJBxgoql5gw9n+ZkqnjZPizX8GlDjddvI= github.com/davidkhala/goutils v1.6.0 h1:uDjfyEiRUf/FQSfX3W5xUrQuNrX95iAb41r+/6FGqzI= github.com/davidkhala/goutils v1.6.0/go.mod h1:vUEFyO8rG7HzRhaL413vZZfngC8146L4Qqf75Buvw2M= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= @@ -10,7 +12,7 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hyperledger/fabric-chaincode-go v0.0.0-20240124143825-7dec3c7e7d45 h1:tZeJCTwbAE3cwi6XId+dYd/gTtfTKzZ3uEb1ksvQf7I= github.com/hyperledger/fabric-chaincode-go v0.0.0-20240124143825-7dec3c7e7d45/go.mod h1:YZBt6/ZlJCzyPoWecbfFp34G+ZIYKodTQA46c0sxHIk= @@ -56,10 +58,10 @@ golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 h1:AB/lmRny7e2pLhFEYIbl5qkDAUt2h0ZRO4wGPhZf+ik= -google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405/go.mod h1:67X1fPuzjcrkymZzZV1vvkFeTn2Rvc6lYF9MYFGCcwE= -google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk= -google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 h1:/jFB8jK5R3Sq3i/lmeZO0cATSzFfZaJq1J2Euan3XKU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0/go.mod h1:FUoWkonphQm3RhTS+kOEhF8h0iDpm4tdXolVCeZ9KKA= +google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0= +google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= diff --git a/shim.go b/shim.go index 0b44410..8f393ab 100644 --- a/shim.go +++ b/shim.go @@ -1,13 +1,13 @@ package golang import ( - "github.com/davidkhala/fabric-common-chaincode-golang/cid" + "github.com/davidkhala/fabric-common/golang/format" "github.com/davidkhala/goutils" "github.com/hyperledger/fabric-chaincode-go/shim" ) // GetMSPID From https://github.com/hyperledger/fabric-chaincode-go/commit/2d899240a7ed642a381ba9df2f6b0c303cb149dc -func GetMSPID() cid.MSPID { +func GetMSPID() format.MSPID { var mspId, err = shim.GetMSPID() goutils.PanicError(err) return mspId diff --git a/struct.go b/struct.go index 24d4118..0a64f36 100644 --- a/struct.go +++ b/struct.go @@ -56,13 +56,3 @@ func ParseStates(iterator shim.StateQueryIteratorInterface, filter func(StateKV) } return kvs } - -// PeerResponse a readable structure of peer.response -type PeerResponse struct { - // A status code that should follow the HTTP status codes. - Status int32 `json:"status,omitempty"` - // A message associated with the response code. - Message string `json:"message,omitempty"` - // A payload that can be used to include metadata with this response. - Payload string `json:"payload,omitempty"` -}