Skip to content

Commit 3b700ad

Browse files
authored
Merge pull request #45 from freenet/fix-flatbuffers-and-add-notfound
feat: add NotFound variant to ContractResponse
2 parents 944829c + 9ae9d1f commit 3b700ad

File tree

5 files changed

+224
-17
lines changed

5 files changed

+224
-17
lines changed

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "freenet-stdlib"
3-
version = "0.1.28"
3+
version = "0.1.29"
44
edition = "2021"
55
rust-version = "1.71.1"
66
publish = true

rust/src/client_api/client_events.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ use crate::generated::host_response::{
3131
ContractResponse as FbsContractResponse, ContractResponseArgs, ContractResponseType,
3232
DelegateKey as FbsDelegateKey, DelegateKeyArgs, DelegateResponse as FbsDelegateResponse,
3333
DelegateResponseArgs, GetResponse as FbsGetResponse, GetResponseArgs,
34-
HostResponse as FbsHostResponse, HostResponseArgs, HostResponseType, Ok as FbsOk, OkArgs,
35-
OutboundDelegateMsg as FbsOutboundDelegateMsg, OutboundDelegateMsgArgs,
36-
OutboundDelegateMsgType, PutResponse as FbsPutResponse, PutResponseArgs,
37-
RequestUserInput as FbsRequestUserInput, RequestUserInputArgs,
34+
HostResponse as FbsHostResponse, HostResponseArgs, HostResponseType, NotFound as FbsNotFound,
35+
NotFoundArgs, Ok as FbsOk, OkArgs, OutboundDelegateMsg as FbsOutboundDelegateMsg,
36+
OutboundDelegateMsgArgs, OutboundDelegateMsgType, PutResponse as FbsPutResponse,
37+
PutResponseArgs, RequestUserInput as FbsRequestUserInput, RequestUserInputArgs,
3838
SetSecretRequest as FbsSetSecretRequest, SetSecretRequestArgs,
3939
UpdateNotification as FbsUpdateNotification, UpdateNotificationArgs,
4040
UpdateResponse as FbsUpdateResponse, UpdateResponseArgs,
@@ -1349,6 +1349,41 @@ impl HostResponse {
13491349
Ok(builder.finished_data().to_vec())
13501350
}
13511351
ContractResponse::SubscribeResponse { .. } => todo!(),
1352+
ContractResponse::NotFound { instance_id } => {
1353+
let instance_data = builder.create_vector(instance_id.as_bytes());
1354+
let instance_offset = FbsContractInstanceId::create(
1355+
&mut builder,
1356+
&ContractInstanceIdArgs {
1357+
data: Some(instance_data),
1358+
},
1359+
);
1360+
1361+
let not_found_offset = FbsNotFound::create(
1362+
&mut builder,
1363+
&NotFoundArgs {
1364+
instance_id: Some(instance_offset),
1365+
},
1366+
);
1367+
1368+
let contract_response_offset = FbsContractResponse::create(
1369+
&mut builder,
1370+
&ContractResponseArgs {
1371+
contract_response_type: ContractResponseType::NotFound,
1372+
contract_response: Some(not_found_offset.as_union_value()),
1373+
},
1374+
);
1375+
1376+
let response_offset = FbsHostResponse::create(
1377+
&mut builder,
1378+
&HostResponseArgs {
1379+
response: Some(contract_response_offset.as_union_value()),
1380+
response_type: HostResponseType::ContractResponse,
1381+
},
1382+
);
1383+
1384+
finish_host_response_buffer(&mut builder, response_offset);
1385+
Ok(builder.finished_data().to_vec())
1386+
}
13521387
},
13531388
HostResponse::DelegateResponse { key, values } => {
13541389
let key_data = builder.create_vector(key.bytes());
@@ -1588,6 +1623,9 @@ impl Display for HostResponse {
15881623
ContractResponse::SubscribeResponse { key, .. } => {
15891624
f.write_fmt(format_args!("subscribe response for `{key}`"))
15901625
}
1626+
ContractResponse::NotFound { instance_id } => {
1627+
f.write_fmt(format_args!("not found for `{instance_id}`"))
1628+
}
15911629
},
15921630
HostResponse::DelegateResponse { .. } => write!(f, "delegate responses"),
15931631
HostResponse::Ok => write!(f, "ok response"),
@@ -1624,6 +1662,13 @@ pub enum ContractResponse<T = WrappedState> {
16241662
key: ContractKey,
16251663
subscribed: bool,
16261664
},
1665+
/// Contract was not found after exhaustive search.
1666+
/// This is an explicit response that distinguishes "contract doesn't exist"
1667+
/// from other failure modes like timeouts or network errors.
1668+
NotFound {
1669+
/// The instance ID that was searched for.
1670+
instance_id: ContractInstanceId,
1671+
},
16271672
}
16281673

16291674
impl<T> From<ContractResponse<T>> for HostResponse<T> {

rust/src/generated/client_request_generated.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4707,7 +4707,7 @@ pub mod client_request {
47074707
/// `root_as_client_request_unchecked`.
47084708
pub fn root_as_client_request(
47094709
buf: &[u8],
4710-
) -> Result<ClientRequest<'_>, flatbuffers::InvalidFlatbuffer> {
4710+
) -> Result<ClientRequest, flatbuffers::InvalidFlatbuffer> {
47114711
flatbuffers::root::<ClientRequest>(buf)
47124712
}
47134713
#[inline]
@@ -4719,7 +4719,7 @@ pub mod client_request {
47194719
/// `size_prefixed_root_as_client_request_unchecked`.
47204720
pub fn size_prefixed_root_as_client_request(
47214721
buf: &[u8],
4722-
) -> Result<ClientRequest<'_>, flatbuffers::InvalidFlatbuffer> {
4722+
) -> Result<ClientRequest, flatbuffers::InvalidFlatbuffer> {
47234723
flatbuffers::size_prefixed_root::<ClientRequest>(buf)
47244724
}
47254725
#[inline]
@@ -4752,14 +4752,14 @@ pub mod client_request {
47524752
/// Assumes, without verification, that a buffer of bytes contains a ClientRequest and returns it.
47534753
/// # Safety
47544754
/// Callers must trust the given bytes do indeed contain a valid `ClientRequest`.
4755-
pub unsafe fn root_as_client_request_unchecked(buf: &[u8]) -> ClientRequest<'_> {
4755+
pub unsafe fn root_as_client_request_unchecked(buf: &[u8]) -> ClientRequest {
47564756
flatbuffers::root_unchecked::<ClientRequest>(buf)
47574757
}
47584758
#[inline]
47594759
/// Assumes, without verification, that a buffer of bytes contains a size prefixed ClientRequest and returns it.
47604760
/// # Safety
47614761
/// Callers must trust the given bytes do indeed contain a valid size prefixed `ClientRequest`.
4762-
pub unsafe fn size_prefixed_root_as_client_request_unchecked(buf: &[u8]) -> ClientRequest<'_> {
4762+
pub unsafe fn size_prefixed_root_as_client_request_unchecked(buf: &[u8]) -> ClientRequest {
47634763
flatbuffers::size_prefixed_root_unchecked::<ClientRequest>(buf)
47644764
}
47654765
#[inline]

rust/src/generated/host_response_generated.rs

Lines changed: 164 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@ pub mod host_response {
2828
since = "2.0.0",
2929
note = "Use associated constants instead. This will no longer be generated in 2021."
3030
)]
31-
pub const ENUM_MAX_CONTRACT_RESPONSE_TYPE: u8 = 4;
31+
pub const ENUM_MAX_CONTRACT_RESPONSE_TYPE: u8 = 5;
3232
#[deprecated(
3333
since = "2.0.0",
3434
note = "Use associated constants instead. This will no longer be generated in 2021."
3535
)]
3636
#[allow(non_camel_case_types)]
37-
pub const ENUM_VALUES_CONTRACT_RESPONSE_TYPE: [ContractResponseType; 5] = [
37+
pub const ENUM_VALUES_CONTRACT_RESPONSE_TYPE: [ContractResponseType; 6] = [
3838
ContractResponseType::NONE,
3939
ContractResponseType::GetResponse,
4040
ContractResponseType::PutResponse,
4141
ContractResponseType::UpdateNotification,
4242
ContractResponseType::UpdateResponse,
43+
ContractResponseType::NotFound,
4344
];
4445

4546
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
@@ -52,15 +53,17 @@ pub mod host_response {
5253
pub const PutResponse: Self = Self(2);
5354
pub const UpdateNotification: Self = Self(3);
5455
pub const UpdateResponse: Self = Self(4);
56+
pub const NotFound: Self = Self(5);
5557

5658
pub const ENUM_MIN: u8 = 0;
57-
pub const ENUM_MAX: u8 = 4;
59+
pub const ENUM_MAX: u8 = 5;
5860
pub const ENUM_VALUES: &'static [Self] = &[
5961
Self::NONE,
6062
Self::GetResponse,
6163
Self::PutResponse,
6264
Self::UpdateNotification,
6365
Self::UpdateResponse,
66+
Self::NotFound,
6467
];
6568
/// Returns the variant's name or "" if unknown.
6669
pub fn variant_name(self) -> Option<&'static str> {
@@ -70,6 +73,7 @@ pub mod host_response {
7073
Self::PutResponse => Some("PutResponse"),
7174
Self::UpdateNotification => Some("UpdateNotification"),
7275
Self::UpdateResponse => Some("UpdateResponse"),
76+
Self::NotFound => Some("NotFound"),
7377
_ => None,
7478
}
7579
}
@@ -996,6 +1000,134 @@ pub mod host_response {
9961000
ds.finish()
9971001
}
9981002
}
1003+
pub enum NotFoundOffset {}
1004+
#[derive(Copy, Clone, PartialEq)]
1005+
1006+
pub struct NotFound<'a> {
1007+
pub _tab: flatbuffers::Table<'a>,
1008+
}
1009+
1010+
impl<'a> flatbuffers::Follow<'a> for NotFound<'a> {
1011+
type Inner = NotFound<'a>;
1012+
#[inline]
1013+
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
1014+
Self {
1015+
_tab: flatbuffers::Table::new(buf, loc),
1016+
}
1017+
}
1018+
}
1019+
1020+
impl<'a> NotFound<'a> {
1021+
pub const VT_INSTANCE_ID: flatbuffers::VOffsetT = 4;
1022+
1023+
#[inline]
1024+
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
1025+
NotFound { _tab: table }
1026+
}
1027+
#[allow(unused_mut)]
1028+
pub fn create<
1029+
'bldr: 'args,
1030+
'args: 'mut_bldr,
1031+
'mut_bldr,
1032+
A: flatbuffers::Allocator + 'bldr,
1033+
>(
1034+
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr, A>,
1035+
args: &'args NotFoundArgs<'args>,
1036+
) -> flatbuffers::WIPOffset<NotFound<'bldr>> {
1037+
let mut builder = NotFoundBuilder::new(_fbb);
1038+
if let Some(x) = args.instance_id {
1039+
builder.add_instance_id(x);
1040+
}
1041+
builder.finish()
1042+
}
1043+
1044+
#[inline]
1045+
pub fn instance_id(&self) -> super::common::ContractInstanceId<'a> {
1046+
// Safety:
1047+
// Created from valid Table for this object
1048+
// which contains a valid value in this slot
1049+
unsafe {
1050+
self._tab
1051+
.get::<flatbuffers::ForwardsUOffset<super::common::ContractInstanceId>>(
1052+
NotFound::VT_INSTANCE_ID,
1053+
None,
1054+
)
1055+
.unwrap()
1056+
}
1057+
}
1058+
}
1059+
1060+
impl flatbuffers::Verifiable for NotFound<'_> {
1061+
#[inline]
1062+
fn run_verifier(
1063+
v: &mut flatbuffers::Verifier,
1064+
pos: usize,
1065+
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
1066+
use self::flatbuffers::Verifiable;
1067+
v.visit_table(pos)?
1068+
.visit_field::<flatbuffers::ForwardsUOffset<super::common::ContractInstanceId>>(
1069+
"instance_id",
1070+
Self::VT_INSTANCE_ID,
1071+
true,
1072+
)?
1073+
.finish();
1074+
Ok(())
1075+
}
1076+
}
1077+
pub struct NotFoundArgs<'a> {
1078+
pub instance_id: Option<flatbuffers::WIPOffset<super::common::ContractInstanceId<'a>>>,
1079+
}
1080+
impl<'a> Default for NotFoundArgs<'a> {
1081+
#[inline]
1082+
fn default() -> Self {
1083+
NotFoundArgs {
1084+
instance_id: None, // required field
1085+
}
1086+
}
1087+
}
1088+
1089+
pub struct NotFoundBuilder<'a: 'b, 'b, A: flatbuffers::Allocator + 'a> {
1090+
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a, A>,
1091+
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
1092+
}
1093+
impl<'a: 'b, 'b, A: flatbuffers::Allocator + 'a> NotFoundBuilder<'a, 'b, A> {
1094+
#[inline]
1095+
pub fn add_instance_id(
1096+
&mut self,
1097+
instance_id: flatbuffers::WIPOffset<super::common::ContractInstanceId<'b>>,
1098+
) {
1099+
self.fbb_
1100+
.push_slot_always::<flatbuffers::WIPOffset<super::common::ContractInstanceId>>(
1101+
NotFound::VT_INSTANCE_ID,
1102+
instance_id,
1103+
);
1104+
}
1105+
#[inline]
1106+
pub fn new(
1107+
_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a, A>,
1108+
) -> NotFoundBuilder<'a, 'b, A> {
1109+
let start = _fbb.start_table();
1110+
NotFoundBuilder {
1111+
fbb_: _fbb,
1112+
start_: start,
1113+
}
1114+
}
1115+
#[inline]
1116+
pub fn finish(self) -> flatbuffers::WIPOffset<NotFound<'a>> {
1117+
let o = self.fbb_.end_table(self.start_);
1118+
self.fbb_
1119+
.required(o, NotFound::VT_INSTANCE_ID, "instance_id");
1120+
flatbuffers::WIPOffset::new(o.value())
1121+
}
1122+
}
1123+
1124+
impl core::fmt::Debug for NotFound<'_> {
1125+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1126+
let mut ds = f.debug_struct("NotFound");
1127+
ds.field("instance_id", &self.instance_id());
1128+
ds.finish()
1129+
}
1130+
}
9991131
pub enum ContractResponseOffset {}
10001132
#[derive(Copy, Clone, PartialEq)]
10011133

@@ -1122,6 +1254,20 @@ pub mod host_response {
11221254
None
11231255
}
11241256
}
1257+
1258+
#[inline]
1259+
#[allow(non_snake_case)]
1260+
pub fn contract_response_as_not_found(&self) -> Option<NotFound<'a>> {
1261+
if self.contract_response_type() == ContractResponseType::NotFound {
1262+
let u = self.contract_response();
1263+
// Safety:
1264+
// Created from a valid Table for this object
1265+
// Which contains a valid union in this slot
1266+
Some(unsafe { NotFound::init_from_table(u) })
1267+
} else {
1268+
None
1269+
}
1270+
}
11251271
}
11261272

11271273
impl flatbuffers::Verifiable for ContractResponse<'_> {
@@ -1138,6 +1284,7 @@ pub mod host_response {
11381284
ContractResponseType::PutResponse => v.verify_union_variant::<flatbuffers::ForwardsUOffset<PutResponse>>("ContractResponseType::PutResponse", pos),
11391285
ContractResponseType::UpdateNotification => v.verify_union_variant::<flatbuffers::ForwardsUOffset<UpdateNotification>>("ContractResponseType::UpdateNotification", pos),
11401286
ContractResponseType::UpdateResponse => v.verify_union_variant::<flatbuffers::ForwardsUOffset<UpdateResponse>>("ContractResponseType::UpdateResponse", pos),
1287+
ContractResponseType::NotFound => v.verify_union_variant::<flatbuffers::ForwardsUOffset<NotFound>>("ContractResponseType::NotFound", pos),
11411288
_ => Ok(()),
11421289
}
11431290
})?
@@ -1249,6 +1396,16 @@ pub mod host_response {
12491396
)
12501397
}
12511398
}
1399+
ContractResponseType::NotFound => {
1400+
if let Some(x) = self.contract_response_as_not_found() {
1401+
ds.field("contract_response", &x)
1402+
} else {
1403+
ds.field(
1404+
"contract_response",
1405+
&"InvalidFlatbuffer: Union discriminant does not match value.",
1406+
)
1407+
}
1408+
}
12521409
_ => {
12531410
let x: Option<()> = None;
12541411
ds.field("contract_response", &x)
@@ -3338,7 +3495,7 @@ pub mod host_response {
33383495
/// `root_as_host_response_unchecked`.
33393496
pub fn root_as_host_response(
33403497
buf: &[u8],
3341-
) -> Result<HostResponse<'_>, flatbuffers::InvalidFlatbuffer> {
3498+
) -> Result<HostResponse, flatbuffers::InvalidFlatbuffer> {
33423499
flatbuffers::root::<HostResponse>(buf)
33433500
}
33443501
#[inline]
@@ -3350,7 +3507,7 @@ pub mod host_response {
33503507
/// `size_prefixed_root_as_host_response_unchecked`.
33513508
pub fn size_prefixed_root_as_host_response(
33523509
buf: &[u8],
3353-
) -> Result<HostResponse<'_>, flatbuffers::InvalidFlatbuffer> {
3510+
) -> Result<HostResponse, flatbuffers::InvalidFlatbuffer> {
33543511
flatbuffers::size_prefixed_root::<HostResponse>(buf)
33553512
}
33563513
#[inline]
@@ -3383,14 +3540,14 @@ pub mod host_response {
33833540
/// Assumes, without verification, that a buffer of bytes contains a HostResponse and returns it.
33843541
/// # Safety
33853542
/// Callers must trust the given bytes do indeed contain a valid `HostResponse`.
3386-
pub unsafe fn root_as_host_response_unchecked(buf: &[u8]) -> HostResponse<'_> {
3543+
pub unsafe fn root_as_host_response_unchecked(buf: &[u8]) -> HostResponse {
33873544
flatbuffers::root_unchecked::<HostResponse>(buf)
33883545
}
33893546
#[inline]
33903547
/// Assumes, without verification, that a buffer of bytes contains a size prefixed HostResponse and returns it.
33913548
/// # Safety
33923549
/// Callers must trust the given bytes do indeed contain a valid size prefixed `HostResponse`.
3393-
pub unsafe fn size_prefixed_root_as_host_response_unchecked(buf: &[u8]) -> HostResponse<'_> {
3550+
pub unsafe fn size_prefixed_root_as_host_response_unchecked(buf: &[u8]) -> HostResponse {
33943551
flatbuffers::size_prefixed_root_unchecked::<HostResponse>(buf)
33953552
}
33963553
#[inline]

0 commit comments

Comments
 (0)