noncestore: disable dev noncestore

* system noncestore defaults to redis
* remove system account init, move to action provider
This commit is contained in:
Mohamed Sohail 2022-10-23 10:42:19 +00:00
parent 827bdb2bd9
commit fb2091da17
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
8 changed files with 68 additions and 159 deletions

View File

@ -8,7 +8,6 @@ import (
postgres_keystore "github.com/grassrootseconomics/cic-custodial/internal/keystore/providers/postgres" postgres_keystore "github.com/grassrootseconomics/cic-custodial/internal/keystore/providers/postgres"
"github.com/grassrootseconomics/cic-custodial/internal/noncestore" "github.com/grassrootseconomics/cic-custodial/internal/noncestore"
redis_noncestore "github.com/grassrootseconomics/cic-custodial/internal/noncestore/providers/redis" redis_noncestore "github.com/grassrootseconomics/cic-custodial/internal/noncestore/providers/redis"
system_provider "github.com/grassrootseconomics/cic-custodial/internal/system"
tasker_client "github.com/grassrootseconomics/cic-custodial/internal/tasker/client" tasker_client "github.com/grassrootseconomics/cic-custodial/internal/tasker/client"
"github.com/grassrootseconomics/cic-go-sdk/chain" "github.com/grassrootseconomics/cic-go-sdk/chain"
"github.com/knadh/koanf" "github.com/knadh/koanf"
@ -65,19 +64,6 @@ func initChainProvider() *chain.Provider {
return provider return provider
} }
func initSystemProvider() *system_provider.SystemProvider {
systemProvider, err := system_provider.NewSystemProvider(system_provider.Opts{
SystemPublicKey: ko.MustString("admin.public"),
SystemPrivateKey: ko.MustString("admin.key"),
ChainProvider: chainProvider,
})
if err != nil {
lo.Fatal("initSystemProvider", "error", err)
}
return systemProvider
}
func initTaskerClient() *tasker_client.TaskerClient { func initTaskerClient() *tasker_client.TaskerClient {
return tasker_client.NewTaskerClient(tasker_client.Opts{ return tasker_client.NewTaskerClient(tasker_client.Opts{
RedisDSN: ko.MustString("tasker.dsn"), RedisDSN: ko.MustString("tasker.dsn"),

View File

@ -42,12 +42,17 @@ func main() {
taskerClient = initTaskerClient() taskerClient = initTaskerClient()
actionsProvider := actions.NewActionsProvider(actions.Opts{ actionsProvider, err := actions.NewActionsProvider(actions.Opts{
SystemProvider: initSystemProvider(), SystemPublicKey: ko.MustString("admin.public"),
ChainProvider: chainProvider, SystemPrivateKey: ko.MustString("admin.key"),
Keystore: initKeystore(), ChainProvider: chainProvider,
Noncestore: initNoncestore(), Keystore: initKeystore(),
Noncestore: initNoncestore(),
Logger: lo,
}) })
if err != nil {
lo.Fatal("initActionsProvider", "err", err)
}
httpServer = api.BootstrapHTTPServer(api.Opts{ httpServer = api.BootstrapHTTPServer(api.Opts{
ActionsProvider: actionsProvider, ActionsProvider: actionsProvider,

View File

@ -2,13 +2,15 @@ package actions
import ( import (
"context" "context"
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
eth_crypto "github.com/ethereum/go-ethereum/crypto"
"github.com/grassrootseconomics/cic-custodial/internal/ethereum" "github.com/grassrootseconomics/cic-custodial/internal/ethereum"
"github.com/grassrootseconomics/cic-custodial/internal/keystore" "github.com/grassrootseconomics/cic-custodial/internal/keystore"
"github.com/grassrootseconomics/cic-custodial/internal/noncestore" "github.com/grassrootseconomics/cic-custodial/internal/noncestore"
system_provider "github.com/grassrootseconomics/cic-custodial/internal/system"
"github.com/grassrootseconomics/cic-go-sdk/chain" "github.com/grassrootseconomics/cic-go-sdk/chain"
"github.com/zerodha/logf"
) )
type Actions interface { type Actions interface {
@ -24,26 +26,46 @@ type Actions interface {
} }
type Opts struct { type Opts struct {
SystemProvider *system_provider.SystemProvider SystemPublicKey string
ChainProvider *chain.Provider SystemPrivateKey string
Keystore keystore.Keystore ChainProvider *chain.Provider
Noncestore noncestore.Noncestore Keystore keystore.Keystore
Noncestore noncestore.Noncestore
Logger logf.Logger
} }
type ActionsProvider struct { type ActionsProvider struct {
SystemProvider *system_provider.SystemProvider SystemPublicKey string
ChainProvider *chain.Provider SystemPrivateKey *ecdsa.PrivateKey
Keystore keystore.Keystore ChainProvider *chain.Provider
Noncestore noncestore.Noncestore Keystore keystore.Keystore
Noncestore noncestore.Noncestore
Lo logf.Logger
} }
func NewActionsProvider(o Opts) *ActionsProvider { func NewActionsProvider(o Opts) (*ActionsProvider, error) {
var _ Actions = (*ActionsProvider)(nil) var _ Actions = (*ActionsProvider)(nil)
return &ActionsProvider{ loadedPrivateKey, err := eth_crypto.HexToECDSA(o.SystemPrivateKey)
SystemProvider: o.SystemProvider, if err != nil {
ChainProvider: o.ChainProvider, return nil, err
Keystore: o.Keystore,
Noncestore: o.Noncestore,
} }
_, err = o.Noncestore.Peek(context.Background(), o.SystemPublicKey)
if err != nil {
nonce, err := o.Noncestore.SyncNetworkNonce(context.Background(), o.SystemPublicKey)
o.Logger.Debug("actionsProvider: syncing system nonce", "nonce", nonce)
if err != nil {
return nil, err
}
}
return &ActionsProvider{
SystemPublicKey: o.SystemPublicKey,
SystemPrivateKey: loadedPrivateKey,
ChainProvider: o.ChainProvider,
Keystore: o.Keystore,
Noncestore: o.Noncestore,
Lo: o.Logger,
}, nil
} }

View File

@ -15,9 +15,14 @@ const (
) )
func (ap *ActionsProvider) SignGiftGasTx(ctx context.Context, giftTo string) (*types.Transaction, error) { func (ap *ActionsProvider) SignGiftGasTx(ctx context.Context, giftTo string) (*types.Transaction, error) {
builtTx, err := ap.ChainProvider.BuildGasTransferTx(ap.SystemProvider.SystemPrivateKey, chain.TransactionData{ nonce, err := ap.Noncestore.Acquire(ctx, ap.SystemPublicKey)
if err != nil {
return nil, err
}
builtTx, err := ap.ChainProvider.BuildGasTransferTx(ap.SystemPrivateKey, chain.TransactionData{
To: w3.A(giftTo), To: w3.A(giftTo),
Nonce: ap.SystemProvider.SystemNoncestore.Acquire(), Nonce: nonce,
}, big.NewInt(initialGiftGasValue)) }, big.NewInt(initialGiftGasValue))
if err != nil { if err != nil {
return &types.Transaction{}, err return &types.Transaction{}, err
@ -27,9 +32,14 @@ func (ap *ActionsProvider) SignGiftGasTx(ctx context.Context, giftTo string) (*t
} }
func (ap *ActionsProvider) SignTopUpGasTx(ctx context.Context, giftTo string) (*types.Transaction, error) { func (ap *ActionsProvider) SignTopUpGasTx(ctx context.Context, giftTo string) (*types.Transaction, error) {
builtTx, err := ap.ChainProvider.BuildGasTransferTx(ap.SystemProvider.SystemPrivateKey, chain.TransactionData{ nonce, err := ap.Noncestore.Acquire(ctx, ap.SystemPublicKey)
if err != nil {
return nil, err
}
builtTx, err := ap.ChainProvider.BuildGasTransferTx(ap.SystemPrivateKey, chain.TransactionData{
To: w3.A(giftTo), To: w3.A(giftTo),
Nonce: ap.SystemProvider.SystemNoncestore.Acquire(), Nonce: nonce,
}, big.NewInt(topupGiftGasValue)) }, big.NewInt(topupGiftGasValue))
if err != nil { if err != nil {
return &types.Transaction{}, err return &types.Transaction{}, err

View File

@ -10,10 +10,3 @@ type Noncestore interface {
SyncNetworkNonce(context.Context, string) (uint64, error) SyncNetworkNonce(context.Context, string) (uint64, error)
SetNewAccountNonce(context.Context, string) error SetNewAccountNonce(context.Context, string) error
} }
// SystemNoncestore represents a standalone noncestore for a single system account
type SystemNoncestore interface {
Peek() uint64
Acquire() uint64
Return()
}

View File

@ -13,7 +13,8 @@ import (
) )
const ( const (
mutexLockTTL = 200 * time.Millisecond mutexLockTTL = 200 * time.Millisecond
mutexKeyPrefix = "lock_"
) )
// Opts represents the Redis nonce store specific params // Opts represents the Redis nonce store specific params
@ -52,7 +53,7 @@ func NewRedisNoncestore(o Opts) (noncestore.Noncestore, error) {
} }
func (ns *RedisNoncestore) Peek(ctx context.Context, publicKey string) (uint64, error) { func (ns *RedisNoncestore) Peek(ctx context.Context, publicKey string) (uint64, error) {
lock, err := ns.redisLockProvider.Obtain(ctx, publicKey, mutexLockTTL, nil) lock, err := ns.redisLockProvider.Obtain(ctx, mutexKeyPrefix+publicKey, mutexLockTTL, nil)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -71,7 +72,7 @@ func (ns *RedisNoncestore) Acquire(ctx context.Context, publicKey string) (uint6
nonce uint64 nonce uint64
) )
lock, err := ns.redisLockProvider.Obtain(ctx, publicKey, mutexLockTTL, nil) lock, err := ns.redisLockProvider.Obtain(ctx, mutexKeyPrefix+publicKey, mutexLockTTL, nil)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -98,7 +99,7 @@ func (ns *RedisNoncestore) Acquire(ctx context.Context, publicKey string) (uint6
} }
func (ns *RedisNoncestore) Return(ctx context.Context, publicKey string) (uint64, error) { func (ns *RedisNoncestore) Return(ctx context.Context, publicKey string) (uint64, error) {
lock, err := ns.redisLockProvider.Obtain(ctx, publicKey, mutexLockTTL, nil) lock, err := ns.redisLockProvider.Obtain(ctx, mutexKeyPrefix+publicKey, mutexLockTTL, nil)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -124,7 +125,7 @@ func (ns *RedisNoncestore) SyncNetworkNonce(ctx context.Context, publicKey strin
networkNonce uint64 networkNonce uint64
) )
lock, err := ns.redisLockProvider.Obtain(ctx, publicKey, mutexLockTTL, nil) lock, err := ns.redisLockProvider.Obtain(ctx, mutexKeyPrefix+publicKey, mutexLockTTL, nil)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -147,7 +148,7 @@ func (ns *RedisNoncestore) SyncNetworkNonce(ctx context.Context, publicKey strin
} }
func (ns *RedisNoncestore) SetNewAccountNonce(ctx context.Context, publicKey string) error { func (ns *RedisNoncestore) SetNewAccountNonce(ctx context.Context, publicKey string) error {
lock, err := ns.redisLockProvider.Obtain(ctx, publicKey, mutexLockTTL, nil) lock, err := ns.redisLockProvider.Obtain(ctx, mutexKeyPrefix+publicKey, mutexLockTTL, nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,65 +0,0 @@
package system
import (
"context"
"sync"
"github.com/grassrootseconomics/cic-custodial/internal/noncestore"
"github.com/grassrootseconomics/cic-go-sdk/chain"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
)
type Opts struct {
ChainProvider *chain.Provider
AccountAddress string
}
type SystemNoncestore struct {
mx sync.Mutex
nonceValue uint64
}
func NewSystemNoncestore(o Opts) (noncestore.SystemNoncestore, error) {
var (
networkNonce uint64
)
err := o.ChainProvider.EthClient.CallCtx(
context.Background(),
eth.Nonce(w3.A(o.AccountAddress), nil).Returns(&networkNonce),
)
if err != nil {
return nil, err
}
return &SystemNoncestore{
nonceValue: networkNonce,
}, nil
}
func (ns *SystemNoncestore) Peek() uint64 {
ns.mx.Lock()
defer ns.mx.Unlock()
return ns.nonceValue
}
func (ns *SystemNoncestore) Acquire() uint64 {
ns.mx.Lock()
defer ns.mx.Unlock()
nextNonce := ns.nonceValue
ns.nonceValue++
return nextNonce
}
func (ns *SystemNoncestore) Return() {
ns.mx.Lock()
defer ns.mx.Unlock()
if ns.nonceValue > 0 {
ns.nonceValue--
}
}

View File

@ -1,43 +0,0 @@
package system
import (
"crypto/ecdsa"
eth_crypto "github.com/ethereum/go-ethereum/crypto"
"github.com/grassrootseconomics/cic-custodial/internal/noncestore"
system_noncestore "github.com/grassrootseconomics/cic-custodial/internal/noncestore/providers/system"
"github.com/grassrootseconomics/cic-go-sdk/chain"
)
type Opts struct {
SystemPublicKey string
SystemPrivateKey string
ChainProvider *chain.Provider
}
type SystemProvider struct {
SystemNoncestore noncestore.SystemNoncestore
SystemPublicKey string
SystemPrivateKey *ecdsa.PrivateKey
}
func NewSystemProvider(o Opts) (*SystemProvider, error) {
loadedPrivateKey, err := eth_crypto.HexToECDSA(o.SystemPrivateKey)
if err != nil {
return nil, err
}
systemNoncestore, err := system_noncestore.NewSystemNoncestore(system_noncestore.Opts{
ChainProvider: o.ChainProvider,
AccountAddress: o.SystemPublicKey,
})
if err != nil {
return nil, err
}
return &SystemProvider{
SystemNoncestore: systemNoncestore,
SystemPublicKey: o.SystemPublicKey,
SystemPrivateKey: loadedPrivateKey,
}, nil
}