Skip to content

Commit 863dfd5

Browse files
author
Roman Bylbas
committed
Added query_global_state rpc call
Signed-off-by: Roman Bylbas <romka.bulbas@gmail.com>
1 parent 600801b commit 863dfd5

6 files changed

Lines changed: 113 additions & 0 deletions

File tree

docs/API/RpcClientAPI.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,20 @@ Returns an item from a Dictionary ([StoredValue](../Entity/StoredValue.md) objec
204204
| `$stateRootHash` | `string` | Hex-encoded hash of the state root | Yes |
205205
| `$dictionaryItemKey` | `string` | The dictionary item key formatted as a string | Yes |
206206
| `$seedUref` | `string` | The dictionary's seed URef | Yes |
207+
208+
---
209+
## Get global state
210+
```php
211+
getGlobalState(
212+
string $blockHash,
213+
string $key,
214+
array $path
215+
): GlobalState
216+
```
217+
Returns an [GlobalState](../Entity/GlobalState.md) object by the given block hash and key
218+
### Parameters
219+
| Name | Type | Description | Required |
220+
|---|------|-------------|----|
221+
| `$blockHash` | `string` | Hex-encoded hash of the block | Yes |
222+
| `$key` | `string` | `casper_types::Key` as formatted string | Yes |
223+
| `$path` | `array` | The path components starting from the key as base | No |

docs/Entity/GlobalState.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# GlobalState
2+
3+
```php
4+
getBlockHeader(): BlockHeader
5+
```
6+
Returns [BlockHeader](BlockHeader.md) object
7+
8+
---
9+
```php
10+
getStoredValue(): StoredValue
11+
```
12+
Returns [StoredValue](StoredValue.md) object

src/Entity/GlobalState.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Casper\Entity;
4+
5+
class GlobalState
6+
{
7+
private BlockHeader $blockHeader;
8+
9+
private StoredValue $storedValue;
10+
11+
public function __construct(BlockHeader $blockHeader, StoredValue $storedValue)
12+
{
13+
$this->blockHeader = $blockHeader;
14+
$this->storedValue = $storedValue;
15+
}
16+
17+
public function getBlockHeader(): BlockHeader
18+
{
19+
return $this->blockHeader;
20+
}
21+
22+
public function getStoredValue(): StoredValue
23+
{
24+
return $this->storedValue;
25+
}
26+
}

src/Rpc/RpcClient.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Casper\Serializer\CLPublicKeySerializer;
99
use Casper\Serializer\CLURefSerializer;
1010
use Casper\Serializer\EraSummarySerializer;
11+
use Casper\Serializer\GlobalStateSerializer;
1112
use Casper\Serializer\PeerSerializer;
1213
use Casper\Serializer\BlockSerializer;
1314
use Casper\Serializer\DeploySerializer;
@@ -24,6 +25,7 @@
2425
use Casper\Entity\Block;
2526
use Casper\Entity\Deploy;
2627
use Casper\Entity\EraSummary;
28+
use Casper\Entity\GlobalState;
2729
use Casper\Entity\Peer;
2830
use Casper\Entity\Status;
2931
use Casper\Entity\StoredValue;
@@ -50,6 +52,7 @@ class RpcClient
5052
private const RPC_METHOD_GET_ACCOUNT_BALANCE = 'state_get_balance';
5153
private const RPC_METHOD_GET_ERA_INFO_BY_SWITCH_BLOCK = 'chain_get_era_info_by_switch_block';
5254
private const RPC_METHOD_GET_DICTIONARY_ITEM = 'state_get_dictionary_item';
55+
private const RPC_METHOD_QUERY_GLOBAL_STATE = 'query_global_state';
5356

5457
private string $nodeUrl;
5558

@@ -348,6 +351,25 @@ public function getDictionaryItemByURef(
348351
return StoredValueSerializer::fromJson($response['stored_value']);
349352
}
350353

354+
/**
355+
* @throws RpcError
356+
*/
357+
public function getGlobalState(string $blockHash, string $key, array $path = []): GlobalState
358+
{
359+
$response = $this->rpcCallMethod(
360+
self::RPC_METHOD_QUERY_GLOBAL_STATE,
361+
array(
362+
'state_identifier' => array(
363+
'BlockHash' => $blockHash
364+
),
365+
'key' => $key,
366+
'path' => $path,
367+
)
368+
);
369+
370+
return GlobalStateSerializer::fromJson($response);
371+
}
372+
351373
/**
352374
* @throws RpcError
353375
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Casper\Serializer;
4+
5+
use Casper\Entity\GlobalState;
6+
7+
class GlobalStateSerializer extends JsonSerializer
8+
{
9+
/**
10+
* @param GlobalState $globalState
11+
*/
12+
public static function toJson($globalState): array
13+
{
14+
return array(
15+
'node_id' => BlockHeaderSerializer::toJson($globalState->getBlockHeader()),
16+
'address' => StoredValueSerializer::toJson($globalState->getStoredValue()),
17+
);
18+
}
19+
20+
public static function fromJson(array $json): GlobalState
21+
{
22+
return new GlobalState(
23+
BlockHeaderSerializer::fromJson($json['block_header']),
24+
StoredValueSerializer::fromJson($json['stored_value'])
25+
);
26+
}
27+
}

tests/Functional/Rpc/RpcClientTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,13 @@ public function testGetEraSummaryBySwitchBlockHeight(): void
240240
$this->assertEquals('de8649985929090b7cb225e35a5a7b4087fb8fcb3d18c8c9a58da68e4eda8a2e', strtolower($eraSummary->getBlockHash()));
241241
$this->assertNotNull($eraSummary->getStoredValue()->getEraInfo());
242242
}
243+
244+
public function testGetGlobalState(): void
245+
{
246+
$blockHashFromTheTestnet = '009516c04e6cb56d1d9b43070fd45cd80bf968739d39555282d8e66a8194e2e3';
247+
$deployHashFromTheTestnet = 'deploy-39cf80560c87af0e69eb4a2c49f2404842244eafc63c497a6c8eb92f89b3c102';
248+
249+
$globalState = $this->rpcClient->getGlobalState($blockHashFromTheTestnet, $deployHashFromTheTestnet);
250+
$this->assertEquals($deployHashFromTheTestnet, 'deploy-' . $globalState->getStoredValue()->getDeployInfo()->getDeployHash());
251+
}
243252
}

0 commit comments

Comments
 (0)