notary: reuse (*Transaction).Copy where possible

Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
This commit is contained in:
Ekaterina Pavlova 2024-04-23 16:05:11 +02:00
parent 956fd08adb
commit 8da7c0a671

View file

@ -272,7 +272,7 @@ func (n *Notary) OnNewRequest(payload *payload.P2PNotaryRequest) {
// Avoid changes in the main transaction witnesses got from the notary request pool to // Avoid changes in the main transaction witnesses got from the notary request pool to
// keep the pooled tx valid. We will update its copy => the copy's size will be changed. // keep the pooled tx valid. We will update its copy => the copy's size will be changed.
r = &request{ r = &request{
main: safeCopy(payload.MainTransaction), main: payload.MainTransaction.Copy(),
minNotValidBefore: nvbFallback, minNotValidBefore: nvbFallback,
} }
n.requests[payload.MainTransaction.Hash()] = r n.requests[payload.MainTransaction.Hash()] = r
@ -285,7 +285,7 @@ func (n *Notary) OnNewRequest(payload *payload.P2PNotaryRequest) {
// size won't be changed after finalisation, the witness bytes changes may // size won't be changed after finalisation, the witness bytes changes may
// affect the other users of notary pool and cause race. Avoid this by making // affect the other users of notary pool and cause race. Avoid this by making
// the copy. // the copy.
r.fallbacks = append(r.fallbacks, safeCopy(payload.FallbackTransaction)) r.fallbacks = append(r.fallbacks, payload.FallbackTransaction.Copy())
if exists && r.isMainCompleted() || validationErr != nil { if exists && r.isMainCompleted() || validationErr != nil {
return return
} }
@ -345,21 +345,6 @@ func (n *Notary) OnNewRequest(payload *payload.P2PNotaryRequest) {
} }
} }
// safeCopy creates a copy of provided transaction by dereferencing it and creating
// fresh witnesses so that the tx's witnesses may be modified without affecting the
// copy's ones.
func safeCopy(tx *transaction.Transaction) *transaction.Transaction {
cp := *tx
cp.Scripts = make([]transaction.Witness, len(tx.Scripts))
for i := range cp.Scripts {
cp.Scripts[i] = transaction.Witness{
InvocationScript: bytes.Clone(tx.Scripts[i].InvocationScript),
VerificationScript: bytes.Clone(tx.Scripts[i].VerificationScript),
}
}
return &cp
}
// OnRequestRemoval is a callback which is called after fallback transaction is removed // OnRequestRemoval is a callback which is called after fallback transaction is removed
// from the notary payload pool due to expiration, main tx appliance or any other reason. // from the notary payload pool due to expiration, main tx appliance or any other reason.
func (n *Notary) OnRequestRemoval(pld *payload.P2PNotaryRequest) { func (n *Notary) OnRequestRemoval(pld *payload.P2PNotaryRequest) {