Skip to content

Commit

Permalink
[transaction-builder-generator] Add Swift support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Zakian authored and bors-libra committed Dec 3, 2021
1 parent b887290 commit dbbc96c
Show file tree
Hide file tree
Showing 15 changed files with 797 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ jobs:
key: ${{ needs.prepare.outputs.changes-target-branch }}
- name: run codegen unit tests
run: |
$pre_command && mkdir -p target/junit-reports && cargo nextest --jobs ${max_threads} --tries ${nextest_tries} --failure-output=immediate-final -p transaction-builder-generator -p shuffle --unit --changed-since "origin/$TARGET_BRANCH" --run-ignored=ignored-only --junit target/junit-reports/codegen-unit-test.xml
source $HOME/.profile && $pre_command && mkdir -p target/junit-reports && cargo nextest --jobs ${max_threads} --tries ${nextest_tries} --failure-output=immediate-final -p transaction-builder-generator -p shuffle --unit --changed-since "origin/$TARGET_BRANCH" --run-ignored=ignored-only --junit target/junit-reports/codegen-unit-test.xml
env:
TARGET_BRANCH: ${{ needs.prepare.outputs.changes-target-branch }}
- name: upload codegen test results
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/diem-documentation-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ edition = "2018"

[dependencies]
serde_yaml = "0.8.17"
serde-reflection = "0.3.4"
serde-generate = "0.20.2"
serde-reflection = "0.3.5"
serde-generate = "0.20.6"
anyhow = "1.0.38"
regex = "1.4.3"
structopt = "0.3.21"
Expand Down
4 changes: 2 additions & 2 deletions diem-move/transaction-builder-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ heck = "0.3.2"
regex = "1.4.3"
structopt = "0.3.21"
textwrap = "0.13.4"
serde-reflection = "0.3.4"
serde-generate = "0.20.2"
serde_yaml = "0.8.17"

diem-types = { path = "../../types" }
diem-workspace-hack = { path = "../../crates/diem-workspace-hack" }
move-core-types = { path = "../../language/move-core/types" }
serde-reflection = "0.3.5"
serde-generate = "0.20.6"
bcs = "0.1.2"

[dev-dependencies]
Expand Down
48 changes: 48 additions & 0 deletions diem-move/transaction-builder-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ The following languages are currently supported:

* TypeScript / JavaScript

* Swift (version 5.3)


## Quick Start

Expand Down Expand Up @@ -195,6 +197,52 @@ dotnet add Demo.csproj reference ../Bcs/Bcs.csproj
dotnet run --project Demo.csproj
```

### Swift

To install Swift source `Serde`, `Bcs`, `DiemTypes`, and `DiemStdlib` into a target directory `$DEST`, run:
```bash
target/debug/generate-transaction-builders \
--language swift \
--module-name DiemStdlib \
--with-diem-types "testsuite/generate-format/tests/staged/diem.yaml" \
--target-source-dir "$DEST" \
"language/diem-framework/DPN/releases/legacy" \
"language/diem-framework/DPN/releases/artifacts/current"
```

Next, you may copy the [Swift demo](examples/swift/main.swift), create the
Swift package for the transaction builders, and execute the example with the
following:

```bash
cp language/transaction-builder/generator/examples/swift/main.swift "$DEST/Sources/DiemStdlib"
cd "$DEST"
cat >Package.swift <<EOF
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "DiemStdlib",
targets: [
.target(
name: "Serde",
dependencies: []
),
.target(
name: "DiemTypes",
dependencies: ["Serde"]
),
.target(
name: "DiemStdlib",
dependencies: ["Serde", "DiemTypes"]
),
]
)
EOF
swift run
```

## Adding Support for a New Language

Supporting transaction builders in an additional programming language boils down to providing the following items:
Expand Down
78 changes: 78 additions & 0 deletions diem-move/transaction-builder-generator/examples/swift/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import DiemTypes

func demo_peer_to_peer_script() throws {
let address = DiemTypes.AccountAddress(value: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
let module = DiemTypes.Identifier(value: "XDX")
let name = DiemTypes.Identifier(value: "XDX")
let type_params: [DiemTypes.TypeTag] = []
let struct_tag = DiemTypes.StructTag(
address: address,
module: module,
name: name,
type_params: type_params
)
let token = DiemTypes.TypeTag.Struct(struct_tag)
let payee = DiemTypes.AccountAddress(value: [
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
0x22,
])
let amount: UInt64 = 1234567
let script = encode_peer_to_peer_with_metadata_script(
currency: token,
payee: payee,
amount: amount,
metadata: [],
metadata_signature: []
)
switch try decode_peer_to_peer_with_metadata_script(script: script) {
case .PeerToPeerWithMetadata(_, let p, let a, _, _):
assert(p == payee, "Payee doesn't match")
assert(a == amount, "Amount doesn't match")
default: assertionFailure("Invalid scriptcall")
}

for o in try script.bcsSerialize() {
print(o, terminator: " ")
}
print()
}

func demo_peer_to_peer_script_function() throws {
let address = DiemTypes.AccountAddress(value: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
let module = DiemTypes.Identifier(value: "XDX")
let name = DiemTypes.Identifier(value: "XDX")
let type_params: [DiemTypes.TypeTag] = []
let struct_tag = DiemTypes.StructTag(
address: address,
module: module,
name: name,
type_params: type_params
)
let token = DiemTypes.TypeTag.Struct(struct_tag)
let payee = DiemTypes.AccountAddress(value: [
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
0x22,
])
let amount: UInt64 = 1234567
let script = try PaymentScripts.encode_peer_to_peer_with_metadata_script_function(
currency: token,
payee: payee,
amount: amount,
metadata: [],
metadata_signature: []
)
switch try PaymentScripts.decode_peer_to_peer_with_metadata_script_function(payload: script) {
case .PeerToPeerWithMetadata(_, let p, let a, _, _):
assert(p == payee, "Payee doesn't match")
assert(a == amount, "Amount doesn't match")
default: assertionFailure("Invalid script function call")
}

for o in try script.bcsSerialize() {
print(o, terminator: " ")
}
print()
}

try demo_peer_to_peer_script()
try demo_peer_to_peer_script_function()
3 changes: 1 addition & 2 deletions diem-move/transaction-builder-generator/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ pub(crate) fn make_abi_enum_container(abis: &[ScriptABI]) -> ContainerFormat {
for arg in abi.args() {
fields.push(quote_parameter_as_field(arg));
}
let format = VariantFormat::Struct(fields);
variants.insert(
index as u32,
Named {
name: abi.name().to_camel_case(),
value: format,
value: VariantFormat::Struct(fields),
},
);
}
Expand Down
9 changes: 8 additions & 1 deletion diem-move/transaction-builder-generator/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ enum Language {
Java,
Csharp,
Go,
TypeScript
TypeScript,
Swift,
}
}

Expand Down Expand Up @@ -114,6 +115,9 @@ fn main() {
Language::TypeScript => {
buildgen::typescript::output(&mut out, &abis).unwrap();
}
Language::Swift => {
buildgen::swift::output(&mut out, &abis).unwrap();
}
Language::Csharp => {
panic!("Code generation in C# requires --target_source_dir");
}
Expand All @@ -138,6 +142,7 @@ fn main() {
Language::TypeScript => {
Box::new(serdegen::typescript::Installer::new(install_dir.clone()))
}
Language::Swift => Box::new(serdegen::swift::Installer::new(install_dir.clone())),
Language::Go => Box::new(serdegen::golang::Installer::new(
install_dir.clone(),
options.serde_package_name.clone(),
Expand Down Expand Up @@ -171,6 +176,7 @@ fn main() {
Language::Csharp => ("Diem.Types".to_string(), vec!["Diem", "Types"]),
Language::Go => ("diemtypes".to_string(), vec!["diemtypes"]),
Language::TypeScript => ("diemTypes".to_string(), vec!["diemTypes"]),
Language::Swift => ("DiemTypes".to_string(), vec!["DiemTypes"]),
_ => ("diem_types".to_string(), vec!["diem_types"]),
};
let custom_diem_code = buildgen::read_custom_code_from_paths(
Expand All @@ -192,6 +198,7 @@ fn main() {
options.diem_package_name,
)),
Language::TypeScript => Box::new(buildgen::typescript::Installer::new(install_dir)),
Language::Swift => Box::new(buildgen::swift::Installer::new(install_dir)),
Language::Rust => Box::new(buildgen::rust::Installer::new(
install_dir,
options.diem_version_number,
Expand Down
2 changes: 2 additions & 0 deletions diem-move/transaction-builder-generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub mod java;
pub mod python3;
/// Support for code-generation in Rust.
pub mod rust;
/// Support for code-generation in Swift.
pub mod swift;
/// Support for code-generation in TypeScript.
pub mod typescript;

Expand Down
Loading

0 comments on commit dbbc96c

Please sign in to comment.