Skip to content

Commit 2743df3

Browse files
notary: refactor Copy() of transaction.Transaction
Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
1 parent 142631d commit 2743df3

File tree

1 file changed

+2
-17
lines changed

1 file changed

+2
-17
lines changed

pkg/services/notary/notary.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (n *Notary) OnNewRequest(payload *payload.P2PNotaryRequest) {
272272
// Avoid changes in the main transaction witnesses got from the notary request pool to
273273
// keep the pooled tx valid. We will update its copy => the copy's size will be changed.
274274
r = &request{
275-
main: safeCopy(payload.MainTransaction),
275+
main: payload.MainTransaction.Copy(),
276276
minNotValidBefore: nvbFallback,
277277
}
278278
n.requests[payload.MainTransaction.Hash()] = r
@@ -285,7 +285,7 @@ func (n *Notary) OnNewRequest(payload *payload.P2PNotaryRequest) {
285285
// size won't be changed after finalisation, the witness bytes changes may
286286
// affect the other users of notary pool and cause race. Avoid this by making
287287
// the copy.
288-
r.fallbacks = append(r.fallbacks, safeCopy(payload.FallbackTransaction))
288+
r.fallbacks = append(r.fallbacks, payload.FallbackTransaction.Copy())
289289
if exists && r.isMainCompleted() || validationErr != nil {
290290
return
291291
}
@@ -345,21 +345,6 @@ func (n *Notary) OnNewRequest(payload *payload.P2PNotaryRequest) {
345345
}
346346
}
347347

348-
// safeCopy creates a copy of provided transaction by dereferencing it and creating
349-
// fresh witnesses so that the tx's witnesses may be modified without affecting the
350-
// copy's ones.
351-
func safeCopy(tx *transaction.Transaction) *transaction.Transaction {
352-
cp := *tx
353-
cp.Scripts = make([]transaction.Witness, len(tx.Scripts))
354-
for i := range cp.Scripts {
355-
cp.Scripts[i] = transaction.Witness{
356-
InvocationScript: bytes.Clone(tx.Scripts[i].InvocationScript),
357-
VerificationScript: bytes.Clone(tx.Scripts[i].VerificationScript),
358-
}
359-
}
360-
return &cp
361-
}
362-
363348
// OnRequestRemoval is a callback which is called after fallback transaction is removed
364349
// from the notary payload pool due to expiration, main tx appliance or any other reason.
365350
func (n *Notary) OnRequestRemoval(pld *payload.P2PNotaryRequest) {

0 commit comments

Comments
 (0)