forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
txnpool_common.go
254 lines (212 loc) · 7.18 KB
/
txnpool_common.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package common
import (
"github.com/dnaproject2/DNA/common"
"github.com/dnaproject2/DNA/core/types"
"github.com/dnaproject2/DNA/errors"
)
const (
MAX_CAPACITY = 100140 // The tx pool's capacity that holds the verified txs
MAX_PENDING_TXN = 4096 * 10 // The max length of pending txs
MAX_WORKER_NUM = 2 // The max concurrent workers
MAX_RCV_TXN_LEN = MAX_WORKER_NUM * MAX_PENDING_TXN // The max length of the queue that server can hold
MAX_RETRIES = 0 // The retry times to verify tx
EXPIRE_INTERVAL = 9 // The timeout that verify tx
STATELESS_MASK = 0x1 // The mask of stateless validator
STATEFUL_MASK = 0x2 // The mask of stateful validator
VERIFY_MASK = STATELESS_MASK | STATEFUL_MASK // The mask that indicates tx valid
MAX_LIMITATION = 10000 // The length of pending tx from net and http
UPDATE_FREQUENCY = 100 // The frequency to update gas price from global params
MAX_TX_SIZE = 1024 * 1024 // The max size of a transaction to prevent DOS attacks
)
// ActorType enumerates the kind of actor
type ActorType uint8
const (
_ ActorType = iota
TxActor // Actor that handles new transaction
TxPoolActor // Actor that handles consensus msg
VerifyRspActor // Actor that handles the response from valdiators
NetActor // Actor to send msg to the net actor
MaxActor
)
// SenderType enumerates the kind of tx submitter
type SenderType uint8
const (
NilSender SenderType = iota
NetSender // Net sends tx req
HttpSender // Http sends tx req
)
func (sender SenderType) Sender() string {
switch sender {
case NilSender:
return "nil sender"
case NetSender:
return "net sender"
case HttpSender:
return "http sender"
default:
return "unknown sender"
}
}
// TxnStatsType enumerates the kind of tx statistics
type TxnStatsType uint8
const (
_ TxnStatsType = iota
RcvStats // The count that the tx pool receive from the actor bus
SuccessStats // The count that the transactions are verified successfully
FailureStats // The count that the transactions are invalid
DuplicateStats // The count that the transactions are duplicated input
SigErrStats // The count that the transactions' signature error
StateErrStats // The count that the transactions are invalid in database
MaxStats
)
// CheckBlkResult contains a verifed tx list,
// an unverified tx list and an old tx list
// to be re-verifed
type CheckBlkResult struct {
VerifiedTxs []*VerifyTxResult
UnverifiedTxs []*types.Transaction
OldTxs []*types.Transaction
}
// TxStatus contains the attributes of a transaction
type TxStatus struct {
Hash common.Uint256 // transaction hash
Attrs []*TXAttr // transaction's status
}
type TxResult struct {
Err errors.ErrCode
Hash common.Uint256
Desc string
}
// TxReq specifies the api that how to submit a new transaction.
// Input: transacton and submitter type
type TxReq struct {
Tx *types.Transaction
Sender SenderType
TxResultCh chan *TxResult
}
// TxRsp returns the result of submitting tx, including
// a transaction hash and error code.
type TxRsp struct {
Hash common.Uint256
ErrCode errors.ErrCode
}
// restful api
// GetTxnReq specifies the api that how to get the transaction.
// Input: a transaction hash
type GetTxnReq struct {
Hash common.Uint256
}
// GetTxnRsp returns a transaction for the specified tx hash.
type GetTxnRsp struct {
Txn *types.Transaction
}
// CheckTxnReq specifies the api that how to check whether a
// transaction in the pool.
// Input: a transaction hash
type CheckTxnReq struct {
Hash common.Uint256
}
// CheckTxnRsp returns a value for the CheckTxnReq, if the
// transaction in the pool, value is true, or false.
type CheckTxnRsp struct {
Ok bool
}
// GetTxnStatusReq specifies the api that how to get a transaction
// status.
// Input: a transaction hash.
type GetTxnStatusReq struct {
Hash common.Uint256
}
// GetTxnStatusRsp returns a transaction status for GetTxnStatusReq.
// Output: a transaction hash and it's verified result.
type GetTxnStatusRsp struct {
Hash common.Uint256
TxStatus []*TXAttr
}
// GetTxnStats specifies the api that how to get the tx statistics.
type GetTxnStats struct {
}
// GetTxnStatsRso returns the tx statistics.
type GetTxnStatsRsp struct {
Count []uint64
}
// GetTxnCountReq specifies the api that how to get the tx count
type GetTxnCountReq struct {
}
// GetTxnCountRsp returns current tx count, including pending, and verified
type GetTxnCountRsp struct {
Count []uint32
}
// GetPendingTxnReq specifies the api that how to get a pending tx list
// in the pool.
type GetPendingTxnReq struct {
ByCount bool
}
// GetPendingTxnRsp returns a transaction list for GetPendingTxnReq.
type GetPendingTxnRsp struct {
Txs []*types.Transaction
}
// consensus messages
// GetTxnPoolReq specifies the api that how to get the valid transaction list.
type GetTxnPoolReq struct {
ByCount bool
Height uint32
}
// GetTxnPoolRsp returns a transaction list for GetTxnPoolReq.
type GetTxnPoolRsp struct {
TxnPool []*TXEntry
}
// VerifyBlockReq specifies that api that how to verify a block from consensus.
type VerifyBlockReq struct {
Height uint32
Txs []*types.Transaction
}
// VerifyTxResult returns a single transaction's verified result.
type VerifyTxResult struct {
Height uint32
Tx *types.Transaction
ErrCode errors.ErrCode
}
// VerifyBlockRsp returns a verified result for VerifyBlockReq.
type VerifyBlockRsp struct {
TxnPool []*VerifyTxResult
}
/*
* Implement sort.Interface
*/
type LB struct {
Size int
WorkerID uint8
}
type LBSlice []LB
func (this LBSlice) Len() int {
return len(this)
}
func (this LBSlice) Swap(i, j int) {
this[i].Size, this[j].Size = this[j].Size, this[i].Size
this[i].WorkerID, this[j].WorkerID = this[j].WorkerID, this[i].WorkerID
}
func (this LBSlice) Less(i, j int) bool {
return this[i].Size < this[j].Size
}
type OrderByNetWorkFee []*TXEntry
func (n OrderByNetWorkFee) Len() int { return len(n) }
func (n OrderByNetWorkFee) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (n OrderByNetWorkFee) Less(i, j int) bool { return n[j].Tx.GasPrice < n[i].Tx.GasPrice }