forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitoredtx.go
196 lines (160 loc) · 5.27 KB
/
monitoredtx.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
package ethtxmanager
import (
"encoding/hex"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
const (
// MonitoredTxStatusCreated mean the tx was just added to the storage
MonitoredTxStatusCreated = MonitoredTxStatus("created")
// MonitoredTxStatusSent means that at least a eth tx was sent to the network
MonitoredTxStatusSent = MonitoredTxStatus("sent")
// MonitoredTxStatusFailed means the tx was already mined and failed with an
// error that can't be recovered automatically, ex: the data in the tx is invalid
// and the tx gets reverted
MonitoredTxStatusFailed = MonitoredTxStatus("failed")
// MonitoredTxStatusConfirmed means the tx was already mined and the receipt
// status is Successful
MonitoredTxStatusConfirmed = MonitoredTxStatus("confirmed")
// MonitoredTxStatusReorged is used when a monitored tx was already confirmed but
// the L1 block where this tx was confirmed has been reorged, in this situation
// the caller needs to review this information and wait until it gets confirmed
// again in a future block
MonitoredTxStatusReorged = MonitoredTxStatus("reorged")
// MonitoredTxStatusDone means the tx was set by the owner as done
MonitoredTxStatusDone = MonitoredTxStatus("done")
)
// MonitoredTxStatus represents the status of a monitored tx
type MonitoredTxStatus string
// String returns a string representation of the status
func (s MonitoredTxStatus) String() string {
return string(s)
}
// monitoredTx represents a set of information used to build tx
// plus information to monitor if the transactions was sent successfully
type monitoredTx struct {
// owner is the common identifier among all the monitored tx to identify who
// created this, it's a identification provided by the caller in order to be
// used in the future to query the monitored tx by the owner, this allows the
// caller to be free of implementing a persistence layer to monitor the txs
owner string
// id is the tx identifier controller by the caller
id string
// sender of the tx, used to identify which private key should be used to sing the tx
from common.Address
// receiver of the tx
to *common.Address
// nonce used to create the tx
nonce uint64
// tx value
value *big.Int
// tx data
data []byte
// tx gas
gas uint64
// tx gas offset
gasOffset uint64
// tx gas price
gasPrice *big.Int
// status of this monitoring
status MonitoredTxStatus
// blockNumber represents the block where the tx was identified
// to be mined, it's the same as the block number found in the
// tx receipt, this is used to control reorged monitored txs
blockNumber *big.Int
// history represent all transaction hashes from
// transactions created using this struct data and
// sent to the network
history map[common.Hash]bool
// createdAt date time it was created
createdAt time.Time
// updatedAt last date time it was updated
updatedAt time.Time
}
// Tx uses the current information to build a tx
func (mTx monitoredTx) Tx() *types.Transaction {
tx := types.NewTx(&types.LegacyTx{
To: mTx.to,
Nonce: mTx.nonce,
Value: mTx.value,
Data: mTx.data,
Gas: mTx.gas + mTx.gasOffset,
GasPrice: mTx.gasPrice,
})
return tx
}
// AddHistory adds a transaction to the monitoring history
func (mTx monitoredTx) AddHistory(tx *types.Transaction) error {
if _, found := mTx.history[tx.Hash()]; found {
return ErrAlreadyExists
}
mTx.history[tx.Hash()] = true
return nil
}
// toStringPtr returns the current to field as a string pointer
func (mTx *monitoredTx) toStringPtr() *string {
var to *string
if mTx.to != nil {
s := mTx.to.String()
to = &s
}
return to
}
// valueU64Ptr returns the current value field as a uint64 pointer
func (mTx *monitoredTx) valueU64Ptr() *uint64 {
var value *uint64
if mTx.value != nil {
tmp := mTx.value.Uint64()
value = &tmp
}
return value
}
// dataStringPtr returns the current data field as a string pointer
func (mTx *monitoredTx) dataStringPtr() *string {
var data *string
if mTx.data != nil {
tmp := hex.EncodeToString(mTx.data)
data = &tmp
}
return data
}
// historyStringSlice returns the current history field as a string slice
func (mTx *monitoredTx) historyStringSlice() []string {
history := make([]string, 0, len(mTx.history))
for h := range mTx.history {
history = append(history, h.String())
}
return history
}
// historyHashSlice returns the current history field as a string slice
func (mTx *monitoredTx) historyHashSlice() []common.Hash {
history := make([]common.Hash, 0, len(mTx.history))
for h := range mTx.history {
history = append(history, h)
}
return history
}
// blockNumberU64Ptr returns the current blockNumber as a uint64 pointer
func (mTx *monitoredTx) blockNumberU64Ptr() *uint64 {
var blockNumber *uint64
if mTx.blockNumber != nil {
tmp := mTx.blockNumber.Uint64()
blockNumber = &tmp
}
return blockNumber
}
// MonitoredTxResult represents the result of a execution of a monitored tx
type MonitoredTxResult struct {
ID string
Status MonitoredTxStatus
BlockNumber *big.Int
Txs map[common.Hash]TxResult
}
// TxResult represents the result of a execution of a ethereum transaction in the block chain
type TxResult struct {
Tx *types.Transaction
Receipt *types.Receipt
RevertMessage string
}