Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(SPV-000): simplification of chainstate and proposed fixes for transaction #677

Closed
wants to merge 5 commits into from

Conversation

chris-4chain
Copy link
Contributor

@chris-4chain chris-4chain commented Aug 28, 2024

Duting analyss of transaction flow I found several places which can be simplified of fixed.

  • This is only my proposition. None of them are necessary to apply - but maybe it would be beneficial to do it :)

See my comments to the most important places of my changes.

Pull Request Checklist

  • 📖 I created my PR using provided : CODE_STANDARDS
  • 📖 I have read the short Code of Conduct: CODE_OF_CONDUCT
  • 🏠 I tested my changes locally.
  • ✅ I have provided tests for my changes.
  • 📝 I have used conventional commits.
  • 📗 I have updated any related documentation.
  • 💾 PR was issued based on the Github or Jira issue.

@chris-4chain chris-4chain requested a review from a team as a code owner August 28, 2024 07:08
Copy link

Manual Tests

ℹ️ Remember to ask team members to perform manual tests and to assign tested label after testing.

@codecov-commenter
Copy link

codecov-commenter commented Aug 28, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 51.89873% with 38 lines in your changes missing coverage. Please review.

Project coverage is 48.98%. Comparing base (c1ebc7b) to head (427c3b2).

Files Patch % Lines
engine/chainstate/broadcast_submit_tx.go 55.26% 14 Missing and 3 partials ⚠️
engine/sync_tx_service.go 35.71% 9 Missing ⚠️
engine/chainstate/chainstate.go 38.46% 7 Missing and 1 partial ⚠️
engine/chainstate/client.go 66.66% 0 Missing and 2 partials ⚠️
engine/chainstate/transaction.go 50.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #677      +/-   ##
==========================================
- Coverage   49.01%   48.98%   -0.03%     
==========================================
  Files         264      263       -1     
  Lines       12226    12152      -74     
==========================================
- Hits         5992     5953      -39     
+ Misses       5640     5605      -35     
  Partials      594      594              
Flag Coverage Δ
unittests 48.98% <51.89%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
engine/action_paymails.go 50.47% <100.00%> (ø)
engine/chainstate/broadcast_utils.go 100.00% <ø> (+80.00%) ⬆️
engine/chainstate/client_options.go 75.80% <ø> (-2.46%) ⬇️
engine/client.go 64.61% <ø> (-0.54%) ⬇️
engine/client_internal.go 77.06% <100.00%> (+0.21%) ⬆️
engine/client_options.go 73.47% <ø> (-0.29%) ⬇️
engine/model_draft_transactions.go 75.43% <100.00%> (-0.05%) ⬇️
engine/model_sync_results.go 53.84% <ø> (ø)
engine/chainstate/client.go 61.53% <66.66%> (-3.28%) ⬇️
engine/chainstate/transaction.go 82.97% <50.00%> (+0.37%) ⬆️
... and 3 more

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c1ebc7b...427c3b2. Read the comment docs.

"github.com/bitcoin-sv/go-broadcast-client/broadcast"
)

func (chainstate *Client) submitTransaction(ctx context.Context, txID, txHex string, format HexFormatFlag) *BroadcastFailure {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the code moved from broadcast_providers.go because I found that we have only one supported way so no "providers" abstraction is needed.

ctxWithCancel, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

failure := c.submitTransaction(ctxWithCancel, txID, txHex, format)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In previous implementation we was waiting for the first successful response from one of the providers. That's why there was additional goroutines and channels.
But:

  • There was only one "provider" - the broadcastClient
  • So I'm proposing to simplyfy this logic without unnecessary goroutines and no "providers" abstraction.

Comment on lines +54 to +59
// check in Mempool as fallback - if transaction is there -> GREAT SUCCESS
if _, err := c.QueryTransaction(ctx, txID, requiredInMempool, timeout); err != nil {
return &BroadcastFailure{
InvalidTx: failure.InvalidTx,
Error: fmt.Errorf("query tx failed: %w, initial error: %s", err, failure.Error.Error()),
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I don't understand this block of code 🙈

@@ -54,7 +54,7 @@ type (
//
// If no options are given, it will use the defaultClientOptions()
// ctx may contain a NewRelic txn (or one will be created)
func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error) {
func NewClient(ctx context.Context, logger *zerolog.Logger, opts ...ClientOps) (ClientInterface, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the logger as normal parameter instead of an option because in our implementation we expect the logger to exists here.

Comment on lines -93 to -107
// Debug will set the debug flag
func (c *Client) Debug(on bool) {
c.options.debug = on
}

// DebugLog will display verbose logs
func (c *Client) DebugLog(text string) {
c.options.logger.Debug().Msg(text)
}

// IsDebug will return if debugging is enabled
func (c *Client) IsDebug() bool {
return c.options.debug
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed those methods, because - as far as I remember - we wanted to get rid of this approach, in favor of zerolog.Logger's levels.

@@ -17,7 +18,7 @@ type HTTPInterface interface {
// ChainService is the chain related methods
type ChainService interface {
SupportedBroadcastFormats() HexFormatFlag
Broadcast(ctx context.Context, id, txHex string, format HexFormatFlag, timeout time.Duration) *BroadcastResult
Broadcast(ctx context.Context, id, txHex string, format HexFormatFlag, timeout time.Duration) *BroadcastFailure
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BroadcastResult contained the BroadcastFailure and the name of "provider".

  • So, because of I removed the "providers" abstraction - the name became unused.

Comment on lines -166 to -168
conditions := map[string]interface{}{
xPubIDField: m.XpubID,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition with xPubIDField is not needed (redundant), because the conditions map is passed to GetPaymailAddressesByXPubID as "additional condition".

  • In the GetPaymailAddressesByXPubID the field is set to xPubIDField: m.XpubID, anyway.

@@ -111,27 +111,27 @@ func broadcastSyncTransaction(ctx context.Context, syncTx *SyncTransaction) erro
client := syncTx.Client()
chainstateSrv := client.Chainstate()

// Get the transaction HEX
// Get the transaction from syncTx otherwise load it by ID from the database
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was misleading (to me) - so I've adjusted it.

tx := syncTx.transaction
if tx == nil || tx.Hex == "" {
if tx, err = _getTransaction(ctx, syncTx.ID, syncTx.GetOptions(false)); err != nil {
return nil
return nil // TODO: should we return an error here?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we skip the error here?

  • Maybe it's ok, but some comment would be great.

@chris-4chain chris-4chain deleted the refactor-transactionflow branch August 28, 2024 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants