Skip to content

Commit

Permalink
Retrieve entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-casper committed May 8, 2024
1 parent f9dfbef commit 5c9ab16
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 10 deletions.
20 changes: 20 additions & 0 deletions resources/test/rpc_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,18 @@
"name": "key",
"key": "hash-0000000000000000000000000000000000000000000000000000000000000000"
}
],
"entry_points": [
{
"V1CasperVm": {
"name": "entry_point",
"args": [],
"ret": "Unit",
"access": "Public",
"entry_point_type": "Caller",
"entry_point_payment": "Caller"
}
}
]
}
},
Expand Down Expand Up @@ -6783,6 +6795,7 @@
"type": "object",
"required": [
"entity",
"entry_points",
"named_keys"
],
"properties": {
Expand All @@ -6801,6 +6814,13 @@
"$ref": "#/components/schemas/NamedKeys"
}
]
},
"entry_points": {
"description": "The entry points of the addressable entity.",
"type": "array",
"items": {
"$ref": "#/components/schemas/EntryPointValue"
}
}
}
}
Expand Down
53 changes: 49 additions & 4 deletions rpc_sidecar/src/rpcs/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::rpcs::error::Error;
use casper_types::{
account::AccountHash, addressable_entity::NamedKeys, bytesrepr::ToBytes,
global_state::TrieMerkleProof, Account, AddressableEntity, AvailableBlockRange, BlockHeader,
BlockIdentifier, EntityAddr, GlobalStateIdentifier, Key, SignedBlock, StoredValue,
BlockIdentifier, EntityAddr, EntryPointValue, GlobalStateIdentifier, Key, SignedBlock,
StoredValue,
};

use crate::NodeClient;
Expand Down Expand Up @@ -51,6 +52,8 @@ pub enum EntityOrAccount {
entity: AddressableEntity,
/// The named keys of the addressable entity.
named_keys: NamedKeys,
/// The entry points of the addressable entity.
entry_points: Vec<EntryPointValue>,
},
/// A legacy account.
LegacyAccount(Account),
Expand Down Expand Up @@ -153,9 +156,15 @@ pub async fn resolve_account_hash(
return Err(Error::InvalidAddressableEntity);
};
let named_keys =
resolve_entity_named_keys(node_client, entity_addr, state_identifier).await?;
get_entity_named_keys(node_client, entity_addr, state_identifier).await?;
let entry_points =
get_entity_entry_points(node_client, entity_addr, state_identifier).await?;
(
EntityOrAccount::AddressableEntity { entity, named_keys },
EntityOrAccount::AddressableEntity {
entity,
named_keys,
entry_points,
},
merkle_proof,
)
}
Expand Down Expand Up @@ -190,7 +199,7 @@ pub async fn resolve_entity_addr(
}))
}

pub async fn resolve_entity_named_keys(
pub async fn get_entity_named_keys(
node_client: &dyn NodeClient,
entity_addr: EntityAddr,
state_identifier: Option<GlobalStateIdentifier>,
Expand Down Expand Up @@ -221,6 +230,42 @@ pub async fn resolve_entity_named_keys(
Ok(NamedKeys::from(named_keys))
}

pub async fn get_entity_entry_points(
node_client: &dyn NodeClient,
entity_addr: EntityAddr,
state_identifier: Option<GlobalStateIdentifier>,
) -> Result<Vec<EntryPointValue>, Error> {
let stored_values_v1 = node_client
.query_global_state_by_prefix(
state_identifier,
KeyPrefix::EntryPointsV1ByEntity(entity_addr),
)
.await
.map_err(|err| Error::NodeRequest("entity named keys", err))?;
let stored_values_v2 = node_client
.query_global_state_by_prefix(
state_identifier,
KeyPrefix::EntryPointsV2ByEntity(entity_addr),
)
.await
.map_err(|err| Error::NodeRequest("entity named keys", err))?;

stored_values_v1
.into_iter()
.chain(stored_values_v2)
.map(|stored_value| {
if let StoredValue::EntryPoint(entry_point) = stored_value {
Ok(entry_point)
} else {
Err(Error::InvalidNamedKeys(format!(
"unexpected stored value: {}",
stored_value.type_name()
)))
}
})
.collect::<Result<_, _>>()
}

pub fn encode_proof(proof: &Vec<TrieMerkleProof<Key, StoredValue>>) -> Result<String, Error> {
Ok(base16::encode_lower(
&proof.to_bytes().map_err(Error::BytesreprFailure)?,
Expand Down
5 changes: 4 additions & 1 deletion rpc_sidecar/src/rpcs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ pub enum Error {
InvalidAddressableEntity,
#[error("the auction state was invalid")]
InvalidAuctionState,
#[error("the named keys were invalid")]
#[error("the named keys were invalid: {0}")]
InvalidNamedKeys(String),
#[error("the entry points were invalid: {0}")]
InvalidEntryPoints(String),
#[error("speculative execution returned nothing")]
SpecExecReturnedNothing,
#[error("unexpected bytesrepr failure: {0}")]
Expand Down Expand Up @@ -101,6 +103,7 @@ impl Error {
| Error::InvalidAddressableEntity
| Error::InvalidAuctionState
| Error::InvalidNamedKeys(_)
| Error::InvalidEntryPoints(_)
| Error::BytesreprFailure(_) => None,
}
}
Expand Down
67 changes: 62 additions & 5 deletions rpc_sidecar/src/rpcs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use casper_types::{
AUCTION,
},
AddressableEntity, AddressableEntityHash, AuctionState, BlockHash, BlockHeader, BlockHeaderV2,
BlockIdentifier, BlockTime, BlockV2, CLValue, Digest, EntityAddr, GlobalStateIdentifier, Key,
KeyTag, PublicKey, SecretKey, StoredValue, URef, U512,
BlockIdentifier, BlockTime, BlockV2, CLValue, Digest, EntityAddr, EntryPoint, EntryPointValue,
GlobalStateIdentifier, Key, KeyTag, PublicKey, SecretKey, StoredValue, URef, U512,
};
#[cfg(test)]
use rand::Rng;
Expand Down Expand Up @@ -94,6 +94,9 @@ static GET_ADDRESSABLE_ENTITY_RESULT: Lazy<GetAddressableEntityResult> =
.cloned()
.collect::<BTreeMap<_, _>>()
.into(),
entry_points: vec![EntryPointValue::new_v1_entry_point_value(
EntryPoint::default_with_name("entry_point"),
)],
},
});
static GET_DICTIONARY_ITEM_PARAMS: Lazy<GetDictionaryItemParams> =
Expand Down Expand Up @@ -588,12 +591,14 @@ impl RpcWithParams for GetAddressableEntity {
.await?
.ok_or(Error::AddressableEntityNotFound)?;
let named_keys =
common::resolve_entity_named_keys(&*node_client, addr, state_identifier)
.await?;
common::get_entity_named_keys(&*node_client, addr, state_identifier).await?;
let entry_points =
common::get_entity_entry_points(&*node_client, addr, state_identifier).await?;
(
EntityOrAccount::AddressableEntity {
entity: result.value,
named_keys,
entry_points,
},
result.merkle_proof,
)
Expand Down Expand Up @@ -1371,6 +1376,7 @@ mod tests {
struct ClientMock {
entity: AddressableEntity,
named_keys: NamedKeys,
entry_points: Vec<EntryPointValue>,
entity_hash: AddressableEntityHash,
}

Expand Down Expand Up @@ -1449,6 +1455,44 @@ mod tests {
&[],
))
}
BinaryRequest::Get(GetRequest::State(req))
if matches!(
&*req,
GlobalStateRequest::ItemsByPrefix {
key_prefix: KeyPrefix::EntryPointsV1ByEntity(_),
..
}
) =>
{
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(
self.entry_points
.iter()
.cloned()
.map(StoredValue::EntryPoint)
.collect::<Vec<_>>(),
SUPPORTED_PROTOCOL_VERSION,
),
&[],
))
}
BinaryRequest::Get(GetRequest::State(req))
if matches!(
&*req,
GlobalStateRequest::ItemsByPrefix {
key_prefix: KeyPrefix::EntryPointsV2ByEntity(_),
..
}
) =>
{
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(
Vec::<StoredValue>::new(),
SUPPORTED_PROTOCOL_VERSION,
),
&[],
))
}
req => unimplemented!("unexpected request: {:?}", req),
}
}
Expand All @@ -1473,13 +1517,22 @@ mod tests {
.take(named_key_count)
.collect::<BTreeMap<_, _>>()
.into();
let entry_point_count = rng.gen_range(0..10);
let entry_points = iter::repeat_with(|| {
EntryPointValue::new_v1_entry_point_value(EntryPoint::default_with_name(
rng.random_string(1..10),
))
})
.take(entry_point_count)
.collect::<Vec<_>>();

let entity_identifier = EntityIdentifier::random(rng);

let resp = GetAddressableEntity::do_handle_request(
Arc::new(ClientMock {
entity: entity.clone(),
named_keys: named_keys.clone(),
entry_points: entry_points.clone(),
entity_hash,
}),
GetAddressableEntityParams {
Expand All @@ -1494,7 +1547,11 @@ mod tests {
resp,
GetAddressableEntityResult {
api_version: CURRENT_API_VERSION,
entity: EntityOrAccount::AddressableEntity { entity, named_keys },
entity: EntityOrAccount::AddressableEntity {
entity,
named_keys,
entry_points
},
merkle_proof: String::from("00000000"),
}
);
Expand Down

0 comments on commit 5c9ab16

Please sign in to comment.