cic-custodial/internal/nonce/redis.go

131 lines
2.9 KiB
Go
Raw Normal View History

2022-11-30 10:51:24 +01:00
package nonce
import (
"context"
"errors"
2022-11-30 10:51:24 +01:00
"github.com/grassrootseconomics/celoutils"
"github.com/grassrootseconomics/cic-custodial/internal/store"
2022-11-30 10:51:24 +01:00
redispool "github.com/grassrootseconomics/cic-custodial/pkg/redis"
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
"github.com/jackc/pgx/v5"
"github.com/redis/go-redis/v9"
2022-11-30 10:51:24 +01:00
)
type (
Opts struct {
ChainProvider *celoutils.Provider
RedisPool *redispool.RedisPool
Store store.Store
}
2022-11-30 10:51:24 +01:00
// RedisNoncestore implements `Noncestore`
RedisNoncestore struct {
chainProvider *celoutils.Provider
redis *redispool.RedisPool
store store.Store
}
)
2022-11-30 10:51:24 +01:00
func NewRedisNoncestore(o Opts) Noncestore {
return &RedisNoncestore{
chainProvider: o.ChainProvider,
redis: o.RedisPool,
store: o.Store,
2022-11-30 10:51:24 +01:00
}
}
func (n *RedisNoncestore) Peek(ctx context.Context, publicKey string) (uint64, error) {
nonce, err := n.redis.Client.Get(ctx, publicKey).Uint64()
if err != nil {
if err == redis.Nil {
nonce, err = n.bootstrap(ctx, publicKey)
if err != nil {
return 0, err
}
return nonce, nil
} else {
return 0, err
}
2022-11-30 10:51:24 +01:00
}
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 {
if err == redis.Nil {
nonce, err = n.bootstrap(ctx, publicKey)
if err != nil {
return 0, err
}
} else {
return 0, err
}
2022-11-30 10:51:24 +01:00
}
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) SetAccountNonce(ctx context.Context, publicKey string, nonce uint64) error {
if err := n.redis.Client.Set(ctx, publicKey, nonce, 0).Err(); err != nil {
2022-11-30 10:51:24 +01:00
return err
}
return nil
}
// bootstrap can be used to restore a destroyed redis nonce cache automatically.
// It first uses the otx_sign table as a source of nonce values.
// If the otx_sign table is corrupted, it can fallback to the network nonce.
// Ideally, the redis nonce cache should never be lost.
func (n *RedisNoncestore) bootstrap(ctx context.Context, publicKey string) (uint64, error) {
lastDbNonce, err := n.store.GetNextNonce(ctx, publicKey)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
err := n.chainProvider.Client.CallCtx(
ctx,
eth.Nonce(celoutils.HexToAddress(publicKey), nil).Returns(&lastDbNonce),
)
if err != nil {
return 0, err
}
} else {
return 0, err
}
}
if err := n.SetAccountNonce(ctx, publicKey, lastDbNonce); err != nil {
return 0, err
}
return lastDbNonce, nil
}