add more unit tests + errors clean up (#114)

This commit is contained in:
noot
2022-05-15 23:36:03 -04:00
committed by GitHub
parent 2b631d2d36
commit 55ae59046c
55 changed files with 470 additions and 246 deletions

View File

@@ -2,7 +2,7 @@ package monero
import (
"github.com/noot/atomic-swap/common"
"github.com/noot/atomic-swap/common/rpcclient"
"github.com/noot/atomic-swap/common/rpctypes"
mcrypto "github.com/noot/atomic-swap/crypto/monero"
)
@@ -74,7 +74,7 @@ func (c *client) Refresh() error {
func (c *client) refresh() error {
const method = "refresh"
resp, err := rpcclient.PostRPC(c.endpoint, method, "{}")
resp, err := rpctypes.PostRPC(c.endpoint, method, "{}")
if err != nil {
return err
}
@@ -97,7 +97,7 @@ func (c *client) OpenWallet(filename, password string) error {
func (c *client) CloseWallet() error {
const method = "close_wallet"
resp, err := rpcclient.PostRPC(c.endpoint, method, "{}")
resp, err := rpctypes.PostRPC(c.endpoint, method, "{}")
if err != nil {
return err
}

View File

@@ -3,7 +3,7 @@ package monero
import (
"encoding/json"
"github.com/noot/atomic-swap/common/rpcclient"
"github.com/noot/atomic-swap/common/rpctypes"
)
// DaemonClient represents a monerod client.
@@ -40,7 +40,7 @@ func (c *client) callGenerateBlocks(address string, amount uint) error {
return err
}
resp, err := rpcclient.PostRPC(c.endpoint, method, string(params))
resp, err := rpctypes.PostRPC(c.endpoint, method, string(params))
if err != nil {
return err
}

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/noot/atomic-swap/common/rpcclient"
"github.com/noot/atomic-swap/common/rpctypes"
mcrypto "github.com/noot/atomic-swap/crypto/monero"
)
@@ -46,7 +46,7 @@ func (c *client) callGenerateFromKeys(sk *mcrypto.PrivateSpendKey, vk *mcrypto.P
return err
}
resp, err := rpcclient.PostRPC(c.endpoint, method, string(params))
resp, err := rpctypes.PostRPC(c.endpoint, method, string(params))
if err != nil {
return err
}
@@ -101,7 +101,7 @@ func (c *client) callSweepAll(to string, accountIdx uint) (*SweepAllResponse, er
return nil, err
}
resp, err := rpcclient.PostRPC(c.endpoint, method, string(params))
resp, err := rpctypes.PostRPC(c.endpoint, method, string(params))
if err != nil {
return nil, err
}
@@ -152,7 +152,7 @@ func (c *client) callTransfer(destinations []Destination, accountIdx uint) (*Tra
return nil, err
}
resp, err := rpcclient.PostRPC(c.endpoint, method, string(params))
resp, err := rpctypes.PostRPC(c.endpoint, method, string(params))
if err != nil {
return nil, err
}
@@ -193,7 +193,7 @@ func (c *client) callGetBalance(idx uint) (*GetBalanceResponse, error) {
return nil, err
}
resp, err := rpcclient.PostRPC(c.endpoint, method, string(params))
resp, err := rpctypes.PostRPC(c.endpoint, method, string(params))
if err != nil {
return nil, err
}
@@ -230,7 +230,7 @@ func (c *client) callGetAddress(idx uint) (*getAddressResponse, error) {
return nil, err
}
resp, err := rpcclient.PostRPC(c.endpoint, method, string(params))
resp, err := rpctypes.PostRPC(c.endpoint, method, string(params))
if err != nil {
return nil, err
}
@@ -254,7 +254,7 @@ type getAccountsResponse struct {
func (c *client) callGetAccounts() (*getAccountsResponse, error) {
const method = "get_accounts"
resp, err := rpcclient.PostRPC(c.endpoint, method, "{}")
resp, err := rpctypes.PostRPC(c.endpoint, method, "{}")
if err != nil {
return nil, err
}
@@ -289,7 +289,7 @@ func (c *client) callOpenWallet(filename, password string) error {
return err
}
resp, err := rpcclient.PostRPC(c.endpoint, method, string(params))
resp, err := rpctypes.PostRPC(c.endpoint, method, string(params))
if err != nil {
return err
}
@@ -321,7 +321,7 @@ func (c *client) callCreateWallet(filename, password string) error {
return err
}
resp, err := rpcclient.PostRPC(c.endpoint, method, string(params))
resp, err := rpctypes.PostRPC(c.endpoint, method, string(params))
if err != nil {
return err
}
@@ -340,7 +340,7 @@ type getHeightResponse struct {
func (c *client) callGetHeight() (uint, error) {
const method = "get_height"
resp, err := rpcclient.PostRPC(c.endpoint, method, "{}")
resp, err := rpctypes.PostRPC(c.endpoint, method, "{}")
if err != nil {
return 0, err
}

View File

@@ -19,32 +19,35 @@ var (
log = logging.Logger("monero")
)
// WaitForBlocks waits for a new block to arrive.
func WaitForBlocks(client Client) (uint, error) {
// WaitForBlocks waits for `count` new blocks to arrive.
// It returns the height of the chain.
func WaitForBlocks(client Client, count int) (uint, error) {
prevHeight, err := client.GetHeight()
if err != nil {
return 0, fmt.Errorf("failed to get height: %w", err)
}
for i := 0; i < maxRetries; i++ {
if err := client.Refresh(); err != nil {
return 0, err
}
for j := 0; j < count; j++ {
for i := 0; i < maxRetries; i++ {
if err := client.Refresh(); err != nil {
return 0, err
}
height, err := client.GetHeight()
if err != nil {
continue
}
height, err := client.GetHeight()
if err != nil {
continue
}
if height > prevHeight {
return height, nil
}
if height > prevHeight {
return height, nil
}
log.Infof("waiting for next block, current height=%d", height)
time.Sleep(blockSleepDuration)
log.Infof("waiting for next block, current height=%d", height)
time.Sleep(blockSleepDuration)
}
}
return 0, fmt.Errorf("timed out waiting for next block")
return 0, fmt.Errorf("timed out waiting for blocks")
}
// CreateMoneroWallet creates a monero wallet from a private keypair.

View File

@@ -20,7 +20,7 @@ func TestWaitForBlocks(t *testing.T) {
_ = daemon.callGenerateBlocks(addr.Address, 181)
}()
_, err = WaitForBlocks(c)
_, err = WaitForBlocks(c, 1)
require.NoError(t, err)
}