• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang ledger.Ledger类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/hyperledger/fabric/core/ledger.Ledger的典型用法代码示例。如果您正苦于以下问题:Golang Ledger类的具体用法?Golang Ledger怎么用?Golang Ledger使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Ledger类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:ysj3223498,项目名称:fabric,代码行数:29,代码来源:exectransaction.go


示例2: appendAll

	"github.com/hyperledger/fabric/core/ledger"
	"github.com/hyperledger/fabric/core/ledger/statemgmt"
	"github.com/hyperledger/fabric/core/util"
	"github.com/hyperledger/fabric/protos"
)

func appendAll(content ...[]byte) []byte {
	combinedContent := []byte{}
	for _, b := range content {
		combinedContent = append(combinedContent, b...)
	}
	return combinedContent
}

var _ = Describe("Ledger", func() {
	var ledgerPtr *ledger.Ledger

	SetupTestConfig()

	Context("Ledger with preexisting uncommitted state", func() {

		BeforeEach(func() {
			ledgerPtr = InitSpec()

			Expect(ledgerPtr.BeginTxBatch(1)).To(BeNil())
			ledgerPtr.TxBegin("txUuid")
			Expect(ledgerPtr.SetState("chaincode1", "key1", []byte("value1"))).To(BeNil())
			Expect(ledgerPtr.SetState("chaincode2", "key2", []byte("value2"))).To(BeNil())
			Expect(ledgerPtr.SetState("chaincode3", "key3", []byte("value3"))).To(BeNil())
			ledgerPtr.TxFinished("txUuid", true)
		})
开发者ID:Colearo,项目名称:fabric,代码行数:31,代码来源:ledger_test.go


示例3: markTxFinish

func markTxFinish(ledger *ledger.Ledger, t *pb.Transaction, successful bool) {
	if t.Type == pb.Transaction_CHAINCODE_QUERY {
		return
	}
	ledger.TxFinished(t.Uuid, successful)
}
开发者ID:magooster,项目名称:obc-peer,代码行数:6,代码来源:exectransaction.go


示例4: markTxBegin

func markTxBegin(ledger *ledger.Ledger, t *pb.Transaction) {
	if t.Type == pb.Transaction_CHAINCODE_QUERY {
		return
	}
	ledger.TxBegin(t.Uuid)
}
开发者ID:magooster,项目名称:obc-peer,代码行数:6,代码来源:exectransaction.go


示例5: buildTestLedger2

// buildTestLedger2 builds a simple ledger data structure that contains a blockchain
// of 5 blocks, with each block containing the same number of transactions as its
// index within the blockchain. Block 0, 0 transactions. Block 1, 1 transaction,
// and so on.
func buildTestLedger2(ledger *ledger.Ledger, t *testing.T) {
	// -----------------------------<Block #0>---------------------
	// Add the 0th (genesis block)
	ledger.BeginTxBatch(0)
	ledger.CommitTxBatch(0, []*protos.Transaction{}, nil, []byte("dummy-proof"))
	// -----------------------------<Block #0>---------------------

	// -----------------------------<Block #1>------------------------------------

	// Deploy a contract
	// To deploy a contract, we call the 'NewContract' function in the 'Contracts' contract
	// TODO Use chaincode instead of contract?
	// TODO Two types of transactions. Execute transaction, deploy/delete/update contract
	ledger.BeginTxBatch(1)
	transaction1a, err := protos.NewTransaction(protos.ChaincodeID{Path: "Contracts"}, generateUUID(t), "NewContract", []string{"name: MyContract1, code: var x; function setX(json) {x = json.x}}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	// VM runs transaction1a and updates the global state with the result
	// In this case, the 'Contracts' contract stores 'MyContract1' in its state
	ledger.TxBegin(transaction1a.Txid)
	ledger.SetState("MyContract1", "code", []byte("code example"))
	ledger.TxFinished(transaction1a.Txid, true)
	ledger.CommitTxBatch(1, []*protos.Transaction{transaction1a}, nil, []byte("dummy-proof"))

	// -----------------------------</Block #1>-----------------------------------

	// -----------------------------<Block #2>------------------------------------

	ledger.BeginTxBatch(2)
	transaction2a, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyContract"}, generateUUID(t), "setX", []string{"{x: \"hello\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	transaction2b, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyOtherContract"}, generateUUID(t), "setY", []string{"{y: \"goodbuy\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}

	// Run this transction in the VM. The VM updates the state
	ledger.TxBegin(transaction2a.Txid)
	ledger.SetState("MyContract", "x", []byte("hello"))
	ledger.SetState("MyOtherContract", "y", []byte("goodbuy"))
	ledger.TxFinished(transaction2a.Txid, true)

	// Commit txbatch that creates the 2nd block on blockchain
	ledger.CommitTxBatch(2, []*protos.Transaction{transaction2a, transaction2b}, nil, []byte("dummy-proof"))
	// -----------------------------</Block #2>-----------------------------------

	// -----------------------------<Block #3>------------------------------------

	ledger.BeginTxBatch(3)
	transaction3a, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyContract"}, generateUUID(t), "setX", []string{"{x: \"hello\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	transaction3b, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyOtherContract"}, generateUUID(t), "setY", []string{"{y: \"goodbuy\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	transaction3c, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyImportantContract"}, generateUUID(t), "setZ", []string{"{z: \"super\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	ledger.TxBegin(transaction3a.Txid)
	ledger.SetState("MyContract", "x", []byte("hello"))
	ledger.SetState("MyOtherContract", "y", []byte("goodbuy"))
	ledger.SetState("MyImportantContract", "z", []byte("super"))
	ledger.TxFinished(transaction3a.Txid, true)
	ledger.CommitTxBatch(3, []*protos.Transaction{transaction3a, transaction3b, transaction3c}, nil, []byte("dummy-proof"))

	// -----------------------------</Block #3>-----------------------------------

	// -----------------------------<Block #4>------------------------------------

	ledger.BeginTxBatch(4)
	// Now we want to run the function 'setX' in 'MyContract

	// Create a transaction'
	transaction4a, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyContract"}, generateUUID(t), "setX", []string{"{x: \"hello\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	transaction4b, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyOtherContract"}, generateUUID(t), "setY", []string{"{y: \"goodbuy\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	transaction4c, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyImportantContract"}, generateUUID(t), "setZ", []string{"{z: \"super\"}"})
//.........这里部分代码省略.........
开发者ID:yoshiharay,项目名称:fabric,代码行数:101,代码来源:api_test.go


示例6: buildTestLedger1

// buildTestLedger1 builds a simple ledger data structure that contains a blockchain with 3 blocks.
func buildTestLedger1(ledger1 *ledger.Ledger, t *testing.T) {
	// -----------------------------<Block #0>---------------------
	// Add the 0th (genesis block)
	ledger1.BeginTxBatch(0)
	err := ledger1.CommitTxBatch(0, []*protos.Transaction{}, nil, []byte("dummy-proof"))
	if err != nil {
		t.Fatalf("Error in commit: %s", err)
	}

	// -----------------------------<Block #0>---------------------

	// -----------------------------<Block #1>------------------------------------

	// Deploy a contract
	// To deploy a contract, we call the 'NewContract' function in the 'Contracts' contract
	// TODO Use chaincode instead of contract?
	// TODO Two types of transactions. Execute transaction, deploy/delete/update contract
	ledger1.BeginTxBatch(1)
	transaction1a, err := protos.NewTransaction(protos.ChaincodeID{Path: "Contracts"}, generateUUID(t), "NewContract", []string{"name: MyContract1, code: var x; function setX(json) {x = json.x}}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	// VM runs transaction1a and updates the global state with the result
	// In this case, the 'Contracts' contract stores 'MyContract1' in its state
	ledger1.TxBegin(transaction1a.Txid)
	ledger1.SetState("MyContract1", "code", []byte("code example"))
	ledger1.TxFinished(transaction1a.Txid, true)
	ledger1.CommitTxBatch(1, []*protos.Transaction{transaction1a}, nil, []byte("dummy-proof"))
	// -----------------------------</Block #1>-----------------------------------

	// -----------------------------<Block #2>------------------------------------

	ledger1.BeginTxBatch(2)
	transaction2a, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyContract"}, generateUUID(t), "setX", []string{"{x: \"hello\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	transaction2b, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyOtherContract"}, generateUUID(t), "setY", []string{"{y: \"goodbuy\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}

	// Run this transction in the VM. The VM updates the state
	ledger1.TxBegin(transaction2a.Txid)
	ledger1.SetState("MyContract", "x", []byte("hello"))
	ledger1.SetState("MyOtherContract", "y", []byte("goodbuy"))
	ledger1.TxFinished(transaction2a.Txid, true)

	// Commit txbatch that creates the 2nd block on blockchain
	ledger1.CommitTxBatch(2, []*protos.Transaction{transaction2a, transaction2b}, nil, []byte("dummy-proof"))
	// -----------------------------</Block #2>-----------------------------------
	return
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:57,代码来源:api_test.go



注:本文中的github.com/hyperledger/fabric/core/ledger.Ledger类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang ledger.TxSimulator类代码示例发布时间:2022-05-28
下一篇:
Golang ledger.InitTestLedger函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap