Skip to content
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

Add Constructor Support for Solang Solidity Contracts on Soroban #1674

Merged
Merged
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
10 changes: 5 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions/checkout@v4
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that change related to the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

soroban-sdk 22.0.0-rc.3.2 requires rustc 1.79.0 or newer, and soroban-cli (which we use in the CI) now requires rustc 1.81.0 or newer.

- name: Build
run: cargo build --verbose --release
- name: Run tests
Expand All @@ -41,7 +41,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y gcc g++ make
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- name: Get LLVM
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
- name: Extract LLVM
Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:
run: unzip c:\llvm.zip -d c:/
- name: Add LLVM to Path
run: echo "c:\llvm16.0\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
with:
components: clippy
- name: Build
Expand All @@ -97,7 +97,7 @@ jobs:
uses: actions/checkout@v4
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- name: Get LLVM
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
- name: Extract LLVM
Expand All @@ -124,7 +124,7 @@ jobs:
uses: actions/checkout@v4
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- name: Get LLVM
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
- name: Extract LLVM
Expand Down
20 changes: 10 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.75.0
toolchain: 1.81.0
components: |
llvm-tools
clippy
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y gcc g++ make
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- name: Get LLVM
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
- name: Extract LLVM
Expand Down Expand Up @@ -153,7 +153,7 @@ jobs:
# Use C:\ as D:\ might run out of space
- name: "Use C: for rust temporary files"
run: echo "CARGO_TARGET_DIR=C:\target" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
with:
components: clippy
# We run clippy on Linux in the lint job above, but this does not check #[cfg(windows)] items
Expand All @@ -179,7 +179,7 @@ jobs:
uses: actions/checkout@v4
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- name: Get LLVM
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
- name: Extract LLVM
Expand All @@ -205,7 +205,7 @@ jobs:
uses: actions/checkout@v4
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- name: Get LLVM
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
- name: Extract LLVM
Expand Down Expand Up @@ -267,7 +267,7 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: '16'
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- name: Setup yarn
run: npm install -g yarn
- uses: actions/[email protected]
Expand Down Expand Up @@ -318,7 +318,7 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: '16'
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- uses: actions/[email protected]
with:
name: solang-linux-x86-64
Expand All @@ -329,7 +329,7 @@ jobs:
echo "$(pwd)/bin" >> $GITHUB_PATH

- name: Install Soroban
run: cargo install --locked soroban-cli --version 21.0.0-rc.1
run: cargo install --locked soroban-cli --version 22.0.0
- name: Add cargo install location to PATH
run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH
- run: npm install
Expand Down Expand Up @@ -360,7 +360,7 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: '16'
- uses: dtolnay/rust-toolchain@1.75.0
- uses: dtolnay/rust-toolchain@1.81.0
- uses: actions/[email protected]
with:
name: solang-linux-x86-64
Expand Down Expand Up @@ -537,7 +537,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.75.0
toolchain: 1.81.0
components: llvm-tools
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "Apache-2.0"
build = "build.rs"
description = "Solang Solidity Compiler"
keywords = [ "solidity", "compiler", "solana", "polkadot", "substrate" ]
rust-version = "1.75.0"
rust-version = "1.81.0"
edition = "2021"
exclude = [ "/.*", "/docs", "/examples", "/solana-library", "/tests", "/integration", "/vscode", "/testdata" ]

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

[dev-dependencies]
num-derive = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ COPY . src
WORKDIR /src/stdlib/
RUN make

RUN rustup default 1.75.0
RUN rustup default 1.81.0

WORKDIR /src
RUN cargo build --release
Expand Down
2 changes: 1 addition & 1 deletion docs/installing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Option 5: Build Solang from source

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

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

Expand Down
4 changes: 0 additions & 4 deletions integration/soroban/counter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ describe('Counter', () => {

// load contract
contract = new StellarSdk.Contract(contractAddr);

// initialize the contract
await call_contract_function("init", server, keypair, contract);

});

it('get correct initial counter', async () => {
Expand Down
38 changes: 25 additions & 13 deletions src/emit/soroban/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ use crate::codegen::{

use crate::emit::cfg::emit_cfg;
use crate::{emit::Binary, sema::ast};
use funty::Fundamental;
use inkwell::{
context::Context,
module::{Linkage, Module},
};
use soroban_sdk::xdr::{
DepthLimitedWrite, ScEnvMetaEntry, ScSpecEntry, ScSpecFunctionInputV0, ScSpecFunctionV0,
ScSpecTypeDef, StringM, WriteXdr,
Limited, Limits, ScEnvMetaEntry, ScEnvMetaEntryInterfaceVersion, ScSpecEntry,
ScSpecFunctionInputV0, ScSpecFunctionV0, ScSpecTypeDef, StringM, WriteXdr,
};
use std::ffi::CString;
use std::sync;

const SOROBAN_ENV_INTERFACE_VERSION: u64 = 90194313216;
const SOROBAN_ENV_INTERFACE_VERSION: ScEnvMetaEntryInterfaceVersion =
ScEnvMetaEntryInterfaceVersion {
protocol: 22,
pre_release: 0,
};
pub const PUT_CONTRACT_DATA: &str = "l._";
pub const GET_CONTRACT_DATA: &str = "l.1";
pub const LOG_FROM_LINEAR_MEMORY: &str = "x._";
Expand Down Expand Up @@ -129,9 +134,15 @@ impl SorobanTarget {
}

fn emit_env_meta_entries<'a>(context: &'a Context, binary: &mut Binary<'a>, opt: &'a Options) {
let mut meta = DepthLimitedWrite::new(Vec::new(), 10);
let soroban_env_interface_version =
opt.soroban_version.unwrap_or(SOROBAN_ENV_INTERFACE_VERSION);
let mut meta = Limited::new(Vec::new(), Limits::none());
let soroban_env_interface_version = opt.soroban_version;
let soroban_env_interface_version = match soroban_env_interface_version {
Some(version) => ScEnvMetaEntryInterfaceVersion {
protocol: version.as_u32(),
pre_release: 0,
},
None => SOROBAN_ENV_INTERFACE_VERSION,
};
ScEnvMetaEntry::ScEnvMetaKindInterfaceVersion(soroban_env_interface_version)
.write_xdr(&mut meta)
.expect("writing env meta interface version to xdr");
Expand All @@ -146,7 +157,7 @@ impl SorobanTarget {
) {
if cfg.public && !cfg.is_placeholder() {
// TODO: Emit custom type spec entries
let mut spec = DepthLimitedWrite::new(Vec::new(), 10);
let mut spec = Limited::new(Vec::new(), Limits::none());
ScSpecEntry::FunctionV0(ScSpecFunctionV0 {
name: name
.try_into()
Expand Down Expand Up @@ -252,13 +263,13 @@ impl SorobanTarget {
}

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

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

Self::emit_function_spec_entry(binary.context, &cfg, "init".to_string(), binary);
Self::emit_function_spec_entry(binary.context, &cfg, "__constructor".to_string(), binary);

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

let void_type = binary.context.i64_type().fn_type(&[], false);
let init = binary
.module
.add_function("init", void_type, Some(Linkage::External));
let entry = binary.context.append_basic_block(init, "entry");
let constructor =
binary
.module
.add_function("__constructor", void_type, Some(Linkage::External));
let entry = binary.context.append_basic_block(constructor, "entry");

binary.builder.position_at_end(entry);
binary
Expand Down
5 changes: 4 additions & 1 deletion tests/soroban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn build_solidity(src: &str) -> SorobanEnv {
log_prints: true,
#[cfg(feature = "wasm_opt")]
wasm_opt: Some(contract_build::OptimizationPasses::Z),
soroban_version: Some(85899345977),
soroban_version: None,
..Default::default()
},
std::vec!["unknown".to_string()],
Expand All @@ -59,6 +59,9 @@ impl SorobanEnv {
}

pub fn register_contract(&mut self, contract_wasm: Vec<u8>) -> Address {
// For now, we keep using `register_contract_wasm`. To use `register`, we have to figure
// out first what to pass for `constructor_args`
#[allow(deprecated)]
let addr = self
.env
.register_contract_wasm(None, contract_wasm.as_slice());
Expand Down
6 changes: 0 additions & 6 deletions tests/soroban_testcases/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ fn log_runtime_error() {

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

let _res = src.invoke_contract(addr, "init", vec![]);

src.invoke_contract(addr, "decrement", vec![]);

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

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

let _res = src.invoke_contract(addr, "init", vec![]);

src.invoke_contract(addr, "print", vec![]);

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

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

let _res = src.invoke_contract(addr, "init", vec![]);

src.invoke_contract(addr, "decrement", vec![]);

let logs = src.invoke_contract_expect_error(addr, "decrement", vec![]);
Expand Down
2 changes: 0 additions & 2 deletions tests/soroban_testcases/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ fn counter() {

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

let _res = src.invoke_contract(addr, "init", vec![]);

let res = src.invoke_contract(addr, "count", vec![]);
let expected: Val = 10_u64.into_val(&src.env);
assert!(expected.shallow_eq(&res));
Expand Down
Loading