refactor: nonce store interface

* A nonce value can  now be bootstrapped externally from any source
* Removed unecessary SetNewAccountNonce method
This commit is contained in:
Mohamed Sohail 2023-03-29 07:37:59 +00:00
parent 750fdd9c1f
commit 448b142f7c
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
3 changed files with 7 additions and 36 deletions

View File

@ -7,6 +7,5 @@ type Noncestore interface {
Peek(context.Context, string) (uint64, error) Peek(context.Context, string) (uint64, error)
Acquire(context.Context, string) (uint64, error) Acquire(context.Context, string) (uint64, error)
Return(context.Context, string) error Return(context.Context, string) error
SyncNetworkNonce(context.Context, string) (uint64, error) SetAccountNonce(context.Context, string, uint64) error
SetNewAccountNonce(context.Context, string) error
} }

View File

@ -3,27 +3,21 @@ package nonce
import ( import (
"context" "context"
"github.com/grassrootseconomics/celoutils"
redispool "github.com/grassrootseconomics/cic-custodial/pkg/redis" redispool "github.com/grassrootseconomics/cic-custodial/pkg/redis"
"github.com/grassrootseconomics/w3-celo-patch"
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
) )
type Opts struct { type Opts struct {
RedisPool *redispool.RedisPool RedisPool *redispool.RedisPool
CeloProvider *celoutils.Provider
} }
// RedisNoncestore implements `Noncestore` // RedisNoncestore implements `Noncestore`
type RedisNoncestore struct { type RedisNoncestore struct {
chainProvider *celoutils.Provider redis *redispool.RedisPool
redis *redispool.RedisPool
} }
func NewRedisNoncestore(o Opts) Noncestore { func NewRedisNoncestore(o Opts) Noncestore {
return &RedisNoncestore{ return &RedisNoncestore{
redis: o.RedisPool, redis: o.RedisPool,
chainProvider: o.CeloProvider,
} }
} }
@ -70,30 +64,8 @@ func (n *RedisNoncestore) Return(ctx context.Context, publicKey string) error {
return nil return nil
} }
func (n *RedisNoncestore) SyncNetworkNonce(ctx context.Context, publicKey string) (uint64, error) { func (n *RedisNoncestore) SetAccountNonce(ctx context.Context, publicKey string, nonce uint64) error {
var ( if err := n.redis.Client.Set(ctx, publicKey, nonce, 0).Err(); err != nil {
networkNonce uint64
)
err := n.chainProvider.Client.CallCtx(
ctx,
eth.Nonce(w3.A(publicKey), nil).Returns(&networkNonce),
)
if err != nil {
return 0, err
}
err = n.redis.Client.Set(ctx, publicKey, networkNonce, 0).Err()
if err != nil {
return 0, err
}
return networkNonce, nil
}
func (n *RedisNoncestore) SetNewAccountNonce(ctx context.Context, publicKey string) error {
err := n.redis.Client.Set(ctx, publicKey, 0, 0).Err()
if err != nil {
return err return err
} }

View File

@ -24,7 +24,7 @@ func AccountPrepare(cu *custodial.Custodial) func(context.Context, *asynq.Task)
return fmt.Errorf("account: failed %v: %w", err, asynq.SkipRetry) return fmt.Errorf("account: failed %v: %w", err, asynq.SkipRetry)
} }
if err := cu.Noncestore.SetNewAccountNonce(ctx, payload.PublicKey); err != nil { if err := cu.Noncestore.SetAccountNonce(ctx, payload.PublicKey, 0); err != nil {
return err return err
} }