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

Draft: Resolves #80, Bumped fabric-proto-go to fabric-protos-go-apiv2 #84

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added amm.md
Empty file.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ module github.com/hyperledger/fabric-chaincode-go
go 1.20

require (
github.com/golang/protobuf v1.5.3
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.54.0
google.golang.org/protobuf v1.28.1
)

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.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.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
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
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/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=
Expand Down
4 changes: 2 additions & 2 deletions pkg/attrmgr/attrmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions pkg/cid/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/cid/cid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-----
Expand Down
6 changes: 3 additions & 3 deletions pkg/statebased/statebasedimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/statebased/statebasedimpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion shim/chaincodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
52 changes: 26 additions & 26 deletions shim/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Expand All @@ -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
}
Expand All @@ -125,18 +125,18 @@ 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
}

// sendReceive sends msg to the peer and waits for the response to arrive on
// 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
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions shim/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
Expand All @@ -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,
}

Expand Down Expand Up @@ -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,
}

Expand All @@ -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{}
Expand Down
12 changes: 6 additions & 6 deletions shim/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion shim/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading
Loading