本文整理匯總了Golang中github.com/hyperledger/fabric/core/ledger.GetLedger函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetLedger函數的具體用法?Golang GetLedger怎麽用?Golang GetLedger使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetLedger函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ExecuteTransactions
//ExecuteTransactions - will execute transactions on the array one by one
//will return an array of errors one for each transaction. If the execution
//succeeded, array element will be nil. returns []byte of state hash or
//error
func ExecuteTransactions(ctxt context.Context, cname ChainName, xacts []*pb.Transaction) (succeededTXs []*pb.Transaction, stateHash []byte, ccevents []*pb.ChaincodeEvent, txerrs []error, err error) {
var chain = GetChain(cname)
if chain == nil {
// TODO: We should never get here, but otherwise a good reminder to better handle
panic(fmt.Sprintf("[ExecuteTransactions]Chain %s not found\n", cname))
}
txerrs = make([]error, len(xacts))
ccevents = make([]*pb.ChaincodeEvent, len(xacts))
var succeededTxs = make([]*pb.Transaction, 0)
for i, t := range xacts {
_, ccevents[i], txerrs[i] = Execute(ctxt, chain, t)
if txerrs[i] == nil {
succeededTxs = append(succeededTxs, t)
}
}
var lgr *ledger.Ledger
lgr, err = ledger.GetLedger()
if err == nil {
stateHash, err = lgr.GetTempStateHash()
}
return succeededTxs, stateHash, ccevents, txerrs, err
}
示例2: invoke
// Invoke or query a chaincode.
func invoke(ctx context.Context, spec *pb.ChaincodeSpec, typ pb.Transaction_Type) (*pb.ChaincodeEvent, string, []byte, error) {
chaincodeInvocationSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
// Now create the Transactions message and send to Peer.
uuid := util.GenerateUUID()
var transaction *pb.Transaction
var err error
transaction, err = pb.NewChaincodeExecute(chaincodeInvocationSpec, uuid, typ)
if err != nil {
return nil, uuid, nil, fmt.Errorf("Error invoking chaincode: %s ", err)
}
var retval []byte
var execErr error
var ccevt *pb.ChaincodeEvent
if typ == pb.Transaction_CHAINCODE_QUERY {
retval, ccevt, execErr = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
} else {
ledger, _ := ledger.GetLedger()
ledger.BeginTxBatch("1")
retval, ccevt, execErr = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
if err != nil {
return nil, uuid, nil, fmt.Errorf("Error invoking chaincode: %s ", err)
}
ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil)
}
return ccevt, uuid, retval, execErr
}
示例3: NewPeerWithEngine
// NewPeerWithHandler returns a Peer which uses the supplied handler factory function for creating new handlers on new Chat service invocations.
func NewPeerWithEngine(secHelperFunc func() crypto.Peer, engFactory EngineFactory) (peer *PeerImpl, err error) {
peer = new(PeerImpl)
peer.handlerMap = &handlerMap{m: make(map[pb.PeerID]MessageHandler)}
peer.isValidator = ValidatorEnabled()
peer.secHelper = secHelperFunc()
// Install security object for peer
if SecurityEnabled() {
if peer.secHelper == nil {
return nil, fmt.Errorf("Security helper not provided")
}
}
peer.engine, err = engFactory(peer)
if err != nil {
return nil, err
}
peer.handlerFactory = peer.engine.GetHandlerFactory()
if peer.handlerFactory == nil {
return nil, errors.New("Cannot supply nil handler factory")
}
ledgerPtr, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Error constructing NewPeerWithHandler: %s", err)
}
peer.ledgerWrapper = &ledgerWrapper{ledger: ledgerPtr}
go peer.chatWithPeer(viper.GetString("peer.discovery.rootnode"))
return peer, nil
}
示例4: deploy
// Deploy a chaincode - i.e., build and initialize.
func deploy(ctx context.Context, spec *pb.ChaincodeSpec) ([]byte, error) {
// First build and get the deployment spec
chaincodeDeploymentSpec, err := getDeploymentSpec(ctx, spec)
if err != nil {
return nil, err
}
tid := chaincodeDeploymentSpec.ChaincodeSpec.ChaincodeID.Name
// Now create the Transactions message and send to Peer.
transaction, err := createDeployTransaction(chaincodeDeploymentSpec, tid)
if err != nil {
return nil, fmt.Errorf("Error deploying chaincode: %s ", err)
}
ledger, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Failed to get handle to ledger: %s ", err)
}
ledger.BeginTxBatch("1")
b, err := Execute(ctx, GetChain(DefaultChain), transaction)
if err != nil {
return nil, fmt.Errorf("Error deploying chaincode: %s", err)
}
ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil)
return b, err
}
示例5: NewPeerWithHandler
// NewPeerWithHandler returns a Peer which uses the supplied handler factory function for creating new handlers on new Chat service invocations.
func NewPeerWithHandler(secHelperFunc func() crypto.Peer, handlerFact HandlerFactory, discInstance discovery.Discovery) (*PeerImpl, error) {
peer := new(PeerImpl)
peer.discoverySvc = discInstance
if handlerFact == nil {
return nil, errors.New("Cannot supply nil handler factory")
}
peer.handlerFactory = handlerFact
peer.handlerMap = &handlerMap{m: make(map[pb.PeerID]MessageHandler)}
peer.secHelper = secHelperFunc()
// Install security object for peer
if SecurityEnabled() {
if peer.secHelper == nil {
return nil, fmt.Errorf("Security helper not provided")
}
}
ledgerPtr, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Error constructing NewPeerWithHandler: %s", err)
}
peer.ledgerWrapper = &ledgerWrapper{ledger: ledgerPtr}
peer.chatWithSomePeers(peer.discoverySvc.GetRootNodes())
return peer, nil
}
示例6: VerifyBlockchain
// VerifyBlockchain checks the integrity of the blockchain between indices start and finish,
// returning the first block who's PreviousBlockHash field does not match the hash of the previous block
func (h *Helper) VerifyBlockchain(start, finish uint64) (uint64, error) {
ledger, err := ledger.GetLedger()
if err != nil {
return finish, fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.VerifyChain(start, finish)
}
示例7: GetBlock
// GetBlock returns a block from the chain
func (h *Helper) GetBlock(blockNumber uint64) (block *pb.Block, err error) {
ledger, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.GetBlockByNumber(blockNumber)
}
示例8: getBlockData
func (i *Noops) getBlockData() (*pb.Block, *statemgmt.StateDelta, error) {
ledger, err := ledger.GetLedger()
if err != nil {
return nil, nil, fmt.Errorf("Fail to get the ledger: %v", err)
}
blockHeight := ledger.GetBlockchainSize()
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Preparing to broadcast with block number %v", blockHeight)
}
block, err := ledger.GetBlockByNumber(blockHeight - 1)
if nil != err {
return nil, nil, err
}
//delta, err := ledger.GetStateDeltaBytes(blockHeight)
delta, err := ledger.GetStateDelta(blockHeight - 1)
if nil != err {
return nil, nil, err
}
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Got the delta state of block number %v", blockHeight)
}
return block, delta, nil
}
示例9: transferOwnership
func transferOwnership(owner crypto.Client, ownerCert crypto.CertificateHandler, asset string, newOwnerCert crypto.CertificateHandler) error {
// Get a transaction handler to be used to submit the execute transaction
// and bind the chaincode access control logic using the binding
submittingCertHandler, err := owner.GetTCertificateHandlerNext()
if err != nil {
return err
}
txHandler, err := submittingCertHandler.GetTransactionHandler()
if err != nil {
return err
}
binding, err := txHandler.GetBinding()
if err != nil {
return err
}
chaincodeInput := &pb.ChaincodeInput{Function: "transfer", Args: []string{asset, string(newOwnerCert.GetCertificate())}}
chaincodeInputRaw, err := proto.Marshal(chaincodeInput)
if err != nil {
return err
}
// Access control. Owner signs chaincodeInputRaw || binding to confirm his identity
sigma, err := ownerCert.Sign(append(chaincodeInputRaw, binding...))
if err != nil {
return err
}
// Prepare spec and submit
spec := &pb.ChaincodeSpec{
Type: 1,
ChaincodeID: &pb.ChaincodeID{Name: "mycc"},
CtorMsg: chaincodeInput,
Metadata: sigma, // Proof of identity
ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC,
}
var ctx = context.Background()
chaincodeInvocationSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
tid := chaincodeInvocationSpec.ChaincodeSpec.ChaincodeID.Name
// Now create the Transactions message and send to Peer.
transaction, err := txHandler.NewChaincodeExecute(chaincodeInvocationSpec, tid)
if err != nil {
return fmt.Errorf("Error deploying chaincode: %s ", err)
}
ledger, err := ledger.GetLedger()
ledger.BeginTxBatch("1")
_, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
if err != nil {
return fmt.Errorf("Error deploying chaincode: %s", err)
}
ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil)
return err
}
示例10: GetCurrentStateHash
// GetCurrentStateHash returns the current/temporary state hash
func (h *Helper) GetCurrentStateHash() (stateHash []byte, err error) {
ledger, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.GetTempStateHash()
}
示例11: CommitTxBatch
// CommitTxBatch gets invoked when the current transaction-batch needs
// to be committed. This function returns successfully iff the
// transactions details and state changes (that may have happened
// during execution of this transaction-batch) have been committed to
// permanent storage.
func (h *Helper) CommitTxBatch(id interface{}, metadata []byte) (*pb.Block, error) {
ledger, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Failed to get the ledger: %v", err)
}
// TODO fix this one the ledger has been fixed to implement
if err := ledger.CommitTxBatch(id, h.curBatch, h.curBatchErrs, metadata); err != nil {
return nil, fmt.Errorf("Failed to commit transaction to the ledger: %v", err)
}
size := ledger.GetBlockchainSize()
defer func() {
h.curBatch = nil // TODO, remove after issue 579
h.curBatchErrs = nil // TODO, remove after issue 579
}()
block, err := ledger.GetBlockByNumber(size - 1)
if err != nil {
return nil, fmt.Errorf("Failed to get the block at the head of the chain: %v", err)
}
logger.Debugf("Committed block with %d transactions, intended to include %d", len(block.Transactions), len(h.curBatch))
return block, nil
}
示例12: GetBlockchainSize
// GetBlockchainSize returns the current size of the blockchain
func (h *Helper) GetBlockchainSize() (uint64, error) {
ledger, err := ledger.GetLedger()
if err != nil {
return 0, fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.GetBlockchainSize(), nil
}
示例13: EmptyState
// EmptyState completely empties the state and prepares it to restore a snapshot
func (h *Helper) EmptyState() error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.DeleteALLStateKeysAndValues()
}
示例14: deploy
func deploy(admCert crypto.CertificateHandler) error {
// Prepare the spec. The metadata includes the identity of the administrator
spec := &pb.ChaincodeSpec{
Type: 1,
ChaincodeID: &pb.ChaincodeID{Name: "mycc"},
CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("init")},
Metadata: admCert.GetCertificate(),
ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC,
}
// First build and get the deployment spec
var ctx = context.Background()
chaincodeDeploymentSpec, err := getDeploymentSpec(ctx, spec)
if err != nil {
return err
}
tid := chaincodeDeploymentSpec.ChaincodeSpec.ChaincodeID.Name
// Now create the Transactions message and send to Peer.
transaction, err := administrator.NewChaincodeDeployTransaction(chaincodeDeploymentSpec, tid)
if err != nil {
return fmt.Errorf("Error deploying chaincode: %s ", err)
}
ledger, err := ledger.GetLedger()
ledger.BeginTxBatch("1")
_, _, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
if err != nil {
return fmt.Errorf("Error deploying chaincode: %s", err)
}
ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil)
return err
}
示例15: RollbackStateDelta
// RollbackStateDelta undoes the results of ApplyStateDelta to revert
// the current state back to the state before ApplyStateDelta was invoked
func (h *Helper) RollbackStateDelta(id interface{}) error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.RollbackStateDelta(id)
}