Skip to content

Commit 9adfb7d

Browse files
committed
[refactor] hyperledger-iroha#132: refactor codegen for enum
Signed-off-by: Artemii Gerasimovich <[email protected]>
1 parent 437b307 commit 9adfb7d

File tree

99 files changed

+1061
-420
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1061
-420
lines changed

Cargo.lock

Lines changed: 221 additions & 128 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ name = "iroha2"
1414
crate-type = ["cdylib"]
1515

1616
[dependencies]
17-
iroha_client = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-stable" }
18-
iroha_crypto = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-stable" }
19-
iroha_data_model = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-stable" }
20-
iroha_version = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-stable" }
17+
iroha_client = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-dev" }
18+
iroha_config = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-dev" }
19+
iroha_crypto = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-dev" }
20+
iroha_data_model = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-dev" }
21+
iroha_version = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-dev" }
2122

2223
color-eyre = "0.6"
2324
parity-scale-codec = "3.1"
24-
pyo3 = { version = "0.16.4", features = ["extension-module", "multiple-pymethods"] }
25+
pyo3 = { version = "0.16.6", features = ["extension-module", "multiple-pymethods"] }
2526
pythonize = "0.16.0"
2627
serde = "1"
2728
serde_json = "1"

example/config.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
{
2-
"TORII_API_URL": "http://iroha0:8080",
3-
"ACCOUNT_ID": {
4-
"name": "alice",
5-
"domain_id": {
6-
"name": "wonderland"
7-
}
2+
"PUBLIC_KEY": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0",
3+
"PRIVATE_KEY": {
4+
"digest_function": "ed25519",
5+
"payload": "9ac47abf59b356e0bd7dcbbbb4dec080e302156a48ca907e47cb6aea1d32719e7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"
86
},
7+
"ACCOUNT_ID": "alice@wonderland",
98
"BASIC_AUTH": {
109
"web_login": "mad_hatter",
1110
"password": "ilovetea"
1211
},
13-
"PUBLIC_KEY": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0",
14-
"PRIVATE_KEY": {
15-
"digest_function": "ed25519",
16-
"payload": "9ac47abf59b356e0bd7dcbbbb4dec080e302156a48ca907e47cb6aea1d32719e7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"
12+
"TORII_API_URL": "http://127.0.0.1:8080",
13+
"TORII_TELEMETRY_URL": "http://127.0.0.1:8180",
14+
"TRANSACTION_TIME_TO_LIVE_MS": 100000,
15+
"TRANSACTION_STATUS_TIMEOUT_MS": 15000,
16+
"TRANSACTION_LIMITS": {
17+
"max_instruction_number": 4096,
18+
"max_wasm_size_bytes": 4194304
1719
},
18-
"LOGGER_CONFIGURATION": {}
20+
"ADD_TRANSACTION_NONCE": false
1921
}

example/test.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
from iroha2.data_model.domain import Domain
88
from iroha2.data_model.account import Account
99
from iroha2.data_model import asset, account
10-
from iroha2.data_model.events import FilterBox, pipeline
10+
from iroha2.data_model.events import FilterBox, pipeline, Event
1111
from iroha2.crypto import KeyPair
1212

1313

1414
def wait_for_tx(cl: Client, hash: str):
15-
filter = FilterBox.Pipeline(
15+
filter = FilterBox(
1616
pipeline.EventFilter(
1717
entity_kind=pipeline.EntityKind.Transaction(),
1818
status_kind=None,
@@ -22,14 +22,13 @@ def wait_for_tx(cl: Client, hash: str):
2222
listener = cl.listen(filter)
2323

2424
for event in listener:
25-
if event.variant is event.Type.Pipeline:
26-
if event.value.hash == hash:
27-
if event.value.status.variant is pipeline.Status.Type.Committed:
28-
return
29-
elif event.value.status.variant is pipeline.Status.Type.Validating:
30-
pass
31-
else:
32-
raise RuntimeError(f"Tx rejected: {event.value.status}")
25+
if isinstance(event, Event.Pipeline) and event.hash == hash:
26+
if isinstance(event.status, pipeline.Status.Committed):
27+
return
28+
elif isinstance(event.status, pipeline.Status.Validating):
29+
pass
30+
else:
31+
raise RuntimeError(f"Tx rejected: {event.value.status}")
3332

3433

3534
cfg = json.loads(open("./config.json").read())

generate/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ license-file = "../../LICENSE"
99
typing = []
1010

1111
[dependencies]
12-
iroha_schema = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-stable" }
13-
iroha_schema_gen = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-stable" }
14-
iroha_data_model = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-stable" }
12+
iroha_schema = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-dev" }
13+
iroha_schema_gen = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-dev" }
14+
iroha_data_model = { git = "https://github.com/hyperledger/iroha", branch = "iroha2-dev" }
1515

16-
color-eyre = "0.5"
16+
color-eyre = "0.6.2"
1717
serde_json = "1"
1818
either = "1.6"
1919
syn = { version = "1", features = ["full"] }
20+
topological-sort = "0.2.2"

generate/src/as_py.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,23 @@ impl fmt::Display for UnnamedStructClass {
246246

247247
impl fmt::Display for EnumClass {
248248
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
249+
let typespec = self
250+
.variants
251+
.iter()
252+
.map(|(_, pytype)| format!("get_class({pytype})"))
253+
.collect::<Vec<_>>()
254+
.join(", ");
249255
let variants = self
250256
.variants
251257
.iter()
252-
.map(|(name, ty)| format!("(\"{}\", {})", name, ty))
258+
.map(|(name, pytype)| format!("(\"{name}\", get_class({pytype}))"))
253259
.collect::<Vec<_>>()
254260
.join(", ");
255-
write!(f, "{} = Enum[{}] ", self.name, variants)
261+
writeln!(
262+
f,
263+
"{} = make_enum(\"{}\", [{variants}], typing.Union[{typespec}])",
264+
self.name, self.name
265+
)
256266
}
257267
}
258268

generate/src/module.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ impl Module {
9999
}
100100
writeln!(
101101
f,
102-
"from .{}rust import Enum, make_struct, make_tuple, Dict",
102+
r#"
103+
from .{}rust import make_enum, make_struct, make_tuple, get_class, SelfResolvingTypeVar, Dict
104+
import typing
105+
"#,
103106
".".repeat(r#in.mods.len())
104107
)?;
105108

@@ -108,6 +111,8 @@ impl Module {
108111
Self::write_meta(&mut f, name.clone(), ty.clone())
109112
.wrap_err_with(|| eyre!("Failed to write metadata for type {}", name))?;
110113
}
114+
115+
writeln!(f, "SelfResolvingTypeVar.resolve_all()")?;
111116
drop(f);
112117

113118
for (name, either_module) in modules {

iroha2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def submit_tx(self, tx: list):
2222
return self.cl.submit_all_with_metadata(tx, {})
2323

2424
def submit_isi(self, isi):
25-
return self.submit_tx([_Instruction(isi)])
25+
return self.submit_tx([])
2626

2727
def submit_tx_blocking(self, tx: list):
2828
tx = [i.to_rust() for i in tx]

iroha2/data_model/domain/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def __init__(self, id: Union[Id, str], logo: Optional[str] = None):
2020
logo=logo,
2121
accounts={},
2222
asset_definitions={},
23-
metadata={})
23+
metadata={},
24+
asset_total_quantities={})
2425

2526
def registrable(self):
2627
return _NewDomain(id=self.id, logo=self.logo, metadata={})
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
from ..rust import Enum, make_struct, make_tuple, Dict
1+
2+
from ..rust import make_enum, make_struct, make_tuple, get_class, SelfResolvingTypeVar, Dict
3+
import typing
4+
5+
SelfResolvingTypeVar.resolve_all()

0 commit comments

Comments
 (0)