ab_contracts_common/method.rs
1use crate::metadata::ContractMetadataKind;
2use ab_blake3::const_hash;
3use ab_core_primitives::hashes::Blake3Hash;
4use ab_io_type::trivial_type::TrivialType;
5use derive_more::Display;
6
7/// Hash of method's compact metadata, which uniquely represents method signature.
8///
9/// While nothing can be said about method implementation, matching method fingerprint means method
10/// name, inputs and outputs are what they are expected to be (struct and field names are ignored as
11/// explained in [`ContractMetadataKind::compact`].
12#[derive(Debug, Display, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, TrivialType)]
13#[repr(C)]
14pub struct MethodFingerprint(Blake3Hash);
15
16impl MethodFingerprint {
17 /// Create a new method fingerprint from its metadata.
18 ///
19 /// `None` is returned for invalid metadata (see
20 /// [`ContractMetadataKind::compact_external_args()`] for details).
21 pub const fn new(method_metadata: &[u8]) -> Option<Self> {
22 // `?` is not supported in `const` environment
23 let Some((compact_metadata_scratch, compact_metadata_size)) =
24 ContractMetadataKind::compact_external_args(method_metadata)
25 else {
26 return None;
27 };
28 // The same as `&compact_metadata_scratch[..compact_metadata_size]`, but it is not allowed
29 // in const environment yet
30 let compact_metadata = compact_metadata_scratch.split_at(compact_metadata_size).0;
31
32 Some(Self(Blake3Hash::new(const_hash(compact_metadata))))
33 }
34
35 #[inline(always)]
36 pub const fn to_bytes(&self) -> &[u8; Blake3Hash::SIZE] {
37 self.0.as_bytes()
38 }
39}
40
41/// Marker trait for external arguments when calling methods.
42///
43/// # Safety
44/// Struct that implements this trait must be `#[repr(C)]` and valid `ExternalArgs` for the contract
45/// method being called.
46///
47/// **Do not implement this trait explicitly!** Implementation is automatically generated by the
48/// macro which generates contract implementation.
49pub unsafe trait ExternalArgs {
50 /// Fingerprint of the method being called
51 const FINGERPRINT: MethodFingerprint;
52 /// Metadata that corresponds to a method being called
53 const METADATA: &[u8];
54}