github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/marble_example/main/marble_example.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"os"
    23  
    24  	configtxtest "github.com/hyperledger/fabric/common/configtx/test"
    25  	"github.com/hyperledger/fabric/core/ledger"
    26  	"github.com/hyperledger/fabric/core/ledger/kvledger/example"
    27  	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
    28  	"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
    29  	"github.com/hyperledger/fabric/core/ledger/testutil"
    30  	"github.com/hyperledger/fabric/core/ledger/util"
    31  	"github.com/hyperledger/fabric/protos/common"
    32  	logging "github.com/op/go-logging"
    33  )
    34  
    35  const (
    36  	ledgerID = "Default"
    37  )
    38  
    39  var logger = logging.MustGetLogger("main")
    40  
    41  var peerLedger ledger.PeerLedger
    42  var marbleApp *example.MarbleApp
    43  var committer *example.Committer
    44  var consenter *example.Consenter
    45  
    46  func init() {
    47  
    48  	// Initialization will get a handle to the ledger at the specified path
    49  	// Note, if subledgers are supported in the future,
    50  	// the various ledgers could be created/managed at this level
    51  	logger.Debugf("Marble Example main init()")
    52  
    53  	//call a helper method to load the core.yaml
    54  	testutil.SetupCoreYAMLConfig()
    55  
    56  	cleanup()
    57  	ledgermgmt.Initialize()
    58  	var err error
    59  	gb, _ := configtxtest.MakeGenesisBlock(ledgerID)
    60  	peerLedger, err = ledgermgmt.CreateLedger(gb)
    61  
    62  	if err != nil {
    63  		panic(fmt.Errorf("Error in NewKVLedger(): %s", err))
    64  	}
    65  	marbleApp = example.ConstructMarbleAppInstance(peerLedger)
    66  	committer = example.ConstructCommitter(peerLedger)
    67  	consenter = example.ConstructConsenter()
    68  }
    69  
    70  func main() {
    71  	defer ledgermgmt.Close()
    72  	// Each of the functions here will emulate endorser, orderer,
    73  	// and committer by calling ledger APIs to similate the proposal,
    74  	// get simulation results, create a transaction, add it to a block,
    75  	// and then commit the block.
    76  
    77  	initApp()
    78  	transferMarble()
    79  
    80  }
    81  
    82  func initApp() {
    83  	logger.Debugf("Marble Example initApp() to create a marble")
    84  	marble := []string{"marble1", "blue", "35", "tom"}
    85  	tx, err := marbleApp.CreateMarble(marble)
    86  	handleError(err, true)
    87  	rawBlock := consenter.ConstructBlock(tx)
    88  	err = committer.Commit(rawBlock)
    89  	handleError(err, true)
    90  	printBlocksInfo(rawBlock)
    91  }
    92  
    93  func transferMarble() {
    94  	logger.Debugf("Marble Example transferMarble()")
    95  	tx1, err := marbleApp.TransferMarble([]string{"marble1", "jerry"})
    96  	handleError(err, true)
    97  	rawBlock := consenter.ConstructBlock(tx1)
    98  	err = committer.Commit(rawBlock)
    99  	handleError(err, true)
   100  	printBlocksInfo(rawBlock)
   101  }
   102  
   103  func printBlocksInfo(block *common.Block) {
   104  	// Read invalid transactions filter
   105  	txsFltr := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
   106  	numOfInvalid := 0
   107  	// Count how many transaction indeed invalid
   108  	for i := 0; i < len(block.Data.Data); i++ {
   109  		if txsFltr.IsInvalid(i) {
   110  			numOfInvalid++
   111  		}
   112  	}
   113  	fmt.Printf("Num txs in rawBlock = [%d], num invalidTxs = [%d]\n",
   114  		len(block.Data.Data), numOfInvalid)
   115  }
   116  
   117  func handleError(err error, quit bool) {
   118  	if err != nil {
   119  		if quit {
   120  			panic(fmt.Errorf("Error: %s\n", err))
   121  		} else {
   122  			fmt.Printf("Error: %s\n", err)
   123  		}
   124  	}
   125  }
   126  
   127  func cleanup() {
   128  	ledgerRootPath := ledgerconfig.GetRootPath()
   129  	os.RemoveAll(ledgerRootPath)
   130  }