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

Initialize transactions once #2521

Merged
merged 2 commits into from
Dec 20, 2023
Merged

Initialize transactions once #2521

merged 2 commits into from
Dec 20, 2023

Conversation

StephenButtolph
Copy link
Contributor

Why this should be merged

Currently, when creating a block, we re-initialize all transactions being placed into the block. This is inefficient, as the transactions are already initialized. However, this can also introduce racy behavior. In a concurrent setting we generally want to be able to treat transactions as immuable. However calling .ID on a transaction that could be re-initialized in a different goroutine would be racy.

How this works

Ensure that transactions are only initialized once, during creation or parsing.

How this was tested

  • CI

@StephenButtolph StephenButtolph added the cleanup Code quality improvement label Dec 20, 2023
@StephenButtolph StephenButtolph added this to the v1.10.18 milestone Dec 20, 2023
@StephenButtolph StephenButtolph self-assigned this Dec 20, 2023
Comment on lines -161 to -175
func initializeTx(cm codec.Manager, tx *Tx) error {
signedBytes, err := cm.Marshal(CodecVersion, tx)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}

unsignedBytesLen, err := cm.Size(CodecVersion, &tx.Unsigned)
if err != nil {
return fmt.Errorf("couldn't calculate UnsignedTx marshal length: %w", err)
}

unsignedBytes := signedBytes[:unsignedBytesLen]
tx.SetBytes(unsignedBytes, signedBytes)
return 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 100% duplicated from tx.Initialize. I have no recollection as to why this was done... but something seems to have been a mistake imo.

Comment on lines -133 to -139
func (p *parser) InitializeTx(tx *Tx) error {
return initializeTx(p.cm, tx)
}

func (p *parser) InitializeGenesisTx(tx *Tx) error {
return initializeTx(p.gcm, 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.

Tx has an Initialize function and we already expose the codec - so these functions aren't needed. Additionally, InitializeTx was only used in tests.

Comment on lines -92 to -98
func (p *parser) InitializeBlock(block Block) error {
return initialize(block, p.Codec())
}

func (p *parser) InitializeGenesisBlock(block Block) error {
return initialize(block, p.GenesisCodec())
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These functions were never used.

@@ -88,5 +88,16 @@ func NewStandardBlock(
Time: uint64(timestamp.Unix()),
Transactions: txs,
}
return blk, initialize(blk, cm)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling initialize here used to call blk.initialize which would re-initialize the transactions. Now we explicitly do not re-initialize the transactions.

@StephenButtolph StephenButtolph added vm This involves virtual machines Durango durango fork labels Dec 20, 2023
@StephenButtolph StephenButtolph added this pull request to the merge queue Dec 20, 2023
Merged via the queue into dev with commit 5c02d0c Dec 20, 2023
17 checks passed
@StephenButtolph StephenButtolph deleted the initialize-txs-once branch December 20, 2023 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Code quality improvement Durango durango fork vm This involves virtual machines
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants