Пример #1
0
// 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.Uuid)
	ledger1.SetState("MyContract1", "code", []byte("code example"))
	ledger1.TxFinished(transaction1a.Uuid, 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.Uuid)
	ledger1.SetState("MyContract", "x", []byte("hello"))
	ledger1.SetState("MyOtherContract", "y", []byte("goodbuy"))
	ledger1.TxFinished(transaction2a.Uuid, true)

	// Commit txbatch that creates the 2nd block on blockchain
	ledger1.CommitTxBatch(2, []*protos.Transaction{transaction2a, transaction2b}, nil, []byte("dummy-proof"))
	// -----------------------------</Block #2>-----------------------------------
	return
}
Пример #2
0
func (testWrapper *blockchainTestWrapper) populateBlockChainWithSampleData() (blocks []*protos.Block, hashes [][]byte, err error) {
	var allBlocks []*protos.Block
	var allHashes [][]byte

	// -----------------------------<Genesis block>-------------------------------
	// Add the first (genesis block)
	block1 := protos.NewBlock(nil, nil)
	allBlocks = append(allBlocks, block1)
	allHashes = append(allHashes, []byte("stateHash1"))
	testWrapper.addNewBlock(block1, []byte("stateHash1"))

	// -----------------------------</Genesis block>------------------------------

	// -----------------------------<Block 2>-------------------------------------
	// Deploy a chaincode
	transaction2a, err := protos.NewTransaction(protos.ChaincodeID{Path: "Contracts"}, testutil.GenerateUUID(testWrapper.t), "NewContract", []string{"name: MyContract1, code: var x; function setX(json) {x = json.x}}"})
	if err != nil {
		return nil, nil, err
	}
	// Now we add the transaction to the block 2 and add the block to the chain
	transactions2a := []*protos.Transaction{transaction2a}
	block2 := protos.NewBlock(transactions2a, nil)

	allBlocks = append(allBlocks, block2)
	allHashes = append(allHashes, []byte("stateHash2"))
	testWrapper.addNewBlock(block2, []byte("stateHash2"))
	// -----------------------------</Block 2>------------------------------------

	// -----------------------------<Block 3>-------------------------------------
	// Create a transaction'
	transaction3a, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyContract"}, testutil.GenerateUUID(testWrapper.t), "setX", []string{"{x: \"hello\"}"})
	if err != nil {
		return nil, nil, err
	}
	// Create the third block and add it to the chain
	transactions3a := []*protos.Transaction{transaction3a}
	block3 := protos.NewBlock(transactions3a, nil)
	allBlocks = append(allBlocks, block3)
	allHashes = append(allHashes, []byte("stateHash3"))
	testWrapper.addNewBlock(block3, []byte("stateHash3"))

	// -----------------------------</Block 3>------------------------------------
	return allBlocks, allHashes, nil
}
Пример #3
0
func buildTestTx(t *testing.T) (*protos.Transaction, string) {
	uuid, _ := util.GenerateUUID()
	tx, err := protos.NewTransaction(protos.ChaincodeID{Path: "testUrl"}, uuid, "anyfunction", []string{"param1, param2"})
	testutil.AssertNil(t, err)
	return tx, uuid
}
Пример #4
0
// 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.Uuid)
	ledger.SetState("MyContract1", "code", []byte("code example"))
	ledger.TxFinished(transaction1a.Uuid, 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.Uuid)
	ledger.SetState("MyContract", "x", []byte("hello"))
	ledger.SetState("MyOtherContract", "y", []byte("goodbuy"))
	ledger.TxFinished(transaction2a.Uuid, 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.Uuid)
	ledger.SetState("MyContract", "x", []byte("hello"))
	ledger.SetState("MyOtherContract", "y", []byte("goodbuy"))
	ledger.SetState("MyImportantContract", "z", []byte("super"))
	ledger.TxFinished(transaction3a.Uuid, 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\"}"})
	if err != nil {
		t.Logf("Error creating NewTransaction: %s", err)
		t.Fail()
	}
	transaction4d, err := protos.NewTransaction(protos.ChaincodeID{Path: "MyMEGAContract"}, generateUUID(t), "setMEGA", []string{"{mega: \"MEGA\"}"})
	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(transaction4a.Uuid)
	ledger.SetState("MyContract", "x", []byte("hello"))
	ledger.SetState("MyOtherContract", "y", []byte("goodbuy"))
	ledger.SetState("MyImportantContract", "z", []byte("super"))
	ledger.SetState("MyMEGAContract", "mega", []byte("MEGA"))
	ledger.TxFinished(transaction4a.Uuid, true)

	// Create the 4th block and add it to the chain
	ledger.CommitTxBatch(4, []*protos.Transaction{transaction4a, transaction4b, transaction4c, transaction4d}, nil, []byte("dummy-proof"))
	// -----------------------------</Block #4>-----------------------------------

	return
}
Пример #5
0
func constructDummyTx(tb testing.TB) *protos.Transaction {
	uuid := util.GenerateUUID()
	tx, err := protos.NewTransaction(protos.ChaincodeID{Path: "dummyChaincodeId"}, uuid, "dummyFunction", []string{"dummyParamValue1, dummyParamValue2"})
	testutil.AssertNil(tb, err)
	return tx
}
Пример #6
0
func buildTestTx() (*protos.Transaction, string) {
	uuid, _ := util.GenerateUUID()
	return protos.NewTransaction(protos.ChaincodeID{Path: "testUrl"}, uuid, "anyfunction", []string{"param1, param2"}), uuid
}