github.com/turingchain2020/turingchain@v1.1.21/executor/plugin_txindex.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 executor
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/turingchain2020/turingchain/common/address"
    11  	"github.com/turingchain2020/turingchain/types"
    12  )
    13  
    14  func init() {
    15  	RegisterPlugin("txindex", &txindexPlugin{})
    16  }
    17  
    18  type txindexPlugin struct {
    19  	pluginBase
    20  }
    21  
    22  func (p *txindexPlugin) CheckEnable(executor *executor, enable bool) (kvs []*types.KeyValue, ok bool, err error) {
    23  	return nil, true, nil
    24  }
    25  
    26  func (p *txindexPlugin) ExecLocal(executor *executor, data *types.BlockDetail) (kvs []*types.KeyValue, err error) {
    27  	for i := 0; i < len(data.Block.Txs); i++ {
    28  		tx := data.Block.Txs[i]
    29  		receipt := data.Receipts[i]
    30  		kv := getTx(executor, tx, receipt, i)
    31  		kvs = append(kvs, kv...)
    32  	}
    33  	return kvs, nil
    34  }
    35  
    36  func (p *txindexPlugin) ExecDelLocal(executor *executor, data *types.BlockDetail) (kvs []*types.KeyValue, err error) {
    37  	for i := 0; i < len(data.Block.Txs); i++ {
    38  		tx := data.Block.Txs[i]
    39  		receipt := data.Receipts[i]
    40  		//del:tx
    41  		kvdel := getTx(executor, tx, receipt, i)
    42  		for k := range kvdel {
    43  			kvdel[k].Value = nil
    44  		}
    45  		kvs = append(kvs, kvdel...)
    46  	}
    47  	return kvs, nil
    48  }
    49  
    50  //获取公共的信息
    51  func getTx(executor *executor, tx *types.Transaction, receipt *types.ReceiptData, index int) []*types.KeyValue {
    52  	types.AssertConfig(executor.api)
    53  	cfg := executor.api.GetConfig()
    54  	txhash := tx.Hash()
    55  	//构造txresult 信息保存到db中
    56  	var txresult types.TxResult
    57  	txresult.Height = executor.height
    58  	txresult.Index = int32(index)
    59  	txresult.Tx = tx
    60  	txresult.Receiptdate = receipt
    61  	txresult.Blocktime = executor.blocktime
    62  	txresult.ActionName = tx.ActionName()
    63  	var kvlist []*types.KeyValue
    64  	kvlist = append(kvlist, &types.KeyValue{Key: cfg.CalcTxKey(txhash), Value: cfg.CalcTxKeyValue(&txresult)})
    65  	if cfg.IsEnable("quickIndex") {
    66  		kvlist = append(kvlist, &types.KeyValue{Key: types.CalcTxShortKey(txhash), Value: []byte("1")})
    67  	}
    68  	return kvlist
    69  }
    70  
    71  type txIndex struct {
    72  	from      string
    73  	to        string
    74  	heightstr string
    75  	index     *types.ReplyTxInfo
    76  }
    77  
    78  //交易中 from/to 的索引
    79  func getTxIndex(executor *executor, tx *types.Transaction, receipt *types.ReceiptData, index int) *txIndex {
    80  	var txIndexInfo txIndex
    81  	var txinf types.ReplyTxInfo
    82  	txinf.Hash = tx.Hash()
    83  	txinf.Height = executor.height
    84  	txinf.Index = int64(index)
    85  	ety := types.LoadExecutorType(string(tx.Execer))
    86  	// none exec has not execType
    87  	if ety != nil {
    88  		var err error
    89  		txinf.Assets, err = ety.GetAssets(tx)
    90  		if err != nil {
    91  			elog.Error("getTxIndex ", "GetAssets err", err)
    92  		}
    93  	}
    94  
    95  	txIndexInfo.index = &txinf
    96  	heightstr := fmt.Sprintf("%018d", executor.height*types.MaxTxsPerBlock+int64(index))
    97  	txIndexInfo.heightstr = heightstr
    98  
    99  	txIndexInfo.from = address.PubKeyToAddress(tx.GetSignature().GetPubkey()).String()
   100  	txIndexInfo.to = tx.GetRealToAddr()
   101  	return &txIndexInfo
   102  }