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