Skip to content

Commit 46b48d7

Browse files
committed
loading storage file and read code from the file
1 parent ede5ae1 commit 46b48d7

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

tanour/src/contract.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Contract {
4848
let file_path = Path::new(&params.storage_path)
4949
.join(format!("{}.storage", crate::address_to_hex(address)));
5050
let storage_file = StorageFile::create(
51-
file_path.to_str().unwrap(),
51+
file_path.to_str().unwrap(), // TODO: no unwrap
5252
storage_size_in_mb,
5353
owner,
5454
current_block_no,
@@ -71,23 +71,30 @@ impl Contract {
7171
})
7272
}
7373

74-
// pub fn load(
75-
// provider: P,
76-
// address: &Address,
77-
// params: Params,
78-
// ) -> Result<Self> {
79-
80-
// let state = Arc::new(Mutex::new(State::new(provider, PAGE_SIZE)));
81-
// let executor =
82-
// wasmer::WasmerExecutor::new(code, memory_limit_page, metering_limit, state.clone())?;
83-
84-
// Ok(Contract {
85-
// executor: Box::new(executor),
86-
// _state: state,
87-
// buffer: Vec::new(),
88-
// address: address.clone(),
89-
// })
90-
// }
74+
pub fn load(
75+
blockchain_api: Box<dyn BlockchainAPI>,
76+
address: &Address,
77+
params: Params,
78+
) -> Result<Self> {
79+
let file_path = Path::new(&params.storage_path)
80+
.join(format!("{}.storage", crate::address_to_hex(address)));
81+
let mut storage_file = StorageFile::load(file_path.to_str().unwrap())?;
82+
let code = storage_file.read_code()?;
83+
let state = Arc::new(Mutex::new(Provider::new(blockchain_api, storage_file)));
84+
let executor = wasmer::WasmerExecutor::new(
85+
&code,
86+
params.memory_limit_page,
87+
params.metering_limit,
88+
state.clone(),
89+
)?;
90+
91+
Ok(Contract {
92+
executor: Box::new(executor),
93+
_state: state,
94+
buffer: Vec::new(),
95+
_address: *address,
96+
})
97+
}
9198

9299
fn call_exported_fn<'a, E: Encode<()>, D: Decode<'a, ()>>(
93100
&'a mut self,

tanour/src/storage_file.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use crate::{
2-
error::{Result},
3-
page::Page,
4-
Address,
5-
};
1+
use crate::{error::Result, page::Page, Address};
62
use std::{
73
collections::{hash_map::Entry, HashMap},
84
io::Write,
@@ -22,8 +18,9 @@ struct Header {
2218
created_at: u32,
2319
valid_until: u32,
2420
code_offset: u32,
21+
code_length: u32,
2522
data_offset: u32,
26-
reserved: [u8; 88],
23+
reserved: [u8; 84],
2724
}
2825

2926
const PAGE_SIZE: u32 = 1024 * 1024; // 1 MB
@@ -53,8 +50,8 @@ impl StorageFile {
5350
file.write_all(&zeros)?;
5451

5552
let code_offset = std::mem::size_of::<Header>() as u32;
56-
let code_len = code.len() as u32;
57-
let data_offset = code_offset + code_len + (128 - (code_len % 128));
53+
let code_length = code.len() as u32;
54+
let data_offset = code_offset + code_length + (128 - (code_length % 128));
5855

5956
let header = Header {
6057
bom: 0x7374,
@@ -63,8 +60,9 @@ impl StorageFile {
6360
created_at,
6461
valid_until,
6562
code_offset,
63+
code_length,
6664
data_offset,
67-
reserved: [0; 88],
65+
reserved: [0; 84],
6866
};
6967

7068
file.rewind()?;
@@ -102,6 +100,14 @@ impl StorageFile {
102100
})
103101
}
104102

103+
pub fn read_code(&mut self) -> Result<Vec<u8>> {
104+
let mut code = vec![0u8; self.header.code_length as usize];
105+
self.file
106+
.seek(SeekFrom::Start(self.header.code_offset as u64))?;
107+
self.file.read_exact(&mut code)?;
108+
Ok(code)
109+
}
110+
105111
pub fn read_storage(&mut self, offset: u32, length: u32) -> Result<Vec<u8>> {
106112
log::debug!("read_storage, offset: {offset}, length: {length}");
107113
let first_page = offset / PAGE_SIZE;

0 commit comments

Comments
 (0)