-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project import generated by Copybara.
FolderOrigin-RevId: /tmp/fabric-sdk
- Loading branch information
Deploy
authored and
Deploy
committed
Sep 3, 2019
1 parent
b69abba
commit 35a0aec
Showing
7 changed files
with
261 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters