Skip to content

Commit

Permalink
feat(wallet)_: store route execution data to db
Browse files Browse the repository at this point in the history
  • Loading branch information
dlipicar committed Nov 7, 2024
1 parent f15c64c commit ddfcc79
Show file tree
Hide file tree
Showing 14 changed files with 994 additions and 58 deletions.
4 changes: 4 additions & 0 deletions services/wallet/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func ZeroBigIntValue() *big.Int {
return big.NewInt(0)
}

func ZeroHash() ethCommon.Hash {
return ethCommon.Hash{}
}

func (c ChainID) String() string {
return strconv.FormatUint(uint64(c), 10)
}
Expand Down
47 changes: 41 additions & 6 deletions services/wallet/routeexecution/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package routeexecution

import (
"context"
"database/sql"
"time"

"go.uber.org/zap"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"

"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/logutils"

status_common "github.com/status-im/status-go/common"
statusErrors "github.com/status-im/status-go/errors"
Expand All @@ -23,16 +28,26 @@ type Manager struct {
router *router.Router
transactionManager *transfer.TransactionManager
transferController *transfer.Controller
db *DB

// Local data used for storage purposes
buildInputParams *requests.RouterBuildTransactionsParams
}

func NewManager(router *router.Router, transactionManager *transfer.TransactionManager, transferController *transfer.Controller) *Manager {
func NewManager(walletDB *sql.DB, router *router.Router, transactionManager *transfer.TransactionManager, transferController *transfer.Controller) *Manager {
return &Manager{
router: router,
transactionManager: transactionManager,
transferController: transferController,
db: NewDB(walletDB),
}
}

func (m *Manager) clearLocalRouteData() {
m.buildInputParams = nil
m.transactionManager.ClearLocalRouterTransactionsData()
}

func (m *Manager) BuildTransactionsFromRoute(ctx context.Context, buildInputParams *requests.RouterBuildTransactionsParams) {
go func() {
defer status_common.LogOnPanic()
Expand All @@ -48,7 +63,7 @@ func (m *Manager) BuildTransactionsFromRoute(ctx context.Context, buildInputPara

defer func() {
if err != nil {
m.transactionManager.ClearLocalRouterTransactionsData()
m.clearLocalRouteData()

Check warning on line 66 in services/wallet/routeexecution/manager.go

View check run for this annotation

Codecov / codecov/patch

services/wallet/routeexecution/manager.go#L66

Added line #L66 was not covered by tests
err = statusErrors.CreateErrorResponseFromError(err)
response.SendDetails.ErrorResponse = err.(*statusErrors.ErrorResponse)
}
Expand All @@ -62,6 +77,8 @@ func (m *Manager) BuildTransactionsFromRoute(ctx context.Context, buildInputPara
return
}

m.buildInputParams = buildInputParams

updateFields(response.SendDetails, routeInputParams)

// notify client that sending transactions started (has 3 steps, building txs, signing txs, sending txs)
Expand Down Expand Up @@ -108,7 +125,7 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
}

if clearLocalData {
m.transactionManager.ClearLocalRouterTransactionsData()
m.clearLocalRouteData()
}

if err != nil {
Expand Down Expand Up @@ -163,6 +180,20 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
//////////////////////////////////////////////////////////////////////////////

response.SentTransactions, err = m.transactionManager.SendRouterTransactions(ctx, multiTx)
if err != nil {
log.Error("Error sending router transactions", "error", err)

Check warning on line 184 in services/wallet/routeexecution/manager.go

View check run for this annotation

Codecov / codecov/patch

services/wallet/routeexecution/manager.go#L184

Added line #L184 was not covered by tests
// TODO #16556: Handle partially successful Tx sends?
// Don't return, store whichever transactions were successfully sent
}

// don't overwrite err since we want to process it in the deferred function
var tmpErr error
routerTransactions := m.transactionManager.GetRouterTransactions()
routeData := NewRouteData(&routeInputParams, m.buildInputParams, routerTransactions)
tmpErr = m.db.PutRouteData(routeData)
if tmpErr != nil {
log.Error("Error storing route data", "error", tmpErr)

Check warning on line 195 in services/wallet/routeexecution/manager.go

View check run for this annotation

Codecov / codecov/patch

services/wallet/routeexecution/manager.go#L195

Added line #L195 was not covered by tests
}

var (
chainIDs []uint64
Expand All @@ -173,13 +204,17 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
addresses = append(addresses, common.Address(tx.FromAddress))
go func(chainId uint64, txHash common.Hash) {
defer status_common.LogOnPanic()
err = m.transactionManager.WatchTransaction(context.Background(), chainId, txHash)
if err != nil {
tmpErr = m.transactionManager.WatchTransaction(context.Background(), chainId, txHash)
if tmpErr != nil {
logutils.ZapLogger().Error("Error watching transaction", zap.Error(tmpErr))

Check warning on line 209 in services/wallet/routeexecution/manager.go

View check run for this annotation

Codecov / codecov/patch

services/wallet/routeexecution/manager.go#L209

Added line #L209 was not covered by tests
return
}
}(tx.FromChain, common.Hash(tx.Hash))
}
err = m.transferController.CheckRecentHistory(chainIDs, addresses)
tmpErr = m.transferController.CheckRecentHistory(chainIDs, addresses)
if tmpErr != nil {
logutils.ZapLogger().Error("Error checking recent history", zap.Error(tmpErr))

Check warning on line 216 in services/wallet/routeexecution/manager.go

View check run for this annotation

Codecov / codecov/patch

services/wallet/routeexecution/manager.go#L216

Added line #L216 was not covered by tests
}
}()
}

Expand Down
Loading

0 comments on commit ddfcc79

Please sign in to comment.