Skip to content

Commit de4fc34

Browse files
authored
Add Constructor Support for Solang Solidity Contracts on Soroban (#1674)
1 parent ef9e5fd commit de4fc34

File tree

10 files changed

+48
-45
lines changed

10 files changed

+48
-45
lines changed

.github/workflows/release.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
uses: actions/checkout@v4
1616
with:
1717
submodules: recursive
18-
- uses: dtolnay/rust-toolchain@1.75.0
18+
- uses: dtolnay/rust-toolchain@1.81.0
1919
- name: Build
2020
run: cargo build --verbose --release
2121
- name: Run tests
@@ -41,7 +41,7 @@ jobs:
4141
run: |
4242
sudo apt-get update
4343
sudo apt-get install -y gcc g++ make
44-
- uses: dtolnay/rust-toolchain@1.75.0
44+
- uses: dtolnay/rust-toolchain@1.81.0
4545
- name: Get LLVM
4646
run: curl -sSL --output llvm16.0-linux-arm64.tar.xz https://github.com/hyperledger-solang/solang-llvm/releases/download/llvm16-0/llvm16.0-linux-arm64.tar.xz
4747
- name: Extract LLVM
@@ -74,7 +74,7 @@ jobs:
7474
run: unzip c:\llvm.zip -d c:/
7575
- name: Add LLVM to Path
7676
run: echo "c:\llvm16.0\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
77-
- uses: dtolnay/rust-toolchain@1.75.0
77+
- uses: dtolnay/rust-toolchain@1.81.0
7878
with:
7979
components: clippy
8080
- name: Build
@@ -97,7 +97,7 @@ jobs:
9797
uses: actions/checkout@v4
9898
with:
9999
submodules: recursive
100-
- uses: dtolnay/rust-toolchain@1.75.0
100+
- uses: dtolnay/rust-toolchain@1.81.0
101101
- name: Get LLVM
102102
run: curl -sSL --output llvm16.0-mac-arm.tar.xz https://github.com/hyperledger-solang/solang-llvm/releases/download/llvm16-0/llvm16.0-mac-arm.tar.xz
103103
- name: Extract LLVM
@@ -124,7 +124,7 @@ jobs:
124124
uses: actions/checkout@v4
125125
with:
126126
submodules: recursive
127-
- uses: dtolnay/rust-toolchain@1.75.0
127+
- uses: dtolnay/rust-toolchain@1.81.0
128128
- name: Get LLVM
129129
run: wget -q -O llvm16.0-mac-intel.tar.xz https://github.com/hyperledger-solang/solang-llvm/releases/download/llvm16-0/llvm16.0-mac-intel.tar.xz
130130
- name: Extract LLVM

.github/workflows/test.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
- name: Install Rust
6464
uses: dtolnay/rust-toolchain@master
6565
with:
66-
toolchain: 1.75.0
66+
toolchain: 1.81.0
6767
components: |
6868
llvm-tools
6969
clippy
@@ -120,7 +120,7 @@ jobs:
120120
run: |
121121
sudo apt-get update
122122
sudo apt-get install -y gcc g++ make
123-
- uses: dtolnay/rust-toolchain@1.75.0
123+
- uses: dtolnay/rust-toolchain@1.81.0
124124
- name: Get LLVM
125125
run: curl -sSL --output llvm16.0-linux-arm64.tar.xz https://github.com/hyperledger-solang/solang-llvm/releases/download/llvm16-0/llvm16.0-linux-arm64.tar.xz
126126
- name: Extract LLVM
@@ -153,7 +153,7 @@ jobs:
153153
# Use C:\ as D:\ might run out of space
154154
- name: "Use C: for rust temporary files"
155155
run: echo "CARGO_TARGET_DIR=C:\target" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
156-
- uses: dtolnay/rust-toolchain@1.75.0
156+
- uses: dtolnay/rust-toolchain@1.81.0
157157
with:
158158
components: clippy
159159
# We run clippy on Linux in the lint job above, but this does not check #[cfg(windows)] items
@@ -179,7 +179,7 @@ jobs:
179179
uses: actions/checkout@v4
180180
with:
181181
submodules: recursive
182-
- uses: dtolnay/rust-toolchain@1.75.0
182+
- uses: dtolnay/rust-toolchain@1.81.0
183183
- name: Get LLVM
184184
run: curl -sSL --output llvm16.0-mac-arm.tar.xz https://github.com/hyperledger-solang/solang-llvm/releases/download/llvm16-0/llvm16.0-mac-arm.tar.xz
185185
- name: Extract LLVM
@@ -205,7 +205,7 @@ jobs:
205205
uses: actions/checkout@v4
206206
with:
207207
submodules: recursive
208-
- uses: dtolnay/rust-toolchain@1.75.0
208+
- uses: dtolnay/rust-toolchain@1.81.0
209209
- name: Get LLVM
210210
run: wget -q -O llvm16.0-mac-intel.tar.xz https://github.com/hyperledger-solang/solang-llvm/releases/download/llvm16-0/llvm16.0-mac-intel.tar.xz
211211
- name: Extract LLVM
@@ -267,7 +267,7 @@ jobs:
267267
- uses: actions/setup-node@v4
268268
with:
269269
node-version: '16'
270-
- uses: dtolnay/rust-toolchain@1.75.0
270+
- uses: dtolnay/rust-toolchain@1.81.0
271271
- name: Setup yarn
272272
run: npm install -g yarn
273273
- uses: actions/[email protected]
@@ -318,7 +318,7 @@ jobs:
318318
- uses: actions/setup-node@v4
319319
with:
320320
node-version: '16'
321-
- uses: dtolnay/rust-toolchain@1.75.0
321+
- uses: dtolnay/rust-toolchain@1.81.0
322322
- uses: actions/[email protected]
323323
with:
324324
name: solang-linux-x86-64
@@ -329,7 +329,7 @@ jobs:
329329
echo "$(pwd)/bin" >> $GITHUB_PATH
330330
331331
- name: Install Soroban
332-
run: cargo install --locked soroban-cli --version 21.0.0-rc.1
332+
run: cargo install --locked soroban-cli --version 22.0.0
333333
- name: Add cargo install location to PATH
334334
run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH
335335
- run: npm install
@@ -360,7 +360,7 @@ jobs:
360360
- uses: actions/setup-node@v4
361361
with:
362362
node-version: '16'
363-
- uses: dtolnay/rust-toolchain@1.75.0
363+
- uses: dtolnay/rust-toolchain@1.81.0
364364
- uses: actions/[email protected]
365365
with:
366366
name: solang-linux-x86-64
@@ -537,7 +537,7 @@ jobs:
537537
- name: Install Rust
538538
uses: dtolnay/rust-toolchain@master
539539
with:
540-
toolchain: 1.75.0
540+
toolchain: 1.81.0
541541
components: llvm-tools
542542
- name: Install cargo-llvm-cov
543543
uses: taiki-e/install-action@cargo-llvm-cov

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "Apache-2.0"
88
build = "build.rs"
99
description = "Solang Solidity Compiler"
1010
keywords = [ "solidity", "compiler", "solana", "polkadot", "substrate" ]
11-
rust-version = "1.75.0"
11+
rust-version = "1.81.0"
1212
edition = "2021"
1313
exclude = [ "/.*", "/docs", "/examples", "/solana-library", "/tests", "/integration", "/vscode", "/testdata" ]
1414

@@ -71,7 +71,7 @@ forge-fmt = { path = "fmt", optional = true }
7171
# We don't use ethers-core directly, but need the correct version for the
7272
# build to work.
7373
ethers-core = { version = "2.0.10", optional = true }
74-
soroban-sdk = { version = "20.0.0-rc2", features = ["testutils"], optional = true }
74+
soroban-sdk = { version = "22.0.0-rc.3.2", features = ["testutils"], optional = true }
7575

7676
[dev-dependencies]
7777
num-derive = "0.4"

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ COPY . src
44
WORKDIR /src/stdlib/
55
RUN make
66

7-
RUN rustup default 1.75.0
7+
RUN rustup default 1.81.0
88

99
WORKDIR /src
1010
RUN cargo build --release

docs/installing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Option 5: Build Solang from source
8989

9090
In order to build Solang from source, you will need:
9191

92-
* Rust version 1.75.0 or higher
92+
* Rust version 1.81.0 or higher
9393
* A C++ compiler with support for C++17
9494
* A build of LLVM based on the Solana LLVM tree. There are a few LLVM patches required that are not upstream yet.
9595

integration/soroban/counter.spec.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ describe('Counter', () => {
3030

3131
// load contract
3232
contract = new StellarSdk.Contract(contractAddr);
33-
34-
// initialize the contract
35-
await call_contract_function("init", server, keypair, contract);
36-
3733
});
3834

3935
it('get correct initial counter', async () => {

src/emit/soroban/mod.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@ use crate::codegen::{
88

99
use crate::emit::cfg::emit_cfg;
1010
use crate::{emit::Binary, sema::ast};
11+
use funty::Fundamental;
1112
use inkwell::{
1213
context::Context,
1314
module::{Linkage, Module},
1415
};
1516
use soroban_sdk::xdr::{
16-
DepthLimitedWrite, ScEnvMetaEntry, ScSpecEntry, ScSpecFunctionInputV0, ScSpecFunctionV0,
17-
ScSpecTypeDef, StringM, WriteXdr,
17+
Limited, Limits, ScEnvMetaEntry, ScEnvMetaEntryInterfaceVersion, ScSpecEntry,
18+
ScSpecFunctionInputV0, ScSpecFunctionV0, ScSpecTypeDef, StringM, WriteXdr,
1819
};
1920
use std::ffi::CString;
2021
use std::sync;
2122

22-
const SOROBAN_ENV_INTERFACE_VERSION: u64 = 90194313216;
23+
const SOROBAN_ENV_INTERFACE_VERSION: ScEnvMetaEntryInterfaceVersion =
24+
ScEnvMetaEntryInterfaceVersion {
25+
protocol: 22,
26+
pre_release: 0,
27+
};
2328
pub const PUT_CONTRACT_DATA: &str = "l._";
2429
pub const GET_CONTRACT_DATA: &str = "l.1";
2530
pub const LOG_FROM_LINEAR_MEMORY: &str = "x._";
@@ -129,9 +134,15 @@ impl SorobanTarget {
129134
}
130135

131136
fn emit_env_meta_entries<'a>(context: &'a Context, binary: &mut Binary<'a>, opt: &'a Options) {
132-
let mut meta = DepthLimitedWrite::new(Vec::new(), 10);
133-
let soroban_env_interface_version =
134-
opt.soroban_version.unwrap_or(SOROBAN_ENV_INTERFACE_VERSION);
137+
let mut meta = Limited::new(Vec::new(), Limits::none());
138+
let soroban_env_interface_version = opt.soroban_version;
139+
let soroban_env_interface_version = match soroban_env_interface_version {
140+
Some(version) => ScEnvMetaEntryInterfaceVersion {
141+
protocol: version.as_u32(),
142+
pre_release: 0,
143+
},
144+
None => SOROBAN_ENV_INTERFACE_VERSION,
145+
};
135146
ScEnvMetaEntry::ScEnvMetaKindInterfaceVersion(soroban_env_interface_version)
136147
.write_xdr(&mut meta)
137148
.expect("writing env meta interface version to xdr");
@@ -146,7 +157,7 @@ impl SorobanTarget {
146157
) {
147158
if cfg.public && !cfg.is_placeholder() {
148159
// TODO: Emit custom type spec entries
149-
let mut spec = DepthLimitedWrite::new(Vec::new(), 10);
160+
let mut spec = Limited::new(Vec::new(), Limits::none());
150161
ScSpecEntry::FunctionV0(ScSpecFunctionV0 {
151162
name: name
152163
.try_into()
@@ -252,13 +263,13 @@ impl SorobanTarget {
252263
}
253264

254265
fn emit_initializer(binary: &mut Binary, _ns: &ast::Namespace) {
255-
let mut cfg = ControlFlowGraph::new("init".to_string(), ASTFunction::None);
266+
let mut cfg = ControlFlowGraph::new("__constructor".to_string(), ASTFunction::None);
256267

257268
cfg.public = true;
258269
let void_param = ast::Parameter::new_default(ast::Type::Void);
259270
cfg.returns = sync::Arc::new(vec![void_param]);
260271

261-
Self::emit_function_spec_entry(binary.context, &cfg, "init".to_string(), binary);
272+
Self::emit_function_spec_entry(binary.context, &cfg, "__constructor".to_string(), binary);
262273

263274
let function_name = CString::new(STORAGE_INITIALIZER).unwrap();
264275
let mut storage_initializers = binary
@@ -271,10 +282,11 @@ impl SorobanTarget {
271282
assert!(storage_initializers.next().is_none());
272283

273284
let void_type = binary.context.i64_type().fn_type(&[], false);
274-
let init = binary
275-
.module
276-
.add_function("init", void_type, Some(Linkage::External));
277-
let entry = binary.context.append_basic_block(init, "entry");
285+
let constructor =
286+
binary
287+
.module
288+
.add_function("__constructor", void_type, Some(Linkage::External));
289+
let entry = binary.context.append_basic_block(constructor, "entry");
278290

279291
binary.builder.position_at_end(entry);
280292
binary

tests/soroban.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn build_solidity(src: &str) -> SorobanEnv {
3232
log_prints: true,
3333
#[cfg(feature = "wasm_opt")]
3434
wasm_opt: Some(contract_build::OptimizationPasses::Z),
35-
soroban_version: Some(85899345977),
35+
soroban_version: None,
3636
..Default::default()
3737
},
3838
std::vec!["unknown".to_string()],
@@ -59,6 +59,9 @@ impl SorobanEnv {
5959
}
6060

6161
pub fn register_contract(&mut self, contract_wasm: Vec<u8>) -> Address {
62+
// For now, we keep using `register_contract_wasm`. To use `register`, we have to figure
63+
// out first what to pass for `constructor_args`
64+
#[allow(deprecated)]
6265
let addr = self
6366
.env
6467
.register_contract_wasm(None, contract_wasm.as_slice());

tests/soroban_testcases/print.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ fn log_runtime_error() {
1818

1919
let addr = src.contracts.last().unwrap();
2020

21-
let _res = src.invoke_contract(addr, "init", vec![]);
22-
2321
src.invoke_contract(addr, "decrement", vec![]);
2422

2523
let logs = src.invoke_contract_expect_error(addr, "decrement", vec![]);
@@ -40,8 +38,6 @@ fn print() {
4038

4139
let addr = src.contracts.last().unwrap();
4240

43-
let _res = src.invoke_contract(addr, "init", vec![]);
44-
4541
src.invoke_contract(addr, "print", vec![]);
4642

4743
let logs = src.env.logs().all();
@@ -65,8 +61,6 @@ fn print_then_runtime_error() {
6561

6662
let addr = src.contracts.last().unwrap();
6763

68-
let _res = src.invoke_contract(addr, "init", vec![]);
69-
7064
src.invoke_contract(addr, "decrement", vec![]);
7165

7266
let logs = src.invoke_contract_expect_error(addr, "decrement", vec![]);

tests/soroban_testcases/storage.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ fn counter() {
2323

2424
let addr = src.contracts.last().unwrap();
2525

26-
let _res = src.invoke_contract(addr, "init", vec![]);
27-
2826
let res = src.invoke_contract(addr, "count", vec![]);
2927
let expected: Val = 10_u64.into_val(&src.env);
3028
assert!(expected.shallow_eq(&res));

0 commit comments

Comments
 (0)