Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
FolderOrigin-RevId: /tmp/fabric-sdk
  • Loading branch information
Deploy authored and Deploy committed Sep 3, 2019
1 parent b69abba commit 35a0aec
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 28 deletions.
2 changes: 1 addition & 1 deletion client/chaincode/system/qscc.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *qscc) GetTransactionByID(ctx context.Context, channelName string, tx ap
}

func (c *qscc) GetBlockByTxID(ctx context.Context, channelName string, tx api.ChaincodeTx) (*common.Block, error) {
if blockBytes, err := c.endorse(ctx, qsccPkg.GetBlockByTxID, string(tx)); err != nil {
if blockBytes, err := c.endorse(ctx, qsccPkg.GetBlockByTxID, channelName, string(tx)); err != nil {
return nil, errors.Wrap(err, `failed to get block`)
} else {
block := new(common.Block)
Expand Down
28 changes: 24 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,42 @@ module github.com/s7techlab/hlf-sdk-go
go 1.12

require (
github.com/Shopify/sarama v1.22.1 // indirect
github.com/cloudflare/cfssl v0.0.0-20190627231140-2001f384ec4f
github.com/DataDog/zstd v1.4.0 // indirect
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/Shopify/sarama v1.23.1 // indirect
github.com/cloudflare/cfssl v0.0.0-20190510060611-9c027c93ba9e
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gogo/protobuf v1.2.1
github.com/golang/protobuf v1.3.1
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
github.com/hashicorp/go-version v1.2.0 // indirect
github.com/hyperledger/fabric v1.4.1
github.com/hyperledger/fabric-lib-go v1.0.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/pkg/errors v0.8.1
github.com/s7techlab/cckit v0.6.0
github.com/s7techlab/cckit v0.6.3
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.3.0
github.com/sykesm/zap-logfmt v0.0.2 // indirect
github.com/willf/bitset v1.1.10 // indirect
go.opencensus.io v0.22.0
go.uber.org/zap v1.10.0
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
google.golang.org/grpc v1.21.1
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
google.golang.org/genproto v0.0.0-20190701230453-710ae3a149df // indirect
google.golang.org/grpc v1.22.0
gopkg.in/jcmturner/goidentity.v3 v3.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2
)

replace github.com/golang/lint => golang.org/x/lint v0.0.0-20190409202823-959b441ac422

replace git.apache.org/thrift.git => github.com/apache/thrift v0.12.0
111 changes: 89 additions & 22 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions peer/deliver/subs/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (ts *TxSubscription) Handler(block *common.Block) bool {

//println("TXID", chHeader.TxId, txFilter.IsValid(i))
if api.ChaincodeTx(chHeader.TxId) == ts.txId {
//defer ts.ErrorCloser.Close()
if txFilter.IsValid(i) {
ts.result <- &result{code: txFilter.Flag(i), err: nil}
return true
Expand Down
98 changes: 98 additions & 0 deletions util/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package util

import (
"bytes"
"context"

"github.com/gogo/protobuf/proto"
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
"github.com/pkg/errors"

"github.com/s7techlab/hlf-sdk-go/api"
)

var (
ErrOrdererGroupNotFound = errors.New(`orderer addresses not found`)
)

func GetOrdererHostFromChannelConfig(conf *common.Config) (string, error) {
ordValues, ok := conf.ChannelGroup.Values[channelconfig.OrdererAddressesKey]
if !ok {
return ``, ErrOrdererGroupNotFound
}

ordererAddresses := common.OrdererAddresses{}
if err := proto.Unmarshal(ordValues.Value, &ordererAddresses); err != nil {
return ``, errors.Wrap(err, `failed to unmarshal orderer addresses`)
}

// TODO return all addresses instead of first
return ordererAddresses.Addresses[0], nil
}

func ProceedChannelUpdate(ctx context.Context, channelName string, update *common.ConfigUpdate, orderer api.Orderer, id msp.SigningIdentity) error {
confUpdBytes, err := proto.Marshal(update)
if err != nil {
return errors.Wrap(err, `failed to marshal common.ConfigUpdate`)
}

txId, nonce, err := NewTxWithNonce(id)
if err != nil {
return errors.Wrap(err, `failed to get nonce`)
}

signatureHeader, err := NewSignatureHeader(id, nonce)
if err != nil {
return errors.Wrap(err, `failed to get signature header`)
}

buf := bytes.NewBuffer(signatureHeader)
buf.Write(confUpdBytes)

signature, err := id.Sign(buf.Bytes())
if err != nil {
return errors.Wrap(err, `failed to sign bytes`)
}

sig := &common.ConfigSignature{
SignatureHeader: signatureHeader,
Signature: signature,
}

confUpdEnvelope := &common.ConfigUpdateEnvelope{
ConfigUpdate: confUpdBytes,
Signatures: []*common.ConfigSignature{sig},
}

confUpdEnvBytes, err := proto.Marshal(confUpdEnvelope)
if err != nil {
return errors.Wrap(err, `failed to marshal common.ConfigUpdateEnvelope`)
}

channelHeader, err := NewChannelHeader(common.HeaderType_CONFIG_UPDATE, txId, channelName, 0, nil)
if err != nil {
return errors.Wrap(err, `failed to get channel header`)
}

payload, err := NewPayloadFromHeader(channelHeader, signatureHeader, confUpdEnvBytes)
if err != nil {
return errors.Wrap(err, `failed to get payload`)
}

envelope := &common.Envelope{
Payload: payload,
}

envelope.Signature, err = id.Sign(envelope.Payload)
if err != nil {
return errors.WithMessage(err, "signing payload failed")
}

if _, err := orderer.Broadcast(ctx, envelope); err != nil {
return errors.WithMessage(err, "failed broadcast to orderer")
}

return nil
}
46 changes: 46 additions & 0 deletions util/orderer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package util

import (
"context"

"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"

"github.com/s7techlab/hlf-sdk-go/api"
)

// GetConfigBlockFromOrderer returns config block from orderer by channel name
func GetConfigBlockFromOrderer(ctx context.Context, id msp.SigningIdentity, orderer api.Orderer, channelName string) (*common.Block, error) {
startPos, endPos := api.SeekNewest()()

seekEnvelope, err := SeekEnvelope(channelName, startPos, endPos, id)
if err != nil {
return nil, errors.Wrap(err, `failed to create seek envelope`)
}

lastBlock, err := orderer.Deliver(ctx, seekEnvelope)
if err != nil {
return nil, errors.Wrap(err, `failed to fetch last block`)
}

blockId, err := utils.GetLastConfigIndexFromBlock(lastBlock)
if err != nil {
return nil, errors.Wrap(err, `failed to fetch block id with config`)
}

startPos, endPos = api.SeekSingle(blockId)()

seekEnvelope, err = SeekEnvelope(channelName, startPos, endPos, id)
if err != nil {
return nil, errors.Wrap(err, `failed to create seek envelope`)
}

configBlock, err := orderer.Deliver(ctx, seekEnvelope)
if err != nil {
return nil, errors.Wrap(err, `failed to fetch block with config`)
}

return configBlock, nil
}
3 changes: 2 additions & 1 deletion util/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"crypto/sha256"
"encoding/hex"

"github.com/s7techlab/hlf-sdk-go/crypto"
"github.com/hyperledger/fabric/msp"
"github.com/pkg/errors"

"github.com/s7techlab/hlf-sdk-go/crypto"
)

// NewTxWithNonce generates new transaction id with crypto nonce
Expand Down

0 comments on commit 35a0aec

Please sign in to comment.