Skip to content

Commit 118d076

Browse files
committed
Merge remote-tracking branch 'origin/fix/optimize-inputs'
2 parents 76cfdeb + 4dcba94 commit 118d076

File tree

13 files changed

+124
-57
lines changed

13 files changed

+124
-57
lines changed

script/interpreter/operations.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,9 +2016,9 @@ func opcodeCheckSig(op *ParsedOpcode, t *thread) error {
20162016
return err
20172017
}
20182018

2019-
txCopy := t.tx.Clone()
2020-
txCopy.Inputs[t.inputIdx].SourceTransaction.Outputs[txCopy.Inputs[t.inputIdx].SourceTxOutIndex].LockingScript = up
2021-
// txCopy.Inputs[t.inputIdx].SourceTxScript = up
2019+
txCopy := t.tx.ShallowClone()
2020+
sourceTxOut := txCopy.Inputs[t.inputIdx].SourceTxOutput()
2021+
sourceTxOut.LockingScript = up
20222022

20232023
hash, err = txCopy.CalcInputSignatureHash(uint32(t.inputIdx), shf)
20242024
if err != nil {
@@ -2282,9 +2282,12 @@ func opcodeCheckMultiSig(op *ParsedOpcode, t *thread) error {
22822282
}
22832283

22842284
// Generate the signature hash based on the signature hash type.
2285-
txCopy := t.tx.Clone()
2285+
txCopy := t.tx.ShallowClone()
22862286
input := txCopy.Inputs[t.inputIdx]
2287-
input.SourceTransaction.Outputs[input.SourceTxOutIndex].LockingScript = up
2287+
sourceOut := input.SourceTxOutput()
2288+
if sourceOut != nil {
2289+
sourceOut.LockingScript = up
2290+
}
22882291

22892292
signatureHash, err := txCopy.CalcInputSignatureHash(uint32(t.inputIdx), shf)
22902293
if err != nil {

script/interpreter/reference_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func createSpendingTx(sigScript, pkScript *script.Script, outputValue int64) *tr
323323
LockingScript: script.NewFromBytes([]byte{}),
324324
}},
325325
}
326-
spendingTx.Inputs[0].SetSourceTxFromOutput(&transaction.TransactionOutput{
326+
spendingTx.Inputs[0].SetSourceTxOutput(&transaction.TransactionOutput{
327327
LockingScript: pkScript,
328328
})
329329

@@ -612,7 +612,7 @@ testloop:
612612

613613
for k, txin := range tx.Inputs {
614614
prevOut, ok := prevOuts[txIOKey{id: txin.SourceTXID.String(), idx: txin.SourceTxOutIndex}]
615-
txin.SetSourceTxFromOutput(prevOut)
615+
txin.SetSourceTxOutput(prevOut)
616616
if !ok {
617617
t.Errorf("bad test (missing %dth input) %d:%v",
618618
k, i, test)
@@ -762,7 +762,7 @@ testloop:
762762

763763
for k, txin := range tx.Inputs {
764764
prevOut, ok := prevOuts[txIOKey{id: txin.SourceTXID.String(), idx: txin.SourceTxOutIndex}]
765-
txin.SetSourceTxFromOutput(prevOut)
765+
txin.SetSourceTxOutput(prevOut)
766766
if !ok {
767767
t.Errorf("bad test (missing %dth input) %d:%v",
768768
k, i, test)

script/interpreter/thread.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func (t *thread) apply(opts *execOpts) error {
352352
t.astack = newStack(t.cfg, t.hasFlag(scriptflag.VerifyMinimalData))
353353

354354
if t.tx != nil {
355-
t.tx.Inputs[t.inputIdx].SetSourceTxFromOutput(&transaction.TransactionOutput{
355+
t.tx.Inputs[t.inputIdx].SetSourceTxOutput(&transaction.TransactionOutput{
356356
LockingScript: t.prevOutput.LockingScript,
357357
Satoshis: t.prevOutput.Satoshis,
358358
})

script/script.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ func (s *Script) AppendOpcodes(oo ...uint8) error {
127127
return nil
128128
}
129129

130+
// Bytes implements the Byte interface and returns the byte slice of script.
131+
func (s *Script) Bytes() []byte {
132+
return *s
133+
}
134+
130135
// String implements the stringer interface and returns the hex string of script.
131136
func (s *Script) String() string {
132137
return hex.EncodeToString(*s)

transaction/broadcaster/arc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (f *ArcResponse) Scan(value interface{}) error {
6767
func (a *Arc) Broadcast(t *transaction.Transaction) (*transaction.BroadcastSuccess, *transaction.BroadcastFailure) {
6868
var buf *bytes.Buffer
6969
for _, input := range t.Inputs {
70-
if input.SourceTransaction == nil {
70+
if input.SourceTxOutput() == nil {
7171
buf = bytes.NewBuffer(t.Bytes())
7272
break
7373
}

transaction/fees.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ func (tx *Transaction) Fee(f FeeModel, changeDistribution ChangeDistribution) er
2424
}
2525
satsIn := uint64(0)
2626
for _, i := range tx.Inputs {
27-
if i.SourceTransaction == nil {
27+
sourceSats := i.SourceTxSatoshis()
28+
if sourceSats == nil {
2829
return ErrEmptyPreviousTx
2930
}
30-
satsIn += *i.SourceTxSatoshis()
31+
satsIn += *sourceSats
3132
}
3233
satsOut := uint64(0)
3334
changeOuts := uint64(0)

transaction/input.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,31 @@ type TransactionInput struct {
3535
SourceTxOutIndex uint32
3636
SequenceNumber uint32
3737
SourceTransaction *Transaction
38+
sourceOutput *TransactionOutput
3839
UnlockingScriptTemplate UnlockingScriptTemplate
3940
}
4041

42+
func (i *TransactionInput) SourceTxOutput() *TransactionOutput {
43+
if i.SourceTransaction != nil {
44+
return i.SourceTransaction.Outputs[i.SourceTxOutIndex]
45+
}
46+
return i.sourceOutput
47+
}
48+
4149
func (i *TransactionInput) SourceTxScript() *script.Script {
42-
if i.SourceTransaction == nil {
43-
return nil
50+
sourceTxOut := i.SourceTxOutput()
51+
if sourceTxOut != nil {
52+
return sourceTxOut.LockingScript
4453
}
45-
return i.SourceTransaction.Outputs[i.SourceTxOutIndex].LockingScript
54+
return nil
4655
}
4756

4857
func (i *TransactionInput) SourceTxSatoshis() *uint64 {
49-
if i.SourceTransaction == nil {
50-
return nil
58+
sourceTxOut := i.SourceTxOutput()
59+
if sourceTxOut != nil {
60+
return &sourceTxOut.Satoshis
5161
}
52-
return &i.SourceTransaction.Outputs[i.SourceTxOutIndex].Satoshis
62+
return nil
5363
}
5464

5565
// ReadFrom reads from the `io.Reader` into the `transaction.TransactionInput`.
@@ -132,7 +142,7 @@ func (i *TransactionInput) readFrom(r io.Reader, extended bool) (int64, error) {
132142
return bytesRead, errors.Wrapf(err, "script(%d): got %d bytes", scriptLen.Length(), n)
133143
}
134144

135-
i.SetSourceTxFromOutput(&TransactionOutput{
145+
i.SetSourceTxOutput(&TransactionOutput{
136146
Satoshis: binary.LittleEndian.Uint64(prevSatoshis),
137147
LockingScript: script.NewFromBytes(scriptBytes),
138148
})
@@ -179,9 +189,6 @@ func (i *TransactionInput) Bytes(clear bool) []byte {
179189
return append(h, util.LittleEndianBytes(i.SequenceNumber, 4)...)
180190
}
181191

182-
func (i *TransactionInput) SetSourceTxFromOutput(txo *TransactionOutput) {
183-
prevTx := &Transaction{}
184-
prevTx.Outputs = make([]*TransactionOutput, i.SourceTxOutIndex+1)
185-
prevTx.Outputs[i.SourceTxOutIndex] = txo
186-
i.SourceTransaction = prevTx
192+
func (i *TransactionInput) SetSourceTxOutput(txo *TransactionOutput) {
193+
i.sourceOutput = txo
187194
}

transaction/signaturehash.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (tx *Transaction) CalcInputPreimage(inputNumber uint32, sigHashFlag sighash
6767
if len(in.SourceTXID) == 0 {
6868
return nil, ErrEmptyPreviousTxID
6969
}
70-
if in.SourceTransaction == nil {
70+
if in.SourceTxOutput() == nil {
7171
return nil, ErrEmptyPreviousTx
7272
}
7373

@@ -95,7 +95,7 @@ func (tx *Transaction) CalcInputPreimage(inputNumber uint32, sigHashFlag sighash
9595
hashOutputs = tx.OutputsHash(int32(inputNumber))
9696
}
9797

98-
buf := make([]byte, 0)
98+
buf := make([]byte, 0, 256)
9999

100100
// Version
101101
v := make([]byte, 4)
@@ -160,7 +160,7 @@ func (tx *Transaction) CalcInputPreimageLegacy(inputNumber uint32, shf sighash.F
160160
if len(in.SourceTXID) == 0 {
161161
return nil, ErrEmptyPreviousTxID
162162
}
163-
if in.SourceTransaction == nil {
163+
if in.SourceTxOutput() == nil {
164164
return nil, ErrEmptyPreviousTx
165165
}
166166

@@ -188,14 +188,14 @@ func (tx *Transaction) CalcInputPreimageLegacy(inputNumber uint32, shf sighash.F
188188
return defaultHex, nil
189189
}
190190

191-
txCopy := tx.Clone()
191+
txCopy := tx.ShallowClone()
192192

193193
for i := range txCopy.Inputs {
194194
if i == int(inputNumber) {
195-
txCopy.Inputs[i].SourceTransaction = tx.Inputs[inputNumber].SourceTransaction
195+
txCopy.Inputs[i].sourceOutput = in.SourceTxOutput()
196196
} else {
197197
txCopy.Inputs[i].UnlockingScript = &script.Script{}
198-
txCopy.Inputs[i].SetSourceTxFromOutput(&TransactionOutput{})
198+
txCopy.Inputs[i].sourceOutput = &TransactionOutput{}
199199
}
200200
}
201201

transaction/signaturehash_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestTx_CalcInputPreimage(t *testing.T) {
6363
// Add the UTXO amount and script (PreviousTx already in unsigned tx)
6464
prevScript, err := script.NewFromHex(test.SourceTxScript)
6565
require.NoError(t, err)
66-
tx.InputIdx(test.index).SetSourceTxFromOutput(&transaction.TransactionOutput{
66+
tx.InputIdx(test.index).SetSourceTxOutput(&transaction.TransactionOutput{
6767
LockingScript: prevScript,
6868
Satoshis: test.previousTxSatoshis,
6969
})
@@ -156,7 +156,7 @@ func TestTx_CalcInputSignatureHash(t *testing.T) {
156156
// Add the UTXO amount and script (PreviousTx already in unsigned tx)
157157
prevScript, err := script.NewFromHex(test.SourceTxScript)
158158
require.NoError(t, err)
159-
tx.Inputs[test.index].SetSourceTxFromOutput(&transaction.TransactionOutput{
159+
tx.Inputs[test.index].SetSourceTxOutput(&transaction.TransactionOutput{
160160
LockingScript: prevScript,
161161
Satoshis: test.previousTxSatoshis,
162162
})
@@ -222,7 +222,7 @@ func TestTx_CalcInputPreimageLegacy(t *testing.T) {
222222
// Add the UTXO amount and script (PreviousTx already in unsigned tx)
223223
prevScript, err := script.NewFromHex(test.SourceTxScript)
224224
require.NoError(t, err)
225-
tx.Inputs[test.index].SetSourceTxFromOutput(&transaction.TransactionOutput{
225+
tx.Inputs[test.index].SetSourceTxOutput(&transaction.TransactionOutput{
226226
LockingScript: prevScript,
227227
Satoshis: test.previousTxSatoshis,
228228
})

transaction/template/p2pkh/p2pkh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type P2PKH struct {
4747
}
4848

4949
func (p *P2PKH) Sign(tx *transaction.Transaction, inputIndex uint32) (*script.Script, error) {
50-
if tx.Inputs[inputIndex].SourceTransaction == nil {
50+
if tx.Inputs[inputIndex].SourceTxOutput() == nil {
5151
return nil, transaction.ErrEmptyPreviousTx
5252
}
5353

0 commit comments

Comments
 (0)