github.com/turingchain2020/turingchain@v1.1.21/types/localkv.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 types
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  )
    11  
    12  // 定义key值
    13  var (
    14  	LocalPrefix            = []byte("LODB")
    15  	FlagTxQuickIndex       = []byte("FLAG:FlagTxQuickIndex")
    16  	FlagKeyMVCC            = []byte("FLAG:keyMVCCFlag")
    17  	TxHashPerfix           = []byte("TX:")
    18  	TxShortHashPerfix      = []byte("STX:")
    19  	TxAddrHash             = []byte("TxAddrHash:")
    20  	TxAddrDirHash          = []byte("TxAddrDirHash:")
    21  	AddrTxsCount           = []byte("AddrTxsCount:")
    22  	ConsensusParaTxsPrefix = []byte("LODBP:Consensus:Para:")            //存贮para共识模块从主链拉取的平行链交易
    23  	FlagReduceLocaldb      = []byte("FLAG:ReduceLocaldb")               // 精简版localdb标记
    24  	ReduceLocaldbHeight    = append(FlagReduceLocaldb, []byte(":H")...) // 精简版localdb高度
    25  )
    26  
    27  // GetLocalDBKeyList 获取localdb的key列表
    28  func GetLocalDBKeyList() [][]byte {
    29  	return [][]byte{
    30  		FlagTxQuickIndex, FlagKeyMVCC, TxHashPerfix, TxShortHashPerfix, FlagReduceLocaldb,
    31  	}
    32  }
    33  
    34  //CalcTxKey local db中保存交易的方法
    35  func (c *TuringchainConfig) CalcTxKey(hash []byte) []byte {
    36  	if c.IsEnable("quickIndex") {
    37  		return append(TxHashPerfix, hash...)
    38  	}
    39  	return hash
    40  }
    41  
    42  // CalcTxKeyValue 保存local db中保存交易的方法
    43  func (c *TuringchainConfig) CalcTxKeyValue(txr *TxResult) []byte {
    44  	if c.IsEnable("reduceLocaldb") {
    45  		txres := &TxResult{
    46  			Height:     txr.GetHeight(),
    47  			Index:      txr.GetIndex(),
    48  			Blocktime:  txr.GetBlocktime(),
    49  			ActionName: txr.GetActionName(),
    50  		}
    51  		return Encode(txres)
    52  	}
    53  	return Encode(txr)
    54  }
    55  
    56  //CalcTxShortKey local db中保存交易的方法
    57  func CalcTxShortKey(hash []byte) []byte {
    58  	return append(TxShortHashPerfix, hash[0:8]...)
    59  }
    60  
    61  //CalcTxAddrHashKey 用于存储地址相关的hash列表,key=TxAddrHash:addr:height*100000 + index
    62  //地址下面所有的交易
    63  func CalcTxAddrHashKey(addr string, heightindex string) []byte {
    64  	return append(TxAddrHash, []byte(fmt.Sprintf("%s:%s", addr, heightindex))...)
    65  }
    66  
    67  //CalcTxAddrDirHashKey 用于存储地址相关的hash列表,key=TxAddrHash:addr:flag:height*100000 + index
    68  //地址下面某个分类的交易
    69  func CalcTxAddrDirHashKey(addr string, flag int32, heightindex string) []byte {
    70  	return append(TxAddrDirHash, []byte(fmt.Sprintf("%s:%d:%s", addr, flag, heightindex))...)
    71  }
    72  
    73  //CalcAddrTxsCountKey 存储地址参与的交易数量。add时加一,del时减一
    74  func CalcAddrTxsCountKey(addr string) []byte {
    75  	return append(AddrTxsCount, []byte(addr)...)
    76  }
    77  
    78  //StatisticFlag 用于记录统计的key
    79  func StatisticFlag() []byte {
    80  	return []byte("Statistics:Flag")
    81  }
    82  
    83  //TotalFeeKey 统计所有费用的key
    84  func TotalFeeKey(hash []byte) []byte {
    85  	key := []byte("TotalFeeKey:")
    86  	return append(key, hash...)
    87  }
    88  
    89  //CalcLocalPrefix 计算localdb key
    90  func CalcLocalPrefix(execer []byte) []byte {
    91  	s := append([]byte("LODB-"), execer...)
    92  	s = append(s, byte('-'))
    93  	return s
    94  }
    95  
    96  //CalcStatePrefix 计算localdb key
    97  func CalcStatePrefix(execer []byte) []byte {
    98  	s := append([]byte("mavl-"), execer...)
    99  	s = append(s, byte('-'))
   100  	return s
   101  }
   102  
   103  //CalcRollbackKey 计算回滚的key
   104  func CalcRollbackKey(execer []byte, hash []byte) []byte {
   105  	prefix := CalcLocalPrefix(execer)
   106  	key := append(prefix, []byte("rollback-")...)
   107  	key = append(key, hash...)
   108  	return key
   109  }
   110  
   111  //CalcConsensusParaTxsKey 平行链localdb中保存的平行链title对应的交易
   112  func CalcConsensusParaTxsKey(key []byte) []byte {
   113  	return append(ConsensusParaTxsPrefix, key...)
   114  }
   115  
   116  //CheckConsensusParaTxsKey 检测para共识模块需要操作的平行链交易的key值
   117  func CheckConsensusParaTxsKey(key []byte) bool {
   118  	return bytes.HasPrefix(key, ConsensusParaTxsPrefix)
   119  }