Skip to content

remove codex duplication #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions api/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use chrono::{DateTime, Utc};
use strict_encoding::TypeName;
use strict_types::{StrictVal, TypeSystem};
use ultrasonic::{
fe256, AuthToken, CallId, CellAddr, CodexId, Consensus, Contract, ContractId, ContractMeta, ContractName, Genesis,
Identity, Input, Operation, StateCell, StateData, StateValue,
fe256, AuthToken, CallId, CellAddr, Codex, CodexId, Consensus, Contract, ContractId, ContractMeta, ContractName,
Genesis, Identity, Input, Operation, StateCell, StateData, StateValue,
};

use crate::{Api, Articles, DataCell, MethodName, Schema, StateAtom, StateName};
Expand Down Expand Up @@ -62,20 +62,37 @@ pub struct IssueParams {
}

impl Schema {
pub fn start_issue(self, method: impl Into<MethodName>, consensus: Consensus, testnet: bool) -> IssueBuilder {
pub fn start_issue(
self,
codex: Codex,
method: impl Into<MethodName>,
consensus: Consensus,
testnet: bool,
) -> IssueBuilder {
assert_eq!(self.codex_id, codex.codex_id(), "schema codex id doesn't match issued contract id");
let builder = Builder::new(self.call_id(method));
IssueBuilder { builder, schema: self, testnet, consensus }
IssueBuilder { builder, codex, schema: self, testnet, consensus }
}

pub fn start_issue_mainnet(self, method: impl Into<MethodName>, consensus: Consensus) -> IssueBuilder {
self.start_issue(method, consensus, false)
pub fn start_issue_mainnet(
self,
codex: Codex,
method: impl Into<MethodName>,
consensus: Consensus,
) -> IssueBuilder {
self.start_issue(codex, method, consensus, false)
}
pub fn start_issue_testnet(self, method: impl Into<MethodName>, consensus: Consensus) -> IssueBuilder {
self.start_issue(method, consensus, true)
pub fn start_issue_testnet(
self,
codex: Codex,
method: impl Into<MethodName>,
consensus: Consensus,
) -> IssueBuilder {
self.start_issue(codex, method, consensus, true)
}

pub fn issue(self, params: IssueParams) -> Articles {
let mut builder = self.start_issue(params.core.method, params.consensus, params.testnet);
pub fn issue(self, codex: Codex, params: IssueParams) -> Articles {
let mut builder = self.start_issue(codex, params.core.method, params.consensus, params.testnet);

for NamedState { name, state } in params.core.global {
builder = builder.append(name, state.verified, state.unverified)
Expand All @@ -92,6 +109,7 @@ impl Schema {
#[derive(Clone, Debug)]
pub struct IssueBuilder {
builder: Builder,
codex: Codex,
schema: Schema,
testnet: bool,
consensus: Consensus,
Expand Down Expand Up @@ -127,13 +145,8 @@ impl IssueBuilder {
name: ContractName::Named(name.into()),
issuer: Identity::default(),
};
let genesis = self.builder.issue_genesis(self.schema.codex.codex_id());
let contract = Contract {
version: default!(),
meta,
codex: self.schema.codex.clone(),
genesis,
};
let genesis = self.builder.issue_genesis(self.schema.codex_id);
let contract = Contract { version: default!(), meta, codex: self.codex, genesis };
Articles { contract, contract_sigs: none!(), schema: self.schema }
}
}
Expand Down
10 changes: 5 additions & 5 deletions api/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use amplify::confinement::{SmallOrdMap, SmallOrdSet, TinyOrdMap};
use commit_verify::ReservedBytes;
use strict_encoding::{StrictDeserialize, StrictSerialize, TypeName};
use strict_types::TypeSystem;
use ultrasonic::{CallId, Codex, LibRepo};
use ultrasonic::{CallId, CodexId, LibRepo};

use crate::sigs::ContentSigs;
use crate::{Annotations, Api, MergeError, MethodName, LIB_NAME_SONIC};
Expand All @@ -37,7 +37,7 @@ use crate::{Annotations, Api, MergeError, MethodName, LIB_NAME_SONIC};
#[strict_type(lib = LIB_NAME_SONIC)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
pub struct Schema {
pub codex: Codex,
pub codex_id: CodexId,
pub default_api: Api,
pub default_api_sigs: ContentSigs,
pub custom_apis: SmallOrdMap<Api, ContentSigs>,
Expand All @@ -56,10 +56,10 @@ impl LibRepo for Schema {
}

impl Schema {
pub fn new(codex: Codex, api: Api, libs: impl IntoIterator<Item = Lib>, types: TypeSystem) -> Self {
pub fn new(codex_id: CodexId, api: Api, libs: impl IntoIterator<Item = Lib>, types: TypeSystem) -> Self {
// TODO: Ensure default API is unnamed?
Schema {
codex,
codex_id,
default_api: api,
default_api_sigs: none!(),
custom_apis: none!(),
Expand All @@ -85,7 +85,7 @@ impl Schema {
}

pub fn merge(&mut self, other: Self) -> Result<bool, MergeError> {
if self.codex.codex_id() != other.codex.codex_id() {
if self.codex_id != other.codex_id {
return Err(MergeError::CodexMismatch);
}
self.codex_sigs.merge(other.codex_sigs);
Expand Down
4 changes: 2 additions & 2 deletions examples/dao/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn main() {
let api = api();

// Creating DAO with three participants
let issuer = Schema::new(codex, api, [libs::success()], types.type_system());
let issuer = Schema::new(codex.codex_id(), api, [libs::success()], types.type_system());
issuer
.save("examples/dao/data/SimpleDAO.issuer")
.expect("unable to save issuer to a file");
Expand All @@ -138,7 +138,7 @@ fn main() {
let carol_auth = next_auth();

let articles = issuer
.start_issue_testnet("setup", Consensus::None)
.start_issue_testnet(codex, "setup", Consensus::None)
// Alice
.append("_parties", svnum!(0u64), Some(ston!(name "alice", identity "Alice Wonderland")))
.assign("signers", alice_auth, svnum!(0u64), None)
Expand Down
2 changes: 1 addition & 1 deletion src/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl<S: Supply> Stock<S> {
return Ok(false);
}

self.articles.schema.codex.verify(
self.articles.contract.codex.verify(
self.articles.contract.contract_id(),
&operation,
&self.state.raw,
Expand Down
Loading