-
Notifications
You must be signed in to change notification settings - Fork 705
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
Conversation
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 | ||
} |
There was a problem hiding this comment.
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.
func (p *parser) InitializeTx(tx *Tx) error { | ||
return initializeTx(p.cm, tx) | ||
} | ||
|
||
func (p *parser) InitializeGenesisTx(tx *Tx) error { | ||
return initializeTx(p.gcm, tx) | ||
} |
There was a problem hiding this comment.
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.
func (p *parser) InitializeBlock(block Block) error { | ||
return initialize(block, p.Codec()) | ||
} | ||
|
||
func (p *parser) InitializeGenesisBlock(block Block) error { | ||
return initialize(block, p.GenesisCodec()) | ||
} |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
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