Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 23ec481

Browse files
committed
[FAB-6695] Fixes for default config and protos
For protos - Moved common/utility functions from snaps to sdk-go to avoid exposing unnecessary protos through SDK-go thirdparty For default config -default config load logic when config env variable is missing is causing error in containers where there is no SDK-Go src available Change-Id: Ic1006de4ce6cd4c648a5be53fdb40e995541f943 Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent 199fae9 commit 23ec481

File tree

7 files changed

+129
-9
lines changed

7 files changed

+129
-9
lines changed

internal/github.com/hyperledger/fabric/core/ledger/util/txvalidationflags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ import (
2727
// TxValidationFlags is array of transaction validation codes. It is used when committer validates block.
2828
type TxValidationFlags []uint8
2929

30+
// NewTxValidationFlags Create new object-array of validation codes with target size. Default values: valid.
31+
func NewTxValidationFlags(size int) TxValidationFlags {
32+
inst := make(TxValidationFlags, size)
33+
return inst
34+
}
35+
3036
// Flag returns validation code at specified transaction
3137
func (obj TxValidationFlags) Flag(txIndex int) peer.TxValidationCode {
3238
return peer.TxValidationCode(obj[txIndex])

internal/github.com/hyperledger/fabric/protos/utils/proputils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ func GetBytesChaincodeProposalPayload(cpp *peer.ChaincodeProposalPayload) ([]byt
184184
return cppBytes, err
185185
}
186186

187+
// GetBytesChaincodeEvent gets the bytes of ChaincodeEvent
188+
func GetBytesChaincodeEvent(event *peer.ChaincodeEvent) ([]byte, error) {
189+
eventBytes, err := proto.Marshal(event)
190+
return eventBytes, err
191+
}
192+
187193
// GetBytesChaincodeActionPayload get the bytes of ChaincodeActionPayload from the message
188194
func GetBytesChaincodeActionPayload(cap *peer.ChaincodeActionPayload) ([]byte, error) {
189195
capBytes, err := proto.Marshal(cap)
@@ -208,6 +214,12 @@ func GetBytesPayload(payl *common.Payload) ([]byte, error) {
208214
return bytes, err
209215
}
210216

217+
// GetBytesEnvelope get the bytes of Envelope from the message
218+
func GetBytesEnvelope(env *common.Envelope) ([]byte, error) {
219+
bytes, err := proto.Marshal(env)
220+
return bytes, err
221+
}
222+
211223
// CreateProposalFromCIS returns a proposal given a serialized identity and a ChaincodeInvocationSpec
212224
func CreateProposalFromCIS(typ common.HeaderType, chainID string, cis *peer.ChaincodeInvocationSpec, creator []byte) (*peer.Proposal, string, error) {
213225
return CreateChaincodeProposal(typ, chainID, cis, creator)

pkg/config/config.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"math/rand"
1515
"os"
1616
"path"
17-
"path/filepath"
1817
"strings"
1918
"time"
2019

@@ -91,16 +90,15 @@ func InitConfigWithCmdRoot(configFile string, cmdRootPrefix string) (*Config, er
9190
return &Config{tlsCertPool: x509.NewCertPool(), configViper: myViper}, nil
9291
}
9392

94-
// load Default confid
93+
// load Default config
9594
func loadDefaultConfig(myViper *viper.Viper) error {
9695
// get Environment Default Config Path
9796
defaultPath := os.Getenv("FABRIC_SDK_CONFIG_PATH")
98-
if defaultPath != "" { // if set, use it to load default config
99-
myViper.AddConfigPath(strings.Replace(defaultPath, "$GOPATH", os.Getenv("GOPATH"), -1))
100-
} else { // else fallback to default DEV path
101-
devPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "hyperledger", "fabric-sdk-go", "pkg", "config")
102-
myViper.AddConfigPath(devPath)
97+
if defaultPath == "" {
98+
return nil
10399
}
100+
// if set, use it to load default config
101+
myViper.AddConfigPath(strings.Replace(defaultPath, "$GOPATH", os.Getenv("GOPATH"), -1))
104102
err := myViper.ReadInConfig() // Find and read the config file
105103
if err != nil { // Handle errors reading the config file
106104
return errors.Wrap(err, "loading config file failed")

pkg/fabric-client/mocks/mockbroadcastserver.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ package mocks
99
import (
1010
"io"
1111

12+
"fmt"
13+
"net"
14+
1215
po "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/orderer"
1316
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
17+
"google.golang.org/grpc"
1418
)
1519

1620
// TestBlock is a test block
@@ -71,3 +75,18 @@ func (m *MockBroadcastServer) Deliver(server po.AtomicBroadcast_DeliverServer) e
7175

7276
return nil
7377
}
78+
79+
//StartMockBroadcastServer starts mock server for unit testing purpose
80+
func StartMockBroadcastServer(broadcastTestURL string) *MockBroadcastServer {
81+
grpcServer := grpc.NewServer()
82+
lis, err := net.Listen("tcp", broadcastTestURL)
83+
if err != nil {
84+
panic(fmt.Sprintf("Error starting BroadcastServer %s", err))
85+
}
86+
broadcastServer := new(MockBroadcastServer)
87+
po.RegisterAtomicBroadcastServer(grpcServer, broadcastServer)
88+
fmt.Printf("Test broadcast server started\n")
89+
go grpcServer.Serve(lis)
90+
91+
return broadcastServer
92+
}

pkg/fabric-client/mocks/mockdata.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ package mocks
88

99
import (
1010
"github.com/golang/protobuf/proto"
11+
"github.com/golang/protobuf/ptypes/timestamp"
1112

13+
cutil "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/util"
14+
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/utils"
1215
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
1316
mb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
1417
ab "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/orderer"
1518
pp "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
1619

20+
"time"
21+
1722
channelConfig "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/channelconfig"
1823
ledger_util "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/core/ledger/util"
1924
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
@@ -459,3 +464,83 @@ func marshalOrPanic(pb proto.Message) []byte {
459464
}
460465
return data
461466
}
467+
468+
// CreateBlockWithCCEvent creates a mock block
469+
func CreateBlockWithCCEvent(events *pp.ChaincodeEvent, txID string,
470+
channelID string) (*common.Block, error) {
471+
chdr := &common.ChannelHeader{
472+
Type: int32(common.HeaderType_ENDORSER_TRANSACTION),
473+
Version: 1,
474+
Timestamp: &timestamp.Timestamp{
475+
Seconds: time.Now().Unix(),
476+
Nanos: 0,
477+
},
478+
ChannelId: channelID,
479+
TxId: txID}
480+
hdr := &common.Header{ChannelHeader: utils.MarshalOrPanic(chdr)}
481+
payload := &common.Payload{Header: hdr}
482+
cea := &pp.ChaincodeEndorsedAction{}
483+
ccaPayload := &pp.ChaincodeActionPayload{Action: cea}
484+
env := &common.Envelope{}
485+
taa := &pp.TransactionAction{}
486+
taas := make([]*pp.TransactionAction, 1)
487+
taas[0] = taa
488+
tx := &pp.Transaction{Actions: taas}
489+
490+
pHashBytes := []byte("proposal_hash")
491+
pResponse := &pp.Response{Status: 200}
492+
results := []byte("results")
493+
eventBytes, err := utils.GetBytesChaincodeEvent(events)
494+
if err != nil {
495+
return nil, err
496+
}
497+
ccaPayload.Action.ProposalResponsePayload, err = utils.GetBytesProposalResponsePayload(pHashBytes, pResponse, results, eventBytes, nil)
498+
if err != nil {
499+
return nil, err
500+
}
501+
tx.Actions[0].Payload, err = utils.GetBytesChaincodeActionPayload(ccaPayload)
502+
if err != nil {
503+
return nil, err
504+
}
505+
payload.Data, err = utils.GetBytesTransaction(tx)
506+
if err != nil {
507+
return nil, err
508+
}
509+
env.Payload, err = utils.GetBytesPayload(payload)
510+
if err != nil {
511+
return nil, err
512+
}
513+
ebytes, err := utils.GetBytesEnvelope(env)
514+
if err != nil {
515+
return nil, err
516+
}
517+
518+
block := newBlock(1, []byte{})
519+
block.Data.Data = append(block.Data.Data, ebytes)
520+
521+
blockbytes := cutil.ConcatenateBytes(block.Data.Data...)
522+
block.Header.DataHash = cutil.ComputeSHA256(blockbytes)
523+
524+
txsfltr := ledger_util.NewTxValidationFlags(len(block.Data.Data))
525+
526+
block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = txsfltr
527+
528+
return block, nil
529+
}
530+
531+
// NewBlock construct a block with no data and no metadata.
532+
func newBlock(seqNum uint64, previousHash []byte) *common.Block {
533+
block := &common.Block{}
534+
block.Header = &common.BlockHeader{}
535+
block.Header.Number = seqNum
536+
block.Header.PreviousHash = previousHash
537+
block.Data = &common.BlockData{}
538+
539+
var metadataContents [][]byte
540+
for i := 0; i < len(common.BlockMetadataIndex_name); i++ {
541+
metadataContents = append(metadataContents, []byte{})
542+
}
543+
block.Metadata = &common.BlockMetadata{Metadata: metadataContents}
544+
545+
return block
546+
}

scripts/third_party_pins/fabric/apply_fabric_client_utils.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ FILTER_FN=
135135
gofilter
136136

137137
FILTER_FILENAME="core/ledger/util/txvalidationflags.go"
138-
FILTER_FN="IsValid,IsInvalid,Flag,IsSetTo"
138+
FILTER_FN="IsValid,IsInvalid,Flag,IsSetTo,NewTxValidationFlags"
139139
gofilter
140140

141141
FILTER_FILENAME="events/consumer/adapter.go"

scripts/third_party_pins/fabric/apply_fabric_protos_internal.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ FILTER_FN+=",GetBytesTransaction,GetBytesPayload,GetHeader,GetBytesProposalRespo
6767
FILTER_FN+=",GetBytesChaincodeProposalPayload,CreateChaincodeProposalWithTransient,ComputeProposalTxID"
6868
FILTER_FN+=",CreateChaincodeProposalWithTxIDNonceAndTransient,CreateDeployProposalFromCDS,CreateUpgradeProposalFromCDS"
6969
FILTER_FN+=",createProposalFromCDS,CreateProposalFromCIS,CreateInstallProposalFromCDS,GetTransaction,GetPayload"
70-
FILTER_FN+=",GetChaincodeActionPayload,GetProposalResponsePayload,GetChaincodeAction,GetChaincodeEvents"
70+
FILTER_FN+=",GetChaincodeActionPayload,GetProposalResponsePayload,GetChaincodeAction,GetChaincodeEvents,GetBytesChaincodeEvent,GetBytesEnvelope"
7171
gofilter
7272

7373
FILTER_FILENAME="protos/utils/txutils.go"

0 commit comments

Comments
 (0)