Skip to content

Commit 0c508d3

Browse files
committed
piecrust: commit-specific storage for bytecodes
piecrust: clippy correction
1 parent c53439c commit 0c508d3

5 files changed

Lines changed: 74 additions & 8 deletions

File tree

piecrust/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525

2626
### Added
2727

28+
- Enriched commit-specific storage to support contract bytecodes [#450]
2829
- Added blocking 'init' method to be called only during deployment [#433]
2930

3031
## [0.27.2] - 2025-02-20
@@ -518,6 +519,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
518519

519520
<!-- ISSUES -->
520521

522+
[#450]: https://github.com/dusk-network/piecrust/issues/450
521523
[#rusk_3470]: https://github.com/dusk-network/rusk/issues/3470
522524
[#rusk_3341]: https://github.com/dusk-network/rusk/issues/3341
523525
[#440]: https://github.com/dusk-network/piecrust/issues/440

piecrust/src/store/commit/finalizer.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use crate::store::baseinfo::BaseInfo;
88
use crate::store::hasher::Hash;
99
use crate::store::{
10-
BASE_FILE, ELEMENT_FILE, LEAF_DIR, MAIN_DIR, MEMORY_DIR, TREE_POS_FILE,
11-
TREE_POS_OPT_FILE,
10+
BASE_FILE, BYTECODE_DIR, ELEMENT_FILE, LEAF_DIR, MAIN_DIR, MEMORY_DIR,
11+
METADATA_EXTENSION, OBJECTCODE_EXTENSION, TREE_POS_FILE, TREE_POS_OPT_FILE,
1212
};
1313
use std::path::Path;
1414
use std::{fs, io};
@@ -26,6 +26,27 @@ impl CommitFinalizer {
2626
let base_info = BaseInfo::from_path(&base_info_path)?;
2727
for contract_hint in base_info.contract_hints {
2828
let contract_hex = hex::encode(contract_hint);
29+
// BYTECODE
30+
let bytecode_src_dir = main_dir.join(BYTECODE_DIR).join(&root);
31+
let bytecode_src_path = bytecode_src_dir.join(&contract_hex);
32+
let module_src_path =
33+
bytecode_src_path.with_extension(OBJECTCODE_EXTENSION);
34+
let metadata_src_path =
35+
bytecode_src_path.with_extension(METADATA_EXTENSION);
36+
let bytecode_dst_path =
37+
main_dir.join(BYTECODE_DIR).join(&contract_hex);
38+
let module_dst_path =
39+
bytecode_dst_path.with_extension(OBJECTCODE_EXTENSION);
40+
let metadata_dst_path =
41+
bytecode_dst_path.with_extension(METADATA_EXTENSION);
42+
if bytecode_src_path.is_file()
43+
&& module_src_path.is_file()
44+
&& metadata_src_path.is_file()
45+
{
46+
fs::rename(&bytecode_src_path, bytecode_dst_path)?;
47+
fs::rename(&module_src_path, module_dst_path)?;
48+
fs::rename(&metadata_src_path, metadata_dst_path)?;
49+
}
2950
// MEMORY
3051
let src_path =
3152
main_dir.join(MEMORY_DIR).join(&contract_hex).join(&root);

piecrust/src/store/commit/reader.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ impl CommitReader {
121121

122122
// Check that all contracts in the index file have a corresponding
123123
// bytecode and memory pages specified.
124-
let bytecode_path = bytecode_dir.join(&contract_hex);
124+
let bytecode_path = commit_id
125+
.as_ref()
126+
.map(|cid| bytecode_dir.join(cid).join(&contract_hex))
127+
.filter(|p| p.is_file())
128+
.unwrap_or(bytecode_dir.join(&contract_hex));
125129
if !bytecode_path.is_file() {
126130
return Err(io::Error::new(
127131
io::ErrorKind::InvalidData,

piecrust/src/store/commit/writer.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ impl CommitWriter {
8989
struct Directories {
9090
main_dir: PathBuf,
9191
bytecode_main_dir: PathBuf,
92+
bytecode_commit_dir: PathBuf,
9293
memory_main_dir: PathBuf,
9394
leaf_main_dir: PathBuf,
9495
}
@@ -100,6 +101,10 @@ impl CommitWriter {
100101
let bytecode_main_dir = main_dir.join(BYTECODE_DIR);
101102
fs::create_dir_all(&bytecode_main_dir)?;
102103

104+
let bytecode_commit_dir =
105+
bytecode_main_dir.join(commit_id.as_ref());
106+
fs::create_dir_all(&bytecode_commit_dir)?;
107+
103108
let memory_main_dir = main_dir.join(MEMORY_DIR);
104109
fs::create_dir_all(&memory_main_dir)?;
105110

@@ -109,6 +114,7 @@ impl CommitWriter {
109114
Directories {
110115
main_dir,
111116
bytecode_main_dir,
117+
bytecode_commit_dir,
112118
memory_main_dir,
113119
leaf_main_dir,
114120
}
@@ -149,13 +155,33 @@ impl CommitWriter {
149155
let metadata_main_path =
150156
bytecode_main_path.with_extension(METADATA_EXTENSION);
151157

158+
let bytecode_commit_path =
159+
directories.bytecode_commit_dir.join(&contract_hex);
160+
let module_commit_path =
161+
bytecode_commit_path.with_extension(OBJECTCODE_EXTENSION);
162+
let metadata_commit_path =
163+
bytecode_commit_path.with_extension(METADATA_EXTENSION);
164+
152165
// If the contract is new, we write the bytecode, module, and
153166
// metadata files to disk.
154167
if contract_data.is_new {
155168
// we write them to the main location
156-
fs::write(bytecode_main_path, &contract_data.bytecode)?;
157-
fs::write(module_main_path, contract_data.module.serialize())?;
158-
fs::write(metadata_main_path, &contract_data.metadata)?;
169+
fs::write(bytecode_commit_path, &contract_data.bytecode)?;
170+
fs::write(
171+
module_commit_path,
172+
contract_data.module.serialize(),
173+
)?;
174+
fs::write(metadata_commit_path, &contract_data.metadata)?;
175+
// if main does not have this contract yet, we store it there as
176+
// well
177+
if !bytecode_main_path.is_file() {
178+
fs::write(bytecode_main_path, &contract_data.bytecode)?;
179+
fs::write(
180+
module_main_path,
181+
contract_data.module.serialize(),
182+
)?;
183+
fs::write(metadata_main_path, &contract_data.metadata)?;
184+
}
159185
dirty = true;
160186
}
161187
if dirty {

piecrust/src/store/session.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,21 @@ impl ContractSession {
297297

298298
let contract_hex = hex::encode(contract);
299299

300-
let bytecode_path =
301-
base_dir.join(BYTECODE_DIR).join(&contract_hex);
300+
let bytecode_path = commit_id
301+
.as_ref()
302+
.map(|hash| {
303+
base_dir
304+
.join(BYTECODE_DIR)
305+
.join(hex::encode(hash.as_bytes()))
306+
.join(&contract_hex)
307+
})
308+
.filter(|p| p.is_file())
309+
.unwrap_or(
310+
base_dir
311+
.join(BYTECODE_DIR)
312+
.join(&contract_hex),
313+
);
314+
302315
let module_path = bytecode_path
303316
.with_extension(OBJECTCODE_EXTENSION);
304317
let metadata_path = bytecode_path

0 commit comments

Comments
 (0)