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 P-chain Builder #3282

Merged
merged 11 commits into from
Aug 12, 2024
Merged

Refactor P-chain Builder #3282

merged 11 commits into from
Aug 12, 2024

Conversation

StephenButtolph
Copy link
Contributor

@StephenButtolph StephenButtolph commented Aug 8, 2024

Why this should be merged

It seems like #3232 is daunting to review - this PR further reduces that diff by refactoring the builder without adding complexity in yet.

How this works

  • This PR refactors ImportTx to always call spend.
  • This PR refactors all the transactions to call spend last.
  • This PR introduces a spendHelper to encapsulate common operations performed in spend
  • This PR introduces a few helper functions that separate UTXOs into different groups.

How this was tested

  • This PR is a pure refactor and passes existing CI
  • Additional helpers are explicitly tested

@StephenButtolph StephenButtolph added this to the v1.11.11 milestone Aug 8, 2024
@StephenButtolph StephenButtolph self-assigned this Aug 8, 2024
@StephenButtolph StephenButtolph marked this pull request as ready for review August 8, 2024 20:18

// Join merges the provided slices into a single slice.
//
// TODO: Use slices.Concat once the minimum go version is 1.22.
Copy link
Contributor

@ARR4N ARR4N Aug 9, 2024

Choose a reason for hiding this comment

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

(no action required) Their signature is interesting:

func Concat[S ~[]E, E any](slices ...S) S {

Without looking too deeply I guess it's to play nicely with other functions in that package / make it easier to use them together without being confused about which type is which.

Copy link
Contributor

@ARR4N ARR4N left a comment

Choose a reason for hiding this comment

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

Partial review before dinner. I've marked where I'm up to with 👀 .

  • This PR refactors ImportTx to always call spend.
  • This PR refactors all the transactions to call spend last.

Why?

@@ -677,6 +713,33 @@ func (b *builder) NewImportTx(
})
}

var (
toBurn map[ids.ID]uint64
Copy link
Contributor

Choose a reason for hiding this comment

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

This section can be simplified further.

toBurn := map[ids.ID]uint64{}
toStake := map[ids.ID]uint64{}
var excessAVAX uint64

if avax := importedAmounts[avaxAssetID]; avax > txFee {
    excessAVAX = avax - txFee
} else {
    toBurn[avaxAssetID] = txFee - avax
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, your explanation made me realise that it should be avax >= txFee.

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 think the code actually works regardless of avax >= txFee or avax > txFee (as setting toBurn[avaxAssetID] = 0 is fine... Although slightly inefficient I suppose.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, it should be functionally identical. Just feels "more correct" to leave excessAVAX == 0 instead of also adding a zero burn.

Copy link
Contributor

@ARR4N ARR4N left a comment

Choose a reason for hiding this comment

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

Approving with comments because I don't need to re-review the implementations (unless you want me to).

)

utxosWithAssetID, utxosWithOtherAssetID := splitUTXOsByLocktime(utxos, unlockedTime)
require.Equal(expectedUnlockedUTXOs, utxosWithAssetID)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an example of where I strongly believe that assert should be used over require. The failure of this comparison doesn't necessarily corrupt the next one, and it may very well be useful to know if/how they fail simultaneously.

Same applies to the next test.

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 know that you prefer the usage of assert for things like this... But currently the status-quo in avalanchego is to ban usage of assert. (And this PR isn't going to change the linting rules to allow assert)

@StephenButtolph StephenButtolph added this pull request to the merge queue Aug 12, 2024
Merged via the queue into master with commit b181671 Aug 12, 2024
21 checks passed
@StephenButtolph StephenButtolph deleted the refactor-tx-builder branch August 12, 2024 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants