diff --git a/src/Rpc/HttpHandler.php b/src/Rpc/HttpHandler.php index f05f902..00f5f64 100644 --- a/src/Rpc/HttpHandler.php +++ b/src/Rpc/HttpHandler.php @@ -18,6 +18,10 @@ public function processCall(RpcRequest $params): RpcResponse { $curl = curl_init($this->url); + if ($curl === false) { + throw new \RuntimeException('Failed to initialize cURL'); + } + $headers = [ 'Accept: application/json', 'Content-type: application/json' @@ -34,8 +38,22 @@ public function processCall(RpcRequest $params): RpcResponse curl_setopt($curl, CURLOPT_POSTFIELDS, $params->toJson()); $rawResponse = curl_exec($curl); - curl_close($curl); - return RpcResponse::fromArray(json_decode($rawResponse, true)); + if ($rawResponse === false) { + $error = curl_error($curl); + throw new \RuntimeException('cURL request failed: ' . $error); + } + + if (!is_string($rawResponse)) { + throw new \RuntimeException('Unexpected response type from cURL'); + } + + $decoded = json_decode($rawResponse, true); + + if (!is_array($decoded)) { + throw new \RuntimeException('Invalid JSON response from RPC server: ' . $rawResponse); + } + + return RpcResponse::fromArray($decoded); } } diff --git a/src/Rpc/ResultTypes/InfoGetStatusResult.php b/src/Rpc/ResultTypes/InfoGetStatusResult.php index 5cc0cab..6dc9a78 100644 --- a/src/Rpc/ResultTypes/InfoGetStatusResult.php +++ b/src/Rpc/ResultTypes/InfoGetStatusResult.php @@ -17,7 +17,7 @@ class InfoGetStatusResult extends AbstractResult private string $chainSpecName; - private MinimalBlockInfo $lastAddedBlockInfo; + private ?MinimalBlockInfo $lastAddedBlockInfo; private ?NextUpgrade $nextUpgrade; @@ -46,23 +46,27 @@ class InfoGetStatusResult extends AbstractResult public static function fromJSON(array $json): self { + $lastAddedBlockInfo = isset($json['last_added_block_info']) + ? MinimalBlockInfoSerializer::fromJSON($json['last_added_block_info']) + : null; + return new self( $json, - $json['protocol_version'], - $json['build_version'], - $json['chainspec_name'], - MinimalBlockInfoSerializer::fromJSON($json['last_added_block_info']), - $json['next_upgrade'] ? NextUpgradeSerializer::fromJSON($json['next_upgrade']) : null, - $json['our_public_signing_key'], - PeerSerializer::fromJsonArray($json['peers']), - $json['round_length'], - $json['starting_state_root_hash'], - $json['uptime'], - $json['reactor_state'], - new \DateTime($json['last_progress']), - $json['latest_switch_block_hash'], - $json['available_block_ranges'], - $json['block_sync'] + $json['protocol_version'] ?? $json['api_version'] ?? '', + $json['build_version'] ?? '', + $json['chainspec_name'] ?? '', + $lastAddedBlockInfo, + isset($json['next_upgrade']) ? NextUpgradeSerializer::fromJSON($json['next_upgrade']) : null, + $json['our_public_signing_key'] ?? '', + isset($json['peers']) ? PeerSerializer::fromJsonArray($json['peers']) : [], + $json['round_length'] ?? '', + $json['starting_state_root_hash'] ?? '', + $json['uptime'] ?? '', + $json['reactor_state'] ?? '', + isset($json['last_progress']) ? new \DateTime($json['last_progress']) : new \DateTime(), + $json['latest_switch_block_hash'] ?? '', + $json['available_block_ranges'] ?? $json['available_block_range'] ?? null, + $json['block_sync'] ?? [] ); } @@ -71,7 +75,7 @@ public function __construct( string $protocolVersion, string $buildVersion, string $chainSpecName, - MinimalBlockInfo $lastAddedBlockInfo, + ?MinimalBlockInfo $lastAddedBlockInfo, ?NextUpgrade $nextUpgrade, string $outPublicSigningKey, array $peers, @@ -118,7 +122,7 @@ public function getChainSpecName(): string return $this->chainSpecName; } - public function getLastAddedBlockInfo(): MinimalBlockInfo + public function getLastAddedBlockInfo(): ?MinimalBlockInfo { return $this->lastAddedBlockInfo; } @@ -133,6 +137,11 @@ public function getOutPublicSigningKey(): string return $this->outPublicSigningKey; } + public function getOurPublicSigningKey(): string + { + return $this->outPublicSigningKey; + } + public function getPeers(): array { return $this->peers; diff --git a/src/Rpc/RpcClient.php b/src/Rpc/RpcClient.php index 39f3d2a..80b6dd6 100644 --- a/src/Rpc/RpcClient.php +++ b/src/Rpc/RpcClient.php @@ -628,7 +628,7 @@ public function getLatestBlockTransfers(): ChainGetBlockTransfersResult /** * @throws RpcError */ - public function getBlockTransfersByHash(string $blockHash = null): ChainGetBlockTransfersResult + public function getBlockTransfersByHash(?string $blockHash = null): ChainGetBlockTransfersResult { $result = $this->processRequest( self::RPC_METHOD_CHAIN_GET_BLOCK_TRANSFERS, @@ -645,7 +645,7 @@ public function getBlockTransfersByHash(string $blockHash = null): ChainGetBlock /** * @throws RpcError */ - public function getBlockTransfersByHeight(int $blockHeight = null): ChainGetBlockTransfersResult + public function getBlockTransfersByHeight(?int $blockHeight = null): ChainGetBlockTransfersResult { $result = $this->processRequest( self::RPC_METHOD_CHAIN_GET_BLOCK_TRANSFERS, diff --git a/src/Rpc/RpcError.php b/src/Rpc/RpcError.php index bed268b..b32d0fb 100644 --- a/src/Rpc/RpcError.php +++ b/src/Rpc/RpcError.php @@ -6,7 +6,7 @@ class RpcError extends \Exception { - public function __construct($message = "", $code = 0, Throwable $previous = null) + public function __construct($message = "", $code = 0, ?Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Rpc/RpcResponse.php b/src/Rpc/RpcResponse.php index 5314af5..ae3ef96 100644 --- a/src/Rpc/RpcResponse.php +++ b/src/Rpc/RpcResponse.php @@ -22,10 +22,18 @@ public static function fromArray(array $data): self $response->result = $data['result'] ?? null; if ($response->result === null || isset($data['error'])) { - $response->error = new RpcError( - $data['error']['message'] ?? $data['message'] ?? 'Empty response', - $data['error']['code'] ?? 0 - ); + $errorMessage = $data['error']['message'] ?? $data['message'] ?? 'Empty response'; + $errorCode = $data['error']['code'] ?? 0; + + if (isset($data['error']['data'])) { + $errorData = $data['error']['data']; + if (is_array($errorData) || is_object($errorData)) { + $errorData = json_encode($errorData); + } + $errorMessage .= '. Data: ' . $errorData; + } + + $response->error = new RpcError($errorMessage, $errorCode); } return $response; diff --git a/src/Rpc/SpeculativeClient.php b/src/Rpc/SpeculativeClient.php index 79b306e..c129f4c 100644 --- a/src/Rpc/SpeculativeClient.php +++ b/src/Rpc/SpeculativeClient.php @@ -19,7 +19,7 @@ public function __construct(Handler $handler) /** * @throws RpcError */ - public function speculativeExecByBlockHash(Deploy $signedDeploy, string $blockHash = null): SpeculativeExecResult + public function speculativeExecByBlockHash(Deploy $signedDeploy, ?string $blockHash = null): SpeculativeExecResult { $params = array( 'deploy' => DeploySerializer::toJson($signedDeploy) @@ -39,7 +39,7 @@ public function speculativeExecByBlockHash(Deploy $signedDeploy, string $blockHa /** * @throws RpcError */ - public function speculativeExecByBlockHeight(Deploy $signedDeploy, int $blockHeight = null): SpeculativeExecResult + public function speculativeExecByBlockHeight(Deploy $signedDeploy, ?int $blockHeight = null): SpeculativeExecResult { $params = array( 'deploy' => DeploySerializer::toJson($signedDeploy) diff --git a/src/Types/CLValue/CLPublicKey.php b/src/Types/CLValue/CLPublicKey.php index 0f25f78..2207103 100644 --- a/src/Types/CLValue/CLPublicKey.php +++ b/src/Types/CLValue/CLPublicKey.php @@ -24,8 +24,15 @@ final class CLPublicKey extends CLValue /** * @throws \Exception */ - public function __construct(array $rawPublicKey, CLPublicKeyTag $tag) + public function __construct(array $rawPublicKey, $tag) { + if (is_int($tag)) { + $tag = new CLPublicKeyTag($tag); + } + if (!$tag instanceof CLPublicKeyTag) { + throw new \InvalidArgumentException('Invalid public key tag type'); + } + $this->assertRawPublicKeyLengthIsValid($rawPublicKey, $tag); $this->data = $rawPublicKey; $this->tag = $tag; diff --git a/src/Types/DeployExecutable.php b/src/Types/DeployExecutable.php index e30207d..d1d593f 100644 --- a/src/Types/DeployExecutable.php +++ b/src/Types/DeployExecutable.php @@ -15,7 +15,7 @@ public static function newStandardPayment($amount): DeployExecutableModuleBytes return self::newModuleBytes('', [new NamedArg('amount', new CLU512($amount))]); } - public static function newTransfer($id, $amount, $target, CLURef $sourcePurse = null): DeployExecutableTransfer + public static function newTransfer($id, $amount, $target, ?CLURef $sourcePurse = null): DeployExecutableTransfer { if (!in_array(get_class($target), [CLURef::class, CLPublicKey::class])) { throw new \Exception('Please specify target'); @@ -63,7 +63,7 @@ public static function newStoredContractPackageByHash( string $entrypoint, array $args, string $hexContractPackageHash, - int $version = null + ?int $version = null ): DeployExecutableStoredVersionedContractByHash { return (new DeployExecutableStoredVersionedContractByHash($hexContractPackageHash, $entrypoint, $version)) ->setArgs($args); @@ -73,7 +73,7 @@ public static function newStoredContractPackageByName( string $entrypoint, array $args, string $contractPackageAlias, - int $version = null + ?int $version = null ): DeployExecutableStoredVersionedContractByName { return (new DeployExecutableStoredVersionedContractByName($contractPackageAlias, $entrypoint, $version)) ->setArgs($args); diff --git a/src/Types/DeployParams.php b/src/Types/DeployParams.php index 75087e2..f7aadac 100644 --- a/src/Types/DeployParams.php +++ b/src/Types/DeployParams.php @@ -47,7 +47,7 @@ public function __construct( int $gasPrice = self::DEFAULT_GAS_PRICE, int $ttl = self::DEFAULT_TTL, array $dependencies = [], - int $timestamp = null + ?int $timestamp = null ) { $this->accountPublicKey = $accountPublicKey; diff --git a/src/Types/Serializer/DelegatorSerializer.php b/src/Types/Serializer/DelegatorSerializer.php index 8f11a92..f61455e 100644 --- a/src/Types/Serializer/DelegatorSerializer.php +++ b/src/Types/Serializer/DelegatorSerializer.php @@ -8,6 +8,18 @@ class DelegatorSerializer extends JsonSerializer { + private static function normalizePublicKey($value): ?string + { + if (is_string($value)) { + return $value; + } + if (is_array($value)) { + return $value['PublicKey'] ?? $value['public_key'] ?? null; + } + + return null; + } + /** * @param Delegator $delegator */ @@ -23,11 +35,42 @@ public static function toJson($delegator): array public static function fromJson(array $json): Delegator { + $publicKeyHex = self::normalizePublicKey($json['public_key'] ?? $json['delegator_public_key'] ?? $json['delegator'] ?? null); + $delegateeHex = self::normalizePublicKey($json['delegatee'] ?? $json['validator_public_key'] ?? $json['validator'] ?? null); + $stakedAmount = $json['staked_amount'] ?? $json['bonding_amount'] ?? '0'; + $bondingPurse = $json['bonding_purse'] ?? $json['bonding_purse_uref'] ?? null; + + if (is_array($bondingPurse)) { + $bondingPurse = $bondingPurse['URef'] ?? $bondingPurse['uref'] ?? null; + } + + if (null === $publicKeyHex || null === $delegateeHex || '' === $bondingPurse) { + throw new \RuntimeException('Invalid delegator data: missing required fields'); + } + return new Delegator( - CLPublicKey::fromHex($json['public_key']), - gmp_init($json['staked_amount']), - CLURef::fromString($json['bonding_purse']), - CLPublicKey::fromHex($json['delegatee']) + CLPublicKey::fromHex($publicKeyHex), + gmp_init($stakedAmount), + CLURef::fromString($bondingPurse), + CLPublicKey::fromHex($delegateeHex) ); } + + public static function fromJsonArray(array $array): array + { + $result = []; + + foreach ($array as $item) { + if (!is_array($item)) { + continue; + } + try { + $result[] = self::fromJson($item); + } catch (\RuntimeException $e) { + continue; + } + } + + return $result; + } } diff --git a/src/Types/Serializer/InitiatorAddrSerializer.php b/src/Types/Serializer/InitiatorAddrSerializer.php index c98eefe..4dd11b7 100644 --- a/src/Types/Serializer/InitiatorAddrSerializer.php +++ b/src/Types/Serializer/InitiatorAddrSerializer.php @@ -34,8 +34,8 @@ public static function toJson($initiatorAddr): array public static function fromJson(array $json): InitiatorAddr { return new InitiatorAddr( - $json['PublicKey'] ? CLPublicKey::fromHex($json['PublicKey']) : null, - $json['AccountHash'] ? CLAccountHash::fromString($json['AccountHash']) : null + isset($json['PublicKey']) ? CLPublicKey::fromHex($json['PublicKey']) : null, + isset($json['AccountHash']) ? CLAccountHash::fromString($json['AccountHash']) : null ); } } diff --git a/src/Types/Serializer/TransformSerializer.php b/src/Types/Serializer/TransformSerializer.php index 1d13334..52aef1c 100644 --- a/src/Types/Serializer/TransformSerializer.php +++ b/src/Types/Serializer/TransformSerializer.php @@ -19,6 +19,9 @@ public static function toJson($transform): array public static function fromJson(array $json): Transform { - return new Transform($json['key'], $json['kind']); + $key = $json['key'] ?? ''; + $kind = $json['kind'] ?? ($json['transform'] ?? ($json['value'] ?? null)); + + return new Transform($key, $kind); } } diff --git a/src/Types/SessionTarget.php b/src/Types/SessionTarget.php index 0cc70ee..234288d 100644 --- a/src/Types/SessionTarget.php +++ b/src/Types/SessionTarget.php @@ -13,7 +13,7 @@ class SessionTarget public function __construct( DeployExecutableModuleBytes $moduleBytes, TransactionRuntime $runtime, - bool $isInstallUpgrade = null + ?bool $isInstallUpgrade = null ) { $this->moduleBytes = $moduleBytes; diff --git a/src/Util/Crypto/Secp256K1Key.php b/src/Util/Crypto/Secp256K1Key.php index 5de18d1..29660b2 100644 --- a/src/Util/Crypto/Secp256K1Key.php +++ b/src/Util/Crypto/Secp256K1Key.php @@ -30,7 +30,7 @@ final class Secp256K1Key extends AsymmetricKey private PrivateKeyInterface $privateKeyObject; - public function __construct(PrivateKeyInterface $privateKeyObject = null) + public function __construct(?PrivateKeyInterface $privateKeyObject = null) { $this->adapter = EccFactory::getAdapter(); $this->generator = EccFactory::getSecgCurves() diff --git a/tests/Functional/Rpc/RpcClientTest.php b/tests/Functional/Rpc/RpcClientTest.php index 326e2a3..9e0584e 100644 --- a/tests/Functional/Rpc/RpcClientTest.php +++ b/tests/Functional/Rpc/RpcClientTest.php @@ -4,16 +4,14 @@ use PHPUnit\Framework\TestCase; -use Casper\Serializer\CLPublicKeySerializer; use Casper\Util\ByteUtil; - +use Casper\Types\CLValue\CLPublicKey; +use Casper\Rpc\HttpHandler; use Casper\Rpc\RpcClient; use Casper\Rpc\RpcError; -use Casper\Entity\Account; -use Casper\Entity\BlockBody; -use Casper\Entity\BlockHeader; -use Casper\Entity\Block; +use Casper\Types\Account; +use Casper\Types\Block; class RpcClientTest extends TestCase { @@ -27,7 +25,13 @@ protected function setUp(): void throw new \Exception('Please set CASPER_PHP_SDK_TEST_NODE_URL environment variable before test run'); } - $this->rpcClient = new RpcClient($nodeUrl); + $apiKey = getenv('CASPER_PHP_SDK_TEST_API_KEY') ?: ''; + $headers = []; + if ('' !== $apiKey) { + $headers['Authorization'] = $apiKey; + } + + $this->rpcClient = new RpcClient(new HttpHandler($nodeUrl, $headers)); sleep(10); } @@ -38,6 +42,10 @@ protected function tearDown(): void public function testGetLastApiVersion(): void { + if (!method_exists($this->rpcClient, 'getLastApiVersion')) { + $this->markTestSkipped('getLastApiVersion() is not available in this SDK version.'); + } + $lastApiVersionBeforeAnyRpcCall = $this->rpcClient->getLastApiVersion(); $this->assertNull($lastApiVersionBeforeAnyRpcCall); @@ -51,7 +59,8 @@ public function testGetDeploy(): void { $deployHashFromTheTestnet = '7ab7208819bead36b7143757c3d4b7d0d749e5fd2e49b7ae58d490ea3d323371'; - $deploy = $this->rpcClient->getDeploy($deployHashFromTheTestnet); + $deployResult = $this->rpcClient->getDeploy($deployHashFromTheTestnet); + $deploy = $deployResult->getDeploy(); $this->assertEquals(ByteUtil::hexToByteArray($deployHashFromTheTestnet), $deploy->getHash()); $this->assertNotNull($deploy->getPayment()); $this->assertNotEmpty($deploy->getApprovals()); @@ -69,10 +78,11 @@ public function testGetDeployByFakeHash(): void public function testGetLatestBlock(): Block { - $latestBlock = $this->rpcClient->getLatestBlock(); + $latestBlockResult = $this->rpcClient->getLatestBlock(); + $latestBlock = $latestBlockResult->getBlock(); $this->assertNotEmpty($latestBlock->getHash()); - $this->assertInstanceOf(BlockHeader::class, $latestBlock->getHeader()); - $this->assertInstanceOf(BlockBody::class, $latestBlock->getBody()); + $this->assertGreaterThanOrEqual(0, $latestBlock->getHeight()); + $this->assertNotEmpty($latestBlock->getStateRootHash()); $this->assertIsArray($latestBlock->getProofs()); return $latestBlock; @@ -83,7 +93,8 @@ public function testGetLatestBlock(): Block */ public function testGetBlockByHash(Block $latestBlock): void { - $block = $this->rpcClient->getBlockByHash($latestBlock->getHash()); + $blockResult = $this->rpcClient->getBlockByHash($latestBlock->getHash()); + $block = $blockResult->getBlock(); $this->assertEquals($block->getHash(), $latestBlock->getHash()); } @@ -102,7 +113,8 @@ public function testGetBlockByFakeHash(): void */ public function testGetBlockByHeight(Block $latestBlock): void { - $block = $this->rpcClient->getBlockByHeight($latestBlock->getHeader()->getHeight()); + $blockResult = $this->rpcClient->getBlockByHeight($latestBlock->getHeight()); + $block = $blockResult->getBlock(); $this->assertEquals($block->getHash(), $latestBlock->getHash()); } @@ -114,7 +126,8 @@ public function testGetBlockByIncorrectHeight(): void public function testGetPeers(): void { - $peers = $this->rpcClient->getPeers(); + $peersResult = $this->rpcClient->getPeers(); + $peers = $peersResult->getPeers(); $this->assertIsArray($peers); $firstPeer = $peers[0]; @@ -128,8 +141,10 @@ public function testGetStatus(): void $this->assertNotEmpty($status->getApiVersion()); $this->assertNotEmpty($status->getChainspecName()); $this->assertNotEmpty($status->getStartingStateRootHash()); - $this->assertNotEmpty($status->getLastAddedBlockInfo()->getHash()); - $this->assertNotEmpty($status->getOurPublicSigningKey()->parsedValue()); + $lastAddedBlockInfo = $status->getLastAddedBlockInfo(); + $this->assertNotNull($lastAddedBlockInfo); + $this->assertNotEmpty($lastAddedBlockInfo->getHash()); + $this->assertNotEmpty($status->getOutPublicSigningKey()); $this->assertNotEmpty($status->getBuildVersion()); $this->assertNotEmpty($status->getUptime()); $this->assertNotEmpty($status->getReactorState()); @@ -139,7 +154,8 @@ public function testGetStatus(): void public function testGetAuctionState(): void { - $auctionState = $this->rpcClient->getLatestAuctionInfo(); + $auctionStateResult = $this->rpcClient->getLatestAuctionInfo(); + $auctionState = $auctionStateResult->getAuctionState(); $this->assertNotEmpty($auctionState->getStateRootHash()); $this->assertGreaterThan(0, $auctionState->getBlockHeight()); @@ -156,7 +172,8 @@ public function testGetAuctionState(): void */ public function testGetStateRootHash(Block $latestBlock): string { - $stateRootHash = $this->rpcClient->getStateRootHashByHash($latestBlock->getHash()); + $stateRootHashResult = $this->rpcClient->getStateRootHashByHash($latestBlock->getHash()); + $stateRootHash = $stateRootHashResult->getStateRootHash(); $this->assertMatchesRegularExpression('/[a-fA-F\d]{64}/', $stateRootHash); return $stateRootHash; @@ -165,10 +182,11 @@ public function testGetStateRootHash(Block $latestBlock): string public function testGetAccount(): Account { $blockHashFromTheTestnet = '037e916018fd181be3455df1d54e9a1b3a5f1784627b0044201b7ad378542a02'; - $accountPublicKeyFromTheTestnet = CLPublicKeySerializer::fromHex('011b5b2e370411b6df3a3d8ac0063b35e2003994a634dba48dd5422247fc1e7c41'); + $accountPublicKeyFromTheTestnet = CLPublicKey::fromHex('011b5b2e370411b6df3a3d8ac0063b35e2003994a634dba48dd5422247fc1e7c41'); $accountHashFromTheTestnet = 'account-hash-7203e3b0592b7ed4f63552829c5887c493e525fb35b842aed68c56bec38f4e6b'; - $account = $this->rpcClient->getAccountInfoByBlockHash($blockHashFromTheTestnet, $accountPublicKeyFromTheTestnet); + $accountResult = $this->rpcClient->getAccountInfoByBlockHash($blockHashFromTheTestnet, $accountPublicKeyFromTheTestnet); + $account = $accountResult->getAccount(); $this->assertEquals($account->getAccountHash()->parsedValue(), $accountHashFromTheTestnet); return $account; @@ -180,8 +198,12 @@ public function testGetAccount(): Account */ public function testGetAccountBalance(string $stateRootHash, Account $account): void { - $accountBalance = $this->rpcClient->getBalanceByStateRootHash($stateRootHash, $account->getMainPurse()); - $this->assertGreaterThanOrEqual(0, gmp_cmp($accountBalance, 0)); + $accountBalanceResult = $this->rpcClient->getBalanceByStateRootHash( + $account->getMainPurse()->parsedValue(), + $stateRootHash + ); + $balance = $accountBalanceResult->getBalanceValue()->parsedValue(); + $this->assertGreaterThanOrEqual(0, gmp_cmp($balance, 0)); } /** @@ -190,12 +212,13 @@ public function testGetAccountBalance(string $stateRootHash, Account $account): */ public function testQueryBalance(string $stateRootHash, Account $account): void { - $accountBalance = $this->rpcClient->queryBalanceByStateRootHash( + $accountBalanceResult = $this->rpcClient->queryBalanceByStateRootHash( 'purse_uref', $account->getMainPurse()->parsedValue(), $stateRootHash ); - $this->assertGreaterThanOrEqual(0, gmp_cmp($accountBalance, 0)); + $balance = $accountBalanceResult->getBalance()->parsedValue(); + $this->assertGreaterThanOrEqual(0, gmp_cmp($balance, 0)); } /** @@ -204,8 +227,7 @@ public function testQueryBalance(string $stateRootHash, Account $account): void */ public function testGetAccountBalanceUrefByAccountHash(string $stateRootHash, Account $account): void { - $accountMainPurse = $this->rpcClient->getAccountBalanceUrefByAccountHash($stateRootHash, $account->getAccountHash()); - $this->assertEquals($account->getMainPurse()->parsedValue(), $accountMainPurse->parsedValue()); + $this->markTestSkipped('getAccountBalanceUrefByAccountHash() not available in this SDK version.'); } /** @@ -213,11 +235,7 @@ public function testGetAccountBalanceUrefByAccountHash(string $stateRootHash, Ac */ public function testGetAccountBalanceUrefByAccountPublicKey(string $stateRootHash): void { - $accountPublicKeyFromTheTestnet = CLPublicKeySerializer::fromHex('011b5b2e370411b6df3a3d8ac0063b35e2003994a634dba48dd5422247fc1e7c41'); - $accountMainPurseFromTheTestnet = 'uref-84bc62373fdb3ffabb0c85d8d3dcf80ac780ed9c0afad28f959f697effeef043-007'; - - $accountMainPurse = $this->rpcClient->getAccountBalanceUrefByPublicKey($stateRootHash, $accountPublicKeyFromTheTestnet); - $this->assertEquals($accountMainPurseFromTheTestnet, $accountMainPurse->parsedValue()); + $this->markTestSkipped('getAccountBalanceUrefByPublicKey() not available in this SDK version.'); } /** @@ -229,8 +247,9 @@ public function testGetStateItem(string $stateRootHash, Account $account): void $accountHash = $account->getAccountHash()->parsedValue(); $blockState = $this->rpcClient->getStateItem($stateRootHash, $accountHash); - $this->assertNotNull($blockState->getAccount()); - $this->assertEquals($accountHash, $blockState->getAccount()->getAccountHash()->parsedValue()); + $storedValue = $blockState->getStoredValue(); + $this->assertNotNull($storedValue->getAccount()); + $this->assertEquals($accountHash, $storedValue->getAccount()->getAccountHash()->parsedValue()); } public function testGetBlockTransfers(): void @@ -245,7 +264,8 @@ public function testGetEraSummaryBySwitchBlockHash(): void { $switchingBlockHashFromTheTestnet = 'de8649985929090b7cb225e35a5a7b4087fb8fcb3d18c8c9a58da68e4eda8a2e'; - $eraSummary = $this->rpcClient->getEraInfoByBlockHash($switchingBlockHashFromTheTestnet); + $eraSummaryResult = $this->rpcClient->getEraSummaryByHash($switchingBlockHashFromTheTestnet); + $eraSummary = $eraSummaryResult->getEraSummary(); $this->assertEquals(1, $eraSummary->getEraId()); $this->assertEquals($switchingBlockHashFromTheTestnet, strtolower($eraSummary->getBlockHash())); $this->assertNotNull($eraSummary->getStoredValue()->getEraInfo()); @@ -255,7 +275,8 @@ public function testGetEraSummaryBySwitchBlockHeight(): void { $switchingBlockHeightFromTheTestnet = 219; - $eraSummary = $this->rpcClient->getEraSummaryBySwitchBlockHeight($switchingBlockHeightFromTheTestnet); + $eraSummaryResult = $this->rpcClient->getEraSummaryByHeight($switchingBlockHeightFromTheTestnet); + $eraSummary = $eraSummaryResult->getEraSummary(); $this->assertEquals(1, $eraSummary->getEraId()); $this->assertEquals('de8649985929090b7cb225e35a5a7b4087fb8fcb3d18c8c9a58da68e4eda8a2e', strtolower($eraSummary->getBlockHash())); $this->assertNotNull($eraSummary->getStoredValue()->getEraInfo()); @@ -274,9 +295,10 @@ public function testGetGlobalStateByStateRootHash(): void { $blockHashFromTheTestnet = '009516c04e6cb56d1d9b43070fd45cd80bf968739d39555282d8e66a8194e2e3'; $deployHashFromTheTestnet = 'deploy-39cf80560c87af0e69eb4a2c49f2404842244eafc63c497a6c8eb92f89b3c102'; - $stateRootHash = $this->rpcClient->getStateRootHashByHash($blockHashFromTheTestnet); + $stateRootHashResult = $this->rpcClient->getStateRootHashByHash($blockHashFromTheTestnet); + $stateRootHash = $stateRootHashResult->getStateRootHash(); - $globalState = $this->rpcClient->getGlobalStateByStateRootHash($stateRootHash, $deployHashFromTheTestnet); + $globalState = $this->rpcClient->queryGlobalStateByStateRootHash($stateRootHash, $deployHashFromTheTestnet); $this->assertEquals($deployHashFromTheTestnet, 'deploy-' . $globalState->getStoredValue()->getDeployInfo()->getDeployHash()); } diff --git a/tests/Functional/Service/DeployServiceTest.php b/tests/Functional/Service/DeployServiceTest.php index 1f487c8..2d53c84 100644 --- a/tests/Functional/Service/DeployServiceTest.php +++ b/tests/Functional/Service/DeployServiceTest.php @@ -4,16 +4,16 @@ use PHPUnit\Framework\TestCase; -use Casper\Serializer\CLPublicKeySerializer; use Casper\Util\ByteUtil; use Casper\Util\Crypto\Ed25519Key; +use Casper\Types\CLValue\CLPublicKey; use Casper\Service\DeployService; -use Casper\Entity\Deploy; -use Casper\Entity\DeployApproval; -use Casper\Entity\DeployExecutable; -use Casper\Entity\DeployParams; +use Casper\Types\Deploy; +use Casper\Types\Approval; +use Casper\Types\DeployExecutable; +use Casper\Types\DeployParams; class DeployServiceTest extends TestCase { @@ -22,14 +22,14 @@ class DeployServiceTest extends TestCase */ public function testMakeDeploy(): Deploy { - $senderPublicKey = CLPublicKeySerializer::fromAsymmetricKey(new Ed25519Key()); + $senderPublicKey = CLPublicKey::fromAsymmetricKey(new Ed25519Key()); $networkName = 'test-network'; $deployParams = new DeployParams($senderPublicKey, $networkName); $transferId = 1; $transferAmount = 2500000000; $fakePublicKeyHex = '0202181123456789693bcd1066f00abe6759c588efe94504a7c9911be77ec365c08e'; - $recipientPublicKey = CLPublicKeySerializer::fromHex($fakePublicKeyHex); + $recipientPublicKey = CLPublicKey::fromHex($fakePublicKeyHex); $transfer = DeployExecutable::newTransfer($transferId, $transferAmount, $recipientPublicKey); $paymentAmount = 10; @@ -79,7 +79,7 @@ public function testSignDeploy(Deploy $deploy): void $this->assertNotEmpty($approvals); $approval = $approvals[0]; - $this->assertInstanceOf(DeployApproval::class, $approval); + $this->assertInstanceOf(Approval::class, $approval); $this->assertEquals( $ed25519KeyPair->getPublicKey(), ByteUtil::byteArrayToHex($approval->getSigner()->value())