Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/API/RpcClientAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,20 @@ Returns an item from a Dictionary ([StoredValue](../Entity/StoredValue.md) objec
| `$stateRootHash` | `string` | Hex-encoded hash of the state root | Yes |
| `$dictionaryItemKey` | `string` | The dictionary item key formatted as a string | Yes |
| `$seedUref` | `string` | The dictionary's seed URef | Yes |

---
## Get global state
```php
getGlobalState(
string $blockHash,
string $key,
array $path
): GlobalState
```
Returns an [GlobalState](../Entity/GlobalState.md) object by the given block hash and key
### Parameters
| Name | Type | Description | Required |
|---|------|-------------|----|
| `$blockHash` | `string` | Hex-encoded hash of the block | Yes |
| `$key` | `string` | `casper_types::Key` as formatted string | Yes |
| `$path` | `array` | The path components starting from the key as base | No |
12 changes: 12 additions & 0 deletions docs/Entity/GlobalState.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# GlobalState

```php
getBlockHeader(): BlockHeader
```
Returns [BlockHeader](BlockHeader.md) object

---
```php
getStoredValue(): StoredValue
```
Returns [StoredValue](StoredValue.md) object
26 changes: 26 additions & 0 deletions src/Entity/GlobalState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Casper\Entity;

class GlobalState
{
private ?BlockHeader $blockHeader;

private StoredValue $storedValue;

public function __construct(?BlockHeader $blockHeader, StoredValue $storedValue)
{
$this->blockHeader = $blockHeader;
$this->storedValue = $storedValue;
}

public function getBlockHeader(): ?BlockHeader
{
return $this->blockHeader;
}

public function getStoredValue(): StoredValue
{
return $this->storedValue;
}
}
38 changes: 38 additions & 0 deletions src/Rpc/RpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Casper\Serializer\CLPublicKeySerializer;
use Casper\Serializer\CLURefSerializer;
use Casper\Serializer\EraSummarySerializer;
use Casper\Serializer\GlobalStateSerializer;
use Casper\Serializer\PeerSerializer;
use Casper\Serializer\BlockSerializer;
use Casper\Serializer\DeploySerializer;
Expand All @@ -24,6 +25,7 @@
use Casper\Entity\Block;
use Casper\Entity\Deploy;
use Casper\Entity\EraSummary;
use Casper\Entity\GlobalState;
use Casper\Entity\Peer;
use Casper\Entity\Status;
use Casper\Entity\StoredValue;
Expand All @@ -50,6 +52,7 @@ class RpcClient
private const RPC_METHOD_GET_ACCOUNT_BALANCE = 'state_get_balance';
private const RPC_METHOD_GET_ERA_INFO_BY_SWITCH_BLOCK = 'chain_get_era_info_by_switch_block';
private const RPC_METHOD_GET_DICTIONARY_ITEM = 'state_get_dictionary_item';
private const RPC_METHOD_QUERY_GLOBAL_STATE = 'query_global_state';

private string $nodeUrl;

Expand Down Expand Up @@ -348,6 +351,41 @@ public function getDictionaryItemByURef(
return StoredValueSerializer::fromJson($response['stored_value']);
}

/**
* @throws RpcError
*/
public function getGlobalStateByBlock(string $blockHash, string $key, array $path = []): GlobalState
{
$response = $this->rpcCallMethod(
self::RPC_METHOD_QUERY_GLOBAL_STATE,
array(
'state_identifier' => array(
'BlockHash' => $blockHash
),
'key' => $key,
'path' => $path,
)
);

return GlobalStateSerializer::fromJson($response);
}

public function getGlobalStateByStateRootHash(string $stateRootHash, string $key, array $path = []): GlobalState
{
$response = $this->rpcCallMethod(
self::RPC_METHOD_QUERY_GLOBAL_STATE,
array(
'state_identifier' => array(
'StateRootHash' => $stateRootHash
),
'key' => $key,
'path' => $path,
)
);

return GlobalStateSerializer::fromJson($response);
}

/**
* @throws RpcError
*/
Expand Down
27 changes: 27 additions & 0 deletions src/Serializer/GlobalStateSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Casper\Serializer;

use Casper\Entity\GlobalState;

class GlobalStateSerializer extends JsonSerializer
{
/**
* @param GlobalState $globalState
*/
public static function toJson($globalState): array
{
return array(
'block_header' => BlockHeaderSerializer::toJson($globalState->getBlockHeader()),
'stored_value' => StoredValueSerializer::toJson($globalState->getStoredValue()),
);
}

public static function fromJson(array $json): GlobalState
{
return new GlobalState(
isset($json['block_header']) ? BlockHeaderSerializer::fromJson($json['block_header']) : null,
StoredValueSerializer::fromJson($json['stored_value'])
);
}
}
19 changes: 19 additions & 0 deletions tests/Functional/Rpc/RpcClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,23 @@ public function testGetEraSummaryBySwitchBlockHeight(): void
$this->assertEquals('de8649985929090b7cb225e35a5a7b4087fb8fcb3d18c8c9a58da68e4eda8a2e', strtolower($eraSummary->getBlockHash()));
$this->assertNotNull($eraSummary->getStoredValue()->getEraInfo());
}

public function testGetGlobalStateByBlock(): void
{
$blockHashFromTheTestnet = '009516c04e6cb56d1d9b43070fd45cd80bf968739d39555282d8e66a8194e2e3';
$deployHashFromTheTestnet = 'deploy-39cf80560c87af0e69eb4a2c49f2404842244eafc63c497a6c8eb92f89b3c102';

$globalState = $this->rpcClient->getGlobalStateByBlock($blockHashFromTheTestnet, $deployHashFromTheTestnet);
$this->assertEquals($deployHashFromTheTestnet, 'deploy-' . $globalState->getStoredValue()->getDeployInfo()->getDeployHash());
}

public function testGetGlobalStateByStateRootHash(): void
{
$blockHashFromTheTestnet = '009516c04e6cb56d1d9b43070fd45cd80bf968739d39555282d8e66a8194e2e3';
$deployHashFromTheTestnet = 'deploy-39cf80560c87af0e69eb4a2c49f2404842244eafc63c497a6c8eb92f89b3c102';
$stateRootHash = $this->rpcClient->getStateRootHash($blockHashFromTheTestnet);

$globalState = $this->rpcClient->getGlobalStateByStateRootHash($stateRootHash, $deployHashFromTheTestnet);
$this->assertEquals($deployHashFromTheTestnet, 'deploy-' . $globalState->getStoredValue()->getDeployInfo()->getDeployHash());
}
}