From 48328d50c6b778ed9e1bcc558bf14d6ab2e525a7 Mon Sep 17 00:00:00 2001 From: tobigiwa Date: Sun, 17 Sep 2023 04:09:03 -0400 Subject: [PATCH 1/2] Resolves #80, this commit contains dependency update from fabric-protos-go to fabric-protos-go-apiv2 and "github.com/golang/protobuf"(deprecated) to "google.golang.org/protobuf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bumped google.golang.org/protobuf to v1.31.0 from v1.5.2 It can be noticed that the package "github.com/golang/protobuf" is still an indirect dependency used by google.golang.org/grpc/credentials v1.53.0, even the latest update v1.58.0 still does. Made sure all tests passed. CHANGES INCLUDE: SOURCE CODE The "AssertProtoEqual" was borrowed from: // https://github.com/hyperledger/fabric-gateway/blob/cd1bc1f3fcf007bd97244120d9a8d112153322cd/pkg/internal/test/transaction.go#L20-L22 which is more apt to test the Protobufs of google.golang.org/protobuf/ implementation. It replaced assertEqual. now := ptypes.TimestampNow() --> now := timestamp.Now() IMPORTS All Import aliases are kept. "github.com/golang/protobuf/proto" --> "google.golang.org/protobuf/proto" pb "github.com/hyperledger/fabric-protos-go/peer" --> pb "github.com/hyperledger/fabric-protos-go-apiv2/peer" "github.com/golang/protobuf/ptypes/timestamp" --> timestamp "google.golang.org/protobuf/types/known/timestamppb" "github.com/hyperledger/fabric-protos-go/ledger/queryresult" --> "github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult" "github.com/hyperledger/fabric-protos-go/ledger/msp" → "github.com/hyperledger/fabric-protos-go-apiv2/ledger/msp" "github.com/hyperledger/fabric-protos-go/common" --> "github.com/hyperledger/fabric-protos-go-apiv2/common" Signed-off-by: tobigiwa --- go.mod | 6 +-- go.sum | 5 ++- pkg/attrmgr/attrmgr.go | 4 +- pkg/cid/cid.go | 4 +- pkg/cid/cid_test.go | 4 +- pkg/statebased/statebasedimpl.go | 6 +-- pkg/statebased/statebasedimpl_test.go | 6 +-- shim/chaincodeserver.go | 2 +- shim/handler.go | 4 +- shim/handler_test.go | 2 +- shim/interfaces.go | 6 +-- shim/internal/client.go | 2 +- shim/internal/client_test.go | 2 +- shim/internal/config_test.go | 2 +- shim/internal/mock/client_stream.go | 2 +- shim/internal/mock/peer_chaincode_stream.go | 2 +- shim/response.go | 2 +- shim/shim.go | 4 +- shim/shim_test.go | 2 +- shim/stub.go | 10 ++--- shim/stub_test.go | 44 ++++++++++++--------- shimtest/mock/chaincode.go | 2 +- shimtest/mockstub.go | 11 +++--- 23 files changed, 71 insertions(+), 63 deletions(-) diff --git a/go.mod b/go.mod index 02cc211b..78d4050e 100644 --- a/go.mod +++ b/go.mod @@ -3,21 +3,21 @@ module github.com/hyperledger/fabric-chaincode-go go 1.20 require ( - github.com/golang/protobuf v1.5.2 - github.com/hyperledger/fabric-protos-go v0.3.0 + github.com/hyperledger/fabric-protos-go-apiv2 v0.3.0 github.com/stretchr/testify v1.8.2 google.golang.org/grpc v1.53.0 + google.golang.org/protobuf v1.31.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/golang/protobuf v1.5.2 // indirect github.com/kr/pretty v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.7.0 // indirect golang.org/x/sys v0.5.0 // indirect golang.org/x/text v0.7.0 // indirect google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect - google.golang.org/protobuf v1.28.1 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f297edb6..fcc3122f 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,9 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw github.com/golang/protobuf v1.5.2/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/hyperledger/fabric-protos-go v0.3.0 h1:MXxy44WTMENOh5TI8+PCK2x6pMj47Go2vFRKDHB2PZs= github.com/hyperledger/fabric-protos-go v0.3.0/go.mod h1:WWnyWP40P2roPmmvxsUXSvVI/CF6vwY1K1UFidnKBys= +github.com/hyperledger/fabric-protos-go-apiv2 v0.3.0 h1:DOmDMloF3vKKJKXz+CsZhFgkUmnXKzP5ei71yGIbeOw= +github.com/hyperledger/fabric-protos-go-apiv2 v0.3.0/go.mod h1:smwq1q6eKByqQAp0SYdVvE1MvDoneF373j11XwWajgA= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= @@ -43,6 +44,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/pkg/attrmgr/attrmgr.go b/pkg/attrmgr/attrmgr.go index 46dbdbd7..48b686c7 100644 --- a/pkg/attrmgr/attrmgr.go +++ b/pkg/attrmgr/attrmgr.go @@ -13,8 +13,8 @@ import ( "errors" "fmt" - "github.com/golang/protobuf/proto" - "github.com/hyperledger/fabric-protos-go/msp" + "github.com/hyperledger/fabric-protos-go-apiv2/msp" + "google.golang.org/protobuf/proto" ) var ( diff --git a/pkg/cid/cid.go b/pkg/cid/cid.go index 80eec6c8..bab1295c 100644 --- a/pkg/cid/cid.go +++ b/pkg/cid/cid.go @@ -12,9 +12,9 @@ import ( "encoding/pem" "fmt" - "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric-chaincode-go/pkg/attrmgr" - "github.com/hyperledger/fabric-protos-go/msp" + "github.com/hyperledger/fabric-protos-go-apiv2/msp" + "google.golang.org/protobuf/proto" ) // GetID returns the ID associated with the invoking identity. This ID diff --git a/pkg/cid/cid_test.go b/pkg/cid/cid_test.go index 23163821..13718d86 100644 --- a/pkg/cid/cid_test.go +++ b/pkg/cid/cid_test.go @@ -7,10 +7,10 @@ import ( "encoding/base64" "testing" - "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric-chaincode-go/pkg/cid" - "github.com/hyperledger/fabric-protos-go/msp" + "github.com/hyperledger/fabric-protos-go-apiv2/msp" "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/proto" ) const certWithOutAttrs = `-----BEGIN CERTIFICATE----- diff --git a/pkg/statebased/statebasedimpl.go b/pkg/statebased/statebasedimpl.go index a8717655..d0ad90c4 100644 --- a/pkg/statebased/statebasedimpl.go +++ b/pkg/statebased/statebasedimpl.go @@ -7,9 +7,9 @@ import ( "fmt" "sort" - "github.com/golang/protobuf/proto" - "github.com/hyperledger/fabric-protos-go/common" - "github.com/hyperledger/fabric-protos-go/msp" + "github.com/hyperledger/fabric-protos-go-apiv2/common" + "github.com/hyperledger/fabric-protos-go-apiv2/msp" + "google.golang.org/protobuf/proto" ) // stateEP implements the KeyEndorsementPolicy diff --git a/pkg/statebased/statebasedimpl_test.go b/pkg/statebased/statebasedimpl_test.go index b62668fc..e2387a80 100644 --- a/pkg/statebased/statebasedimpl_test.go +++ b/pkg/statebased/statebasedimpl_test.go @@ -6,11 +6,11 @@ package statebased_test import ( "testing" - "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric-chaincode-go/pkg/statebased" - "github.com/hyperledger/fabric-protos-go/common" - "github.com/hyperledger/fabric-protos-go/msp" + "github.com/hyperledger/fabric-protos-go-apiv2/common" + "github.com/hyperledger/fabric-protos-go-apiv2/msp" "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/proto" ) func TestAddOrg(t *testing.T) { diff --git a/shim/chaincodeserver.go b/shim/chaincodeserver.go index 2904e967..553198e4 100644 --- a/shim/chaincodeserver.go +++ b/shim/chaincodeserver.go @@ -8,7 +8,7 @@ import ( "errors" "github.com/hyperledger/fabric-chaincode-go/shim/internal" - pb "github.com/hyperledger/fabric-protos-go/peer" + pb "github.com/hyperledger/fabric-protos-go-apiv2/peer" "google.golang.org/grpc/keepalive" ) diff --git a/shim/handler.go b/shim/handler.go index 9b66a087..0c470a1f 100644 --- a/shim/handler.go +++ b/shim/handler.go @@ -8,8 +8,8 @@ import ( "fmt" "sync" - "github.com/golang/protobuf/proto" - pb "github.com/hyperledger/fabric-protos-go/peer" + pb "github.com/hyperledger/fabric-protos-go-apiv2/peer" + "google.golang.org/protobuf/proto" ) type state string diff --git a/shim/handler_test.go b/shim/handler_test.go index 75a74859..9c5f1332 100644 --- a/shim/handler_test.go +++ b/shim/handler_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/hyperledger/fabric-chaincode-go/shim/internal/mock" - peerpb "github.com/hyperledger/fabric-protos-go/peer" + peerpb "github.com/hyperledger/fabric-protos-go-apiv2/peer" "github.com/stretchr/testify/assert" ) diff --git a/shim/interfaces.go b/shim/interfaces.go index 87ecd81d..e655a485 100644 --- a/shim/interfaces.go +++ b/shim/interfaces.go @@ -4,9 +4,9 @@ package shim import ( - "github.com/golang/protobuf/ptypes/timestamp" - "github.com/hyperledger/fabric-protos-go/ledger/queryresult" - pb "github.com/hyperledger/fabric-protos-go/peer" + "github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult" + pb "github.com/hyperledger/fabric-protos-go-apiv2/peer" + timestamp "google.golang.org/protobuf/types/known/timestamppb" ) // Chaincode interface must be implemented by all chaincodes. The fabric runs diff --git a/shim/internal/client.go b/shim/internal/client.go index 6b6dde0e..3ce649ec 100644 --- a/shim/internal/client.go +++ b/shim/internal/client.go @@ -8,7 +8,7 @@ import ( "crypto/tls" "time" - peerpb "github.com/hyperledger/fabric-protos-go/peer" + peerpb "github.com/hyperledger/fabric-protos-go-apiv2/peer" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/keepalive" diff --git a/shim/internal/client_test.go b/shim/internal/client_test.go index d1af8e3f..c162e1ba 100644 --- a/shim/internal/client_test.go +++ b/shim/internal/client_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - pb "github.com/hyperledger/fabric-protos-go/peer" + pb "github.com/hyperledger/fabric-protos-go-apiv2/peer" "github.com/stretchr/testify/assert" "google.golang.org/grpc" "google.golang.org/grpc/keepalive" diff --git a/shim/internal/config_test.go b/shim/internal/config_test.go index d9ece872..c9ddbdf9 100644 --- a/shim/internal/config_test.go +++ b/shim/internal/config_test.go @@ -14,7 +14,7 @@ import ( "time" . "github.com/hyperledger/fabric-chaincode-go/shim/internal" - peerpb "github.com/hyperledger/fabric-protos-go/peer" + peerpb "github.com/hyperledger/fabric-protos-go-apiv2/peer" "github.com/stretchr/testify/assert" "google.golang.org/grpc" "google.golang.org/grpc/credentials" diff --git a/shim/internal/mock/client_stream.go b/shim/internal/mock/client_stream.go index 2ed61512..4df37cb0 100644 --- a/shim/internal/mock/client_stream.go +++ b/shim/internal/mock/client_stream.go @@ -4,7 +4,7 @@ package mock import ( "sync" - "github.com/hyperledger/fabric-protos-go/peer" + "github.com/hyperledger/fabric-protos-go-apiv2/peer" ) type ClientStream struct { diff --git a/shim/internal/mock/peer_chaincode_stream.go b/shim/internal/mock/peer_chaincode_stream.go index 8e12e863..c75b521f 100644 --- a/shim/internal/mock/peer_chaincode_stream.go +++ b/shim/internal/mock/peer_chaincode_stream.go @@ -4,7 +4,7 @@ package mock import ( "sync" - "github.com/hyperledger/fabric-protos-go/peer" + "github.com/hyperledger/fabric-protos-go-apiv2/peer" ) type PeerChaincodeStream struct { diff --git a/shim/response.go b/shim/response.go index abe62866..0fe29348 100644 --- a/shim/response.go +++ b/shim/response.go @@ -4,7 +4,7 @@ package shim import ( - pb "github.com/hyperledger/fabric-protos-go/peer" + pb "github.com/hyperledger/fabric-protos-go-apiv2/peer" ) const ( diff --git a/shim/shim.go b/shim/shim.go index fee7c221..f998db96 100644 --- a/shim/shim.go +++ b/shim/shim.go @@ -13,9 +13,9 @@ import ( "os" "unicode/utf8" - "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric-chaincode-go/shim/internal" - peerpb "github.com/hyperledger/fabric-protos-go/peer" + peerpb "github.com/hyperledger/fabric-protos-go-apiv2/peer" + "google.golang.org/protobuf/proto" ) const ( diff --git a/shim/shim_test.go b/shim/shim_test.go index 44183f74..14cd7bfe 100644 --- a/shim/shim_test.go +++ b/shim/shim_test.go @@ -10,7 +10,7 @@ import ( "testing" "github.com/hyperledger/fabric-chaincode-go/shim/internal/mock" - peerpb "github.com/hyperledger/fabric-protos-go/peer" + peerpb "github.com/hyperledger/fabric-protos-go-apiv2/peer" "github.com/stretchr/testify/assert" ) diff --git a/shim/stub.go b/shim/stub.go index fafaacf5..59ff7ab4 100644 --- a/shim/stub.go +++ b/shim/stub.go @@ -11,11 +11,11 @@ import ( "os" "unicode/utf8" - "github.com/golang/protobuf/proto" - "github.com/golang/protobuf/ptypes/timestamp" - "github.com/hyperledger/fabric-protos-go/common" - "github.com/hyperledger/fabric-protos-go/ledger/queryresult" - pb "github.com/hyperledger/fabric-protos-go/peer" + "github.com/hyperledger/fabric-protos-go-apiv2/common" + "github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult" + pb "github.com/hyperledger/fabric-protos-go-apiv2/peer" + "google.golang.org/protobuf/proto" + timestamp "google.golang.org/protobuf/types/known/timestamppb" ) // ChaincodeStub is an object passed to chaincode for shim side handling of diff --git a/shim/stub_test.go b/shim/stub_test.go index bc98705a..6086e72a 100644 --- a/shim/stub_test.go +++ b/shim/stub_test.go @@ -9,15 +9,17 @@ import ( "os" "testing" - "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric-chaincode-go/shim/internal/mock" - "github.com/hyperledger/fabric-protos-go/common" - "github.com/hyperledger/fabric-protos-go/ledger/queryresult" - peerpb "github.com/hyperledger/fabric-protos-go/peer" + "github.com/hyperledger/fabric-protos-go-apiv2/common" + "github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult" + peerpb "github.com/hyperledger/fabric-protos-go-apiv2/peer" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" + + timestamp "google.golang.org/protobuf/types/known/timestamppb" - "github.com/golang/protobuf/ptypes" - "github.com/golang/protobuf/ptypes/timestamp" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func toChaincodeArgs(args ...string) [][]byte { @@ -28,6 +30,10 @@ func toChaincodeArgs(args ...string) [][]byte { return ccArgs } +func AssertProtoEqual(t *testing.T, expected protoreflect.ProtoMessage, actual protoreflect.ProtoMessage) { + require.True(t, proto.Equal(expected, actual), "Expected %v, got %v", expected, actual) +} + func TestNewChaincodeStub(t *testing.T) { expectedArgs := toChaincodeArgs("function", "arg1", "arg2") expectedDecorations := map[string][]byte{"decoration-key": []byte("decoration-value")} @@ -119,7 +125,7 @@ func TestNewChaincodeStub(t *testing.T) { prop := &peerpb.Proposal{} err = proto.Unmarshal(tt.signedProposal.ProposalBytes, prop) assert.NoError(t, err) - assert.Equal(t, prop, stub.proposal) + AssertProtoEqual(t, prop, stub.proposal) assert.Equal(t, expectedCreator, stub.creator) assert.Equal(t, expectedTransient, stub.transient) @@ -193,7 +199,7 @@ func TestChaincodeStubAccessors(t *testing.T) { } func TestChaincodeStubGetTxTimestamp(t *testing.T) { - now := ptypes.TimestampNow() + now := timestamp.Now() tests := []struct { proposal *peerpb.Proposal ts *timestamp.Timestamp @@ -399,7 +405,7 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error for GetQueryResult: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) sqi, err = s.GetPrivateDataQueryResult("col", "query") if err != nil { @@ -409,7 +415,7 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error for GetPrivateDataQueryResult: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) _, err = s.GetPrivateDataQueryResult("", "query") assert.EqualError(t, err, "collection must not be an empty string") @@ -423,14 +429,14 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error for GetStateByRange: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) // second result assert.True(t, sqi.HasNext()) kv, err = sqi.Next() if err != nil { t.Fatalf("Unexpected error for GetStateByRange: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) err = sqi.Close() assert.NoError(t, err) @@ -439,7 +445,7 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error for GetStateByRangeWithPagination: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) assert.Equal(t, "book", qrm.GetBookmark()) assert.Equal(t, int32(1), qrm.GetFetchedRecordsCount()) @@ -451,7 +457,7 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error for GetPrivateDataByRange: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) _, err = s.GetPrivateDataByRange("", "", "end") assert.EqualError(t, err, "collection must not be an empty string") @@ -461,14 +467,14 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error for GetStateByPartialCompositeKey: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) sqi, err = s.GetPrivateDataByPartialCompositeKey("col", "object", []string{"attr1", "attr2"}) kv, err = sqi.Next() if err != nil { t.Fatalf("Unexpected error for GetPrivateDataByPartialCompositeKey: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) _, err = s.GetPrivateDataByPartialCompositeKey("", "object", []string{"attr1", "attr2"}) assert.EqualError(t, err, "collection must not be an empty string") @@ -483,7 +489,7 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error for GetStateByPartialCompositeKeyWithPagination: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) assert.Equal(t, "book", qrm.GetBookmark()) assert.Equal(t, int32(1), qrm.GetFetchedRecordsCount()) @@ -492,7 +498,7 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error forGetQueryResultWithPagination: %s", err) } - assert.Equal(t, expectedResult, kv) + AssertProtoEqual(t, expectedResult, kv) assert.Equal(t, "book", qrm.GetBookmark()) assert.Equal(t, int32(1), qrm.GetFetchedRecordsCount()) }, @@ -528,7 +534,7 @@ func TestChaincodeStubHandlers(t *testing.T) { if err != nil { t.Fatalf("Unexpected error for GetPrivateDataByRangee: %s", err) } - assert.Equal(t, expectedResult, km) + AssertProtoEqual(t, expectedResult, km) assert.False(t, hqi.HasNext()) }, }, diff --git a/shimtest/mock/chaincode.go b/shimtest/mock/chaincode.go index 6ad21a10..33aabe67 100644 --- a/shimtest/mock/chaincode.go +++ b/shimtest/mock/chaincode.go @@ -5,7 +5,7 @@ import ( "sync" "github.com/hyperledger/fabric-chaincode-go/shim" - "github.com/hyperledger/fabric-protos-go/peer" + "github.com/hyperledger/fabric-protos-go-apiv2/peer" ) type Chaincode struct { diff --git a/shimtest/mockstub.go b/shimtest/mockstub.go index 6e36fe7c..c0709436 100644 --- a/shimtest/mockstub.go +++ b/shimtest/mockstub.go @@ -16,12 +16,11 @@ import ( "strings" "unicode/utf8" - "github.com/golang/protobuf/proto" - "github.com/golang/protobuf/ptypes" - "github.com/golang/protobuf/ptypes/timestamp" "github.com/hyperledger/fabric-chaincode-go/shim" - "github.com/hyperledger/fabric-protos-go/ledger/queryresult" - pb "github.com/hyperledger/fabric-protos-go/peer" + "github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult" + pb "github.com/hyperledger/fabric-protos-go-apiv2/peer" + "google.golang.org/protobuf/proto" + timestamp "google.golang.org/protobuf/types/known/timestamppb" ) const ( @@ -121,7 +120,7 @@ func (stub *MockStub) GetFunctionAndParameters() (function string, params []stri func (stub *MockStub) MockTransactionStart(txid string) { stub.TxID = txid stub.setSignedProposal(&pb.SignedProposal{}) - stub.setTxTimestamp(ptypes.TimestampNow()) + stub.setTxTimestamp(timestamp.Now()) } // MockTransactionEnd End a mocked transaction, clearing the UUID. From a12d7c14d1f9dd1db054515aec34396824edfaaf Mon Sep 17 00:00:00 2001 From: tobigiwa Date: Tue, 19 Sep 2023 12:21:58 -0400 Subject: [PATCH 2/2] Draft: This is a working change on bumping the fabric-chaincode-go to use fabric-protos-go-apiv2 and the new protobufs lib. This commits includes changes to fuction signatures, return values, interface definition and type definition to type "ChaincodeMessage" and "pb.Response" . It is a breaking change. Signed-off-by: tobigiwa --- shim/handler.go | 48 +++++++++++++++++++------------------- shim/handler_test.go | 10 ++++---- shim/interfaces.go | 6 ++--- shim/response.go | 4 ++-- shim/stub.go | 2 +- shim/stub_test.go | 2 +- shimtest/mock/chaincode.go | 40 +++++++++++++++---------------- shimtest/mockstub.go | 8 +++---- 8 files changed, 60 insertions(+), 60 deletions(-) diff --git a/shim/handler.go b/shim/handler.go index 0c470a1f..a4e02431 100644 --- a/shim/handler.go +++ b/shim/handler.go @@ -52,7 +52,7 @@ type Handler struct { // need lock to protect chaincode from attempting // concurrent requests to the peer responseChannelsMutex sync.Mutex - responseChannels map[string]chan pb.ChaincodeMessage + responseChannels map[string]chan *pb.ChaincodeMessage } func shorttxid(txid string) string { @@ -85,7 +85,7 @@ func transactionContextID(chainID, txid string) string { return chainID + txid } -func (h *Handler) createResponseChannel(channelID, txid string) (<-chan pb.ChaincodeMessage, error) { +func (h *Handler) createResponseChannel(channelID, txid string) (<-chan *pb.ChaincodeMessage, error) { h.responseChannelsMutex.Lock() defer h.responseChannelsMutex.Unlock() @@ -98,7 +98,7 @@ func (h *Handler) createResponseChannel(channelID, txid string) (<-chan pb.Chain return nil, fmt.Errorf("[%s] channel exists", shorttxid(txCtxID)) } - responseChan := make(chan pb.ChaincodeMessage) + responseChan := make(chan *pb.ChaincodeMessage) h.responseChannels[txCtxID] = responseChan return responseChan, nil } @@ -125,7 +125,7 @@ func (h *Handler) handleResponse(msg *pb.ChaincodeMessage) error { if responseCh == nil { return fmt.Errorf("[%s] responseChannel does not exist", shorttxid(msg.Txid)) } - responseCh <- *msg + responseCh <- msg return nil } @@ -133,10 +133,10 @@ func (h *Handler) handleResponse(msg *pb.ChaincodeMessage) error { // the provided responseChan. On success, the response message will be // returned. An error will be returned msg was not successfully sent to the // peer. -func (h *Handler) sendReceive(msg *pb.ChaincodeMessage, responseChan <-chan pb.ChaincodeMessage) (pb.ChaincodeMessage, error) { +func (h *Handler) sendReceive(msg *pb.ChaincodeMessage, responseChan <-chan *pb.ChaincodeMessage) (*pb.ChaincodeMessage, error) { err := h.serialSend(msg) if err != nil { - return pb.ChaincodeMessage{}, err + return &pb.ChaincodeMessage{}, err } outmsg := <-responseChan @@ -148,7 +148,7 @@ func newChaincodeHandler(peerChatStream PeerChaincodeStream, chaincode Chaincode return &Handler{ chatStream: peerChatStream, cc: chaincode, - responseChannels: map[string]chan pb.ChaincodeMessage{}, + responseChannels: map[string]chan *pb.ChaincodeMessage{}, state: created, } } @@ -183,7 +183,7 @@ func (h *Handler) handleInit(msg *pb.ChaincodeMessage) (*pb.ChaincodeMessage, er return &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(res.Message), Txid: msg.Txid, ChaincodeEvent: stub.chaincodeEvent, ChannelId: msg.ChannelId}, nil } - resBytes, err := proto.Marshal(&res) + resBytes, err := proto.Marshal(res) if err != nil { return nil, fmt.Errorf("failed to marshal response: %s", err) } @@ -201,15 +201,15 @@ func (h *Handler) handleTransaction(msg *pb.ChaincodeMessage) (*pb.ChaincodeMess } // Create the ChaincodeStub which the chaincode can use to callback - stub, err := newChaincodeStub(h, msg.ChannelId, msg.Txid, input, msg.Proposal) + stubs, err := newChaincodeStub(h, msg.ChannelId, msg.Txid, input, msg.Proposal) if err != nil { return nil, fmt.Errorf("failed to create new ChaincodeStub: %s", err) } - + stub := stubs res := h.cc.Invoke(stub) // Endorser will handle error contained in Response. - resBytes, err := proto.Marshal(&res) + resBytes, err := proto.Marshal(res) if err != nil { return nil, fmt.Errorf("failed to marshal response: %s", err) } @@ -219,11 +219,11 @@ func (h *Handler) handleTransaction(msg *pb.ChaincodeMessage) (*pb.ChaincodeMess // callPeerWithChaincodeMsg sends a chaincode message to the peer for the given // txid and channel and receives the response. -func (h *Handler) callPeerWithChaincodeMsg(msg *pb.ChaincodeMessage, channelID, txid string) (pb.ChaincodeMessage, error) { +func (h *Handler) callPeerWithChaincodeMsg(msg *pb.ChaincodeMessage, channelID, txid string) (*pb.ChaincodeMessage, error) { // Create the channel on which to communicate the response from the peer respChan, err := h.createResponseChannel(channelID, txid) if err != nil { - return pb.ChaincodeMessage{}, err + return &pb.ChaincodeMessage{}, err } defer h.deleteResponseChannel(channelID, txid) @@ -292,7 +292,7 @@ func (h *Handler) handleGetStateMetadata(collection string, key string, channelI var mdResult pb.StateMetadataResult err := proto.Unmarshal(responseMsg.Payload, &mdResult) if err != nil { - return nil, errors.New("Could not unmarshal metadata response") + return nil, errors.New("could not unmarshal metadata response") } metadata := make(map[string][]byte) for _, md := range mdResult.Entries { @@ -451,7 +451,7 @@ func (h *Handler) handleQueryStateNext(id, channelID, txid string) (*pb.QueryRes msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_QUERY_STATE_NEXT, Payload: payloadBytes, Txid: txid, ChannelId: channelID} - var responseMsg pb.ChaincodeMessage + var responseMsg *pb.ChaincodeMessage if responseMsg, err = h.sendReceive(msg, respChan); err != nil { return nil, fmt.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_QUERY_STATE_NEXT) @@ -488,7 +488,7 @@ func (h *Handler) handleQueryStateClose(id, channelID, txid string) (*pb.QueryRe msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_QUERY_STATE_CLOSE, Payload: payloadBytes, Txid: txid, ChannelId: channelID} - var responseMsg pb.ChaincodeMessage + var responseMsg *pb.ChaincodeMessage if responseMsg, err = h.sendReceive(msg, respChan); err != nil { return nil, fmt.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_QUERY_STATE_CLOSE) @@ -552,7 +552,7 @@ func (h *Handler) handleGetHistoryForKey(key string, channelID string, txid stri payloadBytes := marshalOrPanic(&pb.GetHistoryForKey{Key: key}) msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_GET_HISTORY_FOR_KEY, Payload: payloadBytes, Txid: txid, ChannelId: channelID} - var responseMsg pb.ChaincodeMessage + var responseMsg *pb.ChaincodeMessage if responseMsg, err = h.sendReceive(msg, respChan); err != nil { return nil, fmt.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_GET_HISTORY_FOR_KEY) @@ -576,12 +576,12 @@ func (h *Handler) handleGetHistoryForKey(key string, channelID string, txid stri return nil, fmt.Errorf("incorrect chaincode message %s received. Expecting %s or %s", responseMsg.Type, pb.ChaincodeMessage_RESPONSE, pb.ChaincodeMessage_ERROR) } -func (h *Handler) createResponse(status int32, payload []byte) pb.Response { - return pb.Response{Status: status, Payload: payload} +func (h *Handler) createResponse(status int32, payload []byte) *pb.Response { + return &pb.Response{Status: status, Payload: payload} } // handleInvokeChaincode communicates with the peer to invoke another chaincode. -func (h *Handler) handleInvokeChaincode(chaincodeName string, args [][]byte, channelID string, txid string) pb.Response { +func (h *Handler) handleInvokeChaincode(chaincodeName string, args [][]byte, channelID string, txid string) *pb.Response { payloadBytes := marshalOrPanic(&pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: chaincodeName}, Input: &pb.ChaincodeInput{Args: args}}) // Create the channel on which to communicate the response from validating peer @@ -594,7 +594,7 @@ func (h *Handler) handleInvokeChaincode(chaincodeName string, args [][]byte, cha // Send INVOKE_CHAINCODE message to peer chaincode support msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_INVOKE_CHAINCODE, Payload: payloadBytes, Txid: txid, ChannelId: channelID} - var responseMsg pb.ChaincodeMessage + var responseMsg *pb.ChaincodeMessage if responseMsg, err = h.sendReceive(msg, respChan); err != nil { errStr := fmt.Sprintf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_INVOKE_CHAINCODE) @@ -609,11 +609,11 @@ func (h *Handler) handleInvokeChaincode(chaincodeName string, args [][]byte, cha } if respMsg.Type == pb.ChaincodeMessage_COMPLETED { // Success response - res := &pb.Response{} - if err = proto.Unmarshal(respMsg.Payload, res); err != nil { + res := pb.Response{} + if err = proto.Unmarshal(respMsg.Payload, &res); err != nil { return h.createResponse(ERROR, []byte(err.Error())) } - return *res + return &res } return h.createResponse(ERROR, responseMsg.Payload) } diff --git a/shim/handler_test.go b/shim/handler_test.go index 9c5f1332..f90d9932 100644 --- a/shim/handler_test.go +++ b/shim/handler_test.go @@ -27,12 +27,12 @@ type mockChaincode struct { invokeCalled bool } -func (mcc *mockChaincode) Init(stub ChaincodeStubInterface) peerpb.Response { +func (mcc *mockChaincode) Init(stub ChaincodeStubInterface) *peerpb.Response { mcc.initCalled = true return Success(nil) } -func (mcc *mockChaincode) Invoke(stub ChaincodeStubInterface) peerpb.Response { +func (mcc *mockChaincode) Invoke(stub ChaincodeStubInterface) *peerpb.Response { mcc.invokeCalled = true return Success(nil) } @@ -46,7 +46,7 @@ func TestNewHandler_CreatedState(t *testing.T) { expected := &Handler{ chatStream: chatStream, cc: cc, - responseChannels: map[string]chan peerpb.ChaincodeMessage{}, + responseChannels: map[string]chan *peerpb.ChaincodeMessage{}, state: created, } @@ -210,7 +210,7 @@ func TestHandleMessage(t *testing.T) { handler := &Handler{ chatStream: chatStream, cc: cc, - responseChannels: map[string]chan peerpb.ChaincodeMessage{}, + responseChannels: map[string]chan *peerpb.ChaincodeMessage{}, state: ready, } @@ -234,7 +234,7 @@ func TestHandlePeerCalls(t *testing.T) { payload := []byte("error") h := &Handler{ cc: &mockChaincode{}, - responseChannels: map[string]chan peerpb.ChaincodeMessage{}, + responseChannels: map[string]chan *peerpb.ChaincodeMessage{}, state: ready, } chatStream := &mock.PeerChaincodeStream{} diff --git a/shim/interfaces.go b/shim/interfaces.go index e655a485..1b2c5764 100644 --- a/shim/interfaces.go +++ b/shim/interfaces.go @@ -15,12 +15,12 @@ type Chaincode interface { // Init is called during Instantiate transaction after the chaincode container // has been established for the first time, allowing the chaincode to // initialize its internal data - Init(stub ChaincodeStubInterface) pb.Response + Init(stub ChaincodeStubInterface) *pb.Response // Invoke is called to update or query the ledger in a proposal transaction. // Updated state variables are not committed to the ledger until the // transaction is committed. - Invoke(stub ChaincodeStubInterface) pb.Response + Invoke(stub ChaincodeStubInterface) *pb.Response } // ChaincodeStubInterface is used by deployable chaincode apps to access and @@ -71,7 +71,7 @@ type ChaincodeStubInterface interface { // the called chaincode on a different channel is a `Query`, which does not // participate in state validation checks in subsequent commit phase. // If `channel` is empty, the caller's channel is assumed. - InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response + InvokeChaincode(chaincodeName string, args [][]byte, channel string) *pb.Response // GetState returns the value of the specified `key` from the // ledger. Note that GetState doesn't read data from the writeset, which diff --git a/shim/response.go b/shim/response.go index 0fe29348..72cffa98 100644 --- a/shim/response.go +++ b/shim/response.go @@ -20,8 +20,8 @@ const ( ) // Success ... -func Success(payload []byte) pb.Response { - return pb.Response{ +func Success(payload []byte) *pb.Response { + return &pb.Response{ Status: OK, Payload: payload, } diff --git a/shim/stub.go b/shim/stub.go index 59ff7ab4..a07aa6c0 100644 --- a/shim/stub.go +++ b/shim/stub.go @@ -148,7 +148,7 @@ func GetMSPID() (string, error) { // ------------- Call Chaincode functions --------------- // InvokeChaincode documentation can be found in interfaces.go -func (s *ChaincodeStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response { +func (s *ChaincodeStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) *pb.Response { // Internally we handle chaincode name as a composite name if channel != "" { chaincodeName = chaincodeName + "/" + channel diff --git a/shim/stub_test.go b/shim/stub_test.go index 6086e72a..82e624ee 100644 --- a/shim/stub_test.go +++ b/shim/stub_test.go @@ -584,7 +584,7 @@ func TestChaincodeStubHandlers(t *testing.T) { handler := &Handler{ cc: &mockChaincode{}, - responseChannels: map[string]chan peerpb.ChaincodeMessage{}, + responseChannels: map[string]chan *peerpb.ChaincodeMessage{}, state: ready, } stub := &ChaincodeStub{ diff --git a/shimtest/mock/chaincode.go b/shimtest/mock/chaincode.go index 33aabe67..e71a9670 100644 --- a/shimtest/mock/chaincode.go +++ b/shimtest/mock/chaincode.go @@ -9,33 +9,33 @@ import ( ) type Chaincode struct { - InitStub func(shim.ChaincodeStubInterface) peer.Response + InitStub func(shim.ChaincodeStubInterface) *peer.Response initMutex sync.RWMutex initArgsForCall []struct { arg1 shim.ChaincodeStubInterface } initReturns struct { - result1 peer.Response + result1 *peer.Response } initReturnsOnCall map[int]struct { - result1 peer.Response + result1 *peer.Response } - InvokeStub func(shim.ChaincodeStubInterface) peer.Response + InvokeStub func(shim.ChaincodeStubInterface) *peer.Response invokeMutex sync.RWMutex invokeArgsForCall []struct { arg1 shim.ChaincodeStubInterface } invokeReturns struct { - result1 peer.Response + result1 *peer.Response } invokeReturnsOnCall map[int]struct { - result1 peer.Response + result1 *peer.Response } invocations map[string][][]interface{} invocationsMutex sync.RWMutex } -func (fake *Chaincode) Init(arg1 shim.ChaincodeStubInterface) peer.Response { +func (fake *Chaincode) Init(arg1 shim.ChaincodeStubInterface) *peer.Response { fake.initMutex.Lock() ret, specificReturn := fake.initReturnsOnCall[len(fake.initArgsForCall)] fake.initArgsForCall = append(fake.initArgsForCall, struct { @@ -59,7 +59,7 @@ func (fake *Chaincode) InitCallCount() int { return len(fake.initArgsForCall) } -func (fake *Chaincode) InitCalls(stub func(shim.ChaincodeStubInterface) peer.Response) { +func (fake *Chaincode) InitCalls(stub func(shim.ChaincodeStubInterface) *peer.Response) { fake.initMutex.Lock() defer fake.initMutex.Unlock() fake.InitStub = stub @@ -72,30 +72,30 @@ func (fake *Chaincode) InitArgsForCall(i int) shim.ChaincodeStubInterface { return argsForCall.arg1 } -func (fake *Chaincode) InitReturns(result1 peer.Response) { +func (fake *Chaincode) InitReturns(result1 *peer.Response) { fake.initMutex.Lock() defer fake.initMutex.Unlock() fake.InitStub = nil fake.initReturns = struct { - result1 peer.Response + result1 *peer.Response }{result1} } -func (fake *Chaincode) InitReturnsOnCall(i int, result1 peer.Response) { +func (fake *Chaincode) InitReturnsOnCall(i int, result1 *peer.Response) { fake.initMutex.Lock() defer fake.initMutex.Unlock() fake.InitStub = nil if fake.initReturnsOnCall == nil { fake.initReturnsOnCall = make(map[int]struct { - result1 peer.Response + result1 *peer.Response }) } fake.initReturnsOnCall[i] = struct { - result1 peer.Response + result1 *peer.Response }{result1} } -func (fake *Chaincode) Invoke(arg1 shim.ChaincodeStubInterface) peer.Response { +func (fake *Chaincode) Invoke(arg1 shim.ChaincodeStubInterface) *peer.Response { fake.invokeMutex.Lock() ret, specificReturn := fake.invokeReturnsOnCall[len(fake.invokeArgsForCall)] fake.invokeArgsForCall = append(fake.invokeArgsForCall, struct { @@ -119,7 +119,7 @@ func (fake *Chaincode) InvokeCallCount() int { return len(fake.invokeArgsForCall) } -func (fake *Chaincode) InvokeCalls(stub func(shim.ChaincodeStubInterface) peer.Response) { +func (fake *Chaincode) InvokeCalls(stub func(shim.ChaincodeStubInterface) *peer.Response) { fake.invokeMutex.Lock() defer fake.invokeMutex.Unlock() fake.InvokeStub = stub @@ -132,26 +132,26 @@ func (fake *Chaincode) InvokeArgsForCall(i int) shim.ChaincodeStubInterface { return argsForCall.arg1 } -func (fake *Chaincode) InvokeReturns(result1 peer.Response) { +func (fake *Chaincode) InvokeReturns(result1 *peer.Response) { fake.invokeMutex.Lock() defer fake.invokeMutex.Unlock() fake.InvokeStub = nil fake.invokeReturns = struct { - result1 peer.Response + result1 *peer.Response }{result1} } -func (fake *Chaincode) InvokeReturnsOnCall(i int, result1 peer.Response) { +func (fake *Chaincode) InvokeReturnsOnCall(i int, result1 *peer.Response) { fake.invokeMutex.Lock() defer fake.invokeMutex.Unlock() fake.InvokeStub = nil if fake.invokeReturnsOnCall == nil { fake.invokeReturnsOnCall = make(map[int]struct { - result1 peer.Response + result1 *peer.Response }) } fake.invokeReturnsOnCall[i] = struct { - result1 peer.Response + result1 *peer.Response }{result1} } diff --git a/shimtest/mockstub.go b/shimtest/mockstub.go index c0709436..d60ac361 100644 --- a/shimtest/mockstub.go +++ b/shimtest/mockstub.go @@ -142,7 +142,7 @@ func (stub *MockStub) MockPeerChaincode(invokableChaincodeName string, otherStub } // MockInit Initialise this chaincode, also starts and ends a transaction. -func (stub *MockStub) MockInit(uuid string, args [][]byte) pb.Response { +func (stub *MockStub) MockInit(uuid string, args [][]byte) *pb.Response { stub.args = args stub.MockTransactionStart(uuid) res := stub.cc.Init(stub) @@ -151,7 +151,7 @@ func (stub *MockStub) MockInit(uuid string, args [][]byte) pb.Response { } // MockInvoke Invoke this chaincode, also starts and ends a transaction. -func (stub *MockStub) MockInvoke(uuid string, args [][]byte) pb.Response { +func (stub *MockStub) MockInvoke(uuid string, args [][]byte) *pb.Response { stub.args = args stub.MockTransactionStart(uuid) res := stub.cc.Invoke(stub) @@ -165,7 +165,7 @@ func (stub *MockStub) GetDecorations() map[string][]byte { } // MockInvokeWithSignedProposal Invoke this chaincode, also starts and ends a transaction. -func (stub *MockStub) MockInvokeWithSignedProposal(uuid string, args [][]byte, sp *pb.SignedProposal) pb.Response { +func (stub *MockStub) MockInvokeWithSignedProposal(uuid string, args [][]byte, sp *pb.SignedProposal) *pb.Response { stub.args = args stub.MockTransactionStart(uuid) stub.signedProposal = sp @@ -390,7 +390,7 @@ func (stub *MockStub) GetQueryResultWithPagination(query string, pageSize int32, // E.g. stub1.InvokeChaincode("othercc", funcArgs, channel) // Before calling this make sure to create another MockStub stub2, call shim.NewMockStub("othercc", Chaincode) // and register it with stub1 by calling stub1.MockPeerChaincode("othercc", stub2, channel) -func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response { +func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) *pb.Response { // Internally we use chaincode name as a composite name if channel != "" { chaincodeName = chaincodeName + "/" + channel