Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4218: Update state_get_account_info RPC handler r=zacshowa a=zacshowa

# Summary of work done:
- Update `state_get_account_info` RPC handler to handle an `AccountIdentifier` as a parameter
- Add `AccountIdentifier` enum that can be a public key or account hash
- Ensure serde properly formats the `AccountIdentifier` enum to prevent this change from breaking the RPC API

### Sample requests:

Request for an account using a public key from an NCTL network
```
{
  "jsonrpc": "2.0",
  "method": "state_get_account_info",
  "params": {
    "public_key": "01fbabf372fe762ad840834f7dab5d88e522ffa39c9f53d1e991056cef110faeb2",
    "block_identifier": null
  },
  "id": -2271248039839344330
}
```

Associated output:

```
{
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.0.0",
    "account": {
      "account_hash": "account-hash-c029c14904b870e64c1d443d428c606740e82f341bea0f8542ca6494cef1383e",
      "named_keys": [],
      "main_purse": "uref-cf8a03704e4865323fd1b02166e52302ce8fc4c931d76d04e6faea33ee52d93e-007",
      "associated_keys": [
        {
          "account_hash": "account-hash-c029c14904b870e64c1d443d428c606740e82f341bea0f8542ca6494cef1383e",
          "weight": 1
        }
      ],
      "action_thresholds": {
        "deployment": 1,
        "key_management": 1
      }
    },
    "merkle_proof": "[2228 hex chars]"
  },
  "id": -2271248039839344330
}
```

Request for the same account using an account hash instead:

```
{
  "jsonrpc": "2.0",
  "method": "state_get_account_info",
  "params": {
    "public_key": "account-hash-c029c14904b870e64c1d443d428c606740e82f341bea0f8542ca6494cef1383e",
    "block_identifier": null
  },
  "id": -3436401869523751496
}
```

Associated output:

```
{
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.0.0",
    "account": {
      "account_hash": "account-hash-c029c14904b870e64c1d443d428c606740e82f341bea0f8542ca6494cef1383e",
      "named_keys": [],
      "main_purse": "uref-cf8a03704e4865323fd1b02166e52302ce8fc4c931d76d04e6faea33ee52d93e-007",
      "associated_keys": [
        {
          "account_hash": "account-hash-c029c14904b870e64c1d443d428c606740e82f341bea0f8542ca6494cef1383e",
          "weight": 1
        }
      ],
      "action_thresholds": {
        "deployment": 1,
        "key_management": 1
      }
    },
    "merkle_proof": "[2228 hex chars]"
  },
  "id": -3436401869523751496
}
```

# Associated ticket(s):
- casper-network#4203 
- [Update the RPC handler for state_get_account_info](https://app.zenhub.com/workspaces/rust-sdk-64b185bf2cb87924e7759d9d/issues/gh/casper-network/casper-node/4203) 


Co-authored-by: Zach Showalter <[email protected]>
Co-authored-by: Zach Showalter <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2023
2 parents be04f3e + 0dda8ba commit 5ed9cbc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
20 changes: 17 additions & 3 deletions node/src/components/rpc_server/rpcs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static GET_ACCOUNT_INFO_PARAMS: Lazy<GetAccountInfoParams> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([0; 32]).unwrap();
let public_key = PublicKey::from(&secret_key);
GetAccountInfoParams {
public_key,
account_identifier: AccountIdentifier::PublicKey(public_key),
block_identifier: Some(BlockIdentifier::Hash(*Block::doc_example().hash())),
}
});
Expand Down Expand Up @@ -468,12 +468,23 @@ impl RpcWithOptionalParams for GetAuctionInfo {
}
}

/// Identifier of an account.
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[serde(deny_unknown_fields, untagged)]
pub enum AccountIdentifier {
/// The public key of an account
PublicKey(PublicKey),
/// The account hash of an account
AccountHash(AccountHash),
}

/// Params for "state_get_account_info" RPC request
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetAccountInfoParams {
/// The public key of the Account.
pub public_key: PublicKey,
#[serde(alias = "public_key")]
pub account_identifier: AccountIdentifier,
/// The block identifier.
pub block_identifier: Option<BlockIdentifier>,
}
Expand Down Expand Up @@ -530,7 +541,10 @@ impl RpcWithParams for GetAccountInfo {

let state_root_hash = *block.header().state_root_hash();
let base_key = {
let account_hash = params.public_key.to_account_hash();
let account_hash = match params.account_identifier {
AccountIdentifier::PublicKey(public_key) => public_key.to_account_hash(),
AccountIdentifier::AccountHash(account_hash) => account_hash,
};
Key::Account(account_hash)
};
let (stored_value, merkle_proof) =
Expand Down
27 changes: 24 additions & 3 deletions resources/test/rpc_schema_hashing.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@
"summary": "returns an Account from the network",
"params": [
{
"name": "public_key",
"name": "account_identifier",
"schema": {
"description": "The public key of the Account.",
"$ref": "#/components/schemas/PublicKey"
"$ref": "#/components/schemas/AccountIdentifier"
},
"required": true
},
Expand Down Expand Up @@ -359,7 +359,7 @@
"name": "state_get_account_info_example",
"params": [
{
"name": "public_key",
"name": "account_identifier",
"value": "013b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29"
},
{
Expand Down Expand Up @@ -3246,6 +3246,27 @@
},
"additionalProperties": false
},
"AccountIdentifier": {
"description": "Identifier of an account.",
"anyOf": [
{
"description": "The public key of an account",
"allOf": [
{
"$ref": "#/components/schemas/PublicKey"
}
]
},
{
"description": "The account hash of an account",
"allOf": [
{
"$ref": "#/components/schemas/AccountHash"
}
]
}
]
},
"BlockIdentifier": {
"description": "Identifier for possible ways to retrieve a block.",
"anyOf": [
Expand Down

0 comments on commit 5ed9cbc

Please sign in to comment.