Skip to content

Commit 57e04db

Browse files
committed
fix: try to minimize the key sizes
1 parent c0dbe96 commit 57e04db

File tree

8 files changed

+108
-129
lines changed

8 files changed

+108
-129
lines changed

contracts/registry/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
use loam_sdk::soroban_sdk;
33
use loam_subcontract_core::{admin::Admin, Core};
44

5-
use metadata::PublishedWasm;
65
use registry::{
7-
contract::Contract as Contract_, Claimable, Deployable, DevDeployable, Publishable
6+
contract::Contract as Contract_, wasm::Wasm, Claimable, Deployable, DevDeployable,
7+
Publishable,
88
};
99

1010
pub mod error;
@@ -18,7 +18,7 @@ use version::Version;
1818

1919
#[loam_sdk::derive_contract(
2020
Core(Admin),
21-
Publishable(PublishedWasm),
21+
Publishable(Wasm),
2222
Deployable(Contract_),
2323
Claimable(Contract_),
2424
DevDeployable(Contract_)

contracts/registry/src/metadata.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
mod contract;
2-
pub use contract::*;
1+
use loam_sdk::soroban_sdk::{self, contracttype, to_string, BytesN, String};
32

4-
pub mod wasm;
5-
pub use wasm::*;
3+
4+
5+
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Default)]
6+
#[contracttype]
7+
pub struct Metadata {
8+
pub repo: Option<String>,
9+
}
10+
11+
impl Metadata {
12+
pub fn new(repo: String) -> Self {
13+
Self { repo: Some(repo) }
14+
}
15+
}
16+
17+
/// Contains info about specific version of published binary
18+
#[contracttype]
19+
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd,)]
20+
pub struct PublishedWasm {
21+
pub hash: BytesN<32>,
22+
pub metadata: Metadata,
23+
}

contracts/registry/src/metadata/contract.rs

Lines changed: 0 additions & 68 deletions
This file was deleted.

contracts/registry/src/metadata/wasm.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

contracts/registry/src/registry.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use crate::{
88
pub mod contract;
99
pub mod wasm;
1010

11+
pub use contract::Contract;
12+
pub use wasm::Wasm;
13+
1114
#[loam_sdk::subcontract]
1215
pub trait IsPublishable {
1316
/// Fetch the hash from the registry
@@ -27,7 +30,7 @@ pub trait IsPublishable {
2730
&self,
2831
contract_name: soroban_sdk::String,
2932
version: Option<Version>,
30-
) -> Result<crate::metadata::Wasm, Error>;
33+
) -> Result<crate::metadata::PublishedWasm, Error>;
3134

3235
/// Publish a binary. If contract had been previously published only previous author can publish again
3336
fn publish(

contracts/registry/src/registry/contract.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use loam_sdk::{
88
};
99

1010
use crate::{
11-
error::Error, metadata::PublishedWasm, registry::Publishable, util::hash_string,
11+
error::Error, registry::Publishable, util::hash_string,
1212
version::Version, Contract as Contract_,
1313
};
1414

15-
use super::{IsClaimable, IsDeployable, IsDevDeployable};
15+
use super::{wasm::Wasm, IsClaimable, IsDeployable, IsDevDeployable};
1616

1717
loam_sdk::import_contract!(example_core);
1818

@@ -87,7 +87,7 @@ impl IsDeployable for Contract {
8787
// Publish a deploy event
8888
let version = version.map_or_else(
8989
|| {
90-
PublishedWasm::default().most_recent_version(&contract_name)
90+
Wasm::default().most_recent_version(&contract_name)
9191
},
9292
Ok,
9393
)?;

contracts/registry/src/registry/wasm.rs

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,73 @@
1-
use loam_sdk::soroban_sdk::{self, env, Address, String};
1+
use loam_sdk::{loamstorage, soroban_sdk::{self, env, Address, LoamKey, PersistentMap, String}};
22

33
use crate::{
44
error::Error,
5-
metadata::{Metadata, PublishedWasm, Wasm},
5+
metadata::{Metadata, PublishedWasm},
66
version::{self, Version, INITAL_VERSION},
77
};
88

99
use super::IsPublishable;
1010

11-
impl IsPublishable for PublishedWasm {
12-
fn fetch(&self, contract_name: String, version: Option<Version>) -> Result<Wasm, Error> {
11+
/// Contains
12+
#[loamstorage]
13+
pub struct Wasm {
14+
pub versions: PersistentMap<(String, Version), PublishedWasm>,
15+
pub author: PersistentMap<String, Address>,
16+
pub most_recent_version: PersistentMap<String, Version>,
17+
}
18+
19+
impl Wasm {
20+
pub fn new(name: &String, author: Address) -> Self {
21+
let mut s = Self::default();
22+
s.author.set(name.clone(), &author);
23+
s
24+
}
25+
}
26+
27+
impl Wasm {
28+
pub fn most_recent_version(&self, name: &String) -> Result<Version, Error> {
29+
self.most_recent_version
30+
.get(name.clone())
31+
.ok_or(Error::NoSuchVersion)
32+
}
33+
34+
pub fn set_most_recent_version(&mut self, name: &String, version: Version) {
35+
self.most_recent_version.set(name.clone(), &version);
36+
}
37+
38+
pub fn get(&self, name: &String, version: Option<Version>) -> Result<PublishedWasm, Error> {
39+
let version = if let Some(version) = version {
40+
version
41+
} else {
42+
self.most_recent_version(name)?
43+
};
44+
self.versions
45+
.get((name.clone(), version))
46+
.ok_or(Error::NoSuchVersion)
47+
}
48+
49+
pub fn set(
50+
&mut self,
51+
name: String,
52+
version: Option<Version>,
53+
binary: PublishedWasm,
54+
) -> Result<(), Error> {
55+
let version = if let Some(version) = version {
56+
version
57+
} else {
58+
self.most_recent_version(&name)?
59+
};
60+
self.versions.set((name, version), &binary);
61+
Ok(())
62+
}
63+
64+
pub fn author(&self, name: &String) -> Option<Address> {
65+
self.author.get(name.clone())
66+
}
67+
}
68+
69+
impl IsPublishable for Wasm {
70+
fn fetch(&self, contract_name: String, version: Option<Version>) -> Result<PublishedWasm, Error> {
1371
self.get(&contract_name, version)
1472
}
1573

@@ -51,13 +109,13 @@ impl IsPublishable for PublishedWasm {
51109
new_version.log();
52110

53111
let metadata = if let Some(repo) = repo {
54-
Metadata { repo }
112+
Metadata::new(repo)
55113
} else if new_version == INITAL_VERSION {
56114
Metadata::default()
57115
} else {
58116
self.get(&wasm_name, Some(last_version))?.metadata
59117
};
60-
let published_binary = Wasm {
118+
let published_binary = PublishedWasm {
61119
hash: wasm_hash,
62120
metadata,
63121
};

contracts/registry/src/version.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,10 @@ use loam_sdk::soroban_sdk::{self, contracttype, env, log, Env};
55
/// Represents the version of the contract
66
#[contracttype]
77
#[derive(Default, Eq, PartialEq, Clone, Debug)]
8-
pub struct Version {
9-
patch: u32,
10-
minor: u32,
11-
major: u32,
12-
}
8+
pub struct Version(u32, u32, u32);
9+
1310

14-
pub const INITAL_VERSION: Version = Version {
15-
major: 0,
16-
minor: 0,
17-
patch: 1,
18-
};
11+
pub const INITAL_VERSION: Version = Version(0,0,1);
1912

2013
impl Display for Version {
2114
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
@@ -30,21 +23,21 @@ impl Version {
3023

3124
#[must_use]
3225
pub fn publish_patch(mut self) -> Self {
33-
self.patch += 1;
26+
self.2 += 1;
3427
self
3528
}
3629

3730
#[must_use]
3831
pub fn publish_minor(mut self) -> Self {
39-
self.minor += 1;
40-
self.patch = 0;
32+
self.1 += 1;
33+
self.2 = 0;
4134
self
4235
}
4336
#[must_use]
4437
pub fn publish_major(mut self) -> Self {
45-
self.major += 1;
46-
self.minor = 0;
47-
self.patch = 0;
38+
self.0 += 1;
39+
self.1 = 0;
40+
self.2 = 0;
4841
self
4942
}
5043

@@ -57,15 +50,15 @@ impl Version {
5750
}
5851
}
5952
pub fn patch(&self) -> u32 {
60-
self.patch
53+
self.2
6154
}
6255

6356
pub fn minor(&self) -> u32 {
64-
self.minor
57+
self.1
6558
}
6659

6760
pub fn major(&self) -> u32 {
68-
self.major
61+
self.0
6962
}
7063
}
7164

0 commit comments

Comments
 (0)