From 0d9fa87943e23013aa9d76162afc36ed4a676435 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Tue, 9 Jul 2019 16:21:00 +0700 Subject: [PATCH] Move nonWitnessUtxo cache tests out into own test --- test/fixtures/psbt.json | 9 ++++++++- test/psbt.js | 33 +++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/test/fixtures/psbt.json b/test/fixtures/psbt.json index 0525bf6..9e85b63 100644 --- a/test/fixtures/psbt.json +++ b/test/fixtures/psbt.json @@ -428,5 +428,12 @@ "transaction": "020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000", "result": "cHNidP8BAJoCAAAAAljoeiG1ba8MI76OcHBFbDNvfLqlyHV5JPVFiHuyq911AAAAAAD/////g40EJ9DsZQpoqka7CwmK6kQiwHGyyng1Kgd5WdB86h0BAAAAAP////8CcKrwCAAAAAAWABTYXCtx0AYLCcmIauuBXlCZHdoSTQDh9QUAAAAAFgAUAK6pouXw+HaliN9VRuh0LR2HAI8AAAAAAAAAAAA=" } - ] + ], + "cache": { + "nonWitnessUtxo": { + "psbt": "cHNidP8BAJoCAAAAAljoeiG1ba8MI76OcHBFbDNvfLqlyHV5JPVFiHuyq911AAAAAAD/////g40EJ9DsZQpoqka7CwmK6kQiwHGyyng1Kgd5WdB86h0BAAAAAP////8CcKrwCAAAAAAWABTYXCtx0AYLCcmIauuBXlCZHdoSTQDh9QUAAAAAFgAUAK6pouXw+HaliN9VRuh0LR2HAI8AAAAAAAEER1IhApWDvzmuCmCXR60Zmt3WNPphCFWdbFzTm0whg/GrluB/IQLath/0mhTban0CsM0fu3j8SxgxK1tOVNrk26L7/vU211KuIgYClYO/Oa4KYJdHrRma3dY0+mEIVZ1sXNObTCGD8auW4H8Q2QxqTwAAAIAAAACAAAAAgCIGAtq2H/SaFNtqfQKwzR+7ePxLGDErW05U2uTbovv+9TbXENkMak8AAACAAAAAgAEAAIAAAQEgAMLrCwAAAAAXqRS39fr0Dj1ApaRZsds1NfK3L6kh6IcBBCIAIIwjUxc3Q7WV37Sge3K6jkLjeX2nTof+fZ10l+OyAokDAQVHUiEDCJ3BDHrG21T5EymvYXMz2ziM6tDCMfcjN50bmQMLAtwhAjrdkE89bc9Z3bkGsN7iNSm3/7ntUOXoYVGSaGAiHw5zUq4iBgI63ZBPPW3PWd25BrDe4jUpt/+57VDl6GFRkmhgIh8OcxDZDGpPAAAAgAAAAIADAACAIgYDCJ3BDHrG21T5EymvYXMz2ziM6tDCMfcjN50bmQMLAtwQ2QxqTwAAAIAAAACAAgAAgAAiAgOppMN/WZbTqiXbrGtXCvBlA5RJKUJGCzVHU+2e7KWHcRDZDGpPAAAAgAAAAIAEAACAACICAn9jmXV9Lv9VoTatAsaEsYOLZVbl8bazQoKpS2tQBRCWENkMak8AAACAAAAAgAUAAIAA", + "nonWitnessUtxo": "Buffer.from('0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000', 'hex')", + "inputIndex": 0 + } + } } diff --git a/test/psbt.js b/test/psbt.js index 25bbfa9..ce92dc6 100644 --- a/test/psbt.js +++ b/test/psbt.js @@ -83,16 +83,6 @@ describe(`Psbt`, () => { arg.forEach(a => adder(i, a)) } else { adder(i, arg) - if (attr === 'nonWitnessUtxo') { - const first = psbt.inputs[i].nonWitnessUtxo - psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[i] = undefined - const second = psbt.inputs[i].nonWitnessUtxo - psbt.inputs[i].nonWitnessUtxo = Buffer.from([1,2,3]) - psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[i] = undefined - const third = psbt.inputs[i].nonWitnessUtxo - assert.ok(first.equals(second)) - assert.ok(first.equals(third)) - } } } } @@ -473,4 +463,27 @@ describe(`Psbt`, () => { assert.ok(psbt.__CACHE.__TX); }) }) + + describe('Cache', () => { + it('non-witness UTXOs are cached', () => { + const f = fixtures.cache.nonWitnessUtxo; + const psbt = Psbt.fromBase64(f.psbt) + const index = f.inputIndex; + + // Cache is empty + assert.strictEqual(psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index], undefined) + + // Cache is populated + psbt.addNonWitnessUtxoToInput(index, f.nonWitnessUtxo) + const value = psbt.inputs[index].nonWitnessUtxo + assert.ok(psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(value)) + assert.ok(psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(f.nonWitnessUtxo)) + + // Cache is rebuilt from internal transaction object when cleared + psbt.inputs[index].nonWitnessUtxo = Buffer.from([1,2,3]) + psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index] = undefined + assert.ok(psbt.inputs[index].nonWitnessUtxo.equals(value)) + }) + }) }) +