Skip to content

Move context lock into issueTx #2524

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

Merged
merged 3 commits into from
Dec 20, 2023
Merged

Move context lock into issueTx #2524

merged 3 commits into from
Dec 20, 2023

Conversation

StephenButtolph
Copy link
Contributor

Why this should be merged

We are moving the context lock acquisition into network.IssueTx. This PR separates this work to make moving the lock through boundaries clearly correct.

How this works

  • Ensures that vm.issueTx does not hold the context lock.
  • Ensures the wallet service maintains a consistent view of the UTXO set by holding the context lock throughout the issuance. This means we will no longer perform state checks against transactions issued over the wallet service... However I don't think this is an important change in behavior.

How this was tested

  • CI (the json string changed because we are no longer parsing the tx, so the tx provided from the node was initialized manually rather than by the codec parser)

@StephenButtolph StephenButtolph self-assigned this Dec 20, 2023
@StephenButtolph StephenButtolph added this to the v1.10.18 milestone Dec 20, 2023
@StephenButtolph StephenButtolph added the vm This involves virtual machines label Dec 20, 2023
@@ -622,7 +622,7 @@ func TestServiceGetTxJSON_CreateAssetTx(t *testing.T) {

// contains the address in the right format
require.Contains(jsonString, `"outputs":[{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"groupID":1,"locktime":0,"threshold":1},{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"groupID":2,"locktime":0,"threshold":1}]}`)
require.Contains(jsonString, `"initialStates":[{"fxIndex":0,"fxID":"spdxUxVJQbX85MGxMHbKw1sHxMnSqJ3QBzDyDYEP3h6TLuxqQ","outputs":[{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"locktime":0,"threshold":1},{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"locktime":0,"threshold":1}]},{"fxIndex":1,"fxID":"qd2U4HDWUvMrVUeTcCHp6xH3Qpnn1XbU5MDdnBoiifFqvgXwT","outputs":[{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"groupID":1,"locktime":0,"threshold":1},{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"groupID":2,"locktime":0,"threshold":1}]},{"fxIndex":2,"fxID":"rXJsCSEYXg2TehWxCEEGj6JU2PWKTkd6cBdNLjoe2SpsKD9cy","outputs":[{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"locktime":0,"threshold":1},{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"locktime":0,"threshold":1}]}]},"credentials":[],"id":"2MDgrsBHMRsEPa4D4NA1Bo1pjkVLUK173S3dd9BgT2nCJNiDuS"}`)
require.Contains(jsonString, `"initialStates":[{"fxIndex":0,"fxID":"spdxUxVJQbX85MGxMHbKw1sHxMnSqJ3QBzDyDYEP3h6TLuxqQ","outputs":[{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"locktime":0,"threshold":1},{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"locktime":0,"threshold":1}]},{"fxIndex":1,"fxID":"qd2U4HDWUvMrVUeTcCHp6xH3Qpnn1XbU5MDdnBoiifFqvgXwT","outputs":[{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"groupID":1,"locktime":0,"threshold":1},{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"groupID":2,"locktime":0,"threshold":1}]},{"fxIndex":2,"fxID":"rXJsCSEYXg2TehWxCEEGj6JU2PWKTkd6cBdNLjoe2SpsKD9cy","outputs":[{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"locktime":0,"threshold":1},{"addresses":["X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"],"locktime":0,"threshold":1}]}]},"credentials":null,"id":"2MDgrsBHMRsEPa4D4NA1Bo1pjkVLUK173S3dd9BgT2nCJNiDuS"}`)
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 needed because we no longer parse the tx in issueTx

Comment on lines -485 to -487
if !vm.bootstrapped || vm.Builder == nil {
return ids.ID{}, errBootstrapping
}
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 isn't needed anymore. We only allow API traffic after bootstrapping has finished, and we are guaranteed to have Linearize called during bootstrapping.

Comment on lines -481 to -483
// If onDecide is specified, the function will be called when the transaction is
// either accepted or rejected with the appropriate status. This function will
// go out of scope when the transaction is removed from memory.
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 very wrong... probably stale from years ago

for {
txID, tx, ok := w.pendingTxs.Oldest()
if !ok {
return
}

txBytes := tx.Bytes()
_, err := w.vm.IssueTx(txBytes)
err := w.vm.mempool.Add(tx)
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 a behavior change. Previously we would validate the tx against the current state... now we just issue the tx into the mempool. Because this is only for transactions issued over the wallet API (which is deprecated) I think this minor change is fine.

if err == nil {
w.vm.ctx.Log.Info("issued tx to mempool over wallet API",
zap.Stringer("txID", txID),
)

w.vm.mempool.RequestBuildBlock()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We may want to just include this inside of Add in the mempool.

@@ -82,11 +76,16 @@ func (w *WalletService) issue(txBytes []byte) (ids.ID, error) {
}

if w.pendingTxs.Len() == 0 {
_, err := w.vm.IssueTx(txBytes)
if err != nil {
if err := w.vm.mempool.Add(tx); err != nil {
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 again a small behavior change - same rationale as above as to why I think it is fine.

@@ -31,27 +31,26 @@ type WalletService struct {
}

func (w *WalletService) decided(txID ids.ID) {
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 called during block.Accept - so we must support holding the context lock here.

Comment on lines +527 to +529
vm.ctx.Lock.Unlock()
txID, err := vm.issueTx(tx)
vm.ctx.Lock.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why can't we modify the tests calling this to just not hold the lock?

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'll make a PR on top of this PR and we can decide which we prefer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -302,7 +306,7 @@ func (w *WalletService) SendMultiple(_ *http.Request, args *SendMultipleArgs, re
return err
}

txID, err := w.issue(tx.Bytes())
txID, err := w.issue(tx)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to ensure atomic access between update and issue - so we use the context lock for exclusion throughout this API.

if err == nil {
w.vm.ctx.Log.Info("issued tx to mempool over wallet API",
zap.Stringer("txID", txID),
)

Copy link
Contributor

Choose a reason for hiding this comment

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

afaict, it wouldn't be easy to change this to be called after all txs in a block have onAccept/onReject on them

@StephenButtolph StephenButtolph added this pull request to the merge queue Dec 20, 2023
Merged via the queue into dev with commit fca1a16 Dec 20, 2023
@StephenButtolph StephenButtolph deleted the move-issuance-lock branch December 20, 2023 22:37
tedim52 pushed a commit to tedim52/avalanchego that referenced this pull request Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vm This involves virtual machines
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants