Skip to content

Commit

Permalink
Added missing ttCONTRACT and added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zgrguric committed Jan 22, 2024
1 parent 19373c6 commit 603e811
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/HookOn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
class HookOn
{
/**
* Default value if null, all triggers are ignored.
* All triggers are ignored.
* Used as basis for adding (triggering) triggers when encoding triggers to string.
*/
const DEFAULT = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff';
const DEFAULT_IGNORED = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff';

/**
* TT codes (https://github.com/Xahau/xahaud/blob/dev/hook/tts.h)
Expand All @@ -29,6 +30,7 @@ class HookOn
const ttNICKNAME_SET = 6; //deprecated
const ttOFFER_CREATE = 7;
const ttOFFER_CANCEL = 8;
const ttCONTRACT = 9; //deprecated
const ttTICKET_CREATE = 10;
const ttSPINAL_TAP = 11; //deprecated
const ttSIGNER_LIST_SET = 12;
Expand Down Expand Up @@ -73,6 +75,7 @@ class HookOn
6 => 'ttNICKNAME_SET', //deprecated
7 => 'ttOFFER_CREATE',
8 => 'ttOFFER_CANCEL',
9 => 'ttCONTRACT', //deprecated
10 => 'ttTICKET_CREATE',
11 => 'ttSPINAL_TAP', //deprecated
12 => 'ttSIGNER_LIST_SET',
Expand Down Expand Up @@ -155,7 +158,7 @@ public static function decode(string $hookonvalue, bool $throwOnInvalid = true):
*/
public static function encode(array $triggers = []): string
{
$hookon = self::normalize(self::DEFAULT);
$hookon = self::normalize(self::DEFAULT_IGNORED);
foreach($triggers as $trigger) {
$hookon = self::trigger($hookon,$trigger);
}
Expand Down
41 changes: 33 additions & 8 deletions tests/HookOnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,48 @@ public function testDecode()
$hookon = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
$triggered = HookOn::decode($hookon);
$this->assertEquals([22 => 'ttHOOK_SET'],$triggered);

$hookon = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
$triggered = HookOn::decode($hookon);
$this->assertEquals([22 => 'ttHOOK_SET'],$triggered);

$hookon = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF';
$triggered = HookOn::decode($hookon);
$this->assertEquals([22 => 'ttHOOK_SET'],$triggered);

$hookon = '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF';
$triggered = HookOn::decode($hookon);
$this->assertEquals([22 => 'ttHOOK_SET'],$triggered);

$hookon = '0XFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF';
$triggered = HookOn::decode($hookon);
$this->assertEquals([22 => 'ttHOOK_SET'],$triggered);

$hookon = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9ffff7';
$triggered = HookOn::decode($hookon);
$this->assertEquals([21 => 'ttACCOUNT_DELETE', 3 => 'ttACCOUNT_SET'],$triggered);


$this->assertEquals([3 => 'ttACCOUNT_SET', 21 => 'ttACCOUNT_DELETE'],$triggered);

//All 256 0-255 minus one (ttHOOK_SET) = 255
$hookon = '0x0000000000000000000000000000000000000000000000000000000000000000';
$triggered = HookOn::decode($hookon,false);
$this->assertEquals(255,count($triggered));

//All 256 0-255 minus one (ttHOOK_SET) = 255
$hookon = '0000000000000000000000000000000000000000000000000000000000000000';
$triggered = HookOn::decode($hookon,false);
$this->assertEquals(255,count($triggered));

//All 256 0-255 minus one (ttHOOK_SET) = 255
$hookon = '0X0000000000000000000000000000000000000000000000000000000000000000';
$triggered = HookOn::decode($hookon,false);
$this->assertEquals(255,count($triggered));

//All 256 0-255 minus two (ttHOOK_SET,ttESCROW_FINISH) = 254
$hookon = '0x0000000000000000000000000000000000000000000000000000000000000004';
$triggered = HookOn::decode($hookon,false);
$this->assertEquals(254,count($triggered));

//ALL (public) triggers
$hookon = '0xfffffffffffffffffffffffffffffffffffffff7fffffffffffc1fffffc00a40';
$triggered = HookOn::decode($hookon);
Expand All @@ -113,6 +134,8 @@ public function testDecode()
unset($expected[HookOn::ttUNL_REPORT]);
unset($expected[HookOn::ttNICKNAME_SET]);
unset($expected[HookOn::ttSPINAL_TAP]);
//Remove "public" contract (added later)
unset($expected[HookOn::ttCONTRACT]);
$this->assertEquals($expected,$triggered);

//ALL triggers except TRUST_SET
Expand All @@ -135,8 +158,10 @@ public function testDecode()
unset($expected[HookOn::ttUNL_REPORT]);
unset($expected[HookOn::ttNICKNAME_SET]);
unset($expected[HookOn::ttSPINAL_TAP]);
//Remove "public" trust set

//Remove "public" trust set and contract
unset($expected[HookOn::ttTRUST_SET]);
unset($expected[HookOn::ttCONTRACT]);
$this->assertEquals($expected,$triggered);
}

Expand All @@ -151,9 +176,9 @@ public function testDecodeInvalid()
{
$hookon = '0x000000000000000000000000000000000000000000000000000000003e3ff5be';
$decoded = HookOn::decode($hookon,false);

$this->assertEquals('ttPAYMENT', $decoded[0]);
$this->assertEquals(null, $decoded[9]);
$this->assertEquals('ttCONTRACT', $decoded[9]);
$this->assertEquals(null, $decoded[255]);
}

Expand Down

0 comments on commit 603e811

Please sign in to comment.