Skip to content

Commit

Permalink
[FAB-6695] Fixes for default config and protos
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
sudeshrshetty committed Oct 20, 2017
1 parent 199fae9 commit 23ec481
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import (
// TxValidationFlags is array of transaction validation codes. It is used when committer validates block.
type TxValidationFlags []uint8

// NewTxValidationFlags Create new object-array of validation codes with target size. Default values: valid.
func NewTxValidationFlags(size int) TxValidationFlags {
inst := make(TxValidationFlags, size)
return inst
}

// Flag returns validation code at specified transaction
func (obj TxValidationFlags) Flag(txIndex int) peer.TxValidationCode {
return peer.TxValidationCode(obj[txIndex])
Expand Down
12 changes: 12 additions & 0 deletions internal/github.com/hyperledger/fabric/protos/utils/proputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ func GetBytesChaincodeProposalPayload(cpp *peer.ChaincodeProposalPayload) ([]byt
return cppBytes, err
}

// GetBytesChaincodeEvent gets the bytes of ChaincodeEvent
func GetBytesChaincodeEvent(event *peer.ChaincodeEvent) ([]byte, error) {
eventBytes, err := proto.Marshal(event)
return eventBytes, err
}

// GetBytesChaincodeActionPayload get the bytes of ChaincodeActionPayload from the message
func GetBytesChaincodeActionPayload(cap *peer.ChaincodeActionPayload) ([]byte, error) {
capBytes, err := proto.Marshal(cap)
Expand All @@ -208,6 +214,12 @@ func GetBytesPayload(payl *common.Payload) ([]byte, error) {
return bytes, err
}

// GetBytesEnvelope get the bytes of Envelope from the message
func GetBytesEnvelope(env *common.Envelope) ([]byte, error) {
bytes, err := proto.Marshal(env)
return bytes, err
}

// CreateProposalFromCIS returns a proposal given a serialized identity and a ChaincodeInvocationSpec
func CreateProposalFromCIS(typ common.HeaderType, chainID string, cis *peer.ChaincodeInvocationSpec, creator []byte) (*peer.Proposal, string, error) {
return CreateChaincodeProposal(typ, chainID, cis, creator)
Expand Down
12 changes: 5 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"math/rand"
"os"
"path"
"path/filepath"
"strings"
"time"

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

// load Default confid
// load Default config
func loadDefaultConfig(myViper *viper.Viper) error {
// get Environment Default Config Path
defaultPath := os.Getenv("FABRIC_SDK_CONFIG_PATH")
if defaultPath != "" { // if set, use it to load default config
myViper.AddConfigPath(strings.Replace(defaultPath, "$GOPATH", os.Getenv("GOPATH"), -1))
} else { // else fallback to default DEV path
devPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "hyperledger", "fabric-sdk-go", "pkg", "config")
myViper.AddConfigPath(devPath)
if defaultPath == "" {
return nil
}
// if set, use it to load default config
myViper.AddConfigPath(strings.Replace(defaultPath, "$GOPATH", os.Getenv("GOPATH"), -1))
err := myViper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return errors.Wrap(err, "loading config file failed")
Expand Down
19 changes: 19 additions & 0 deletions pkg/fabric-client/mocks/mockbroadcastserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ package mocks
import (
"io"

"fmt"
"net"

po "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
"google.golang.org/grpc"
)

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

return nil
}

//StartMockBroadcastServer starts mock server for unit testing purpose
func StartMockBroadcastServer(broadcastTestURL string) *MockBroadcastServer {
grpcServer := grpc.NewServer()
lis, err := net.Listen("tcp", broadcastTestURL)
if err != nil {
panic(fmt.Sprintf("Error starting BroadcastServer %s", err))
}
broadcastServer := new(MockBroadcastServer)
po.RegisterAtomicBroadcastServer(grpcServer, broadcastServer)
fmt.Printf("Test broadcast server started\n")
go grpcServer.Serve(lis)

return broadcastServer
}
85 changes: 85 additions & 0 deletions pkg/fabric-client/mocks/mockdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ package mocks

import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"

cutil "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/utils"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
mb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
ab "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/orderer"
pp "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"

"time"

channelConfig "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/channelconfig"
ledger_util "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
Expand Down Expand Up @@ -459,3 +464,83 @@ func marshalOrPanic(pb proto.Message) []byte {
}
return data
}

// CreateBlockWithCCEvent creates a mock block
func CreateBlockWithCCEvent(events *pp.ChaincodeEvent, txID string,
channelID string) (*common.Block, error) {
chdr := &common.ChannelHeader{
Type: int32(common.HeaderType_ENDORSER_TRANSACTION),
Version: 1,
Timestamp: &timestamp.Timestamp{
Seconds: time.Now().Unix(),
Nanos: 0,
},
ChannelId: channelID,
TxId: txID}
hdr := &common.Header{ChannelHeader: utils.MarshalOrPanic(chdr)}
payload := &common.Payload{Header: hdr}
cea := &pp.ChaincodeEndorsedAction{}
ccaPayload := &pp.ChaincodeActionPayload{Action: cea}
env := &common.Envelope{}
taa := &pp.TransactionAction{}
taas := make([]*pp.TransactionAction, 1)
taas[0] = taa
tx := &pp.Transaction{Actions: taas}

pHashBytes := []byte("proposal_hash")
pResponse := &pp.Response{Status: 200}
results := []byte("results")
eventBytes, err := utils.GetBytesChaincodeEvent(events)
if err != nil {
return nil, err
}
ccaPayload.Action.ProposalResponsePayload, err = utils.GetBytesProposalResponsePayload(pHashBytes, pResponse, results, eventBytes, nil)
if err != nil {
return nil, err
}
tx.Actions[0].Payload, err = utils.GetBytesChaincodeActionPayload(ccaPayload)
if err != nil {
return nil, err
}
payload.Data, err = utils.GetBytesTransaction(tx)
if err != nil {
return nil, err
}
env.Payload, err = utils.GetBytesPayload(payload)
if err != nil {
return nil, err
}
ebytes, err := utils.GetBytesEnvelope(env)
if err != nil {
return nil, err
}

block := newBlock(1, []byte{})
block.Data.Data = append(block.Data.Data, ebytes)

blockbytes := cutil.ConcatenateBytes(block.Data.Data...)
block.Header.DataHash = cutil.ComputeSHA256(blockbytes)

txsfltr := ledger_util.NewTxValidationFlags(len(block.Data.Data))

block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = txsfltr

return block, nil
}

// NewBlock construct a block with no data and no metadata.
func newBlock(seqNum uint64, previousHash []byte) *common.Block {
block := &common.Block{}
block.Header = &common.BlockHeader{}
block.Header.Number = seqNum
block.Header.PreviousHash = previousHash
block.Data = &common.BlockData{}

var metadataContents [][]byte
for i := 0; i < len(common.BlockMetadataIndex_name); i++ {
metadataContents = append(metadataContents, []byte{})
}
block.Metadata = &common.BlockMetadata{Metadata: metadataContents}

return block
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ FILTER_FN=
gofilter

FILTER_FILENAME="core/ledger/util/txvalidationflags.go"
FILTER_FN="IsValid,IsInvalid,Flag,IsSetTo"
FILTER_FN="IsValid,IsInvalid,Flag,IsSetTo,NewTxValidationFlags"
gofilter

FILTER_FILENAME="events/consumer/adapter.go"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ FILTER_FN+=",GetBytesTransaction,GetBytesPayload,GetHeader,GetBytesProposalRespo
FILTER_FN+=",GetBytesChaincodeProposalPayload,CreateChaincodeProposalWithTransient,ComputeProposalTxID"
FILTER_FN+=",CreateChaincodeProposalWithTxIDNonceAndTransient,CreateDeployProposalFromCDS,CreateUpgradeProposalFromCDS"
FILTER_FN+=",createProposalFromCDS,CreateProposalFromCIS,CreateInstallProposalFromCDS,GetTransaction,GetPayload"
FILTER_FN+=",GetChaincodeActionPayload,GetProposalResponsePayload,GetChaincodeAction,GetChaincodeEvents"
FILTER_FN+=",GetChaincodeActionPayload,GetProposalResponsePayload,GetChaincodeAction,GetChaincodeEvents,GetBytesChaincodeEvent,GetBytesEnvelope"
gofilter

FILTER_FILENAME="protos/utils/txutils.go"
Expand Down

0 comments on commit 23ec481

Please sign in to comment.