Skip to content

Commit

Permalink
Add initial CI for Rust contracts (#22)
Browse files Browse the repository at this point in the history
* Add initial CI for Rust contracts

* Improve clippy configuration for CI

* Fix clippy warnings

* Fix clippy warnings

* Add workspace configuration

* fix: add v2 resolver and other basic functionality

* fix: remove default folder

* fix: lower restrictions for running CI

* fix: use the workspace version for easier tracking

---------

Co-authored-by: Geoffrey Mureithi <[email protected]>
Co-authored-by: Geoffrey Mureithi <[email protected]>
  • Loading branch information
3 people authored Jul 31, 2023
1 parent 0f8cad6 commit 4476d86
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 43 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/contracts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Build and test contracts

on:
push:
pull_request:
branches: ["main"]

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: "-Dwarnings"

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up cargo cache
uses: actions/cache@v3
continue-on-error: false
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
lib/target
key: 1-${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.toml') }}
restore-keys: ${{ runner.os }}-cargo-build

- name: Prepare target
run: rustup target add wasm32-unknown-unknown

# This will attempt to build all contracts in the workspace.
# This command will likely change when we have more than contracts in the workspace
- name: Build All
run: cargo build --release --target wasm32-unknown-unknown --verbose

- name: Lint
run: cargo clippy --all-targets --all-features

test:
needs: build
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up cargo cache
uses: actions/cache@v3
continue-on-error: false
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
contracts/target
key: 1-${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.toml') }}
restore-keys: ${{ runner.os }}-cargo-build

- name: Test
run: cargo test
26 changes: 26 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


[workspace]
# We want to use v2 of the cargo dependency resolver.
resolver = "2"
# Our workspace members include the packages in the contracts directory.
members = ["contracts/voting", "contracts/raffle"]


[profile.release]
opt-level = "z"
overflow-checks = true
debug = 0
strip = "symbols"
debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true


[workspace.dependencies.soroban-sdk]
version = "0.9.2"
18 changes: 2 additions & 16 deletions contracts/raffle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,8 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
soroban-sdk = "0.9.2"
soroban-sdk = { workspace = true }
rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }

[dev-dependencies]
soroban-sdk = { version = "0.9.2", features = ["testutils"] }

[profile.release]
opt-level = "z"
overflow-checks = true
debug = 0
strip = "symbols"
debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
soroban-sdk = { workspace = true, features = ["testutils"] }
10 changes: 4 additions & 6 deletions contracts/raffle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ impl RaffleContract {

let storage = env.storage().persistent();

if !storage
.get::<_, bool>(&DataKey::AlreadyInitialized)
.is_some()
if storage
.get::<_, bool>(&DataKey::AlreadyInitialized).is_none()
{
return Err(Error::NotInitialized);
}
Expand Down Expand Up @@ -157,9 +156,8 @@ impl RaffleContract {
pub fn play_raffle(env: Env, random_seed: u64) -> Result<(), Error> {
let storage = env.storage().persistent();

if !storage
.get::<_, bool>(&DataKey::AlreadyInitialized)
.is_some()
if storage
.get::<_, bool>(&DataKey::AlreadyInitialized).is_none()
{
return Err(Error::NotInitialized);
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/raffle/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn admin_is_identified_on_init() {
}

fn assert_auth(
auths: &std::vec::Vec<(Address, AuthorizedInvocation)>,
auths: &[(Address, AuthorizedInvocation)],
idx: usize,
call_addr: Address,
auth_addr: Address,
Expand Down
20 changes: 3 additions & 17 deletions contracts/voting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,8 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
soroban-sdk = "0.9.2"
soroban-sdk = { workspace = true }

[dev-dependencies]
soroban-sdk = { version = "0.9.2", features = ["testutils"] }
rstest = "0.17.0"

[profile.release]
opt-level = "z"
overflow-checks = true
debug = 0
strip = "symbols"
debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
soroban-sdk = { workspace = true, features = ["testutils"] }
rstest = "0.17.0"
6 changes: 3 additions & 3 deletions contracts/voting/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn proposal_creation() {
}

fn assert_auth(
auths: &std::vec::Vec<(Address, AuthorizedInvocation)>,
auths: &[(Address, AuthorizedInvocation)],
idx: usize,
call_addr: Address,
auth_addr: Address,
Expand Down Expand Up @@ -146,14 +146,14 @@ fn cannot_vote_if_total_voters_reached() {

fn advance_ledger_time_in(time: u64, env: &mut Env) {
let mut ledger_info = env.ledger().get();
ledger_info.timestamp = ledger_info.timestamp + time;
ledger_info.timestamp += time;
env.ledger().set(ledger_info)
}

#[rstest]
#[case::rate_50(2, 1, 50_00, true)]
#[case::precision_is_captured_in_bps(3, 1, 33_33, false)]
#[case::rate_100(2, 2, 100_00, true)]
#[case::rate_100(2, 2, 10_000, true)]
#[case::no_votes_no_rate(0, 0, 0, false)]
fn proposal_calculate_approval_rate(
#[case] total_voters: u32,
Expand Down

0 comments on commit 4476d86

Please sign in to comment.