Skip to content

Commit

Permalink
BoC deserialization fixes
Browse files Browse the repository at this point in the history
- Performance improvements
  • Loading branch information
romanzaycev committed Jun 23, 2024
1 parent 136df6b commit 96a95ca
Show file tree
Hide file tree
Showing 21 changed files with 5,038 additions and 138 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"require": {
"php": ">=8.1",
"ext-mbstring": "*",
"olifanton/typed-arrays": "^1",
"olifanton/typed-arrays": "^1.0.1",
"myclabs/deep-copy": "^1.11",
"brick/math": "^0.10"
},
Expand Down
31 changes: 26 additions & 5 deletions src/Olifanton/Interop/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,25 @@ public function toString(?bool $isUserFriendly = null,
}

$addr = new Uint8Array(34);
$addr[0] = $tag;
$addr[1] = $this->wc;
$addr->set($this->hashPart, 2);
$addr->fSet(0, $tag);
$addr->fSet(1, $this->wc);

for ($i = 2; $i < $addr->length; $i++) {
$addr->fSet($i, $this->hashPart->fGet($i - 2));
}

$addressWithChecksum = new Uint8Array(36);
$addressWithChecksum->set($addr);
$addressWithChecksum->set(Checksum::crc16($addr), 34);

for ($i = 0; $i < $addr->length; $i++) {
$addressWithChecksum->fSet($i, $addr->fGet($i));
}

$crc16 = Checksum::crc16($addr);

for ($i = 0; $i < $crc16->length; $i++) {
$addressWithChecksum->fSet($i + 34, $crc16->fGet($i));
}

$addressBase64 = base64_encode(Bytes::arrayToBytes($addressWithChecksum));

if ($isUrlSafe) {
Expand Down Expand Up @@ -209,6 +221,15 @@ public function isUrlSafe(): bool
return $this->isUrlSafe;
}

public function isEqual(Address|string $other): bool
{
if (is_string($other)) {
$other = new Address($other);
}

return Bytes::compareBytes($this->hashPart, $other->hashPart);
}

public function __toString(): string
{
return $this->toString();
Expand Down
34 changes: 23 additions & 11 deletions src/Olifanton/Interop/Boc/BitString.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ class BitString implements \Stringable
public function __construct(int $length)
{
$this->length = $length;
$this->array = new Uint8Array(array_fill(
0,
self::getUint8ArrayLength($length),
0
));
$this->array = new Uint8Array(self::getUint8ArrayLength($length));
}

public static function empty(): self
Expand Down Expand Up @@ -82,7 +78,7 @@ public function get(int $n): bool
{
$this->checkRange($n);

return ($this->array[(int)($n / 8) | 0] & (1 << (7 - ($n % 8)))) > 0;
return ($this->array->fGet((int)($n / 8) | 0) & (1 << (7 - ($n % 8)))) > 0;
}

/**
Expand All @@ -93,7 +89,9 @@ public function get(int $n): bool
public function on(int $n): void
{
$this->checkRange($n);
$this->array[(int)($n / 8) | 0] |= 1 << (7 - ($n % 8));
$key = (int)($n / 8) | 0;
$cV = $this->array->fGet($key);
$this->array->fSet($key, $cV | 1 << (7 - ($n % 8)));
$this->invalidateCell();
}

Expand All @@ -105,7 +103,9 @@ public function on(int $n): void
public function off(int $n): void
{
$this->checkRange($n);
$this->array[(int)($n / 8) | 0] &= ~(1 << (7 - ($n % 8)));
$key = (int)($n / 8) | 0;
$cV = $this->array->fGet($key);
$this->array->fSet($key, $cV & ~(1 << (7 - ($n % 8))));
$this->invalidateCell();
}

Expand All @@ -117,7 +117,9 @@ public function off(int $n): void
public function toggle(int $n): void
{
$this->checkRange($n);
$this->array[(int)($n / 8) | 0] ^= 1 << (7 - ($n % 8));
$key = (int)($n / 8) | 0;
$cV = $this->array->fGet($key);
$this->array->fSet($key, $cV ^ 1 << (7 - ($n % 8)));
$this->invalidateCell();
}

Expand Down Expand Up @@ -281,7 +283,14 @@ public function writeBytes(Uint8Array $ui8): self
*/
public function writeString(string $value): self
{
return $this->writeBytes(new Uint8Array(array_values(unpack('C*', $value))));
$values = array_values(unpack('C*', $value));
$arr = new Uint8Array(count($values));

foreach ($values as $i => $v) {
$arr->fSet($i, $v);
}

return $this->writeBytes($arr);
}

/**
Expand Down Expand Up @@ -543,7 +552,10 @@ private static function incLength(BitString $bitString, int $newLength): BitStri
$bitString->length = $newLength;
$tmpArr = $bitString->array;
$bitString->array = new Uint8Array(self::getUint8ArrayLength($newLength));
$bitString->array->set($tmpArr);

for ($i = 0; $i < $tmpArr->length; $i++) {
$bitString->array->fSet($i, $tmpArr->fGet($i));
}

return $bitString;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Olifanton/Interop/Boc/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function writeBitString(BitString $anotherBitString): self

public function writeRef(Cell $cell): self
{
if (count($cell->refs) === 4) {
if (count($this->cell->refs) === 4) { // @phpstan-ignore-line
throw new \RuntimeException("Refs overflow");
}

Expand Down
Loading

0 comments on commit 96a95ca

Please sign in to comment.