Skip to content

Commit e55aef3

Browse files
reduce preconf lock time and remove timeout preconf tx from txpool (#109)
* reduce preconf lock time and remove timeout preconf tx from txpool * add more unit test and bugfix * Add some logging and remove opstatus.CurrentL1 check, as using l2Origin.L1 is enough --------- Co-authored-by: ivan <314130948@qq.com>
1 parent dd36053 commit e55aef3

File tree

19 files changed

+1394
-182
lines changed

19 files changed

+1394
-182
lines changed

core/events.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,83 @@
1717
package core
1818

1919
import (
20+
"sync"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
"github.com/ethereum/go-ethereum/common/hexutil"
2224
"github.com/ethereum/go-ethereum/core/types"
2325
)
2426

27+
type PreconfStatus string
28+
2529
const (
26-
PreconfStatusSuccess = "success"
27-
PreconfStatusFailed = "failed"
30+
PreconfStatusSuccess PreconfStatus = "success"
31+
PreconfStatusFailed PreconfStatus = "failed"
32+
PreconfStatusTimeout PreconfStatus = "timeout"
33+
PreconfStatusWaiting PreconfStatus = "waiting"
2834
)
2935

36+
// a copy of core/types/log.go
37+
// removed some fields that preconf can't provide
38+
type Log struct {
39+
// Consensus fields:
40+
// address of the contract that generated the event
41+
Address common.Address `json:"address" gencodec:"required"`
42+
// list of topics provided by the contract.
43+
Topics []common.Hash `json:"topics" gencodec:"required"`
44+
// supplied by the contract, usually ABI-encoded
45+
Data hexutil.Bytes `json:"data" gencodec:"required"`
46+
}
47+
48+
func NewLogs(originalLogs []*types.Log) []*Log {
49+
logs := make([]*Log, 0, len(originalLogs))
50+
for _, log := range originalLogs {
51+
logs = append(logs, &Log{
52+
Address: log.Address,
53+
Topics: log.Topics,
54+
Data: log.Data,
55+
})
56+
}
57+
return logs
58+
}
59+
60+
type PreconfTxReceipt struct {
61+
Logs []*Log `json:"logs"`
62+
}
63+
3064
// NewPreconfTxsEvent is posted when a preconf transaction enters the transaction pool.
3165
type NewPreconfTxEvent struct {
32-
TxHash common.Hash `json:"txHash"`
33-
Status string `json:"status"` // "success" | "failed"
34-
Reason string `json:"reason"` // "optional failure message"
35-
PredictedL2BlockNumber hexutil.Uint64 `json:"blockHeight"` // "predicted L2 block number"
66+
TxHash common.Hash `json:"txHash"`
67+
Status PreconfStatus `json:"status"`
68+
Reason string `json:"reason"` // "optional failure message"
69+
PredictedL2BlockNumber hexutil.Uint64 `json:"blockHeight"` // "predicted L2 block number"
70+
Receipt PreconfTxReceipt `json:"receipt"`
3671
}
3772

3873
// NewPreconfTxRequestEvent is posted when a preconf transaction request enters the transaction pool.
3974
type NewPreconfTxRequest struct {
4075
Tx *types.Transaction
76+
mu sync.Mutex
77+
Status PreconfStatus
4178
PreconfResult chan<- *PreconfResponse
4279
ClosePreconfResultFn func()
4380
}
4481

82+
func (e *NewPreconfTxRequest) GetStatus() PreconfStatus {
83+
e.mu.Lock()
84+
defer e.mu.Unlock()
85+
return e.Status
86+
}
87+
88+
func (e *NewPreconfTxRequest) SetStatus(statusBefore, status PreconfStatus) PreconfStatus {
89+
e.mu.Lock()
90+
defer e.mu.Unlock()
91+
if e.Status == statusBefore {
92+
e.Status = status
93+
}
94+
return e.Status
95+
}
96+
4597
type PreconfResponse struct {
4698
Receipt *types.Receipt
4799
Err error

core/txpool/journal.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,25 @@ func (journal *journal) rotate(all map[common.Address]types.Transactions, precon
145145
}
146146
journaled := 0
147147

148+
// add preconf txs to the journal
149+
// preconf txs is priority txs, so it should be the first
150+
preconfTxsMap := make(map[common.Hash]bool)
151+
for _, tx := range preconfTxs {
152+
if err = rlp.Encode(replacement, tx); err != nil {
153+
replacement.Close()
154+
return err
155+
} else {
156+
preconfTxsMap[tx.Hash()] = true
157+
}
158+
}
159+
journaled += len(preconfTxs)
160+
148161
// add all txs to the journal
149162
for _, txs := range all {
150163
for _, tx := range txs {
164+
if preconfTxsMap[tx.Hash()] {
165+
continue
166+
}
151167
if err = rlp.Encode(replacement, tx); err != nil {
152168
replacement.Close()
153169
return err
@@ -156,15 +172,6 @@ func (journal *journal) rotate(all map[common.Address]types.Transactions, precon
156172
journaled += len(txs)
157173
}
158174

159-
// add preconf txs to the journal
160-
for _, tx := range preconfTxs {
161-
if err = rlp.Encode(replacement, tx); err != nil {
162-
replacement.Close()
163-
return err
164-
}
165-
}
166-
journaled += len(preconfTxs)
167-
168175
replacement.Close()
169176

170177
// Replace the live journal with the newly generated one

0 commit comments

Comments
 (0)