cic-custodial/internal/nonce/redis.go
Mohammed Sohail 4d13a14dc2
refactor: breaking API changes
Squashed commit of the following:

commit 05e1396121
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Wed Feb 15 10:03:44 2023 +0300

    feat: add status types to dispatcher

commit 397cd78ca9
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Wed Feb 15 09:39:31 2023 +0300

    deps: bump -> cic-celo-sdk

commit f2ba079232
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Sun Feb 12 16:53:53 2023 +0300

    snapshot: 12-ebening

commit 4f7909e4ee
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Sun Feb 12 12:50:43 2023 +0300

    xnapshot: 12-02

commit 773474cad9
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Feb 9 14:23:37 2023 +0300

    update: deps initializers

commit 8a0880fcfc
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Feb 9 10:42:15 2023 +0300

    wip: refactor taskers

commit 8676450122
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Fri Feb 3 12:29:27 2023 +0300

    refactor: decouple sql queries, remove transfer

    * add inline docs
    * removed transfer taks in prep for re-write

commit b4c09cd11a
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Feb 2 12:29:43 2023 +0000

    refactor: cmd/service/* and api
2023-02-15 10:05:43 +03:00

102 lines
2.0 KiB
Go

package nonce
import (
"context"
celo "github.com/grassrootseconomics/cic-celo-sdk"
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 {
RedisPool *redispool.RedisPool
CeloProvider *celo.Provider
}
// RedisNoncestore implements `Noncestore`
type RedisNoncestore struct {
chainProvider *celo.Provider
redis *redispool.RedisPool
}
func NewRedisNoncestore(o Opts) Noncestore {
return &RedisNoncestore{
redis: o.RedisPool,
chainProvider: o.CeloProvider,
}
}
func (n *RedisNoncestore) Peek(ctx context.Context, publicKey string) (uint64, error) {
nonce, err := n.redis.Client.Get(ctx, publicKey).Uint64()
if err != nil {
return 0, err
}
return nonce, nil
}
func (n *RedisNoncestore) Acquire(ctx context.Context, publicKey string) (uint64, error) {
var (
nonce uint64
)
nonce, err := n.redis.Client.Get(ctx, publicKey).Uint64()
if err != nil {
return 0, nil
}
err = n.redis.Client.Incr(ctx, publicKey).Err()
if err != nil {
return 0, err
}
return nonce, nil
}
func (n *RedisNoncestore) Return(ctx context.Context, publicKey string) error {
nonce, err := n.redis.Client.Get(ctx, publicKey).Uint64()
if err != nil {
return err
}
if nonce > 0 {
err = n.redis.Client.Decr(ctx, publicKey).Err()
if err != nil {
return err
}
}
return nil
}
func (n *RedisNoncestore) SyncNetworkNonce(ctx context.Context, publicKey string) (uint64, error) {
var (
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 nil
}