Skip to content

Commit

Permalink
Fixes in ledger code for new transaction structure
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1199

This commit fixes the ledger code as per new transaction structure.
Also removed the explicit dependency on the msp security code by moving
the code to a separate testutil for a better reuse.

Change-Id: I60d3968ae26684747435c195bbe45a78697f4803
Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi committed Nov 28, 2016
1 parent 61affa0 commit 64e6ce4
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 187 deletions.
21 changes: 1 addition & 20 deletions core/committer/committer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,10 @@ import (
"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/stretchr/testify/assert"

"fmt"

"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/msp"
pb "github.com/hyperledger/fabric/protos/peer"
)

func TestKVLedgerBlockStorage(t *testing.T) {
primitives.SetSecurityLevel("SHA2", 256)

// setup the MSP manager so that we can sign/verify
mspMgrConfigFile := "../../msp/peer-config.json"
msp.GetManager().Setup(mspMgrConfigFile)
mspId := "DEFAULT"
id := "PEER"
signingIdentity := &msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: mspId}, Value: id}
signer, err := msp.GetManager().GetSigningIdentity(signingIdentity)
if err != nil {
os.Exit(-1)
fmt.Printf("Could not initialize msp/signer")
return
}

conf := kvledger.NewConf("/tmp/tests/ledger/", 0)
defer os.RemoveAll("/tmp/tests/ledger/")

Expand All @@ -70,7 +51,7 @@ func TestKVLedgerBlockStorage(t *testing.T) {
simulator.Done()

simRes, _ := simulator.GetTxSimulationResults()
block1 := testutil.ConstructBlockForSimulationResults(t, [][]byte{simRes}, signer)
block1 := testutil.ConstructBlockForSimulationResults(t, [][]byte{simRes}, true)

err = committer.CommitBlock(block1)
assert.NoError(t, err)
Expand Down
35 changes: 25 additions & 10 deletions core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/core/ledger/util/db"
"github.com/op/go-logging"

"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putil "github.com/hyperledger/fabric/protos/utils"
"github.com/op/go-logging"
)

var logger = logging.MustGetLogger("kvledger")
Expand Down Expand Up @@ -466,16 +467,12 @@ func (mgr *blockfileMgr) fetchSerBlock(lp *fileLocPointer) (*pb.SerBlock2, error
}

func (mgr *blockfileMgr) fetchTransaction(lp *fileLocPointer) (*pb.Transaction, error) {
txBytes, err := mgr.fetchRawBytes(lp)
if err != nil {
return nil, err
}
tx := &pb.Transaction{}
err = proto.Unmarshal(txBytes, tx)
if err != nil {
var err error
var txEnvelopeBytes []byte
if txEnvelopeBytes, err = mgr.fetchRawBytes(lp); err != nil {
return nil, err
}
return tx, nil
return extractTransaction(txEnvelopeBytes)
}

func (mgr *blockfileMgr) fetchBlockBytes(lp *fileLocPointer) ([]byte, error) {
Expand Down Expand Up @@ -560,6 +557,24 @@ func scanForLastCompleteBlock(rootDir string, fileNum int, startingOffset int64)
return blockStream.currentOffset, numBlocks, errRead
}

func extractTransaction(txEnvelopeBytes []byte) (*pb.Transaction, error) {
var err error
var txEnvelope *common.Envelope
var txPayload *common.Payload
var tx *pb.Transaction

if txEnvelope, err = putil.GetEnvelope(txEnvelopeBytes); err != nil {
return nil, err
}
if txPayload, err = putil.GetPayload(txEnvelope); err != nil {
return nil, err
}
if tx, err = putil.GetTransaction(txPayload.Data); err != nil {
return nil, err
}
return tx, nil
}

// checkpointInfo
type checkpointInfo struct {
latestFileChunkSuffixNum int
Expand Down
5 changes: 2 additions & 3 deletions core/ledger/blkstorage/fsblkstorage/blockfile_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@ func TestBlockfileMgrGetTxById(t *testing.T) {
blocks := testutil.ConstructTestBlocks(t, 10)
blkfileMgrWrapper.addBlocks(blocks)
for i, blk := range blocks {
for j, txBytes := range blk.Transactions {
for j, txEnvelopeBytes := range blk.Transactions {
// blockNum starts with 1
txID := constructTxID(uint64(i+1), j)
txFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByID(txID)
testutil.AssertNoError(t, err, "Error while retrieving tx from blkfileMgr")
tx := &pb.Transaction{}
err = proto.Unmarshal(txBytes, tx)
tx, err := extractTransaction(txEnvelopeBytes)
testutil.AssertNoError(t, err, "Error while unmarshalling tx")
testutil.AssertEquals(t, txFromFileMgr, tx)
}
Expand Down
7 changes: 2 additions & 5 deletions core/ledger/blkstorage/fsblkstorage/blockindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ import (
"fmt"
"testing"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/testutil"

pb "github.com/hyperledger/fabric/protos/peer"
)

type noopIndex struct {
Expand Down Expand Up @@ -144,8 +141,8 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []blkstorage.Index
tx, err := blockfileMgr.retrieveTransactionByID(constructTxID(1, 0))
if testutil.Contains(indexItems, blkstorage.IndexableAttrTxID) {
testutil.AssertNoError(t, err, "Error while retrieving tx by id")
txOrig := &pb.Transaction{}
proto.Unmarshal(blocks[0].Transactions[0], txOrig)
txOrig, err := extractTransaction(blocks[0].Transactions[0])
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, tx, txOrig)
} else {
testutil.AssertSame(t, err, blkstorage.ErrAttrNotIndexed)
Expand Down
16 changes: 8 additions & 8 deletions core/ledger/kvledger/example/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger"

"github.com/hyperledger/fabric/core/util"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
ptestutils "github.com/hyperledger/fabric/protos/testutils"
)

// App - a sample fund transfer app
Expand All @@ -39,7 +39,7 @@ func ConstructAppInstance(ledger ledger.ValidatedLedger) *App {
}

// Init simulates init transaction
func (app *App) Init(initialBalances map[string]int) (*pb.Transaction, error) {
func (app *App) Init(initialBalances map[string]int) (*common.Envelope, error) {
var txSimulator ledger.TxSimulator
var err error
if txSimulator, err = app.ledger.NewTxSimulator(); err != nil {
Expand All @@ -58,8 +58,7 @@ func (app *App) Init(initialBalances map[string]int) (*pb.Transaction, error) {
}

// TransferFunds simulates a transaction for transferring fund from fromAccount to toAccount
func (app *App) TransferFunds(fromAccount string, toAccount string, transferAmt int) (*pb.Transaction, error) {

func (app *App) TransferFunds(fromAccount string, toAccount string, transferAmt int) (*common.Envelope, error) {
// act as endorsing peer shim code to simulate a transaction on behalf of chaincode
var txSimulator ledger.TxSimulator
var err error
Expand Down Expand Up @@ -102,6 +101,7 @@ func (app *App) QueryBalances(accounts []string) ([]int, error) {
if queryExecutor, err = app.ledger.NewQueryExecutor(); err != nil {
return nil, err
}
defer queryExecutor.Done()
balances := make([]int, len(accounts))
for i := 0; i < len(accounts); i++ {
var balBytes []byte
Expand All @@ -113,9 +113,9 @@ func (app *App) QueryBalances(accounts []string) ([]int, error) {
return balances, nil
}

func constructTransaction(simulationResults []byte) *pb.Transaction {
tx, _ := putils.CreateTx(common.HeaderType_ENDORSER_TRANSACTION, nil, nil, simulationResults, []*pb.Endorsement{})
return tx
func constructTransaction(simulationResults []byte) *common.Envelope {
txEnv, _ := ptestutils.ConstructSingedTxEnvWithDefaultSigner(util.GenerateUUID(), "foo", simulationResults, nil, nil)
return txEnv
}

func toBytes(balance int) []byte {
Expand Down
3 changes: 2 additions & 1 deletion core/ledger/kvledger/example/consenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package example
import (
"github.com/golang/protobuf/proto"

"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
)

Expand All @@ -32,7 +33,7 @@ func ConstructConsenter() *Consenter {
}

// ConstructBlock constructs a block from a list of transactions
func (c *Consenter) ConstructBlock(transactions ...*pb.Transaction) *pb.Block2 {
func (c *Consenter) ConstructBlock(transactions ...*common.Envelope) *pb.Block2 {
logger.Debugf("Construct a block based on the transactions")
block := &pb.Block2{}
for _, tx := range transactions {
Expand Down
8 changes: 3 additions & 5 deletions core/ledger/kvledger/example/marble_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import (

ledger "github.com/hyperledger/fabric/core/ledger"

pb "github.com/hyperledger/fabric/protos/peer"

"github.com/hyperledger/fabric/protos/common"
logging "github.com/op/go-logging"
)

Expand Down Expand Up @@ -55,7 +54,7 @@ type Marble struct {
}

// CreateMarble simulates init transaction
func (marbleApp *MarbleApp) CreateMarble(args []string) (*pb.Transaction, error) {
func (marbleApp *MarbleApp) CreateMarble(args []string) (*common.Envelope, error) {
// 0 1 2 3
// "asdf", "blue", "35", "bob"
logger.Debugf("===COUCHDB=== Entering ----------CreateMarble()----------")
Expand Down Expand Up @@ -126,8 +125,7 @@ func init_marble(args []string) ([]byte, error) {
}

// TransferMarble simulates transfer transaction
func (marbleApp *MarbleApp) TransferMarble(args []string) (*pb.Transaction, error) {

func (marbleApp *MarbleApp) TransferMarble(args []string) (*common.Envelope, error) {
// 0 1
// "name", "bob"
if len(args) < 2 {
Expand Down
25 changes: 2 additions & 23 deletions core/ledger/kvledger/kv_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,10 @@ import (
"testing"

"github.com/hyperledger/fabric/core/ledger/testutil"

"fmt"
"os"

"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/msp"
pb "github.com/hyperledger/fabric/protos/peer"
)

func TestKVLedgerBlockStorage(t *testing.T) {
primitives.SetSecurityLevel("SHA2", 256)

// setup the MSP manager so that we can sign/verify
mspMgrConfigFile := "../../../msp/peer-config.json"
msp.GetManager().Setup(mspMgrConfigFile)
mspId := "DEFAULT"
id := "PEER"
signingIdentity := &msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: mspId}, Value: id}
signer, err := msp.GetManager().GetSigningIdentity(signingIdentity)
if err != nil {
os.Exit(-1)
fmt.Printf("Could not initialize msp/signer")
return
}

env := newTestEnv(t)
defer env.cleanup()
ledger, _ := NewKVLedger(env.conf)
Expand All @@ -60,7 +39,7 @@ func TestKVLedgerBlockStorage(t *testing.T) {
simulator.SetState("ns1", "key3", []byte("value3"))
simulator.Done()
simRes, _ := simulator.GetTxSimulationResults()
block1 := testutil.ConstructBlockForSimulationResults(t, [][]byte{simRes}, signer)
block1 := testutil.ConstructBlockForSimulationResults(t, [][]byte{simRes}, false)
ledger.RemoveInvalidTransactionsAndPrepare(block1)
ledger.Commit()

Expand All @@ -76,7 +55,7 @@ func TestKVLedgerBlockStorage(t *testing.T) {
simulator.SetState("ns1", "key3", []byte("value6"))
simulator.Done()
simRes, _ = simulator.GetTxSimulationResults()
block2 := testutil.ConstructBlockForSimulationResults(t, [][]byte{simRes}, signer)
block2 := testutil.ConstructBlockForSimulationResults(t, [][]byte{simRes}, false)
ledger.RemoveInvalidTransactionsAndPrepare(block2)
ledger.Commit()

Expand Down
54 changes: 13 additions & 41 deletions core/ledger/testutil/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@ import (
"testing"

"github.com/golang/protobuf/proto"

"github.com/hyperledger/fabric/core/util"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
ptestutils "github.com/hyperledger/fabric/protos/testutils"
)

// ConstructBlockForSimulationResults constructs a block that includes a number of transactions - one per simulationResults
func ConstructBlockForSimulationResults(t *testing.T, simulationResults [][]byte, signer msp.SigningIdentity) *pb.Block2 {
func ConstructBlockForSimulationResults(t *testing.T, simulationResults [][]byte, sign bool) *pb.Block2 {
envs := []*common.Envelope{}
for i := 0; i < len(simulationResults); i++ {
env, err := ConstructTestTransaction(t, simulationResults[i], signer)
env, err := ConstructTestTransaction(t, simulationResults[i], sign)
if err != nil {
t.Fatalf("ConstructTestTransaction failed, err %s", err)
}
Expand All @@ -52,38 +50,22 @@ func ConstructTestBlocks(t *testing.T, numBlocks int) []*pb.Block2 {

// ConstructTestBlock constructs a block with 'numTx' number of transactions for testing
func ConstructTestBlock(t *testing.T, numTx int, txSize int, startingTxID int) *pb.Block2 {
txs := []*pb.Transaction{}
txEnvs := []*common.Envelope{}
for i := startingTxID; i < numTx+startingTxID; i++ {
tx, _ := putils.CreateTx(common.HeaderType_ENDORSER_TRANSACTION, []byte{}, []byte{}, ConstructRandomBytes(t, txSize), []*pb.Endorsement{})
txs = append(txs, tx)
txEnv, _ := ConstructTestTransaction(t, ConstructRandomBytes(t, txSize), false)
txEnvs = append(txEnvs, txEnv)
}
return newBlock(txs)
return newBlockEnv(txEnvs)
}

// ConstructTestTransaction constructs a transaction for testing
func ConstructTestTransaction(t *testing.T, simulationResults []byte, signer msp.SigningIdentity) (*common.Envelope, error) {
ss, err := signer.Serialize()
if err != nil {
return nil, err
}

uuid := util.GenerateUUID()
prop, err := putils.CreateChaincodeProposal(uuid, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: &pb.ChaincodeID{Name: "foo"}}}, ss)
if err != nil {
return nil, err
}

presp, err := putils.CreateProposalResponse(prop.Header, prop.Payload, simulationResults, nil, nil, signer)
if err != nil {
return nil, err
}

env, err := putils.CreateSignedTx(prop, signer, presp)
if err != nil {
return nil, err
func ConstructTestTransaction(t *testing.T, simulationResults []byte, sign bool) (*common.Envelope, error) {
ccName := "foo"
txID := util.GenerateUUID()
if sign {
return ptestutils.ConstructSingedTxEnvWithDefaultSigner(txID, ccName, simulationResults, nil, nil)
}

return env, nil
return ptestutils.ConstructUnsingedTxEnv(txID, ccName, simulationResults, nil, nil)
}

// ComputeBlockHash computes the crypto-hash of a block
Expand All @@ -93,16 +75,6 @@ func ComputeBlockHash(t testing.TB, block *pb.Block2) []byte {
return serBlock.ComputeHash()
}

func newBlock(txs []*pb.Transaction) *pb.Block2 {
block := &pb.Block2{}
block.PreviousBlockHash = []byte{}
for i := 0; i < len(txs); i++ {
txBytes, _ := proto.Marshal(txs[i])
block.Transactions = append(block.Transactions, txBytes)
}
return block
}

func newBlockEnv(env []*common.Envelope) *pb.Block2 {
block := &pb.Block2{}
block.PreviousBlockHash = []byte{}
Expand Down
Loading

0 comments on commit 64e6ce4

Please sign in to comment.