github.com/turingchain2020/turingchain@v1.1.21/cmd/execblock/main.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // package main 主要是执行已经同步好的区块链的某个区块
     6  package main
     7  
     8  import (
     9  	"flag"
    10  	"fmt"
    11  	_ "net/http/pprof"
    12  	"os/user"
    13  	"path/filepath"
    14  
    15  	"github.com/turingchain2020/turingchain/common/crypto"
    16  
    17  	"github.com/turingchain2020/turingchain/blockchain"
    18  	"github.com/turingchain2020/turingchain/client"
    19  	clog "github.com/turingchain2020/turingchain/common/log"
    20  	log "github.com/turingchain2020/turingchain/common/log/log15"
    21  	"github.com/turingchain2020/turingchain/executor"
    22  	"github.com/turingchain2020/turingchain/queue"
    23  	"github.com/turingchain2020/turingchain/store"
    24  	"github.com/turingchain2020/turingchain/types"
    25  	"github.com/turingchain2020/turingchain/util"
    26  )
    27  
    28  var height = flag.Int64("height", 1, "exec block height")
    29  var datadir = flag.String("datadir", "", "data dir of turingchain, include logs and datas")
    30  var configPath = flag.String("f", "turingchain.toml", "configfile")
    31  
    32  func resetDatadir(cfg *types.Config, datadir string) {
    33  	// Check in case of paths like "/something/~/something/"
    34  	if datadir[:2] == "~/" {
    35  		usr, _ := user.Current()
    36  		dir := usr.HomeDir
    37  		datadir = filepath.Join(dir, datadir[2:])
    38  	}
    39  	log.Info("current user data dir is ", "dir", datadir)
    40  	cfg.Log.LogFile = filepath.Join(datadir, cfg.Log.LogFile)
    41  	cfg.BlockChain.DbPath = filepath.Join(datadir, cfg.BlockChain.DbPath)
    42  	cfg.P2P.DbPath = filepath.Join(datadir, cfg.P2P.DbPath)
    43  	cfg.Wallet.DbPath = filepath.Join(datadir, cfg.Wallet.DbPath)
    44  	cfg.Store.DbPath = filepath.Join(datadir, cfg.Store.DbPath)
    45  }
    46  
    47  func initEnv() (queue.Queue, queue.Module, queue.Module) {
    48  	cfg := types.NewTuringchainConfig(types.ReadFile(*configPath))
    49  	mcfg := cfg.GetModuleConfig()
    50  	if *datadir != "" {
    51  		resetDatadir(mcfg, *datadir)
    52  	}
    53  	mcfg.Consensus.Minerstart = false
    54  	crypto.Init(mcfg.Crypto, cfg.GetSubConfig().Crypto)
    55  	var q = queue.New("channel")
    56  	q.SetConfig(cfg)
    57  	chain := blockchain.New(cfg)
    58  	chain.SetQueueClient(q.Client())
    59  	exec := executor.New(cfg)
    60  	exec.SetQueueClient(q.Client())
    61  	cfg.SetMinFee(0)
    62  	s := store.New(cfg)
    63  	s.SetQueueClient(q.Client())
    64  	return q, chain, s
    65  }
    66  
    67  func main() {
    68  	clog.SetLogLevel("info")
    69  	flag.Parse()
    70  	q, chain, s := initEnv()
    71  	defer s.Close()
    72  	defer chain.Close()
    73  	defer q.Close()
    74  	qclient, err := client.New(q.Client(), nil)
    75  	if err != nil {
    76  		panic(err)
    77  	}
    78  	req := &types.ReqBlocks{Start: *height - 1, End: *height}
    79  	blocks, err := qclient.GetBlocks(req)
    80  	if err != nil {
    81  		panic(err)
    82  	}
    83  	log.Info("execblock", "block height", *height)
    84  	prevState := blocks.Items[0].Block.StateHash
    85  	block := blocks.Items[1].Block
    86  	receipt, err := util.ExecTx(q.Client(), prevState, block)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	for i, r := range receipt.GetReceipts() {
    91  		println("=======================")
    92  		println("tx index ", i)
    93  		for j, kv := range r.GetKV() {
    94  			fmt.Println("\tKV:", j, kv)
    95  		}
    96  		for k, l := range r.GetLogs() {
    97  			logType := types.LoadLog(block.Txs[i].Execer, int64(l.Ty))
    98  			lTy := "unkownType"
    99  			var logIns interface{}
   100  			if logType != nil {
   101  				logIns, err = logType.Decode(l.GetLog())
   102  				if err != nil {
   103  					panic(err)
   104  				}
   105  				lTy = logType.Name()
   106  			}
   107  			fmt.Printf("\tLog:%d %s->%v\n", k, lTy, logIns)
   108  		}
   109  	}
   110  }