-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(taiko-client): introduce
TxMgrSelector
for proposer / prover (#…
…17986) Co-authored-by: maskpp <[email protected]>
- Loading branch information
1 parent
28d9072
commit 6eb298f
Showing
6 changed files
with
130 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package utils | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/ethereum-optimism/optimism/op-service/txmgr" | ||
) | ||
|
||
var ( | ||
defaultPrivateTxMgrRetryInterval = 5 * time.Minute | ||
) | ||
|
||
// TxMgrSelector is responsible for selecting the correct transaction manager, | ||
// it will choose the transaction manager for a private mempool if it is available and works well, | ||
// otherwise it will choose the normal transaction manager. | ||
type TxMgrSelector struct { | ||
txMgr *txmgr.SimpleTxManager | ||
privateTxMgr *txmgr.SimpleTxManager | ||
privateTxMgrFailedAt *time.Time | ||
privateTxMgrRetryInterval time.Duration | ||
} | ||
|
||
// NewTxMgrSelector creates a new TxMgrSelector instance. | ||
func NewTxMgrSelector( | ||
txMgr *txmgr.SimpleTxManager, | ||
privateTxMgr *txmgr.SimpleTxManager, | ||
privateTxMgrRetryInterval *time.Duration, | ||
) *TxMgrSelector { | ||
retryInterval := defaultPrivateTxMgrRetryInterval | ||
if privateTxMgrRetryInterval != nil { | ||
retryInterval = *privateTxMgrRetryInterval | ||
} | ||
|
||
return &TxMgrSelector{ | ||
txMgr: txMgr, | ||
privateTxMgr: privateTxMgr, | ||
privateTxMgrFailedAt: nil, | ||
privateTxMgrRetryInterval: retryInterval, | ||
} | ||
} | ||
|
||
// Select selects a transaction manager based on the current state. | ||
func (s *TxMgrSelector) Select() (*txmgr.SimpleTxManager, bool) { | ||
// If there is no private transaction manager, return the normal transaction manager. | ||
if s.privateTxMgr == nil { | ||
return s.txMgr, false | ||
} | ||
|
||
// If the private transaction manager has failed, check if it is time to retry. | ||
if s.privateTxMgrFailedAt == nil || time.Now().After(s.privateTxMgrFailedAt.Add(s.privateTxMgrRetryInterval)) { | ||
return s.privateTxMgr, true | ||
} | ||
|
||
// Otherwise, return the normal transaction manager. | ||
return s.txMgr, false | ||
} | ||
|
||
// RecordPrivateTxMgrFailed records the time when the private transaction manager has failed. | ||
func (s *TxMgrSelector) RecordPrivateTxMgrFailed() { | ||
now := time.Now() | ||
s.privateTxMgrFailedAt = &now | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/taiko-client/internal/utils/txmgr_selector_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum-optimism/optimism/op-service/txmgr" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
testTxMgr = &txmgr.SimpleTxManager{} | ||
testSelector = NewTxMgrSelector(testTxMgr, nil, nil) | ||
) | ||
|
||
func TestNewTxMgrSelector(t *testing.T) { | ||
require.Equal(t, defaultPrivateTxMgrRetryInterval, testSelector.privateTxMgrRetryInterval) | ||
} | ||
|
||
func TestSelect(t *testing.T) { | ||
txMgr, isPrivate := testSelector.Select() | ||
require.NotNil(t, txMgr) | ||
require.False(t, isPrivate) | ||
} | ||
|
||
func TestRecordPrivateTxMgrFailed(t *testing.T) { | ||
require.Nil(t, testSelector.privateTxMgrFailedAt) | ||
testSelector.RecordPrivateTxMgrFailed() | ||
require.NotNil(t, testSelector.privateTxMgrFailedAt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters