-
Notifications
You must be signed in to change notification settings - Fork 95
/
types.go
209 lines (184 loc) · 4.82 KB
/
types.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package onroad_pool
import (
"fmt"
"github.com/vitelabs/go-vite/v2/common/db/xleveldb/errors"
"github.com/vitelabs/go-vite/v2/common/types"
ledger "github.com/vitelabs/go-vite/v2/interfaces/core"
"github.com/vitelabs/go-vite/v2/log15"
)
var onroadPoolLog = log15.New("onroadPool", nil)
type orHeightValue []*OnroadTx
func newOrHeightValueFromOnroadTxs(txs []OnroadTx) (orHeightValue, error) {
result := make([]*OnroadTx, len(txs))
for index := range txs {
result[index] = &txs[index]
}
return result, nil
}
func (hv orHeightValue) isEmpty() bool {
return len(hv) == 0
}
func (hv orHeightValue) dirtyTxs() []*OnroadTx {
var result []*OnroadTx
for _, sub := range hv {
if sub.FromIndex == nil {
result = append(result, sub)
}
}
return result
}
func (hv orHeightValue) minTx() (*OnroadTx, error) {
if len(hv) == 0 {
return nil, errors.New("height value is empty")
}
var min *OnroadTx
for _, sub := range hv {
if sub.FromIndex == nil {
return nil, errors.New("sub index is nil")
}
tmp := sub
if min == nil {
min = tmp
} else {
if *sub.FromIndex < *min.FromIndex {
min = tmp
}
}
}
return min, nil
}
type orHashHeight struct {
Hash types.Hash
Height uint64
SubIndex *uint32
// @todo delete
cachedBlock *ledger.AccountBlock
}
func (or orHashHeight) String() string {
if or.SubIndex == nil {
return fmt.Sprintf("orHashHeight: hash=%s,height=%d,subIndex=nil", or.Hash, or.Height)
} else {
return fmt.Sprintf("orHashHeight: hash=%s,height=%d,subIndex=%d", or.Hash, or.Height, *or.SubIndex)
}
}
func newOrHashHeightFromOnroadTx(tx *OnroadTx) *orHashHeight {
return &orHashHeight{
Hash: tx.FromHash,
Height: tx.FromHeight,
SubIndex: tx.FromIndex,
cachedBlock: nil,
}
}
type onRoadList []*orHashHeight
func (oList onRoadList) Len() int { return len(oList) }
func (oList onRoadList) Swap(i, j int) {
oList[i], oList[j] = oList[j], oList[i]
}
func (oList onRoadList) Less(i, j int) bool {
if oList[i].Height < oList[j].Height {
return true
}
if oList[i].Height == oList[j].Height && oList[i].SubIndex != nil && oList[j].SubIndex != nil {
if *oList[i].SubIndex < *oList[j].SubIndex {
return true
}
}
return false
}
type OnRoadBlock struct {
caller types.Address
orAddr types.Address
hashHeight orHashHeight
block *ledger.AccountBlock
}
// func ledgerBlockToOnRoad(inserOrRollback bool, block *ledger.AccountBlock, fromBlock *ledger.AccountBlock) (*OnRoadBlock, error) {
// or := &OnRoadBlock{
// block: block,
// }
// if block.IsSendBlock() {
// or.caller = block.AccountAddress
// or.orAddr = block.ToAddress
// index := uint8(0)
// or.hashHeight = orHashHeight{
// Hash: block.Hash,
// Height: block.Height,
// SubIndex: &index,
// }
// } else {
// if fromBlock == nil {
// return nil, errors.New("failed to find send")
// }
// or.caller = fromBlock.AccountAddress
// or.orAddr = fromBlock.ToAddress
// or.hashHeight = orHashHeight{
// Hash: fromBlock.Hash,
// Height: fromBlock.Height,
// }
// }
// }
func LedgerBlockToOnRoad(chain chainReader, block *ledger.AccountBlock) (*OnRoadBlock, error) {
or := &OnRoadBlock{
block: block,
}
if block.IsSendBlock() {
or.caller = block.AccountAddress
or.orAddr = block.ToAddress
index := uint32(0)
or.hashHeight = orHashHeight{
Hash: block.Hash,
Height: block.Height,
SubIndex: &index,
}
} else {
fromBlock, err := chain.GetAccountBlockByHash(block.FromBlockHash)
if err != nil {
return nil, err
}
if fromBlock == nil {
return nil, errors.New("failed to find send")
}
or.caller = fromBlock.AccountAddress
or.orAddr = fromBlock.ToAddress
or.hashHeight = orHashHeight{
Hash: fromBlock.Hash,
Height: fromBlock.Height,
}
}
if types.IsContractAddr(or.caller) {
completeBlock, err := chain.GetCompleteBlockByHash(or.hashHeight.Hash)
if err != nil {
return nil, err
}
if completeBlock == nil {
return nil, ErrFindCompleteBlock
}
or.hashHeight.Height = completeBlock.Height // refer to its parent receive's height
for k, v := range completeBlock.SendBlockList {
if v.Hash == or.hashHeight.Hash {
idx := uint32(k)
or.hashHeight.SubIndex = &idx
break
}
}
} else {
index := uint32(0)
or.hashHeight.SubIndex = &index
}
return or, nil
}
type PendingOnRoadList []*OnRoadBlock
func (pList PendingOnRoadList) Len() int { return len(pList) }
func (pList PendingOnRoadList) Swap(i, j int) {
pList[i], pList[j] = pList[j], pList[i]
}
func (pList PendingOnRoadList) Less(i, j int) bool {
if pList[i].hashHeight.Height < pList[j].hashHeight.Height {
return true
}
if pList[i].hashHeight.Height == pList[j].hashHeight.Height && pList[i].hashHeight.SubIndex != nil && pList[j].hashHeight.SubIndex != nil {
if *pList[i].hashHeight.SubIndex < *pList[j].hashHeight.SubIndex {
return true
}
}
return false
}