From a6c3265dab6f0919030c7e67034ff808113b0ac7 Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Fri, 22 Sep 2023 18:04:09 +0400 Subject: [PATCH 1/2] script: add redundant tests to math ops. --- test/data/script-tests.json | 12 ++ test/script-test.js | 377 ++++++++++++++++++++++++++++++++++++ 2 files changed, 389 insertions(+) diff --git a/test/data/script-tests.json b/test/data/script-tests.json index 6b7115bc1..89fab98e5 100644 --- a/test/data/script-tests.json +++ b/test/data/script-tests.json @@ -10735,6 +10735,18 @@ "flags": [], "result": "NEGATIVE_LOCKTIME" }, + { + "comments": "CHECKLOCKTIMEVERIFY automatically fails if stack top is negative", + "script": "OP_CHECKLOCKTIMEVERIFY", + "value": 0, + "witness": [ + "81" + ], + "locktime": 0, + "sequence": 4294967295, + "flags": [], + "result": "NEGATIVE_LOCKTIME" + }, { "comments": "CSV fails if stack top is not minimally encoded", "script": "OP_CHECKSEQUENCEVERIFY", diff --git a/test/script-test.js b/test/script-test.js index 65853eb1d..9f436b143 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -8,6 +8,7 @@ const Stack = require('../lib/script/stack'); const Opcode = require('../lib/script/opcode'); const TX = require('../lib/primitives/tx'); const consensus = require('../lib/protocol/consensus'); +const ScriptNum = require('../lib/script/scriptnum'); const scripts = require('./data/script-tests.json'); @@ -328,4 +329,380 @@ describe('Script', function() { assert.ifError(err); }); } + + describe('ScriptNum', function () { + // Src: https://github.com/bitcoin/bitcoin/blob/ff564c75e751db6cfaf2a5f1b8a3b471f510976f/test/functional/test_framework/script.py#L750-L769 + const sn2bytesVector = [ + [0, []], + [1, [0x01]], + [-1, [0x81]], + [0x7F, [0x7F]], + [-0x7F, [0xFF]], + [0x80, [0x80, 0x00]], + [-0x80, [0x80, 0x80]], + [0xFF, [0xFF, 0x00]], + [-0xFF, [0xFF, 0x80]], + [0x100, [0x00, 0x01]], + [-0x100, [0x00, 0x81]], + [0x7FFF, [0xFF, 0x7F]], + [-0x8000, [0x00, 0x80, 0x80]], + [-0x7FFFFF, [0xFF, 0xFF, 0xFF]], + [0x80000000, [0x00, 0x00, 0x00, 0x80, 0x00]], + [-0x80000000, [0x00, 0x00, 0x00, 0x80, 0x80]], + [0xFFFFFFFF, [0xFF, 0xFF, 0xFF, 0xFF, 0x00]], + [123456789, [0x15, 0xCD, 0x5B, 0x07]], + [-54321, [0x31, 0xD4, 0x80]] + ]; + + // Src: https://github.com/bitcoin/bitcoin/blob/ff564c75e751db6cfaf2a5f1b8a3b471f510976f/test/functional/test_framework/script.py#L771-L775 + const serializationVector = [ + 0, 1, -1, -2, 127, 128, -255, 256, (1 << 15) - 1, -(1 << 16), + (1 << 24) - 1, (1 << 31), 1500, -1500 + ]; + + it('should serialize script numbers correctly', () => { + for (const [num, bytes] of sn2bytesVector) { + const sn = ScriptNum.fromNumber(num); + const numBytes = sn.encode(); + const testBuffer = Buffer.from(bytes); + + assert.bufferEqual(numBytes, testBuffer); + } + }); + + it('should serialize/deserialize script numbers correctly', () => { + for (const num of serializationVector) { + const encoded = ScriptNum.fromNumber(num).encode(); + const final = ScriptNum.decode(encoded).toNumber(); + + assert.strictEqual(num, final); + } + }); + }); + + describe('Script - mathops', function () { + const execOK = (scriptStr) => { + const stack = new Stack(); + + let err; + try { + const script = Script.fromString(scriptStr); + script.execute(stack); + } catch (e) { + err = e; + } + + assert(!err, `${scriptStr} ${err}`); + assert(isSuccess(stack), `${scriptStr}`); + }; + + it('should OP_1ADD', () => { + const add1Vectors = [ + '1 OP_1ADD 2 equal', + '0 OP_1ADD 1 equal', + '-1 OP_1ADD 0 equal', + '-2147483647 OP_1ADD -2147483646 equal', + '2147483647 OP_1ADD 2147483648 equal' + ]; + + for (const script of add1Vectors) + execOK(script); + }); + + it('should OP_1SUB', () => { + const sub1Vectors = [ + '1 OP_1SUB 0 equal', + '0 OP_1SUB -1 equal', + '-1 OP_1SUB -2 equal', + '-2147483647 OP_1SUB -2147483648 equal', + '2147483647 OP_1SUB 2147483646 equal' + ]; + + for (const script of sub1Vectors) + execOK(script); + }); + + it('should OP_NEGATE', () => { + const negateVectors = [ + '0 OP_NEGATE 0 equal', + '1 OP_NEGATE -1 equal', + '-1 OP_NEGATE 1 equal', + '-2147483647 OP_NEGATE 2147483647 equal', + '2147483647 OP_NEGATE -2147483647 equal' + ]; + + for (const script of negateVectors) + execOK(script); + }); + + it('should OP_ABS', () => { + const absVectors = [ + '0 OP_ABS 0 equal', + '1 OP_ABS 1 equal', + '-1 OP_ABS 1 equal', + '-2147483647 OP_ABS 2147483647 equal', + '2147483647 OP_ABS 2147483647 equal' + ]; + + for (const script of absVectors) + execOK(script); + }); + + it('should OP_NOT', () => { + const notVectors = [ + '0 OP_NOT 1 equal', + '1 OP_NOT 0 equal', + '-1 OP_NOT 0 equal', + '-2147483647 OP_NOT 0 equal', + '2147483647 OP_NOT 0 equal' + ]; + + for (const script of notVectors) + execOK(script); + }); + + it('should OP_0NOTEQUAL', () => { + const notVectors = [ + '0 OP_0NOTEQUAL 0 equal', + '1 OP_0NOTEQUAL 1 equal', + '-1 OP_0NOTEQUAL 1 equal', + '-2147483647 OP_0NOTEQUAL 1 equal', + '2147483647 OP_0NOTEQUAL 1 equal' + ]; + + for (const script of notVectors) + execOK(script); + }); + + it('should OP_ADD', () => { + const addVectors = [ + '0 0 add 0 equal', + '0 1 add 1 equal', + '1 -1 add 0 equal', + '1 2 add 3 equal', + '-2147483647 2147483647 add 0 equal', + '-2147483647 -2147483647 add -4294967294 equal', + '2147483647 2147483647 add 4294967294 equal' + ]; + + for (const script of addVectors) + execOK(script); + }); + + it('should OP_SUB', () => { + const subVectors = [ + '0 0 sub 0 equal', + '0 1 sub -1 equal', + '1 -1 sub 2 equal', + '1 2 sub -1 equal', + '-2147483647 2147483647 sub -4294967294 equal', + '-2147483647 -2147483647 sub 0 equal', + '2147483647 2147483647 sub 0 equal', + '2147483647 -2147483647 sub 4294967294 equal' + ]; + + for (const script of subVectors) + execOK(script); + }); + + it('should OP_BOOLAND', () => { + const boolandVectors = [ + '0 0 booland 0 equal', + '0 1 booland 0 equal', + '1 -1 booland 1 equal', + '1 2 booland 1 equal', + '-2147483647 2147483647 booland 1 equal', + '-2147483647 -2147483647 booland 1 equal', + '2147483647 2147483647 booland 1 equal', + '2147483647 -2147483647 booland 1 equal', + '2147483647 0 booland 0 equal', + '0 2147483647 booland 0 equal' + ]; + + for (const script of boolandVectors) + execOK(script); + }); + + it('should OP_BOOLOR', () => { + const boolorVectors = [ + '0 0 boolor 0 equal', + '0 1 boolor 1 equal', + '1 -1 boolor 1 equal', + '1 2 boolor 1 equal', + '-2147483647 2147483647 boolor 1 equal', + '-2147483647 -2147483647 boolor 1 equal', + '2147483647 2147483647 boolor 1 equal', + '2147483647 -2147483647 boolor 1 equal', + '2147483647 0 boolor 1 equal', + '0 2147483647 boolor 1 equal' + ]; + + for (const script of boolorVectors) + execOK(script); + }); + + it('should OP_NUMEQUAL', () => { + const numequalVectors = [ + '0 0 numequal 1 equal', + '0 1 numequal 0 equal', + '1 -1 numequal 0 equal', + '1 2 numequal 0 equal', + '-2147483647 2147483647 numequal 0 equal', + '-2147483647 -2147483647 numequal 1 equal', + '2147483647 2147483647 numequal 1 equal', + '2147483647 -2147483647 numequal 0 equal', + '2147483647 0 numequal 0 equal', + '0 2147483647 numequal 0 equal' + ]; + + for (const script of numequalVectors) + execOK(script); + }); + + it('should OP_NUMNOTEQUAL', () => { + const numnotequalVectors = [ + '0 0 numnotequal 0 equal', + '0 1 numnotequal 1 equal', + '1 -1 numnotequal 1 equal', + '1 2 numnotequal 1 equal', + '-2147483647 2147483647 numnotequal 1 equal', + '-2147483647 -2147483647 numnotequal 0 equal', + '2147483647 2147483647 numnotequal 0 equal', + '2147483647 -2147483647 numnotequal 1 equal', + '2147483647 0 numnotequal 1 equal', + '0 2147483647 numnotequal 1 equal' + ]; + + for (const script of numnotequalVectors) + execOK(script); + }); + + it('should OP_LESSTHAN', () => { + const lessthanVectors = [ + '0 0 lessthan 0 equal', + '0 1 lessthan 1 equal', + '1 -1 lessthan 0 equal', + '1 2 lessthan 1 equal', + '-2147483647 2147483647 lessthan 1 equal', + '-2147483647 -2147483647 lessthan 0 equal', + '2147483647 2147483647 lessthan 0 equal', + '2147483647 -2147483647 lessthan 0 equal', + '2147483647 0 lessthan 0 equal', + '0 2147483647 lessthan 1 equal' + ]; + + for (const script of lessthanVectors) + execOK(script); + }); + + it('should OP_GREATERTHAN', () => { + const greaterthanVectors = [ + '0 0 greaterthan 0 equal', + '0 1 greaterthan 0 equal', + '1 -1 greaterthan 1 equal', + '1 2 greaterthan 0 equal', + '-2147483647 2147483647 greaterthan 0 equal', + '-2147483647 -2147483647 greaterthan 0 equal', + '2147483647 2147483647 greaterthan 0 equal', + '2147483647 -2147483647 greaterthan 1 equal', + '2147483647 0 greaterthan 1 equal', + '0 2147483647 greaterthan 0 equal' + ]; + + for (const script of greaterthanVectors) + execOK(script); + }); + + it('should OP_LESSTHANOREQUAL', () => { + const lessthanorequalVectors = [ + '0 0 lessthanorequal 1 equal', + '0 1 lessthanorequal 1 equal', + '1 -1 lessthanorequal 0 equal', + '1 2 lessthanorequal 1 equal', + '-2147483647 2147483647 lessthanorequal 1 equal', + '-2147483647 -2147483647 lessthanorequal 1 equal', + '2147483647 2147483647 lessthanorequal 1 equal', + '2147483647 -2147483647 lessthanorequal 0 equal', + '2147483647 0 lessthanorequal 0 equal', + '0 2147483647 lessthanorequal 1 equal' + ]; + + for (const script of lessthanorequalVectors) + execOK(script); + }); + + it('should OP_GREATERTHANOREQUAL', () => { + const greaterthanorequalVectors = [ + '0 0 greaterthanorequal 1 equal', + '0 1 greaterthanorequal 0 equal', + '1 -1 greaterthanorequal 1 equal', + '1 2 greaterthanorequal 0 equal', + '-2147483647 2147483647 greaterthanorequal 0 equal', + '-2147483647 -2147483647 greaterthanorequal 1 equal', + '2147483647 2147483647 greaterthanorequal 1 equal', + '2147483647 -2147483647 greaterthanorequal 1 equal', + '2147483647 0 greaterthanorequal 1 equal', + '0 2147483647 greaterthanorequal 0 equal' + ]; + + for (const script of greaterthanorequalVectors) + execOK(script); + }); + + it('should OP_MIN', () => { + const minVectors = [ + '0 0 min 0 equal', + '0 1 min 0 equal', + '1 -1 min -1 equal', + '1 2 min 1 equal', + '-2147483647 2147483647 min -2147483647 equal', + '-2147483647 -2147483647 min -2147483647 equal', + '2147483647 2147483647 min 2147483647 equal', + '2147483647 -2147483647 min -2147483647 equal', + '2147483647 0 min 0 equal', + '0 2147483647 min 0 equal' + ]; + + for (const script of minVectors) + execOK(script); + }); + + it('should OP_MAX', () => { + const maxVectors = [ + '0 0 max 0 equal', + '0 1 max 1 equal', + '1 -1 max 1 equal', + '1 2 max 2 equal', + '-2147483647 0 max 0 equal', + '-2147483647 2147483647 max 2147483647 equal', + '-2147483647 -2147483647 max -2147483647 equal', + '2147483647 2147483647 max 2147483647 equal', + '2147483647 -2147483647 max 2147483647 equal', + '2147483647 0 max 2147483647 equal', + '0 2147483647 max 2147483647 equal' + ]; + + for (const script of maxVectors) + execOK(script); + }); + + it('should OP_WITHIN', () => { + const withinVectors = [ + '0 -1 1 within 1 equal', + '0 0 1 within 1 equal', + '0 -2147483647 2147483647 within 1 equal', + '-2147483647 -2147483647 2147483647 within 1 equal', + '2147483646 -2147483647 2147483647 within 1 equal', + '2147483647 -2147483647 2147483647 within 0 equal', + '0 -2147483647 -2147483647 within 0 equal', + '0 2147483647 2147483647 within 0 equal', + '0 2147483647 -2147483647 within 0 equal', + '0 2147483647 0 within 0 equal', + '0 0 2147483647 within 1 equal' + ]; + + for (const script of withinVectors) + execOK(script); + }); + }); }); From 8e44e7e116d41a7d949335859c4531f0d99462a2 Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Sun, 24 Sep 2023 12:14:24 +0400 Subject: [PATCH 2/2] test: adjust getwork timeouts. --- test/getwork-test.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/getwork-test.js b/test/getwork-test.js index aaf4a9d04..f80657fc5 100644 --- a/test/getwork-test.js +++ b/test/getwork-test.js @@ -22,8 +22,10 @@ const {wdb} = node.require('walletdb'); let wallet = null; +const TIMEOUT = 45000; + describe('Get Work', function() { - this.timeout(45000); + this.timeout(TIMEOUT); it('should open chain and miner', async () => { await node.open(); @@ -36,13 +38,15 @@ describe('Get Work', function() { }); it('should mine 10 blocks', async () => { - const connectEvents = forEvent(chain, 'connect', 10, 10000); + const DELAY = 500; + const waits = DELAY * 10; + const connectEvents = forEvent(chain, 'connect', 10, TIMEOUT - waits); for (let i = 0; i < 10; i++) { const block = await miner.mineBlock(); assert(block); await chain.add(block); // lower mtp. - await sleep(500); + await sleep(DELAY); } await connectEvents;