Skip to content

Commit 3eb9458

Browse files
luizstacioxgreenx
andauthored
feat: expose offsets_with_data on configurables (#1685)
- Expose offsets_with_data on Configurables this allows for better DX when interacting with the Standard SRC12 that requires configurable to be provided as `Vec<(u64, Vec<u8>)>`. Note: I have made `offsets_with_data` public and also implement `Into`. I'm not sure what is the best solution here, so once reviewed it can be decided how to procede. Ex.: /// Current way to implement: ```rs // For this two info requires manual change or reading the JSON const PROXY_ORACLE_ID_OFFSET: u64 = 38488; const PROXY_OWNER_OFFSET: u64 = 38440; pub fn build_trade_account_proxy_configurables( oracle_id: ContractId, owner: Identity, ) -> Vec<(u64, Vec<u8>)> { let mut my_configurables: Vec<(u64, Vec<u8>)> = Vec::with_capacity(2); my_configurables.push((PROXY_ORACLE_ID_OFFSET, oracle_id.to_vec())); let mut owner_data: Vec<u8> = Vec::new(); owner_data.extend_from_slice(&[0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8]); // State::Initialized match owner { Identity::Address(address) => { owner_data.extend_from_slice(&[0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]); // Identity::Address owner_data.extend_from_slice(&*address.to_vec()); } Identity::ContractId(contract_id) => { owner_data.extend_from_slice(&[0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8]); // Identity::ContractId owner_data.extend_from_slice(&*contract_id.to_vec()); } } my_configurables.push((PROXY_OWNER_OFFSET, owner_data)); my_configurables } ``` /// With the changes ```rs pub fn build_trade_account_proxy_configurables( oracle_id: ContractId, owner: Identity, ) -> Vec<(u64, Vec<u8>)> { TradingAccountProxyConfigurables::default() .with_ORACLE_CONTRACT_ID(oracle_id) .unwrap() .with_INITIAL_OWNER(State::Initialized(owner)) .unwrap() .into() } ``` The standard implementation is defined as: ```sway impl SRC12 for Contract { #[storage(read, write)] fn register_contract( child_contract: ContractId, configurables: Option<Vec<(u64, Vec<u8>)>>, ) -> Result<BytecodeRoot, str> { ``` --------- Co-authored-by: green <[email protected]>
1 parent d0a0ce7 commit 3eb9458

File tree

6 files changed

+81
-28
lines changed

6 files changed

+81
-28
lines changed

docs/src/codec/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ All types generated by the [`abigen!`](../abigen/index.md) macro implement both
1212

1313
`fuels` also contains implementations for:
1414

15-
- [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html) for the `fuels`-owned types listed [here](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html#implementors) as well as [for some foreign types](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html#foreign-impls) (such as `u8`, `u16`, `std::vec::Vec<T: Tokenizable>`, etc.).
16-
- [`Parameterize`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html) for the `fuels`-owned types listed [here](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html#implementors) as well as [for some foreign types](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html#foreign-impls) (such as `u8`, `u16`, `std::vec::Vec<T: Parameterize>`, etc.).
15+
- [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html) for the `fuels`-owned types listed [here](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html#implementers) as well as [for some foreign types](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html#foreign-impls) (such as `u8`, `u16`, `std::vec::Vec<T: Tokenizable>`, etc.).
16+
- [`Parameterize`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html) for the `fuels`-owned types listed [here](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html#implementers) as well as [for some foreign types](https://docs.rs/fuels/latest/fuels/core/traits/trait.Parameterize.html#foreign-impls) (such as `u8`, `u16`, `std::vec::Vec<T: Parameterize>`, etc.).
1717

1818
## Deriving the traits
1919

packages/fuels-code-gen/src/program_bindings/abigen/configurables.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn generate_struct_decl(configurable_struct_name: &Ident) -> TokenStream {
5050
quote! {
5151
#[derive(Clone, Debug, Default)]
5252
pub struct #configurable_struct_name {
53-
offsets_with_data: ::std::vec::Vec<(u64, ::std::vec::Vec<u8>)>,
53+
offsets_with_data: ::std::vec::Vec<::fuels::core::Configurable>,
5454
encoder: ::fuels::core::codec::ABIEncoder,
5555
}
5656
}
@@ -89,7 +89,10 @@ fn generate_builder_methods(resolved_configurables: &[ResolvedConfigurable]) ->
8989
// Generate the `with_XXX` methods for setting the configurables
9090
pub fn #name(mut self, value: #ttype) -> ::fuels::prelude::Result<Self> {
9191
let encoded = #encoder_code?;
92-
self.offsets_with_data.push((#offset, encoded));
92+
self.offsets_with_data.push(::fuels::core::Configurable {
93+
offset: #offset,
94+
data: encoded,
95+
});
9396
::fuels::prelude::Result::Ok(self)
9497
}
9598
}
@@ -116,5 +119,10 @@ fn generate_from_impl(configurable_struct_name: &Ident) -> TokenStream {
116119
::fuels::core::Configurables::new(config.offsets_with_data)
117120
}
118121
}
122+
impl From<#configurable_struct_name> for ::std::vec::Vec<::fuels::core::Configurable> {
123+
fn from(config: #configurable_struct_name) -> ::std::vec::Vec<::fuels::core::Configurable> {
124+
config.offsets_with_data
125+
}
126+
}
119127
}
120128
}

packages/fuels-core/src/lib.rs

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,47 @@ pub use utils::*;
77

88
use crate::types::errors::Result;
99

10+
#[derive(Debug, Clone, Default, PartialEq)]
11+
pub struct Configurable {
12+
/// The offset (in bytes) within the binary where the data is located.
13+
pub offset: u64,
14+
/// The data related to the configurable.
15+
pub data: Vec<u8>,
16+
}
17+
1018
#[derive(Debug, Clone, Default, PartialEq)]
1119
pub struct Configurables {
12-
offsets_with_data: Vec<(u64, Vec<u8>)>,
20+
pub offsets_with_data: Vec<Configurable>,
1321
}
1422

1523
impl Configurables {
16-
pub fn new(offsets_with_data: Vec<(u64, Vec<u8>)>) -> Self {
24+
pub fn new(offsets_with_data: Vec<Configurable>) -> Self {
1725
Self { offsets_with_data }
1826
}
1927

2028
pub fn with_shifted_offsets(self, shift: i64) -> Result<Self> {
2129
let new_offsets_with_data = self
2230
.offsets_with_data
2331
.into_iter()
24-
.map(|(offset, data)| {
32+
.map(|c| {
2533
let new_offset = if shift.is_negative() {
26-
offset.checked_sub(shift.unsigned_abs())
34+
c.offset.checked_sub(shift.unsigned_abs())
2735
} else {
28-
offset.checked_add(shift.unsigned_abs())
36+
c.offset.checked_add(shift.unsigned_abs())
2937
};
3038

3139
let new_offset = new_offset.ok_or_else(|| {
3240
crate::error!(
3341
Other,
34-
"Overflow occurred while shifting offset: {offset} + {shift}"
42+
"Overflow occurred while shifting offset: {} + {shift}",
43+
c.offset
3544
)
3645
})?;
3746

38-
Ok((new_offset, data.clone()))
47+
Ok(Configurable {
48+
offset: new_offset,
49+
data: c.data,
50+
})
3951
})
4052
.collect::<Result<Vec<_>>>()?;
4153

@@ -45,23 +57,35 @@ impl Configurables {
4557
}
4658

4759
pub fn update_constants_in(&self, binary: &mut [u8]) {
48-
for (offset, data) in &self.offsets_with_data {
49-
let offset = *offset as usize;
50-
binary[offset..offset + data.len()].copy_from_slice(data)
60+
for c in &self.offsets_with_data {
61+
let offset = c.offset as usize;
62+
binary[offset..offset + c.data.len()].copy_from_slice(&c.data)
5163
}
5264
}
5365
}
5466

67+
impl From<Configurables> for Vec<Configurable> {
68+
fn from(config: Configurables) -> Vec<Configurable> {
69+
config.offsets_with_data.clone()
70+
}
71+
}
72+
5573
#[cfg(test)]
5674
mod tests {
5775
use super::*;
5876

5977
#[test]
6078
fn test_with_shifted_offsets_positive_shift() {
61-
let offsets_with_data = vec![(10u64, vec![1, 2, 3])];
79+
let offsets_with_data = vec![Configurable {
80+
offset: 10u64,
81+
data: vec![1, 2, 3],
82+
}];
6283
let configurables = Configurables::new(offsets_with_data.clone());
6384
let shifted_configurables = configurables.with_shifted_offsets(5).unwrap();
64-
let expected_offsets_with_data = vec![(15u64, vec![1, 2, 3])];
85+
let expected_offsets_with_data = vec![Configurable {
86+
offset: 15u64,
87+
data: vec![1, 2, 3],
88+
}];
6589
assert_eq!(
6690
shifted_configurables.offsets_with_data,
6791
expected_offsets_with_data
@@ -70,10 +94,16 @@ mod tests {
7094

7195
#[test]
7296
fn test_with_shifted_offsets_negative_shift() {
73-
let offsets_with_data = vec![(10u64, vec![4, 5, 6])];
97+
let offsets_with_data = vec![Configurable {
98+
offset: 10u64,
99+
data: vec![4, 5, 6],
100+
}];
74101
let configurables = Configurables::new(offsets_with_data.clone());
75102
let shifted_configurables = configurables.with_shifted_offsets(-5).unwrap();
76-
let expected_offsets_with_data = vec![(5u64, vec![4, 5, 6])];
103+
let expected_offsets_with_data = vec![Configurable {
104+
offset: 5u64,
105+
data: vec![4, 5, 6],
106+
}];
77107
assert_eq!(
78108
shifted_configurables.offsets_with_data,
79109
expected_offsets_with_data
@@ -82,7 +112,10 @@ mod tests {
82112

83113
#[test]
84114
fn test_with_shifted_offsets_zero_shift() {
85-
let offsets_with_data = vec![(20u64, vec![7, 8, 9])];
115+
let offsets_with_data = vec![Configurable {
116+
offset: 20u64,
117+
data: vec![7, 8, 9],
118+
}];
86119
let configurables = Configurables::new(offsets_with_data.clone());
87120
let shifted_configurables = configurables.with_shifted_offsets(0).unwrap();
88121
let expected_offsets_with_data = offsets_with_data;
@@ -94,7 +127,10 @@ mod tests {
94127

95128
#[test]
96129
fn test_with_shifted_offsets_overflow() {
97-
let offsets_with_data = vec![(u64::MAX - 1, vec![1, 2, 3])];
130+
let offsets_with_data = vec![Configurable {
131+
offset: u64::MAX - 1,
132+
data: vec![1, 2, 3],
133+
}];
98134
let configurables = Configurables::new(offsets_with_data);
99135
let result = configurables.with_shifted_offsets(10);
100136
assert!(result.is_err());
@@ -108,7 +144,10 @@ mod tests {
108144

109145
#[test]
110146
fn test_with_shifted_offsets_underflow() {
111-
let offsets_with_data = vec![(5, vec![4, 5, 6])];
147+
let offsets_with_data = vec![Configurable {
148+
offset: 5u64,
149+
data: vec![4, 5, 6],
150+
}];
112151
let configurables = Configurables::new(offsets_with_data);
113152
let result = configurables.with_shifted_offsets(-10);
114153
assert!(result.is_err());

packages/fuels-programs/src/executable.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ fn validate_loader_can_be_made_from_code(
213213
mod tests {
214214
use std::io::Write;
215215

216-
use fuels_core::Configurables;
216+
use fuels_core::{Configurable, Configurables};
217217
use tempfile::NamedTempFile;
218218

219219
use super::*;
@@ -272,7 +272,10 @@ mod tests {
272272
// Given: An Executable<Regular> and some configurables
273273
let code = vec![1u8, 2, 3, 4];
274274
let executable = Executable::<Regular>::from_bytes(code);
275-
let configurables = Configurables::new(vec![(2, vec![1])]);
275+
let configurables = Configurables::new(vec![Configurable {
276+
offset: 2,
277+
data: vec![1],
278+
}]);
276279

277280
// When: Setting new configurables
278281
let new_executable = executable.with_configurables(configurables.clone());
@@ -285,7 +288,10 @@ mod tests {
285288
fn test_executable_regular_code() {
286289
// Given: An Executable<Regular> with some code and configurables
287290
let code = vec![1u8, 2, 3, 4];
288-
let configurables = Configurables::new(vec![(1, vec![1])]);
291+
let configurables = Configurables::new(vec![Configurable {
292+
offset: 1,
293+
data: vec![1],
294+
}]);
289295
let executable =
290296
Executable::<Regular>::from_bytes(code.clone()).with_configurables(configurables);
291297

packages/fuels-test-helpers/src/service.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::net::SocketAddr;
2-
31
#[cfg(feature = "fuel-core-lib")]
42
use fuel_core::service::{Config as ServiceConfig, FuelService as CoreFuelService};
53
use fuel_core_chain_config::{ChainConfig, StateConfig};
64
use fuel_core_services::State;
75
use fuels_core::types::errors::{Result, error};
6+
use std::net::SocketAddr;
87

98
use crate::NodeConfig;
109
#[cfg(not(feature = "fuel-core-lib"))]
@@ -90,7 +89,8 @@ impl FuelService {
9089
columns_policy: ColumnsPolicy::Lazy,
9190
},
9291
#[cfg(feature = "rocksdb")]
93-
state_rewind_policy: Default::default(),
92+
state_rewind_policy:
93+
fuel_core::state::historical_rocksdb::StateRewindPolicy::RewindFullRange,
9494
};
9595

9696
ServiceConfig {

packages/fuels/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub mod programs {
3838
}
3939

4040
pub mod core {
41-
pub use fuels_core::{Configurables, codec, constants, offsets, traits};
41+
pub use fuels_core::{Configurable, Configurables, codec, constants, offsets, traits};
4242
}
4343

4444
pub mod crypto {

0 commit comments

Comments
 (0)