Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust token amount + add exp timer #370

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration-tests/common/gauntlet_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (m *OCRv2TestState) fundNodes() ([]string, error) {
for _, key := range nAccounts {
// We are not deploying in parallel here due to testnet limitations (429 too many requests)
l.Debug().Msg(fmt.Sprintf("Funding node with address: %s", key))
_, err := m.Clients.GauntletClient.TransferToken(m.Common.ChainDetails.StarkTokenAddress, key, "1000000000000000000") // Transferring 0.1 STRK to each node
_, err := m.Clients.GauntletClient.TransferToken(m.Common.ChainDetails.StarkTokenAddress, key, "10000000000000000000") // Transferring 10 STRK to each node
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions ops/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ var TestOCR2Config = OCR2Config{
// Transmitters: txKeys, // user defined
OnchainConfig: "",
OffchainConfig: &OffchainConfig{
DeltaProgressNanoseconds: 8000000000,
DeltaResendNanoseconds: 30000000000,
DeltaRoundNanoseconds: 3000000000,
DeltaGraceNanoseconds: 1000000000,
DeltaStageNanoseconds: 20000000000,
// todo: increase delta round but decrease delta stage
DeltaProgressNanoseconds: 150000000000, // 120s
DeltaResendNanoseconds: 150000000000, // 150s
DeltaRoundNanoseconds: 90000000000, // 90s
DeltaGraceNanoseconds: 5000000000, // 5s
DeltaStageNanoseconds: 30000000000, // 20s
RMax: 5,
S: []int{1, 2},
// OffchainPublicKeys: offChainKeys, // user defined
Expand Down
105 changes: 85 additions & 20 deletions relayer/pkg/chainlink/txm/txm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,77 @@ type StarkTXM interface {
TxManager
}

type ExpTimer struct {
waitTime time.Duration
minDuration time.Duration
maxDuration time.Duration
resetDuration time.Duration
lock chan struct{}
done sync.WaitGroup
}

func NewExpTimer(minDuration, maxDuration, resetDuration time.Duration) *ExpTimer {
return &ExpTimer{
waitTime: minDuration,
minDuration: minDuration,
maxDuration: maxDuration,
resetDuration: resetDuration,
lock: make(chan struct{}, 1),
}
}

func (e *ExpTimer) Start(stop <-chan struct{}) {
e.resetLoop(stop)
}

func (e *ExpTimer) Stop() {
e.done.Wait()
}

func (e *ExpTimer) Wait(ctx context.Context) {
e.lock <- struct{}{}

<-time.After(e.waitTime)

e.waitTime *= 2
if e.waitTime > e.maxDuration {
e.waitTime = e.maxDuration
}

<-e.lock
}

func (e *ExpTimer) resetLoop(stop <-chan struct{}) {
e.done.Add(1)

go func() {
loop:
for {
select {
case <-stop:
break loop
case <-time.After(e.resetDuration):
e.lock <- struct{}{}
e.waitTime = e.minDuration
<-e.lock
continue
}
}

e.done.Done()
}()
}

type starktxm struct {
starter utils.StartStopOnce
lggr logger.Logger
done sync.WaitGroup
stop chan struct{}
queue chan Tx
ks KeystoreAdapter
cfg Config
nonce NonceManager
starter utils.StartStopOnce
lggr logger.Logger
done sync.WaitGroup
stop chan struct{}
queue chan Tx
ks KeystoreAdapter
cfg Config
nonce NonceManager
nonceRetryTimer ExpTimer

client *utils.LazyLoad[*starknet.Client]
feederClient *utils.LazyLoad[*starknet.FeederClient]
Expand All @@ -64,14 +126,15 @@ type starktxm struct {
func New(lggr logger.Logger, keystore loop.Keystore, cfg Config, getClient func() (*starknet.Client, error),
getFeederClient func() (*starknet.FeederClient, error)) (StarkTXM, error) {
txm := &starktxm{
lggr: logger.Named(lggr, "StarknetTxm"),
queue: make(chan Tx, MaxQueueLen),
stop: make(chan struct{}),
client: utils.NewLazyLoad(getClient),
feederClient: utils.NewLazyLoad(getFeederClient),
ks: NewKeystoreAdapter(keystore),
cfg: cfg,
txStore: NewChainTxStore(),
lggr: logger.Named(lggr, "StarknetTxm"),
queue: make(chan Tx, MaxQueueLen),
stop: make(chan struct{}),
client: utils.NewLazyLoad(getClient),
feederClient: utils.NewLazyLoad(getFeederClient),
ks: NewKeystoreAdapter(keystore),
cfg: cfg,
txStore: NewChainTxStore(),
nonceRetryTimer: *NewExpTimer(2*time.Second, 10*time.Second, 20*time.Second),
}
txm.nonce = NewNonceManager(txm.lggr)

Expand All @@ -91,6 +154,9 @@ func (txm *starktxm) Start(ctx context.Context) error {
txm.done.Add(2) // waitgroup: tx sender + confirmer
go txm.broadcastLoop()
go txm.confirmLoop()

txm.nonceRetryTimer.Start(txm.stop)

return nil
})
}
Expand Down Expand Up @@ -156,8 +222,8 @@ func (txm *starktxm) handleNonceErr(ctx context.Context, accountAddress *felt.Fe

txm.lggr.Debugw("Handling Nonce Validation Error By Resubmitting Txs...", "account", accountAddress)

// wait for rpc starknet_estimateFee to catch up with nonce returned by starknet_getNonce
<-time.After(utils.WithJitter(time.Second))
// exponential backoff wait for rpc starknet_estimateFee to catch up with nonce returned by starknet_getNonce
txm.nonceRetryTimer.Wait(ctx)

// resync nonce so that new queued txs can be unblocked
client, err := txm.client.Get()
Expand Down Expand Up @@ -300,8 +366,6 @@ func (txm *starktxm) broadcast(ctx context.Context, publicKey *felt.Felt, accoun
return txhash, fmt.Errorf("failed to get FRI estimate")
}

txm.lggr.Infow("Fee estimate", "in units", friEstimate.FeeUnit)

// pad estimate to 150% (add extra because estimate did not include validation)
gasConsumed := friEstimate.GasConsumed.BigInt(new(big.Int))
expandedGas := new(big.Int).Mul(gasConsumed, big.NewInt(150))
Expand Down Expand Up @@ -460,6 +524,7 @@ func (txm *starktxm) Close() error {
return txm.starter.StopOnce("starktxm", func() error {
close(txm.stop)
txm.done.Wait()
txm.nonceRetryTimer.Stop()
return nil
})
}
Expand Down
Loading