RPC: Return the data length for get account requests (#28421)

This commit is contained in:
Justin Starry 2022-10-18 01:56:04 +08:00 committed by GitHub
parent 70445b7402
commit 438d96e499
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 37 deletions

View File

@ -43,6 +43,7 @@ pub struct UiAccount {
pub owner: String,
pub executable: bool,
pub rent_epoch: Epoch,
pub space: Option<u64>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@ -83,6 +84,7 @@ impl UiAccount {
additional_data: Option<AccountAdditionalData>,
data_slice_config: Option<UiDataSliceConfig>,
) -> Self {
let space = account.data().len();
let data = match encoding {
UiAccountEncoding::Binary => {
let data = Self::encode_bs58(account, data_slice_config);
@ -125,6 +127,7 @@ impl UiAccount {
owner: account.owner().to_string(),
executable: account.executable(),
rent_epoch: account.rent_epoch(),
space: Some(space as u64),
}
}

View File

@ -182,6 +182,7 @@ fn test_account_subscription() {
"data": "",
"executable": false,
"rentEpoch": 0,
"space": 0,
},
});

View File

@ -309,7 +309,8 @@ Response:
"executable": false,
"lamports": 1000000000,
"owner": "11111111111111111111111111111111",
"rentEpoch": 2
"rentEpoch": 2,
"space": 80
}
},
"id": 1
@ -360,7 +361,8 @@ Response:
"executable": false,
"lamports": 1000000000,
"owner": "11111111111111111111111111111111",
"rentEpoch": 2
"rentEpoch": 2,
"space": 80
}
},
"id": 1
@ -1849,18 +1851,20 @@ Result:
},
"value": [
{
"data": ["AAAAAAEAAAACtzNsyJrW0g==", "base64"],
"data": ["", "base64"],
"executable": false,
"lamports": 1000000000,
"owner": "11111111111111111111111111111111",
"rentEpoch": 2
"rentEpoch": 2,
"space": 16
},
{
"data": ["", "base64"],
"executable": false,
"lamports": 5000000000,
"owner": "11111111111111111111111111111111",
"rentEpoch": 2
"rentEpoch": 2,
"space": 0
}
]
},
@ -1909,14 +1913,16 @@ Result:
"executable": false,
"lamports": 1000000000,
"owner": "11111111111111111111111111111111",
"rentEpoch": 2
"rentEpoch": 2,
"space": 80
},
{
"data": ["", "base58"],
"executable": false,
"lamports": 5000000000,
"owner": "11111111111111111111111111111111",
"rentEpoch": 2
"rentEpoch": 2,
"space": 0
}
]
},
@ -1990,7 +1996,8 @@ Result:
"executable": false,
"lamports": 15298080,
"owner": "4Nd1mBQtrMJVYVfKf2PJy9NZUZdTAsp7D4xWLs4gDB4T",
"rentEpoch": 28
"rentEpoch": 28,
"space": 16
},
"pubkey": "CxELquR1gPP8wHe33gZ4QxqGB3sZ9RSwsJ2KshVewkFY"
}
@ -2041,7 +2048,8 @@ Result:
"executable": false,
"lamports": 15298080,
"owner": "4Nd1mBQtrMJVYVfKf2PJy9NZUZdTAsp7D4xWLs4gDB4T",
"rentEpoch": 28
"rentEpoch": 28,
"space": 42
},
"pubkey": "CxELquR1gPP8wHe33gZ4QxqGB3sZ9RSwsJ2KshVewkFY"
}
@ -2151,23 +2159,23 @@ Result:
"result": [
{
"slot": 348125,
"prioritizationFee": 0,
"prioritizationFee": 0
},
{
"slot": 348126,
"prioritizationFee": 1000,
"prioritizationFee": 1000
},
{
"slot": 348127,
"prioritizationFee": 500,
"prioritizationFee": 500
},
{
"slot": 348128,
"prioritizationFee": 0,
"prioritizationFee": 0
},
{
"slot": 348129,
"prioritizationFee": 1234,
"prioritizationFee": 1234
}
],
"id": 1
@ -2795,7 +2803,8 @@ Result:
"executable": false,
"lamports": 1726080,
"owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
"rentEpoch": 4
"rentEpoch": 4,
"space": 165
},
"pubkey": "28YTZEwqtMHWrhWcvv34se7pjS7wctgqzCPB3gReCFKp"
}
@ -2902,7 +2911,8 @@ Result:
"executable": false,
"lamports": 1726080,
"owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
"rentEpoch": 4
"rentEpoch": 4,
"space": 165
},
"pubkey": "C2gJg6tKpQs41PRS1nC8aw3ZKNZK3HQQZGVrDFDup5nx"
}
@ -3620,10 +3630,7 @@ Result:
"Program 83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri success"
],
"returnData": {
"data": [
"Kg==",
"base64"
],
"data": ["Kg==", "base64"],
"programId": "83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri"
},
"unitsConsumed": 2366
@ -3716,7 +3723,8 @@ Base58 encoding:
"executable": false,
"lamports": 33594,
"owner": "11111111111111111111111111111111",
"rentEpoch": 635
"rentEpoch": 635,
"space": 80
}
},
"subscription": 23784
@ -3752,7 +3760,8 @@ Parsed-JSON encoding:
"executable": false,
"lamports": 33594,
"owner": "11111111111111111111111111111111",
"rentEpoch": 635
"rentEpoch": 635,
"space": 80
}
},
"subscription": 23784
@ -4280,7 +4289,8 @@ Base58 encoding:
"executable": false,
"lamports": 33594,
"owner": "11111111111111111111111111111111",
"rentEpoch": 636
"rentEpoch": 636,
"space": 80
}
}
},
@ -4319,7 +4329,8 @@ Parsed-JSON encoding:
"executable": false,
"lamports": 33594,
"owner": "11111111111111111111111111111111",
"rentEpoch": 636
"rentEpoch": 636,
"space": 80
}
}
},

View File

@ -5484,7 +5484,8 @@ pub mod tests {
"lamports": TEST_MINT_LAMPORTS,
"data": "",
"executable": false,
"rentEpoch": 0
"rentEpoch": 0,
"space": 0,
},
});
assert_eq!(result, expected);
@ -5502,6 +5503,7 @@ pub mod tests {
let result: Value = parse_success_result(rpc.handle_request_sync(request));
let expected = json!([base64::encode(&data), "base64"]);
assert_eq!(result["value"]["data"], expected);
assert_eq!(result["value"]["space"], 5);
let request = create_test_request(
"getAccountInfo",
@ -5510,6 +5512,7 @@ pub mod tests {
let result: Value = parse_success_result(rpc.handle_request_sync(request));
let expected = json!([base64::encode(&data[1..3]), "base64"]);
assert_eq!(result["value"]["data"], expected);
assert_eq!(result["value"]["space"], 5);
let request = create_test_request(
"getAccountInfo",
@ -5518,6 +5521,7 @@ pub mod tests {
let result: Value = parse_success_result(rpc.handle_request_sync(request));
let expected = bs58::encode(&data[1..3]).into_string();
assert_eq!(result["value"]["data"], expected);
assert_eq!(result["value"]["space"], 5);
let request = create_test_request(
"getAccountInfo",
@ -5561,7 +5565,8 @@ pub mod tests {
"lamports": TEST_MINT_LAMPORTS,
"data": ["", "base64"],
"executable": false,
"rentEpoch": 0
"rentEpoch": 0,
"space": 0,
},
null,
{
@ -5569,7 +5574,8 @@ pub mod tests {
"lamports": 42,
"data": [base64::encode(&data), "base64"],
"executable": false,
"rentEpoch": 0
"rentEpoch": 0,
"space": 5,
}
]);
assert_eq!(result.value, expected);
@ -5593,7 +5599,8 @@ pub mod tests {
"lamports": TEST_MINT_LAMPORTS,
"data": ["", "base58"],
"executable": false,
"rentEpoch": 0
"rentEpoch": 0,
"space": 0,
},
null,
{
@ -5601,7 +5608,8 @@ pub mod tests {
"lamports": 42,
"data": [bs58::encode(&data).into_string(), "base58"],
"executable": false,
"rentEpoch": 0
"rentEpoch": 0,
"space": 5,
}
]);
assert_eq!(result.value, expected);
@ -5838,7 +5846,8 @@ pub mod tests {
"executable": false,
"owner": "11111111111111111111111111111111",
"lamports": rent_exempt_amount,
"rentEpoch": 0
"rentEpoch": 0,
"space": 0,
}
],
"err":null,

View File

@ -934,6 +934,7 @@ mod tests {
"data": [base64::encode(expected_data), encoding],
"executable": false,
"rentEpoch": 0,
"space": expected_data.len(),
},
},
"subscription": 0,
@ -1059,6 +1060,7 @@ mod tests {
"data": expected_data,
"executable": false,
"rentEpoch": 0,
"space": account.data().len(),
},
},
"subscription": 0,
@ -1239,6 +1241,7 @@ mod tests {
"data": "",
"executable": false,
"rentEpoch": 0,
"space": 0,
},
},
"subscription": 0,

View File

@ -1282,7 +1282,14 @@ pub(crate) mod tests {
},
};
fn make_account_result(lamports: u64, subscription: u64, data: &str) -> serde_json::Value {
struct AccountResult {
lamports: u64,
subscription: u64,
data: &'static str,
space: usize,
}
fn make_account_result(account_result: AccountResult) -> serde_json::Value {
json!({
"jsonrpc": "2.0",
"method": "accountNotification",
@ -1290,14 +1297,15 @@ pub(crate) mod tests {
"result": {
"context": { "slot": 1 },
"value": {
"data": data,
"data": account_result.data,
"executable": false,
"lamports": lamports,
"lamports": account_result.lamports,
"owner": "11111111111111111111111111111111",
"rentEpoch": 0,
"space": account_result.space,
},
},
"subscription": subscription,
"subscription": account_result.subscription,
}
})
}
@ -1338,7 +1346,12 @@ pub(crate) mod tests {
0,
&system_program::id(),
);
let expected0 = make_account_result(1, 0, "");
let expected0 = make_account_result(AccountResult {
lamports: 1,
subscription: 0,
space: 0,
data: "",
});
let tx1 = {
let instruction =
@ -1346,7 +1359,12 @@ pub(crate) mod tests {
let message = Message::new(&[instruction], Some(&mint_keypair.pubkey()));
Transaction::new(&[&alice, &mint_keypair], message, blockhash)
};
let expected1 = make_account_result(0, 1, "");
let expected1 = make_account_result(AccountResult {
lamports: 0,
subscription: 1,
space: 0,
data: "",
});
let tx2 = system_transaction::create_account(
&mint_keypair,
@ -1356,7 +1374,12 @@ pub(crate) mod tests {
1024,
&system_program::id(),
);
let expected2 = make_account_result(1, 2, "error: data too large for bs58 encoding");
let expected2 = make_account_result(AccountResult {
lamports: 1,
subscription: 2,
space: 1024,
data: "error: data too large for bs58 encoding",
});
let subscribe_cases = vec![
(alice.pubkey(), tx0, expected0),
@ -1849,6 +1872,7 @@ pub(crate) mod tests {
"lamports": 1,
"owner": "Stake11111111111111111111111111111111111111",
"rentEpoch": 0,
"space": 16,
},
"pubkey": alice.pubkey().to_string(),
},
@ -2015,6 +2039,7 @@ pub(crate) mod tests {
"lamports": lamports,
"owner": "Stake11111111111111111111111111111111111111",
"rentEpoch": 0,
"space": 16,
},
"pubkey": pubkey,
},
@ -2295,6 +2320,7 @@ pub(crate) mod tests {
"lamports": lamports,
"owner": "Stake11111111111111111111111111111111111111",
"rentEpoch": 0,
"space": 16,
},
"pubkey": pubkey,
},
@ -2783,6 +2809,7 @@ pub(crate) mod tests {
"lamports": 1,
"owner": "Stake11111111111111111111111111111111111111",
"rentEpoch": 0,
"space": 16,
},
},
"subscription": 0,
@ -2832,6 +2859,7 @@ pub(crate) mod tests {
"lamports": 1,
"owner": "Stake11111111111111111111111111111111111111",
"rentEpoch": 0,
"space": 16,
},
},
"subscription": 1,