Skip to content

Commit

Permalink
[FAB-5654] SideDB - Tx simulation/validation/commit
Browse files Browse the repository at this point in the history
This CR modifies the tranaction simulation, validation, and commit
code and delivers the end-to-end transaction flow that treats the
private data in a special manner. This CR mainly leverages the earlier
submitted independent CRs for sidedb feature for accomplishing this behavior.

This CR also allows ledger to receive the blocks and the pvt data from
another peer on the same channel (i.e., a peer catching up via state)

This CR is exceptionally large becasue of manily two reasons

1) The way currently the code (and specially the tests) is organized in
simulation/validation/commit flow, its not easy to submit such kind
of changes independently that cuase the change in the whole transaction
processing flow.

2) This CR causes a change in the existing ledger APIs which are used widely
across other packages (specially in the tests) and hence many files are included
for fixing the broken dependencies

Change-Id: Id29575176575f4c01793efd3476b68f8364cb592
Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi committed Aug 10, 2017
1 parent d9e0004 commit 8a87b8a
Show file tree
Hide file tree
Showing 56 changed files with 1,885 additions and 697 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBlockSerialization(t *testing.T) {
}

func TestExtractTxid(t *testing.T) {
txEnv, txid, _ := testutil.ConstructTransaction(t, testutil.ConstructRandomBytes(t, 50), false)
txEnv, txid, _ := testutil.ConstructTransaction(t, testutil.ConstructRandomBytes(t, 50), "", false)
txEnvBytes, _ := putils.GetBytesEnvelope(txEnv)
extractedTxid, err := extractTxID(txEnvBytes)
testutil.AssertNoError(t, err, "")
Expand Down
32 changes: 28 additions & 4 deletions common/ledger/testutil/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func (bg *BlockGenerator) NextBlock(simulationResults [][]byte) *common.Block {
return block
}

// NextBlock constructs next block in sequence that includes a number of transactions - one per simulationResults
func (bg *BlockGenerator) NextBlockWithTxid(simulationResults [][]byte, txids []string) *common.Block {
// Length of simulationResults should be same as the length of txids.
if len(simulationResults) != len(txids) {
return nil
}
block := ConstructBlockWithTxid(bg.t, bg.blockNum, bg.previousHash, simulationResults, txids, bg.signTxs)
bg.blockNum++
bg.previousHash = block.Header.Hash()
return block
}

// NextTestBlock constructs next block in sequence block with 'numTx' number of transactions for testing
func (bg *BlockGenerator) NextTestBlock(numTx int, txSize int) *common.Block {
simulationResults := [][]byte{}
Expand All @@ -72,7 +84,7 @@ func (bg *BlockGenerator) NextTestBlocks(numBlocks int) []*common.Block {
}

// ConstructTransaction constructs a transaction for testing
func ConstructTransaction(_ *testing.T, simulationResults []byte, sign bool) (*common.Envelope, string, error) {
func ConstructTransaction(_ *testing.T, simulationResults []byte, txid string, sign bool) (*common.Envelope, string, error) {
ccid := &pb.ChaincodeID{
Name: "foo",
Version: "v1",
Expand All @@ -82,18 +94,30 @@ func ConstructTransaction(_ *testing.T, simulationResults []byte, sign bool) (*c
var txEnv *common.Envelope
var err error
if sign {
txEnv, txID, err = ptestutils.ConstructSingedTxEnvWithDefaultSigner(util.GetTestChainID(), ccid, nil, simulationResults, nil, nil)
txEnv, txID, err = ptestutils.ConstructSingedTxEnvWithDefaultSigner(util.GetTestChainID(), ccid, nil, simulationResults, txid, nil, nil)
} else {
txEnv, txID, err = ptestutils.ConstructUnsingedTxEnv(util.GetTestChainID(), ccid, nil, simulationResults, nil, nil)
txEnv, txID, err = ptestutils.ConstructUnsingedTxEnv(util.GetTestChainID(), ccid, nil, simulationResults, txid, nil, nil)
}
return txEnv, txID, err
}

func ConstructBlockWithTxid(t *testing.T, blockNum uint64, previousHash []byte, simulationResults [][]byte, txids []string, sign bool) *common.Block {
envs := []*common.Envelope{}
for i := 0; i < len(simulationResults); i++ {
env, _, err := ConstructTransaction(t, simulationResults[i], txids[i], sign)
if err != nil {
t.Fatalf("ConstructTestTransaction failed, err %s", err)
}
envs = append(envs, env)
}
return newBlock(envs, blockNum, previousHash)
}

// ConstructBlock constructs a single block
func ConstructBlock(t *testing.T, blockNum uint64, previousHash []byte, simulationResults [][]byte, sign bool) *common.Block {
envs := []*common.Envelope{}
for i := 0; i < len(simulationResults); i++ {
env, _, err := ConstructTransaction(t, simulationResults[i], sign)
env, _, err := ConstructTransaction(t, simulationResults[i], "", sign)
if err != nil {
t.Fatalf("ConstructTestTransaction failed, err %s", err)
}
Expand Down
17 changes: 16 additions & 1 deletion common/mocks/ledger/queryexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ func (m *MockQueryExecutor) ExecuteQuery(namespace, query string) (ledger.Result
return nil, nil
}

func (m *MockQueryExecutor) Done() {
func (m *MockQueryExecutor) GetPrivateData(namespace, collection, key string) ([]byte, error) {
return nil, nil
}

func (m *MockQueryExecutor) GetPrivateDataMultipleKeys(namespace, collection string, keys []string) ([][]byte, error) {
return nil, nil
}

func (m *MockQueryExecutor) GetPrivateDataRangeScanIterator(namespace, collection, startKey, endKey string) (ledger.ResultsIterator, error) {
return nil, nil
}

func (m *MockQueryExecutor) ExecuteQueryOnPrivateData(namespace, collection, query string) (ledger.ResultsIterator, error) {
return nil, nil
}

func (m *MockQueryExecutor) Done() {
}
4 changes: 2 additions & 2 deletions core/chaincode/ccproviderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ type ccProviderContextImpl struct {
}

// GetContext returns a context for the supplied ledger, with the appropriate tx simulator
func (c *ccProviderImpl) GetContext(ledger ledger.PeerLedger) (context.Context, error) {
func (c *ccProviderImpl) GetContext(ledger ledger.PeerLedger, txid string) (context.Context, error) {
var err error
// get context for the chaincode execution
c.txsim, err = ledger.NewTxSimulator()
c.txsim, err = ledger.NewTxSimulator(txid)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8a87b8a

Please sign in to comment.