forked from meshplus/bitxhub
/
types.go
143 lines (126 loc) · 3.21 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
package common
import (
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/axiomesh/axiom-bft/common/consensus"
"github.com/axiomesh/axiom-kit/txpool"
"github.com/axiomesh/axiom-kit/types"
)
const (
LocalTxEvent = iota
RemoteTxEvent
)
var (
ErrorPreCheck = errors.New("precheck failed")
ErrorAddTxPool = errors.New("add txpool failed")
)
// UncheckedTxEvent represents misc event sent by local modules
type UncheckedTxEvent struct {
EventType int
Event any
}
type TxWithResp struct {
Tx *types.Transaction
CheckCh chan *TxResp
PoolCh chan *TxResp
}
type TxResp struct {
Status bool
ErrorMsg string
}
type CommitEvent struct {
Block *types.Block
StateUpdatedCheckpoint *consensus.Checkpoint
}
type TxSimpleInfo struct {
Hash string
Nonce uint64
Size int
Local bool
LifeTime int64
ArrivedTime int64
}
type TxInfo struct {
Tx *types.Transaction
Local bool
LifeTime int64
ArrivedTime int64
}
type AccountMeta struct {
CommitNonce uint64
PendingNonce uint64
TxCount uint64
Txs []*TxInfo
SimpleTxs []*TxSimpleInfo
}
func AccountMetaFromTxpool(res *txpool.AccountMeta[types.Transaction, *types.Transaction]) *AccountMeta {
if res == nil {
return nil
}
return &AccountMeta{
CommitNonce: res.CommitNonce,
PendingNonce: res.PendingNonce,
TxCount: res.TxCount,
Txs: lo.Map(res.Txs, func(tx *txpool.TxInfo[types.Transaction, *types.Transaction], index int) *TxInfo {
return &TxInfo{
Tx: tx.Tx,
Local: tx.Local,
LifeTime: tx.LifeTime,
ArrivedTime: tx.ArrivedTime,
}
}),
SimpleTxs: lo.Map(res.SimpleTxs, func(tx *txpool.TxSimpleInfo, index int) *TxSimpleInfo {
return &TxSimpleInfo{
Hash: tx.Hash,
Nonce: tx.Nonce,
Size: tx.Size,
Local: tx.Local,
LifeTime: tx.LifeTime,
ArrivedTime: tx.ArrivedTime,
}
}),
}
}
type BatchSimpleInfo struct {
TxCount uint64
Txs []*TxSimpleInfo
Timestamp int64
}
type Meta struct {
TxCountLimit uint64
TxCount uint64
ReadyTxCount uint64
Batches map[string]*BatchSimpleInfo
MissingBatchTxs map[string]map[uint64]string
Accounts map[string]*AccountMeta
}
func MetaFromTxpool(res *txpool.Meta[types.Transaction, *types.Transaction]) *Meta {
if res == nil {
return nil
}
return &Meta{
TxCountLimit: res.TxCountLimit,
TxCount: res.TxCount,
ReadyTxCount: res.ReadyTxCount,
Batches: lo.MapEntries(res.Batches, func(key string, value *txpool.BatchSimpleInfo) (string, *BatchSimpleInfo) {
return key, &BatchSimpleInfo{
TxCount: value.TxCount,
Txs: lo.Map(value.Txs, func(tx *txpool.TxSimpleInfo, index int) *TxSimpleInfo {
return &TxSimpleInfo{
Hash: tx.Hash,
Nonce: tx.Nonce,
Size: tx.Size,
Local: tx.Local,
LifeTime: tx.LifeTime,
ArrivedTime: tx.ArrivedTime,
}
}),
Timestamp: value.Timestamp,
}
}),
MissingBatchTxs: res.MissingBatchTxs,
Accounts: lo.MapEntries(res.Accounts, func(key string, value *txpool.AccountMeta[types.Transaction, *types.Transaction]) (string, *AccountMeta) {
return key, AccountMetaFromTxpool(value)
}),
}
}