-
Notifications
You must be signed in to change notification settings - Fork 232
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
Soroban: Support for Constructors #1675
Open
tareknaser
wants to merge
5
commits into
hyperledger-solang:main
Choose a base branch
from
tareknaser:soroban_no_args_constr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e05b33e
feat(soroban): modify `emit_initializer` to support user-defined cons…
tareknaser 399b070
tests(soroban): add constructor tests and update contract registratio…
tareknaser 362c107
test(soroban): add no-argument constructor example
tareknaser 850628e
chore: add license to `tests/soroban_testcases/constructor.rs`
tareknaser 4f2d6de
fix: clippy warning
tareknaser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
*.js | ||
*.so | ||
*.key | ||
*.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
contract noargsconstructor { | ||
uint64 public count = 1; | ||
|
||
constructor() { | ||
count += 1; | ||
} | ||
|
||
function get() public view returns (uint64) { | ||
return count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as StellarSdk from '@stellar/stellar-sdk'; | ||
import { readFileSync } from 'fs'; | ||
import { expect } from 'chai'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import { call_contract_function } from './test_helpers.js'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const dirname = path.dirname(__filename); | ||
|
||
describe('CounterWithNoArgsConstructor', () => { | ||
let keypair; | ||
const server = new StellarSdk.SorobanRpc.Server( | ||
"https://soroban-testnet.stellar.org:443", | ||
); | ||
|
||
let contractAddr; | ||
let contract; | ||
before(async () => { | ||
|
||
console.log('Setting up counter with no-args constructor contract tests...'); | ||
|
||
// read secret from file | ||
const secret = readFileSync('alice.txt', 'utf8').trim(); | ||
keypair = StellarSdk.Keypair.fromSecret(secret); | ||
|
||
let contractIdFile = path.join(dirname, '.soroban', 'contract-ids', 'noargsconstructor.txt'); | ||
// read contract address from file | ||
contractAddr = readFileSync(contractIdFile, 'utf8').trim().toString(); | ||
|
||
// load contract | ||
contract = new StellarSdk.Contract(contractAddr); | ||
}); | ||
|
||
it('make sure the constructor of the contract was called', async () => { | ||
// get the count | ||
let count = await call_contract_function("get", server, keypair, contract); | ||
expect(count.toString()).eq("2"); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{build_solidity, SorobanEnv}; | ||
use soroban_sdk::testutils::Logs; | ||
use soroban_sdk::{IntoVal, Val}; | ||
|
||
#[test] | ||
fn test_constructor_increments_count() { | ||
let wasm = build_solidity( | ||
r#"contract counter { | ||
uint64 public count = 1; | ||
|
||
constructor() { | ||
count += 1; | ||
} | ||
|
||
function get() public view returns (uint64) { | ||
return count; | ||
} | ||
}"#, | ||
); | ||
let mut src = SorobanEnv::new(); | ||
// No constructor arguments | ||
let constructor_args: soroban_sdk::Vec<Val> = soroban_sdk::Vec::new(&src.env); | ||
let address = src.register_contract(wasm, constructor_args); | ||
|
||
let res = src.invoke_contract(&address, "get", vec![]); | ||
let expected: Val = 2_u64.into_val(&src.env); | ||
assert!( | ||
expected.shallow_eq(&res), | ||
"expected: {:?}, got: {:?}", | ||
expected, | ||
res | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_constructor_logs_message_on_call() { | ||
let wasm = build_solidity( | ||
r#"contract counter { | ||
uint64 public count = 1; | ||
|
||
constructor() { | ||
print("Constructor called"); | ||
} | ||
|
||
function get() public view returns (uint64) { | ||
return count; | ||
} | ||
}"#, | ||
); | ||
let mut src = SorobanEnv::new(); | ||
// No constructor arguments | ||
let constructor_args: soroban_sdk::Vec<Val> = soroban_sdk::Vec::new(&src.env); | ||
let address = src.register_contract(wasm, constructor_args); | ||
|
||
let _res = src.invoke_contract(&address, "get", vec![]); | ||
|
||
let logs = src.env.logs().all(); | ||
assert!(logs[0].contains("Constructor called")); | ||
} | ||
|
||
// FIXME: Uncomment this test once the constructor arguments are supported | ||
// #[test] | ||
fn _test_constructor_set_count_value() { | ||
let wasm = build_solidity( | ||
r#"contract counter { | ||
uint64 public count = 1; | ||
|
||
constructor(uint64 initial_count) { | ||
count = initial_count; | ||
} | ||
|
||
function get() public view returns (uint64) { | ||
return count; | ||
} | ||
}"#, | ||
); | ||
let mut src = SorobanEnv::new(); | ||
let mut constructor_args: soroban_sdk::Vec<Val> = soroban_sdk::Vec::new(&src.env); | ||
constructor_args.push_back(42_u64.into_val(&src.env)); | ||
let address = src.register_contract(wasm, constructor_args); | ||
|
||
// Get the value of count and check it is 42 | ||
let res = src.invoke_contract(&address, "get", vec![]); | ||
let expected: Val = 42_u64.into_val(&src.env); | ||
|
||
assert!( | ||
expected.shallow_eq(&res), | ||
"expected: {:?}, got: {:?}", | ||
expected, | ||
res | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
mod constructor; | ||
mod math; | ||
mod print; | ||
mod storage; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can set
cfg.params
to the parameters of the constructorFunctionValue
's params. This will likely include a mapping that looks something like:This won’t work because we still need to pass the arguments to
build_call()
(on line 296), which, of course, we don’t know.