From eab9282dd5d7bdb2ec4878d9003e611718904342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lewandowski?= <35259896+pawellewandowski98@users.noreply.github.com> Date: Thu, 18 Jan 2024 13:26:00 +0100 Subject: [PATCH 01/10] feat(BUX-498): remove block header model (#541) --- action_destination.go | 52 ++++++++++++++++--------------------------- errors.go | 2 +- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/action_destination.go b/action_destination.go index 4c656dda..7b102328 100644 --- a/action_destination.go +++ b/action_destination.go @@ -2,6 +2,7 @@ package bux import ( "context" + "github.com/mrz1836/go-datastore" ) @@ -9,8 +10,8 @@ import ( // // xPubKey is the raw public xPub func (c *Client) NewDestination(ctx context.Context, xPubKey string, chain uint32, - destinationType string, opts ...ModelOps) (*Destination, error) { - + destinationType string, opts ...ModelOps, +) (*Destination, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "new_destination") @@ -46,8 +47,8 @@ func (c *Client) NewDestination(ctx context.Context, xPubKey string, chain uint3 // NewDestinationForLockingScript will create a new destination based on a locking script func (c *Client) NewDestinationForLockingScript(ctx context.Context, xPubID, lockingScript string, - opts ...ModelOps) (*Destination, error) { - + opts ...ModelOps, +) (*Destination, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "new_destination_for_locking_script") @@ -66,18 +67,6 @@ func (c *Client) NewDestinationForLockingScript(ctx context.Context, xPubID, loc return nil, ErrUnknownLockingScript } -<<<<<<< HEAD - // set the monitoring, passed down from the initiating function - // this will be set when calling NewDestination from http, but not for instance paymail - if monitor { - destination.Monitor = customTypes.NullTime{NullTime: sql.NullTime{ - Valid: true, - Time: time.Now(), - }} - } - -======= ->>>>>>> 06feaba (feat(BUX-417): remove monitor, ITC flag and IncomingTransaction (#532)) // Save the destination if err := destination.Save(ctx); err != nil { return nil, err @@ -89,8 +78,8 @@ func (c *Client) NewDestinationForLockingScript(ctx context.Context, xPubID, loc // GetDestinations will get all the destinations from the Datastore func (c *Client) GetDestinations(ctx context.Context, metadataConditions *Metadata, - conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps) ([]*Destination, error) { - + conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps, +) ([]*Destination, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "get_destinations") @@ -108,8 +97,8 @@ func (c *Client) GetDestinations(ctx context.Context, metadataConditions *Metada // GetDestinationsCount will get a count of all the destinations from the Datastore func (c *Client) GetDestinationsCount(ctx context.Context, metadataConditions *Metadata, - conditions *map[string]interface{}, opts ...ModelOps) (int64, error) { - + conditions *map[string]interface{}, opts ...ModelOps, +) (int64, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "get_destinations_count") @@ -129,8 +118,8 @@ func (c *Client) GetDestinationsCount(ctx context.Context, metadataConditions *M // // metadataConditions are the search criteria used to find destinations func (c *Client) GetDestinationsByXpubID(ctx context.Context, xPubID string, metadataConditions *Metadata, - conditions *map[string]interface{}, queryParams *datastore.QueryParams) ([]*Destination, error) { - + conditions *map[string]interface{}, queryParams *datastore.QueryParams, +) ([]*Destination, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "get_destinations") @@ -147,8 +136,8 @@ func (c *Client) GetDestinationsByXpubID(ctx context.Context, xPubID string, met // GetDestinationsByXpubIDCount will get a count of all destinations based on an xPub func (c *Client) GetDestinationsByXpubIDCount(ctx context.Context, xPubID string, metadataConditions *Metadata, - conditions *map[string]interface{}) (int64, error) { - + conditions *map[string]interface{}, +) (int64, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "get_destinations") @@ -165,7 +154,6 @@ func (c *Client) GetDestinationsByXpubIDCount(ctx context.Context, xPubID string // GetDestinationByID will get a destination by id func (c *Client) GetDestinationByID(ctx context.Context, xPubID, id string) (*Destination, error) { - // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "get_destination_by_id") @@ -187,7 +175,6 @@ func (c *Client) GetDestinationByID(ctx context.Context, xPubID, id string) (*De // GetDestinationByLockingScript will get a destination for a locking script func (c *Client) GetDestinationByLockingScript(ctx context.Context, xPubID, lockingScript string) (*Destination, error) { - // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "get_destination_by_locking_script") @@ -209,7 +196,6 @@ func (c *Client) GetDestinationByLockingScript(ctx context.Context, xPubID, lock // GetDestinationByAddress will get a destination for an address func (c *Client) GetDestinationByAddress(ctx context.Context, xPubID, address string) (*Destination, error) { - // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "get_destination_by_address") @@ -231,8 +217,8 @@ func (c *Client) GetDestinationByAddress(ctx context.Context, xPubID, address st // UpdateDestinationMetadataByID will update the metadata in an existing destination by id func (c *Client) UpdateDestinationMetadataByID(ctx context.Context, xPubID, id string, - metadata Metadata) (*Destination, error) { - + metadata Metadata, +) (*Destination, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "update_destination_by_id") @@ -253,8 +239,8 @@ func (c *Client) UpdateDestinationMetadataByID(ctx context.Context, xPubID, id s // UpdateDestinationMetadataByLockingScript will update the metadata in an existing destination by locking script func (c *Client) UpdateDestinationMetadataByLockingScript(ctx context.Context, xPubID, - lockingScript string, metadata Metadata) (*Destination, error) { - + lockingScript string, metadata Metadata, +) (*Destination, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "update_destination_by_locking_script") @@ -275,8 +261,8 @@ func (c *Client) UpdateDestinationMetadataByLockingScript(ctx context.Context, x // UpdateDestinationMetadataByAddress will update the metadata in an existing destination by address func (c *Client) UpdateDestinationMetadataByAddress(ctx context.Context, xPubID, address string, - metadata Metadata) (*Destination, error) { - + metadata Metadata, +) (*Destination, error) { // Check for existing NewRelic transaction ctx = c.GetOrStartTxn(ctx, "update_destination_by_address") diff --git a/errors.go b/errors.go index 6d0ef244..60f1be78 100644 --- a/errors.go +++ b/errors.go @@ -104,7 +104,7 @@ var ErrUtxoNotReserved = errors.New("transaction utxo has not been reserved for var ErrDraftIDMismatch = errors.New("transaction draft id does not match utxo draft reservation id") // ErrMissingTxHex is when the hex is missing or invalid and creates an empty id -var ErrMissingTxHex = errors.New("transaction hex is invalid or id is missing") +var ErrMissingTxHex = errors.New("transaction hex is empty or id is missing") // ErrUtxoAlreadySpent is when the utxo is already spent, but is trying to be used var ErrUtxoAlreadySpent = errors.New("utxo has already been spent") From 899ee26e2248a455443e3f4a19723003a7bec773 Mon Sep 17 00:00:00 2001 From: wregulski Date: Tue, 30 Jan 2024 10:05:05 +0100 Subject: [PATCH 02/10] feat: update broadcast client dep --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 1a91ebd1..a3be03a9 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21.5 require ( github.com/DATA-DOG/go-sqlmock v1.5.2 - github.com/bitcoin-sv/go-broadcast-client v0.16.0 + github.com/bitcoin-sv/go-broadcast-client v0.16.1 github.com/bitcoin-sv/go-paymail v0.12.1 github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 github.com/bitcoinschema/go-map v0.1.0 diff --git a/go.sum b/go.sum index 184266a8..af50269c 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/aws/aws-sdk-go v1.43.45 h1:2708Bj4uV+ym62MOtBnErm/CDX61C4mFe9V2gXy1ca github.com/aws/aws-sdk-go v1.43.45/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= github.com/bitcoin-sv/go-broadcast-client v0.16.0 h1:KadOLv+i9Y6xAOkHsSl2PIECQ59SpUyYurY6Ysvpz5A= github.com/bitcoin-sv/go-broadcast-client v0.16.0/go.mod h1:GRAliwumNBjEbLRIEkXqIKJpsgmMfjvlIDqgyw/NoJE= +github.com/bitcoin-sv/go-broadcast-client v0.16.1 h1:VG4QZwJEVQY/QQupTDeLMw+PEeqh9mn4id+XzYpAmHs= +github.com/bitcoin-sv/go-broadcast-client v0.16.1/go.mod h1:GRAliwumNBjEbLRIEkXqIKJpsgmMfjvlIDqgyw/NoJE= github.com/bitcoin-sv/go-paymail v0.12.1 h1:MDdMFFOZalymT5O5WDUN0EVVWdn3ygo6EhKsWimkM/E= github.com/bitcoin-sv/go-paymail v0.12.1/go.mod h1:/BGu//F4Ji7jIzvkcHxlwBB9vU90yVRx/tovX91Tbw0= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= From 4200064adc4efaa2ebf4ab0a7a953b99e6261ce2 Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 31 Jan 2024 10:08:46 +0100 Subject: [PATCH 03/10] feat: add update transaction business logic --- action_transaction.go | 41 +++++++++++++++++++++++++++++++ chainstate/broadcast_providers.go | 9 +++++-- chainstate/client.go | 10 ++++++++ chainstate/client_options.go | 8 ++++++ client_options.go | 7 ++++++ cron_job_declarations.go | 2 +- interface.go | 2 ++ utils/utils.go | 19 ++++++++++++++ 8 files changed, 95 insertions(+), 3 deletions(-) diff --git a/action_transaction.go b/action_transaction.go index f6123d7e..3d6af23d 100644 --- a/action_transaction.go +++ b/action_transaction.go @@ -9,6 +9,8 @@ import ( "github.com/BuxOrg/bux/chainstate" "github.com/BuxOrg/bux/utils" + "github.com/bitcoin-sv/go-broadcast-client/broadcast" + "github.com/libsv/go-bc" "github.com/libsv/go-bt" "github.com/mrz1836/go-datastore" ) @@ -381,6 +383,45 @@ func (c *Client) RevertTransaction(ctx context.Context, id string) error { return err } +// UpdateTransaction will update the broadcast callback transaction info, like: block height, block hash, status, bump. +func (c *Client) UpdateTransaction(ctx context.Context, callbackResp *broadcast.SubmittedTx) error { + bump, err := bc.NewBUMPFromStr(callbackResp.MerklePath) + if err != nil { + msg := fmt.Sprintf("failed to parse merkle path from broadcast callback - tx: %v", callbackResp) + c.options.logger.Err(err).Msg(msg) + return err + } + + txInfo := &chainstate.TransactionInfo{ + BlockHash: callbackResp.BlockHash, + BlockHeight: callbackResp.BlockHeight, + ID: callbackResp.TxID, + TxStatus: callbackResp.TxStatus, + BUMP: bump, + // it's not possible to get confirmations from broadcast client; zero would be treated as "not confirmed" that's why -1 + Confirmations: -1, + } + + ids := []string{txInfo.ID} + // we use GetTransactionsByIDs to reuse existing function as the main one requires xpubID + txs, err := c.GetTransactionsByIDs(ctx, ids) + if err != nil || len(txs) != 1 { + msg := fmt.Sprintf("failed to get transaction by id while processing callback: %v", txInfo.ID) + c.options.logger.Err(err).Msg(msg) + return err + } + + tx := txs[0] + tx.setChainInfo(txInfo) + if err = tx.Save(ctx); err != nil { + msg := fmt.Sprintf("failed to save transaction while processing callback: %v", txInfo.ID) + c.options.logger.Err(err).Msg(msg) + return err + } + + return nil +} + func generateTxIDFilterConditions(txIDs []string) *map[string]interface{} { orConditions := make([]map[string]interface{}, len(txIDs)) diff --git a/chainstate/broadcast_providers.go b/chainstate/broadcast_providers.go index 6b3c064a..e66bc419 100644 --- a/chainstate/broadcast_providers.go +++ b/chainstate/broadcast_providers.go @@ -94,14 +94,19 @@ func (provider broadcastClientProvider) broadcast(ctx context.Context, c *Client return broadcastWithBroadcastClient(ctx, c, provider.txID, provider.txHex) } -func broadcastWithBroadcastClient(ctx context.Context, client ClientInterface, txID, hex string) error { +func broadcastWithBroadcastClient(ctx context.Context, client *Client, txID, hex string) error { debugLog(client, txID, "executing broadcast request for "+ProviderBroadcastClient) tx := broadcast.Transaction{ Hex: hex, } - result, err := client.BroadcastClient().SubmitTransaction(ctx, &tx, broadcast.WithRawFormat()) + result, err := client.BroadcastClient().SubmitTransaction( + ctx, + &tx, + broadcast.WithRawFormat(), + broadcast.WithCallback(client.options.config.callbackURL, client.options.config.callbackToken), + ) if err != nil { debugLog(client, txID, "error broadcast request for "+ProviderBroadcastClient+" failed: "+err.Error()) return err diff --git a/chainstate/client.go b/chainstate/client.go index 16a88f34..01c3451f 100644 --- a/chainstate/client.go +++ b/chainstate/client.go @@ -32,6 +32,8 @@ type ( // syncConfig holds all the configuration about the different sync processes syncConfig struct { + callbackURL string // Broadcast callback URL + callbackToken string // Broadcast callback access token excludedProviders []string // List of provider names httpClient HTTPInterface // Custom HTTP client (Minercraft, WOC) minercraftConfig *minercraftConfig // minercraftConfig configuration @@ -199,3 +201,11 @@ func (c *Client) checkFeeUnit() error { } return nil } + +// func (c *Client) getCallbackRoute() error { +// // return c.options.config. +// } + +func (c *Client) getCallbackToken() string { + return c.options.config.callbackToken +} diff --git a/chainstate/client_options.go b/chainstate/client_options.go index 262d8122..fd4ecfb0 100644 --- a/chainstate/client_options.go +++ b/chainstate/client_options.go @@ -165,3 +165,11 @@ func WithConnectionToPulse(url, authToken string) ClientOps { c.config.pulseClient = newPulseClientProvider(url, authToken) } } + +// WithCallback will set broadcast callback settings +func WithCallback(callbackURL, callbackAuthToken string) ClientOps { + return func(c *clientOptions) { + c.config.callbackURL = callbackURL + c.config.callbackToken = callbackAuthToken + } +} diff --git a/client_options.go b/client_options.go index 67562eaf..776a5bfa 100644 --- a/client_options.go +++ b/client_options.go @@ -662,3 +662,10 @@ func WithBroadcastClient(broadcastClient broadcast.Client) ClientOps { c.chainstate.options = append(c.chainstate.options, chainstate.WithBroadcastClient(broadcastClient)) } } + +// WithCallback set callback settings +func WithCallback(callbackURL string, callbackToken string) ClientOps { + return func(c *clientOptions) { + c.chainstate.options = append(c.chainstate.options, chainstate.WithCallback(callbackURL, callbackToken)) + } +} diff --git a/cron_job_declarations.go b/cron_job_declarations.go index 68a05957..f1d6c594 100644 --- a/cron_job_declarations.go +++ b/cron_job_declarations.go @@ -35,7 +35,7 @@ func (c *Client) cronJobs() taskmanager.CronJobs { Handler: handler(taskBroadcastTransactions), }, CronJobNameSyncTransactionSync: { - Period: 120 * time.Second, + Period: 600 * time.Second, Handler: handler(taskSyncTransactions), }, } diff --git a/interface.go b/interface.go index cfd8905b..51838b6e 100644 --- a/interface.go +++ b/interface.go @@ -8,6 +8,7 @@ import ( "github.com/BuxOrg/bux/cluster" "github.com/BuxOrg/bux/notifications" "github.com/BuxOrg/bux/taskmanager" + "github.com/bitcoin-sv/go-broadcast-client/broadcast" "github.com/bitcoin-sv/go-paymail" "github.com/mrz1836/go-cachestore" "github.com/mrz1836/go-datastore" @@ -132,6 +133,7 @@ type TransactionService interface { RecordTransaction(ctx context.Context, xPubKey, txHex, draftID string, opts ...ModelOps) (*Transaction, error) RecordRawTransaction(ctx context.Context, txHex string, opts ...ModelOps) (*Transaction, error) + UpdateTransaction(ctx context.Context, txInfo *broadcast.SubmittedTx) error UpdateTransactionMetadata(ctx context.Context, xPubID, id string, metadata Metadata) (*Transaction, error) RevertTransaction(ctx context.Context, id string) error } diff --git a/utils/utils.go b/utils/utils.go index fef71ea3..e23c2d8f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -8,6 +8,8 @@ import ( "crypto/sha256" "encoding/binary" "encoding/hex" + "fmt" + "hash/adler32" "math" "strconv" @@ -95,6 +97,23 @@ func LittleEndianBytes64(value uint64, resultLength uint32) []byte { return buf } +// HashAdler32 returns computed string calculated with Adler32 function. +func HashAdler32(input string) (string, error) { + if input == "" { + return "", fmt.Errorf("input string is empty - cannot apply adler32 hash function") + } + data := []byte(input) + hasher := adler32.New() + _, err := hasher.Write(data) + if err != nil { + return "", err + } + + sum := hasher.Sum32() + + return fmt.Sprintf("%08x", sum), nil +} + // SafeAssign - Assigns value (not pointer) the src to dest if src is not nil func SafeAssign[T any](dest *T, src *T) { if src != nil { From 0d554ebc2f1cdb428eb039883a52bd8267277c4f Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 31 Jan 2024 10:14:10 +0100 Subject: [PATCH 04/10] feat: simplifies UpdateTransaction bl --- action_transaction.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/action_transaction.go b/action_transaction.go index 3d6af23d..54eac381 100644 --- a/action_transaction.go +++ b/action_transaction.go @@ -402,16 +402,13 @@ func (c *Client) UpdateTransaction(ctx context.Context, callbackResp *broadcast. Confirmations: -1, } - ids := []string{txInfo.ID} - // we use GetTransactionsByIDs to reuse existing function as the main one requires xpubID - txs, err := c.GetTransactionsByIDs(ctx, ids) - if err != nil || len(txs) != 1 { + tx, err := c.GetTransaction(ctx, "", txInfo.ID) + if err != nil { msg := fmt.Sprintf("failed to get transaction by id while processing callback: %v", txInfo.ID) c.options.logger.Err(err).Msg(msg) return err } - tx := txs[0] tx.setChainInfo(txInfo) if err = tx.Save(ctx); err != nil { msg := fmt.Sprintf("failed to save transaction while processing callback: %v", txInfo.ID) From d14b8af4534c8e2303d591c45ec1d57fa7a0e984 Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 31 Jan 2024 10:44:41 +0100 Subject: [PATCH 05/10] feat: remove unused methods --- chainstate/client.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/chainstate/client.go b/chainstate/client.go index 01c3451f..9cfe320a 100644 --- a/chainstate/client.go +++ b/chainstate/client.go @@ -201,11 +201,3 @@ func (c *Client) checkFeeUnit() error { } return nil } - -// func (c *Client) getCallbackRoute() error { -// // return c.options.config. -// } - -func (c *Client) getCallbackToken() string { - return c.options.config.callbackToken -} From cd13929287012e6f0ea835fc4f1a9ada2e6e4d56 Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 31 Jan 2024 10:45:57 +0100 Subject: [PATCH 06/10] chore: add vscode folder to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 4165044d..8c56f94c 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,9 @@ go.list # Jetbrains .idea/ +#VSCode +.vscode/ + # Eclipse .project From e4b2d5e599245d962ff5ec786c8b3d3741c87a05 Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 31 Jan 2024 12:27:00 +0100 Subject: [PATCH 07/10] fix: use Msgf instead of Msg --- action_transaction.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/action_transaction.go b/action_transaction.go index 54eac381..01d0f1f5 100644 --- a/action_transaction.go +++ b/action_transaction.go @@ -387,8 +387,7 @@ func (c *Client) RevertTransaction(ctx context.Context, id string) error { func (c *Client) UpdateTransaction(ctx context.Context, callbackResp *broadcast.SubmittedTx) error { bump, err := bc.NewBUMPFromStr(callbackResp.MerklePath) if err != nil { - msg := fmt.Sprintf("failed to parse merkle path from broadcast callback - tx: %v", callbackResp) - c.options.logger.Err(err).Msg(msg) + c.options.logger.Err(err).Msgf("failed to parse merkle path from broadcast callback - tx: %v", callbackResp) return err } @@ -404,15 +403,13 @@ func (c *Client) UpdateTransaction(ctx context.Context, callbackResp *broadcast. tx, err := c.GetTransaction(ctx, "", txInfo.ID) if err != nil { - msg := fmt.Sprintf("failed to get transaction by id while processing callback: %v", txInfo.ID) - c.options.logger.Err(err).Msg(msg) + c.options.logger.Err(err).Msgf("failed to get transaction by id while processing callback: %v", txInfo.ID) return err } tx.setChainInfo(txInfo) if err = tx.Save(ctx); err != nil { - msg := fmt.Sprintf("failed to save transaction while processing callback: %v", txInfo.ID) - c.options.logger.Err(err).Msg(msg) + c.options.logger.Err(err).Msgf("failed to save transaction while processing callback: %v", txInfo.ID) return err } From b77383d29c5c68027e520409897473b410241f54 Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 31 Jan 2024 14:41:07 +0100 Subject: [PATCH 08/10] fix: make code reusable and update sync transaction --- action_transaction.go | 10 +++++----- sync_tx_repository.go | 19 +++++++++++++++++++ sync_tx_service.go | 14 ++++++-------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/action_transaction.go b/action_transaction.go index 01d0f1f5..74aaa3cd 100644 --- a/action_transaction.go +++ b/action_transaction.go @@ -403,17 +403,17 @@ func (c *Client) UpdateTransaction(ctx context.Context, callbackResp *broadcast. tx, err := c.GetTransaction(ctx, "", txInfo.ID) if err != nil { - c.options.logger.Err(err).Msgf("failed to get transaction by id while processing callback: %v", txInfo.ID) + c.options.logger.Err(err).Msgf("failed to get transaction by id: %v", txInfo.ID) return err } - tx.setChainInfo(txInfo) - if err = tx.Save(ctx); err != nil { - c.options.logger.Err(err).Msgf("failed to save transaction while processing callback: %v", txInfo.ID) + syncTx, err := GetSyncTransactionByTxID(ctx, txInfo.ID, c.DefaultModelOptions()...) + if err != nil { + c.options.logger.Err(err).Msgf("failed to get sync transaction by tx id: %v", txInfo.ID) return err } - return nil + return processSyncTxSave(ctx, txInfo, syncTx, tx) } func generateTxIDFilterConditions(txIDs []string) *map[string]interface{} { diff --git a/sync_tx_repository.go b/sync_tx_repository.go index 8532521e..ecb584e3 100644 --- a/sync_tx_repository.go +++ b/sync_tx_repository.go @@ -30,6 +30,25 @@ func GetSyncTransactionByID(ctx context.Context, id string, opts ...ModelOps) (* return txs[0], nil } +// GetSyncTransactionByTxID will get a sync transaction by it's transaction id. +func GetSyncTransactionByTxID(ctx context.Context, txID string, opts ...ModelOps) (*SyncTransaction, error) { + // Get the records by status + txs, err := _getSyncTransactionsByConditions(ctx, + map[string]interface{}{ + txID: txID, + }, + nil, opts..., + ) + if err != nil { + return nil, err + } + if len(txs) != 1 { + return nil, nil + } + + return txs[0], nil +} + /*** /exported funcs ***/ /*** public unexported funcs ***/ diff --git a/sync_tx_service.go b/sync_tx_service.go index 9f707d65..d5acc738 100644 --- a/sync_tx_service.go +++ b/sync_tx_service.go @@ -221,14 +221,17 @@ func _syncTxDataFromChain(ctx context.Context, syncTx *SyncTransaction, transact } return err } + return processSyncTxSave(ctx, txInfo, syncTx, transaction) +} +func processSyncTxSave(ctx context.Context, txInfo *chainstate.TransactionInfo, syncTx *SyncTransaction, transaction *Transaction) error { if !txInfo.Valid() { syncTx.Client().Logger().Warn(). Str("txID", syncTx.ID). Msgf("txInfo is invalid, will try again later") if syncTx.Client().IsDebug() { - txInfoJSON, _ := json.Marshal(txInfo) //nolint:errchkjson // error is not needed + txInfoJSON, _ := json.Marshal(txInfo) syncTx.Client().Logger().Debug(). Str("txID", syncTx.ID). Msgf("txInfo: %s", string(txInfoJSON)) @@ -238,18 +241,15 @@ func _syncTxDataFromChain(ctx context.Context, syncTx *SyncTransaction, transact transaction.setChainInfo(txInfo) - // Create status message message := "transaction was found on-chain by " + chainstate.ProviderBroadcastClient - // Save the transaction (should NOT error) - if err = transaction.Save(ctx); err != nil { + if err := transaction.Save(ctx); err != nil { _bailAndSaveSyncTransaction( ctx, syncTx, SyncStatusError, syncActionSync, "internal", err.Error(), ) return err } - // Update the sync status syncTx.SyncStatus = SyncStatusComplete syncTx.Results.LastMessage = message syncTx.Results.Results = append(syncTx.Results.Results, &SyncResult{ @@ -259,8 +259,7 @@ func _syncTxDataFromChain(ctx context.Context, syncTx *SyncTransaction, transact StatusMessage: message, }) - // Update the sync transaction record - if err = syncTx.Save(ctx); err != nil { + if err := syncTx.Save(ctx); err != nil { _bailAndSaveSyncTransaction(ctx, syncTx, SyncStatusError, syncActionSync, "internal", err.Error()) return err } @@ -268,7 +267,6 @@ func _syncTxDataFromChain(ctx context.Context, syncTx *SyncTransaction, transact syncTx.Client().Logger().Info(). Str("txID", syncTx.ID). Msgf("Transaction processed successfully") - // Done! return nil } From e076ffa93d3c811a0774498093cb05bc5db64c54 Mon Sep 17 00:00:00 2001 From: wregulski Date: Thu, 1 Feb 2024 12:02:01 +0100 Subject: [PATCH 09/10] fix: update field name in sync_tx_repo --- sync_tx_repository.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sync_tx_repository.go b/sync_tx_repository.go index ecb584e3..42127bc1 100644 --- a/sync_tx_repository.go +++ b/sync_tx_repository.go @@ -35,7 +35,7 @@ func GetSyncTransactionByTxID(ctx context.Context, txID string, opts ...ModelOps // Get the records by status txs, err := _getSyncTransactionsByConditions(ctx, map[string]interface{}{ - txID: txID, + idField: txID, }, nil, opts..., ) From 5ac2858facdc7cdfccd39bc977deea801c496b46 Mon Sep 17 00:00:00 2001 From: wregulski Date: Thu, 1 Feb 2024 13:10:52 +0100 Subject: [PATCH 10/10] feat: change number of bux version --- definitions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/definitions.go b/definitions.go index 2972f487..d234f473 100644 --- a/definitions.go +++ b/definitions.go @@ -20,7 +20,7 @@ const ( dustLimit = uint64(1) // Dust limit mongoTestVersion = "6.0.4" // Mongo Testing Version sqliteTestVersion = "3.37.0" // SQLite Testing Version (dummy version for now) - version = "v0.13.0" // bux version + version = "v0.14.2" // bux version ) // All the base models