Skip to content

Commit 24455a6

Browse files
author
Roman Bylbas
committed
Added getGlobalStateByStateRootHash method to RpcClient
Signed-off-by: Roman Bylbas <romka.bulbas@gmail.com>
1 parent 670dde7 commit 24455a6

4 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/Entity/GlobalState.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
class GlobalState
66
{
7-
private BlockHeader $blockHeader;
7+
private ?BlockHeader $blockHeader;
88

99
private StoredValue $storedValue;
1010

11-
public function __construct(BlockHeader $blockHeader, StoredValue $storedValue)
11+
public function __construct(?BlockHeader $blockHeader, StoredValue $storedValue)
1212
{
1313
$this->blockHeader = $blockHeader;
1414
$this->storedValue = $storedValue;
1515
}
1616

17-
public function getBlockHeader(): BlockHeader
17+
public function getBlockHeader(): ?BlockHeader
1818
{
1919
return $this->blockHeader;
2020
}

src/Rpc/RpcClient.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function getDictionaryItemByURef(
354354
/**
355355
* @throws RpcError
356356
*/
357-
public function getGlobalState(string $blockHash, string $key, array $path = []): GlobalState
357+
public function getGlobalStateByBlock(string $blockHash, string $key, array $path = []): GlobalState
358358
{
359359
$response = $this->rpcCallMethod(
360360
self::RPC_METHOD_QUERY_GLOBAL_STATE,
@@ -370,6 +370,22 @@ public function getGlobalState(string $blockHash, string $key, array $path = [])
370370
return GlobalStateSerializer::fromJson($response);
371371
}
372372

373+
public function getGlobalStateByStateRootHash(string $stateRootHash, string $key, array $path = []): GlobalState
374+
{
375+
$response = $this->rpcCallMethod(
376+
self::RPC_METHOD_QUERY_GLOBAL_STATE,
377+
array(
378+
'state_identifier' => array(
379+
'StateRootHash' => $stateRootHash
380+
),
381+
'key' => $key,
382+
'path' => $path,
383+
)
384+
);
385+
386+
return GlobalStateSerializer::fromJson($response);
387+
}
388+
373389
/**
374390
* @throws RpcError
375391
*/

src/Serializer/GlobalStateSerializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function toJson($globalState): array
2020
public static function fromJson(array $json): GlobalState
2121
{
2222
return new GlobalState(
23-
BlockHeaderSerializer::fromJson($json['block_header']),
23+
isset($json['block_header']) ? BlockHeaderSerializer::fromJson($json['block_header']) : null,
2424
StoredValueSerializer::fromJson($json['stored_value'])
2525
);
2626
}

tests/Functional/Rpc/RpcClientTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,22 @@ public function testGetEraSummaryBySwitchBlockHeight(): void
241241
$this->assertNotNull($eraSummary->getStoredValue()->getEraInfo());
242242
}
243243

244-
public function testGetGlobalState(): void
244+
public function testGetGlobalStateByBlock(): void
245245
{
246246
$blockHashFromTheTestnet = '009516c04e6cb56d1d9b43070fd45cd80bf968739d39555282d8e66a8194e2e3';
247247
$deployHashFromTheTestnet = 'deploy-39cf80560c87af0e69eb4a2c49f2404842244eafc63c497a6c8eb92f89b3c102';
248248

249-
$globalState = $this->rpcClient->getGlobalState($blockHashFromTheTestnet, $deployHashFromTheTestnet);
249+
$globalState = $this->rpcClient->getGlobalStateByBlock($blockHashFromTheTestnet, $deployHashFromTheTestnet);
250+
$this->assertEquals($deployHashFromTheTestnet, 'deploy-' . $globalState->getStoredValue()->getDeployInfo()->getDeployHash());
251+
}
252+
253+
public function testGetGlobalStateByStateRootHash(): void
254+
{
255+
$blockHashFromTheTestnet = '009516c04e6cb56d1d9b43070fd45cd80bf968739d39555282d8e66a8194e2e3';
256+
$deployHashFromTheTestnet = 'deploy-39cf80560c87af0e69eb4a2c49f2404842244eafc63c497a6c8eb92f89b3c102';
257+
$stateRootHash = $this->rpcClient->getStateRootHash($blockHashFromTheTestnet);
258+
259+
$globalState = $this->rpcClient->getGlobalStateByStateRootHash($stateRootHash, $deployHashFromTheTestnet);
250260
$this->assertEquals($deployHashFromTheTestnet, 'deploy-' . $globalState->getStoredValue()->getDeployInfo()->getDeployHash());
251261
}
252262
}

0 commit comments

Comments
 (0)