Skip to content

Commit

Permalink
Engine: Fix hasScriptFailed() behaviour
Browse files Browse the repository at this point in the history
docs say;

The script is considered sucessfully executed only if its stack
contains exactly one item, and that item being `TrueValue`.

Which was what I thought the implementation did too. But the
implementation only checked if stack had a TrueValue on the top
of the stack, ignoring if it had more data.
  • Loading branch information
omerfirmak authored and mkykadir committed Apr 21, 2022
1 parent 26b7df8 commit a00e567
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions source/agora/script/Engine.d
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ public class Engine
private bool hasScriptFailed (/*in*/ ref Stack stack) // peek() is not const
pure nothrow @safe
{
return stack.empty() || stack.peek() != TrueValue;
return stack.count() != 1 || stack.peek() != TrueValue;
}

/***************************************************************************
Expand Down Expand Up @@ -1580,7 +1580,7 @@ unittest
// 3 of 5: ok when sigs are in the same order
assert(engine.execute(
Lock(LockType.Script,
[ubyte(OP.PUSH_NUM_2)] // number of sigs
[ubyte(OP.PUSH_NUM_3)] // number of sigs
~ [ubyte(32)] ~ kp1.address[]
~ [ubyte(32)] ~ kp2.address[]
~ [ubyte(32)] ~ kp3.address[]
Expand All @@ -1596,7 +1596,7 @@ unittest
// 3 of 5: fails when sigs are in the wrong order
assert(engine.execute(
Lock(LockType.Script,
[ubyte(OP.PUSH_NUM_2)] // number of sigs
[ubyte(OP.PUSH_NUM_3)] // number of sigs
~ [ubyte(32)] ~ kp1.address[]
~ [ubyte(32)] ~ kp2.address[]
~ [ubyte(32)] ~ kp3.address[]
Expand Down Expand Up @@ -1648,7 +1648,7 @@ unittest
// ditto but with VERIFY_MULTI_SIG
assert(engine.execute(
Lock(LockType.Script,
[ubyte(OP.PUSH_NUM_2)] // number of sigs
[ubyte(OP.PUSH_NUM_3)] // number of sigs
~ [ubyte(32)] ~ kp1.address[]
~ [ubyte(32)] ~ kp2.address[]
~ [ubyte(32)] ~ kp3.address[]
Expand Down Expand Up @@ -2035,7 +2035,7 @@ unittest
ubyte(42).repeat(65).array.toPushOpcode
~ ubyte(42).repeat(65).array.toPushOpcode
~ [ubyte(OP.CHECK_EQUAL)]),
Unlock(unlock[]), tx, Input.init)
Unlock([]), tx, Input.init)
is null);

Script bad_key_unlock = createUnlockP2PKH(sig.signature, sig.sig_hash, KeyPair.random.address);
Expand Down Expand Up @@ -2180,7 +2180,7 @@ unittest
const tx = Transaction([Input.init], [Output.init]);
const StackMaxItemSize = 512;
assert(engine.execute(
Lock(LockType.Script, [ubyte(1), ubyte(42)] ~ [ubyte(OP.TRUE)]),
Lock(LockType.Script, [ubyte(1), ubyte(42)] ~ [ubyte(OP.DUP)] ~ [ubyte(OP.CHECK_EQUAL)]),
Unlock.init, tx, Input.init)
is null);

Expand Down Expand Up @@ -2252,7 +2252,7 @@ unittest
// will fit, pops TestStackMaxItemSize and pushes 64 bytes
assert(engine.execute(
Lock(LockType.Script, MaxItemPush.repeat(MaxPushes).joiner.array
~ [ubyte(OP.HASH), ubyte(OP.TRUE)]),
~ ubyte(OP.VERIFY_EQUAL).repeat(MaxPushes / 2).array ~ [ubyte(OP.TRUE)]),
Unlock.init, tx, Input.init)
is null);

Expand Down

0 comments on commit a00e567

Please sign in to comment.