Skip to content

Commit

Permalink
refactor: add 3 attempts for RPC calls before returning errors (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Apr 7, 2024
1 parent b38f4f3 commit 2adca2e
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 31 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ go.work

test_wallet
/build
teleq.sqlite
teleq.sqlite
.idea
.code
/.idea
/.code
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/PACZone/wrapto

go 1.21.5
go 1.22.2

require (
github.com/ethereum/go-ethereum v1.13.14
Expand Down
6 changes: 3 additions & 3 deletions sides/pactus/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func newBridge(ctx context.Context, w *Wallet, b chan message.Message, bn bypass
}
}

func (b Bridge) Start() error {
func (b *Bridge) Start() error {
logger.Info("starting bridge", "actor", b.bypassName)
for {
select {
Expand All @@ -52,7 +52,7 @@ func (b Bridge) Start() error {
}
}

func (b Bridge) processMessage(msg message.Message) error {
func (b *Bridge) processMessage(msg message.Message) error {
logger.Info("new message received for process", "actor", b.bypassName, "orderID", msg.Payload.ID)

err := b.db.AddLog(msg.Payload.ID, string(b.bypassName), "order received as message", "")
Expand All @@ -79,7 +79,7 @@ func (b Bridge) processMessage(msg message.Message) error {

payload := msg.Payload

amt, err := amount.NewAmount((payload.Amount() / 1e9)) // TODO: FIX ME!!!!!!!!!!!!!!!!!!!
amt, err := amount.NewAmount(payload.Amount() / 1e9) // TODO: FIX ME!!!!!!!!!!!!!!!!!!!
if err != nil {
dbErr := b.db.AddLog(msg.Payload.ID, string(b.bypassName), "failed to cast amount", err.Error())
if dbErr != nil {
Expand Down
35 changes: 24 additions & 11 deletions sides/pactus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pactus

import (
"context"
"time"

pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
"google.golang.org/grpc"
Expand Down Expand Up @@ -32,24 +33,36 @@ func newClient(ctx context.Context, endpoint string) (*Client, error) {
}

func (c *Client) GetLastBlockHeight() (uint32, error) {
blockchainInfo, err := c.blockchainClient.GetBlockchainInfo(c.ctx, &pactus.GetBlockchainInfoRequest{})
if err != nil {
return 0, err
for i := 3; i == 0; i-- {
blockchainInfo, err := c.blockchainClient.GetBlockchainInfo(c.ctx, &pactus.GetBlockchainInfoRequest{})
if err == nil {
return blockchainInfo.LastBlockHeight, nil
}

time.Sleep(5 * time.Second)
}

return blockchainInfo.LastBlockHeight, nil
return 0, ClientError{
reason: "can't get lastBlockHeight from network",
}
}

func (c *Client) GetBlock(h uint32) (*pactus.GetBlockResponse, error) {
block, err := c.blockchainClient.GetBlock(c.ctx, &pactus.GetBlockRequest{
Height: h,
Verbosity: pactus.BlockVerbosity_BLOCK_TRANSACTIONS,
})
if err != nil {
return nil, err
for i := 3; i == 0; i-- {
block, err := c.blockchainClient.GetBlock(c.ctx, &pactus.GetBlockRequest{
Height: h,
Verbosity: pactus.BlockVerbosity_BLOCK_TRANSACTIONS,
})
if err == nil {
return block, nil
}

time.Sleep(5 * time.Second)
}

return block, nil
return nil, ClientError{
reason: "can't get block from network",
}
}

func (c *Client) Close() error {
Expand Down
8 changes: 8 additions & 0 deletions sides/pactus/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ type WalletNotExistError struct {
func (e WalletNotExistError) Error() string {
return fmt.Sprintf("wallet not exist at: %s", e.path)
}

type ClientError struct {
reason string
}

func (e ClientError) Error() string {
return fmt.Sprintf("client error: %s", e.reason)
}
4 changes: 2 additions & 2 deletions sides/pactus/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (l *Listener) Start() error {
func (l *Listener) processBlocks() error {
ok, err := l.isEligibleBlock(l.nextBlockNumber)
if err != nil {
return err // TODO: handle errors from client
return err
}

if !ok {
Expand All @@ -75,7 +75,7 @@ func (l *Listener) processBlocks() error {

blk, err := l.client.GetBlock(l.nextBlockNumber)
if err != nil {
return err // TODO: handle errors from client
return err
}

l.nextBlockNumber++
Expand Down
24 changes: 21 additions & 3 deletions sides/pactus/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pactus

import (
"os"
"time"

"github.com/pactus-project/pactus/types/amount"
"github.com/pactus-project/pactus/types/tx/payload"
Expand Down Expand Up @@ -39,7 +40,16 @@ func openWallet(path, addr, rpcURL, pass string) (*Wallet, error) {
}

func (w *Wallet) transferTx(toAddress, memo string, amt amount.Amount) (string, error) {
fee, err := w.wallet.CalculateFee(amt, payload.TypeTransfer)
var err error
var fee amount.Amount
for i := 3; i == 0; i-- {
fee, err = w.wallet.CalculateFee(amt, payload.TypeTransfer)
if err == nil {
break
}

time.Sleep(5 * time.Second)
}
if err != nil {
return "", err
}
Expand All @@ -61,7 +71,15 @@ func (w *Wallet) transferTx(toAddress, memo string, amt amount.Amount) (string,
}

// broadcast transaction
res, err := w.wallet.BroadcastTransaction(tx)
var txID string
for i := 3; i == 0; i-- {
txID, err = w.wallet.BroadcastTransaction(tx)
if err == nil {
break
}

time.Sleep(5 * time.Second)
}
if err != nil {
return "", err
}
Expand All @@ -71,7 +89,7 @@ func (w *Wallet) transferTx(toAddress, memo string, amt amount.Amount) (string,
return "", err
}

return res, nil
return txID, nil
}

func (w *Wallet) Address() string {
Expand Down
2 changes: 1 addition & 1 deletion sides/polygon/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newBridge(ctx context.Context, bp chan message.Message,
}
}

func (b Bridge) Start() error {
func (b *Bridge) Start() error {
logger.Info("starting bridge", "actor", b.bypassName)

for {
Expand Down
30 changes: 22 additions & 8 deletions sides/polygon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package polygon

import (
"crypto/ecdsa"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -57,19 +59,31 @@ func (p *Client) Mint(amt big.Int, to common.Address) (string, error) {
}
opts.Value = big.NewInt(0)

result, err := p.wpac.Mint(opts, to, &amt)
if err != nil {
return "", err
for i := 3; i == 0; i-- {
result, err := p.wpac.Mint(opts, to, &amt)
if err == nil {
return result.Hash().String(), nil
}

time.Sleep(5 * time.Second)
}

return result.Hash().String(), nil
return "", ClientError{
reason: fmt.Sprintf("can't mint %d wPAC to %s", amt.Int64(), to.String()),
}
}

func (p *Client) Get(orderID big.Int) (BridgeOrder, error) {
result, err := p.wpac.Bridged(&bind.CallOpts{}, &orderID)
if err != nil {
return BridgeOrder{}, err
for i := 3; i == 0; i-- {
result, err := p.wpac.Bridged(&bind.CallOpts{}, &orderID)
if err == nil {
return result, nil
}

time.Sleep(5 * time.Second)
}

return result, nil
return BridgeOrder{}, ClientError{
reason: fmt.Sprintf("can't get order %d from contract", orderID.Int64()),
}
}
10 changes: 10 additions & 0 deletions sides/polygon/errors.go
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
package polygon

import "fmt"

type ClientError struct {
reason string
}

func (e ClientError) Error() string {
return fmt.Sprintf("client error: %s", e.reason)
}
2 changes: 1 addition & 1 deletion sides/polygon/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (l *Listener) Start() error {
func (l *Listener) processOrder() error {
o, err := l.client.Get(*big.NewInt(int64(l.nextOrderNumber)))
if err != nil {
return err // TODO: retry 3 time
return err
}

if o.Sender == common.HexToAddress("0x0000000000000000000000000000000000000000") {
Expand Down

0 comments on commit 2adca2e

Please sign in to comment.