Skip to content

Commit

Permalink
Merge pull request #343 from rsksmart/QA-Test
Browse files Browse the repository at this point in the history
Qa test
  • Loading branch information
MaximStanciu8 authored Nov 16, 2023
2 parents 4ab545b + 1d0c13c commit 7006439
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 21 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Config struct {
CaptchaSecretKey string `env:"CAPTCHA_SECRET_KEY"`
CaptchaSiteKey string `env:"CAPTCHA_SITE_KEY"`
CaptchaThreshold float32 `env:"CAPTCHA_THRESHOLD"`
BtcWalletPassword string `env:"BTC_WALLET_PASSWORD"`
IsBtcEncryptedWallet bool `env:"BTC_ENCRYPTED_WALLET"`

Server struct {
Port uint `env:"SERVER_PORT"`
Expand Down
50 changes: 43 additions & 7 deletions connectors/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (
)

const (
unknownBtcdVersion = -1
BTC_TO_SATOSHI = 100000000
unknownBtcdVersion = -1
BTC_TO_SATOSHI = 100000000
WalletUnlockSeconds = 30
)

type BtcConfig struct {
Expand Down Expand Up @@ -95,16 +96,20 @@ type BTCClient interface {
inputs []btcjson.PsbtInput, outputs []btcjson.PsbtOutput, locktime *uint32,
options *btcjson.WalletCreateFundedPsbtOpts, bip32Derivs *bool,
) (*btcjson.WalletCreateFundedPsbtResult, error)
GetWalletInfo() (*btcjson.GetWalletInfoResult, error)
WalletPassphrase(passphrase string, timeoutSecs int64) error
}

type BTC struct {
c BTCClient
params chaincfg.Params
TxDefaultFee int64
TxFeeRate float64
c BTCClient
params chaincfg.Params
TxDefaultFee int64
TxFeeRate float64
walletPassword string
encryptedWallet bool
}

func NewBTC(network string) (*BTC, error) {
func NewBTC(network, walletPassword string, encryptedWallet bool) (*BTC, error) {
log.Debug("initializing BTC connector")
btc := BTC{}
switch network {
Expand All @@ -117,6 +122,8 @@ func NewBTC(network string) (*BTC, error) {
default:
return nil, fmt.Errorf("invalid network name: %v", network)
}
btc.walletPassword = walletPassword
btc.encryptedWallet = encryptedWallet
return &btc, nil
}

Expand Down Expand Up @@ -147,6 +154,12 @@ func (btc *BTC) Connect(btcConfig BtcConfig) error {

btc.c = c
btc.TxFeeRate = btcConfig.TxFeeRate
if btc.encryptedWallet {
if err = btc.unlockWallet(); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -523,6 +536,11 @@ func (btc *BTC) SendBtc(address string, amount uint64) (string, error) {
return "", fmt.Errorf("RPC client error: %v", err)
}

if btc.encryptedWallet {
if err = btc.unlockWallet(); err != nil {
return "", err
}
}
hash, err := btc.c.SendToAddress(btcAdd, btcutil.Amount(amount))

if err != nil {
Expand Down Expand Up @@ -1071,6 +1089,12 @@ func (btc *BTC) SendBtcWithOpReturn(address string, amountInSatoshi uint64, opRe
FeeRate: &feeRate,
}

if btc.encryptedWallet {
if err = btc.unlockWallet(); err != nil {
return "", err
}
}

fundedTx, err := btc.c.FundRawTransaction(rawTx, opts, nil)
if err != nil {
return "", err
Expand Down Expand Up @@ -1110,3 +1134,15 @@ func (btc *BTC) EstimateFees(address string, amountInSatoshi uint64) (uint64, er
}
return uint64(simulatedTx.Fee * BTC_TO_SATOSHI), nil
}

func (btc *BTC) unlockWallet() error {
info, err := btc.c.GetWalletInfo()
if err != nil {
return err
}
if info.UnlockedUntil != nil && time.Until(time.Unix(int64(*info.UnlockedUntil), 0)) > 0 {
log.Debug("Wallet already unlocked")
return nil
}
return btc.c.WalletPassphrase(btc.walletPassword, WalletUnlockSeconds)
}
2 changes: 2 additions & 0 deletions docker-compose/.env.regtest
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ PWD_FILE=pwd.txt
CHAIN_ID=33
KEY_SECRET=FlyoverTestEnv/LPS-LOCAL-KEY
PASSWORD_SECRET=FlyoverTestEnv/LPS-LOCAL-PASSWORD
BTC_WALLET_PASSWORD=FlyoverTestEnv/LPS-LOCAL-BTC-WALLET-PASSWORD
BTC_ENCRYPTED_WALLET=true

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
Expand Down
2 changes: 2 additions & 0 deletions docker-compose/local/.env.regtest
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ PWD_FILE=pwd.txt
CHAIN_ID=33
KEY_SECRET=FlyoverTestEnv/LPS-LOCAL-KEY
PASSWORD_SECRET=FlyoverTestEnv/LPS-LOCAL-PASSWORD
BTC_WALLET_PASSWORD=FlyoverTestEnv/LPS-LOCAL-BTC-WALLET-PASSWORD
BTC_ENCRYPTED_WALLET=true

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
Expand Down
2 changes: 1 addition & 1 deletion docker-compose/local/lps-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ done
curl -s "http://127.0.0.1:5555" --user "$BTCD_RPC_USER:$BTCD_RPC_PASS" -H "Content-Type: application/json" -d '{"jsonrpc": "1.0", "method": "getwalletinfo", "params": [], "id":"getwallet"}' | grep "{\"result\":null,\"error\":{\"code\":-18" \
&& echo "No default wallet" \
&& echo "Creating wallet" \
&& curl -s "http://127.0.0.1:5555" --user "$BTCD_RPC_USER:$BTCD_RPC_PASS" -H "Content-Type: application/json" -d '{"jsonrpc": "1.0", "method": "createwallet", "params": ["main", false, false, "", true, false, true], "id":"createwallet"}' \
&& curl -s "http://127.0.0.1:5555" --user "$BTCD_RPC_USER:$BTCD_RPC_PASS" -H "Content-Type: application/json" -d '{"jsonrpc": "1.0", "method": "createwallet", "params": ["main", false, false, "test-password", true, false, true], "id":"createwallet"}' \
&& curl -s "http://127.0.0.1:5555" --user "$BTCD_RPC_USER:$BTCD_RPC_PASS" -H "Content-Type: application/json" -d '{"jsonrpc": "1.0", "method": "getnewaddress", "params": ["main"], "id":"getnewaddress"}' \
| jq .result | xargs -I ADDRESS curl -s "http://127.0.0.1:5555" --user "$BTCD_RPC_USER:$BTCD_RPC_PASS" -H "Content-Type: application/json" -d '{"jsonrpc": "1.0", "method": "generatetoaddress", "params": [500, "ADDRESS"], "id":"generatetoaddress"}' \
&& echo "Wallet created and generated 500 blocks"
Expand Down
2 changes: 1 addition & 1 deletion docker-compose/lps/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ echo "Testing if we have a default wallet"
curl -s "http://bitcoind01:5555" --user "$BTCD_RPC_USER:$BTCD_RPC_PASS" -H "Content-Type: application/json" -d '{"jsonrpc": "1.0", "method": "getwalletinfo", "params": [], "id":"getwallet"}' | grep "{\"result\":null,\"error\":{\"code\":-18" \
&& echo "No default wallet" \
&& echo "Creating wallet" \
&& curl -s "http://bitcoind01:5555" --user "$BTCD_RPC_USER:$BTCD_RPC_PASS" -H "Content-Type: application/json" -d '{"jsonrpc": "1.0", "method": "createwallet", "params": ["main", false, false, "", true, false, true], "id":"createwallet"}'
&& curl -s "http://bitcoind01:5555" --user "$BTCD_RPC_USER:$BTCD_RPC_PASS" -H "Content-Type: application/json" -d '{"jsonrpc": "1.0", "method": "createwallet", "params": ["main", false, false, "test-password", true, false, true], "id":"createwallet"}'

echo "Starting LP Server..."
liquidity-provider-server
14 changes: 13 additions & 1 deletion integration/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ func setup(readyChannel chan<- error, doneChannel chan os.Signal, logHooks ...lo
log.Info("starting liquidity provider server")
log.Debugf("loaded config %+v", cfg)

awsConfiguration, err := awsConfig.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatal("error loading configuration: ", err.Error())
}

secretsStorage := secrets.NewSecretsManagerStorage[any](awsConfiguration)

dbMongo, err := mongoDB.Connect()
if err != nil {
readyChannel <- fmt.Errorf("error connecting to DB: %v", err)
Expand All @@ -149,7 +156,12 @@ func setup(readyChannel chan<- error, doneChannel chan os.Signal, logHooks ...lo
return
}

btc, err := connectors.NewBTC(os.Getenv("BTC_NETWORK"))
walletPassword, err := secretsStorage.GetTextSecret(cfg.BtcWalletPassword)
if err != nil {
log.Fatal("Error getting BTC wallet password: ", err)
}

btc, err := connectors.NewBTC(os.Getenv("BTC_NETWORK"), walletPassword, cfg.IsBtcEncryptedWallet)
if err != nil {
readyChannel <- fmt.Errorf("error initializing BTC connector: %v", err)
return
Expand Down
29 changes: 19 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/rsksmart/liquidity-provider-server/config"
"github.com/rsksmart/liquidity-provider-server/pegout"
"math/big"
Expand Down Expand Up @@ -64,15 +65,11 @@ func initLogger() {
}
}

func startServer(rsk *connectors.RSK, btc *connectors.BTC, dbMongo *mongoDB.DB, endChannel chan<- os.Signal) {
func startServer(
rsk *connectors.RSK, btc *connectors.BTC,
dbMongo *mongoDB.DB, awsConfiguration aws.Config,
secretsStorage secrets.SecretStorage[any], endChannel chan<- os.Signal) {
lpRepository := storage.NewLPRepository(dbMongo, rsk, btc)

awsConfiguration, err := awsConfig.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatal("error loading configuration: ", err.Error())
}

secretsStorage := secrets.NewSecretsManagerStorage[any](awsConfiguration)
secretNames := &account.AccountSecretNames{
KeySecretName: cfg.ProviderCredentials.KeySecret,
PasswordSecretName: cfg.ProviderCredentials.PasswordSecret,
Expand Down Expand Up @@ -133,6 +130,13 @@ func main() {
log.Info("starting liquidity provider server")
log.Debugf("loaded config %+v", cfg)

awsConfiguration, err := awsConfig.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatal("error loading configuration: ", err.Error())
}

secretsStorage := secrets.NewSecretsManagerStorage[any](awsConfiguration)

dbMongo, err := mongoDB.Connect()
if err != nil {
log.Fatal("error connecting to DB: ", err)
Expand All @@ -158,7 +162,12 @@ func main() {
log.Fatal("error connecting to RSK: ", err)
}

btc, err := connectors.NewBTC(os.Getenv("BTC_NETWORK"))
walletPassword, err := secretsStorage.GetTextSecret(cfg.BtcWalletPassword)
if err != nil {
log.Fatal("Error getting BTC wallet password: ", err)
}

btc, err := connectors.NewBTC(os.Getenv("BTC_NETWORK"), walletPassword, cfg.IsBtcEncryptedWallet)
if err != nil {
log.Fatal("error initializing BTC connector: ", err)
}
Expand All @@ -171,7 +180,7 @@ func main() {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

startServer(rsk, btc, dbMongo, done)
startServer(rsk, btc, dbMongo, awsConfiguration, secretsStorage, done)

<-done

Expand Down
2 changes: 1 addition & 1 deletion mongo/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ func (db *DB) GetRetainedPegOutQuote(hash string) (*pegout.RetainedQuote, error)
log.Debug("getting retained quote: ", hash)

coll := db.db.Collection(retainedPegoutQuoteCollection)
filter := bson.D{primitive.E{Key: "quoteHash", Value: hash}}
filter := bson.D{primitive.E{Key: "quotehash", Value: hash}}

var result *pegout.RetainedQuote
err := coll.FindOne(context.TODO(), filter).Decode(&result)
Expand Down
2 changes: 2 additions & 0 deletions sample-config.env
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ PWD_FILE=pwd.txt
CHAIN_ID=33
KEY_SECRET=FlyoverTestEnv/LPS-LOCAL-KEY
PASSWORD_SECRET=FlyoverTestEnv/LPS-LOCAL-PASSWORD
BTC_WALLET_PASSWORD=FlyoverTestEnv/LPS-LOCAL-BTC-WALLET-PASSWORD
BTC_ENCRYPTED_WALLET=true

# From previous .env
LIQUIDITY_PROVIDER_RSK_ADDR=0x9D93929A9099be4355fC2389FbF253982F9dF47c
Expand Down

0 comments on commit 7006439

Please sign in to comment.