From 24c81aaee202cc90f35591eaa85d017a08a92875 Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Wed, 26 Oct 2022 09:21:47 +0000 Subject: [PATCH] fix(noncestore): return nonce incase of task failure * Return must complete successfully else a nonce gap could form. * TODO: add tests for Return * closes#18 --- internal/noncestore/noncestore.go | 2 +- internal/noncestore/providers/redis/redis.go | 13 ++++--------- internal/tasker/server/registration.go | 9 +++++++++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/internal/noncestore/noncestore.go b/internal/noncestore/noncestore.go index ed4e22c..2255c75 100644 --- a/internal/noncestore/noncestore.go +++ b/internal/noncestore/noncestore.go @@ -6,7 +6,7 @@ import "context" type Noncestore interface { Peek(context.Context, string) (uint64, error) Acquire(context.Context, string) (uint64, error) - Return(context.Context, string) (uint64, error) + Return(context.Context, string) error SyncNetworkNonce(context.Context, string) (uint64, error) SetNewAccountNonce(context.Context, string) error } diff --git a/internal/noncestore/providers/redis/redis.go b/internal/noncestore/providers/redis/redis.go index 83ddf4b..45a7528 100644 --- a/internal/noncestore/providers/redis/redis.go +++ b/internal/noncestore/providers/redis/redis.go @@ -8,7 +8,6 @@ import ( "github.com/grassrootseconomics/cic-go-sdk/chain" "github.com/lmittmann/w3" "github.com/lmittmann/w3/module/eth" - "github.com/zerodha/logf" ) // Opts represents the Redis nonce store specific params @@ -18,14 +17,12 @@ type Opts struct { MinIdleConns int PoolSize int ChainProvider *chain.Provider - Lo logf.Logger } // RedisNoncestore implements `noncestore.Noncestore` type RedisNoncestore struct { chainProvider *chain.Provider redis *redis.Client - lo logf.Logger } func NewRedisNoncestore(o Opts) (noncestore.Noncestore, error) { @@ -43,7 +40,6 @@ func NewRedisNoncestore(o Opts) (noncestore.Noncestore, error) { return &RedisNoncestore{ redis: redisClient, chainProvider: o.ChainProvider, - lo: o.Lo, }, nil } @@ -81,20 +77,20 @@ func (ns *RedisNoncestore) Acquire(ctx context.Context, publicKey string) (uint6 return nonce, nil } -func (ns *RedisNoncestore) Return(ctx context.Context, publicKey string) (uint64, error) { +func (ns *RedisNoncestore) Return(ctx context.Context, publicKey string) error { nonce, err := ns.redis.Get(ctx, publicKey).Uint64() if err != nil { - return 0, err + return err } if nonce > 0 { err = ns.redis.Decr(ctx, publicKey).Err() if err != nil { - return 0, err + return err } } - return nonce, nil + return nil } func (ns *RedisNoncestore) SyncNetworkNonce(ctx context.Context, publicKey string) (uint64, error) { @@ -121,7 +117,6 @@ func (ns *RedisNoncestore) SyncNetworkNonce(ctx context.Context, publicKey strin func (ns *RedisNoncestore) SetNewAccountNonce(ctx context.Context, publicKey string) error { err := ns.redis.Set(ctx, publicKey, 0, 0).Err() if err != nil { - ns.lo.Error("noncestore", "err", err) return err } diff --git a/internal/tasker/server/registration.go b/internal/tasker/server/registration.go index 5a6981e..26bc991 100644 --- a/internal/tasker/server/registration.go +++ b/internal/tasker/server/registration.go @@ -62,6 +62,9 @@ func (tp *TaskerProcessor) giftGasProcessor(ctx context.Context, t *asynq.Task) Nonce: nonce, }, big.NewInt(initialGiftGasValue)) if err != nil { + if err := tp.Noncestore.Return(ctx, p.PublicKey); err != nil { + return err + } return err } @@ -69,11 +72,17 @@ func (tp *TaskerProcessor) giftGasProcessor(ctx context.Context, t *asynq.Task) Tx: builtTx, }, client.TxDispatchTask) if err != nil { + if err := tp.Noncestore.Return(ctx, p.PublicKey); err != nil { + return err + } return err } _, err = tp.TaskerClient.CreateRegistrationTask(p, client.ActivateAccountTask) if err != nil { + if err := tp.Noncestore.Return(ctx, p.PublicKey); err != nil { + return err + } return err }