2022-11-30 10:51:24 +01:00
|
|
|
package nonce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-13 12:38:23 +02:00
|
|
|
"errors"
|
2022-11-30 10:51:24 +01:00
|
|
|
|
2023-04-13 12:38:23 +02: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"
|
2023-04-13 12:38:23 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
type (
|
|
|
|
Opts struct {
|
2023-04-13 12:38:23 +02:00
|
|
|
ChainProvider *celoutils.Provider
|
|
|
|
RedisPool *redispool.RedisPool
|
|
|
|
Store store.Store
|
2023-03-29 18:10:58 +02:00
|
|
|
}
|
2022-11-30 10:51:24 +01:00
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
// RedisNoncestore implements `Noncestore`
|
|
|
|
RedisNoncestore struct {
|
2023-04-13 12:38:23 +02:00
|
|
|
chainProvider *celoutils.Provider
|
|
|
|
redis *redispool.RedisPool
|
|
|
|
store store.Store
|
2023-03-29 18:10:58 +02:00
|
|
|
}
|
|
|
|
)
|
2022-11-30 10:51:24 +01:00
|
|
|
|
|
|
|
func NewRedisNoncestore(o Opts) Noncestore {
|
|
|
|
return &RedisNoncestore{
|
2023-04-13 12:38:23 +02:00
|
|
|
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()
|
2023-05-16 14:19:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == redis.Nil {
|
|
|
|
nonce, err = n.bootstrap(ctx, publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nonce, nil
|
|
|
|
} else {
|
2023-04-13 12:38:23 +02:00
|
|
|
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()
|
2023-05-16 14:19:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == redis.Nil {
|
|
|
|
nonce, err = n.bootstrap(ctx, publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-13 12:38:23 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:37:59 +02:00
|
|
|
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
|
|
|
|
}
|
2023-04-13 12:38:23 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|