Querying Global State
This page explains how to query global state to find account, contract, and package details.
Prerequisites
- You have an account and have installed a contract on a Casper network.
Getting the State Root Hash
The first step in querying global state is obtaining the state root hash, which acts as an identifier for the current state of the network. It is like a Git commit ID for commit history, providing a snapshot of the blockchain state at a specific time.
After sending transactions to the network, it's necessary to fetch the new state root hash to see the changes reflected in the global state. Without doing this, you would be querying past versions of the state.
To get the state root hash, use the get-state-root-hash
command:
casper-client get-state-root-hash --node-address [NODE_SERVER_ADDRESS]
Sample request:
casper-client get-state-root-hash --node-address http://localhost:11101
Sample response:
{
"jsonrpc": "2.0",
"id": -4225403855447288375,
"result": {
"api_version": "2.0.0",
"state_root_hash": "6b5849caa605acdc29564f303764396a27ef7a6b1a61f7b13109b129e7e70d81"
}
}
Querying an Account
To find your account details, start by querying global state using the account hash. The response will contain the entity account identifier, needed to query more details about the account. You will need the following information:
-
The node address, representing an IP address of a peer on the network.
-
The state root hash, as a hex-encoded hash of the state root.
-
A key identifier for the query, which in this case would be the account hash.
casper-client query-global-state \
--node-address [NODE_SERVER_ADDRESS] \
--state-root-hash [STATE_ROOT_HASH] \
--key [ACCOUNT_HASH]
Sample request:
casper-client query-global-state \
--node-address http://localhost:11101 \
--state-root-hash 6b5849caa605acdc29564f303764396a27ef7a6b1a61f7b13109b129e7e70d81 \
--key account-hash-6a56c4eee172043975aea72221eaf09f3c3a24f09a379935e44c9979f1ae9463
Sample response:
{
"jsonrpc": "2.0",
"id": 2591514718461273084,
"result": {
"api_version": "2.0.0",
"block_header": null,
"stored_value": {
"CLValue": {
"cl_type": "Key",
"bytes": "11016a56c4eee172043975aea72221eaf09f3c3a24f09a379935e44c9979f1ae9463",
"parsed": "entity-account-6a56c4eee172043975aea72221eaf09f3c3a24f09a379935e44c9979f1ae9463"
}
},
"merkle_proof": "[2566 hex chars]"
}
}
Next, see more account details using the get-entity
command and the entity identifier or the account hash. Both commands will produce the same output. You will need the following information:
-
The node address, representing an IP address of a peer on the network.
-
The identifier for an addressable entity or an account. This can be an entity hash, a public key, or an account hash.
casper-client get-entity \
--node-address [NODE_SERVER_ADDRESS] \
--entity-identifier [FORMATTED_STRING_OR_PATH]
Sample requests:
casper-client get-entity \
--node-address http://localhost:11101 \
--entity-identifier entity-account-6a56c4eee172043975aea72221eaf09f3c3a24f09a379935e44c9979f1ae9463
OR
casper-client get-entity \
--node-address http://localhost:11101 \
--entity-identifier account-hash-6a56c4eee172043975aea72221eaf09f3c3a24f09a379935e44c9979f1ae9463
Sample response:
Notice that the sample response contains several named keys, including "counter", "counter_package_name", and "version". You can use these values to query the contract state further, as shown in the next example.
Expand to see the sample response
{
"jsonrpc": "2.0",
"id": 4470312592511523509,
"result": {
"api_version": "2.0.0",
"entity": {
"AddressableEntity": {
"entity": {
"protocol_version": "2.0.0",
"entity_kind": {
"Account": "account-hash-6a56c4eee172043975aea72221eaf09f3c3a24f09a379935e44c9979f1ae9463"
},
"package_hash": "package-1bf60faed9931e95e99912aa82f545a85f374dcbcd0c145ee2a5820b39b31d51",
"byte_code_hash": "byte-code-0000000000000000000000000000000000000000000000000000000000000000",
"main_purse": "uref-21dc8fc358c4e30ae29786bd4842a5f99da83efa0b9ca8461cd2196ffbfd07f1-007",
"associated_keys": [
{
"account_hash": "account-hash-6a56c4eee172043975aea72221eaf09f3c3a24f09a379935e44c9979f1ae9463",
"weight": 1
}
],
"action_thresholds": {
"deployment": 1,
"upgrade_management": 1,
"key_management": 1
},
"message_topics": []
},
"named_keys": [
{
"name": "counter",
"key": "entity-contract-a1d0bf9d96f3efc9ea67f627df3a7cba390bfc582956032db91060ca5d413e68"
},
{
"name": "counter_access_uref",
"key": "uref-29feb2af8a9d7b6d2ef6be875a0aa326b646a00b7cdd2dd4a65365e84e9f2e9a-007"
},
{
"name": "counter_package_name",
"key": "package-50d487af45f8cec533c6813801a7630ff97e5ee3964daf7915d5451b4812ac94"
},
{
"name": "version",
"key": "uref-7bc25880db57763fccfa858185becd8de40a890d9e006e067352f011bdcf03bf-007"
}
],
"entry_points": []
}
},
"merkle_proof": "[3010 hex chars]"
}
}
If you don't know your account hash, run this command:
casper-client account-address --public-key [PATH_TO_PUBLIC_KEY]