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

Update testnet #196

Merged
merged 24 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
8 changes: 1 addition & 7 deletions .github/workflows/build-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ name: Build Docker Image

on:
workflow_dispatch:
push:
branches:
- devnet
- testnet
- stagenet
- mainnet

jobs:
build:
runs-on: ubuntu-latest
runs-on: self-hosted

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-release-mainnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

jobs:
deploy_image:
runs-on: ubuntu-latest
runs-on: self-hosted

steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ concurrency:
jobs:
rust:
name: 'cargo check'
runs-on: ubuntu-latest
runs-on: self-hosted

env:
CARGO_TERM_COLOR: always
Expand All @@ -30,7 +30,7 @@ jobs:
target: wasm32-unknown-unknown
components: rustfmt clippy rust-src

- run: sudo apt-get install protobuf-compiler jq ripgrep
- run: sudo apt-get install protobuf-compiler jq ripgrep libclang-dev libssl-dev -y

- run: cargo build --locked

Expand All @@ -55,12 +55,12 @@ jobs:

etc:
name: 'shell scripts'
runs-on: ubuntu-latest
runs-on: self-hosted

steps:
- uses: actions/checkout@v4

- run: sudo apt-get install shellcheck fd-find
- run: sudo apt-get install shellcheck fd-find -y

- name: Run ShellCheck
run: fdfind --type executable --exec bash -c 'echo {}; shellcheck {}'
18 changes: 5 additions & 13 deletions .github/workflows/release-binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@ env:

on:
workflow_dispatch:
push:
branches:
- devnet
- testnet
- stagenet
- mainnet
tags:
- 'release-v*'

permissions:
contents: write

jobs:
define_vars:
runs-on: ubuntu-latest
runs-on: self-hosted
outputs:
release_name: ${{ steps.vars.outputs.release_name }}
commit_sha: ${{ steps.vars.outputs.commit_sha }}
Expand All @@ -31,15 +23,15 @@ jobs:
echo "release_name=$(echo ${{ github.ref_name }} | sed 's/^release-//')" >> "$GITHUB_OUTPUT"

linux_build:
runs-on: ubuntu-latest
runs-on: self-hosted
needs: define_vars
outputs:
artifact: ${{ steps.artifact.outputs.name }}

steps:
- run: |
sudo apt-get update
sudo apt-get install protobuf-compiler
sudo apt update -y
sudo apt install protobuf-compiler libclang-dev libssl-dev -y

- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
Expand Down Expand Up @@ -103,7 +95,7 @@ jobs:
if-no-files-found: error

publish_release:
runs-on: ubuntu-latest
runs-on: self-hosted
needs: [define_vars, linux_build, macos_build]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-devnet-chainspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
maybe_release:
permissions:
contents: write
runs-on: ubuntu-latest
runs-on: self-hosted
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ permissions:

jobs:
runtime_release:
runs-on: ubuntu-latest
runs-on: self-hosted

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-testnet-chainspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:

jobs:
maybe_release:
runs-on: ubuntu-latest
runs-on: self-hosted
permissions:
contents: write
steps:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

291 changes: 156 additions & 135 deletions chainspecs/chain-spec.devnet.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions pallets/faucet/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ mod benchmarks {
assert!(Requests::<T>::contains_key(&caller));
}

#[benchmark]
fn refill_faucet() {
let amount = 100u32.into();
let caller: T::AccountId = whitelisted_caller();
#[extrinsic_call]
crate::benchmarking::benchmarks::refill_faucet(RawOrigin::Signed(caller.clone()), amount);

assert!(Requests::<T>::contains_key(&caller));
}

impl_benchmark_test_suite!(
Faucet,
crate::mock::ExtBuilder::default().build(),
Expand Down
38 changes: 29 additions & 9 deletions pallets/faucet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ pub mod pallet {
#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
#[serde(skip)]
_config: sp_std::marker::PhantomData<T>,
/// Initial balance of faucet account
pub initial_balance: BalanceOf<T>,
}

#[pallet::genesis_build]
Expand All @@ -91,8 +91,10 @@ pub mod pallet {
let account_id = <Pallet<T>>::account_id();
let min = T::Currency::minimum_balance();

if T::Currency::free_balance(&account_id) < min {
let _ = T::Currency::make_free_balance_be(&account_id, min);
let initial_balance = self.initial_balance.max(min);

if T::Currency::free_balance(&account_id) < initial_balance {
let _ = T::Currency::make_free_balance_be(&account_id, initial_balance);
}
}
}
Expand All @@ -115,8 +117,8 @@ pub mod pallet {
AmountTooHigh,
/// More than allowed funds requested during `Config::AccumulationPeriod`.
RequestLimitExceeded,
/// No account to send funds.
NoFaucetAccount,
/// No funds on faucet account.
NoAmountToTransfer,
}

#[pallet::call]
Expand Down Expand Up @@ -148,17 +150,35 @@ pub mod pallet {
ensure!(total <= T::FaucetAmount::get(), Error::<T>::RequestLimitExceeded);

let account_id = Self::account_id();
let faucet_balance = T::Currency::free_balance(&account_id);

// Ensure faucet has enough balance
ensure!(faucet_balance >= amount, Error::<T>::NoAmountToTransfer);

let _ = T::Currency::make_free_balance_be(&account_id, amount);
let _ =
T::Currency::transfer(&account_id, &who, amount, ExistenceRequirement::AllowDeath);
T::Currency::transfer(&account_id, &who, amount, ExistenceRequirement::AllowDeath)?;

Requests::<T>::insert(&who, (total, now));

Self::deposit_event(Event::FundsSent { who, amount });

Ok(())
}

/// Refill the faucet account.
#[pallet::call_index(1)]
#[pallet::weight(
(<T as Config>::WeightInfo::refill_faucet(), DispatchClass::Normal, Pays::No)
)]
pub fn refill_faucet(origin: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {
let sender = ensure_signed(origin)?;

let account_id = Self::account_id();

// Transfer funds from the sender to the faucet account
T::Currency::transfer(&sender, &account_id, amount, ExistenceRequirement::KeepAlive)?;

Ok(())
}
}

#[pallet::validate_unsigned]
Expand Down
15 changes: 13 additions & 2 deletions pallets/faucet/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,22 @@ impl pallet_faucet::Config for Test {
}

#[derive(Default)]
pub struct ExtBuilder {}
pub struct ExtBuilder {
initial_balance: Balance,
}

impl ExtBuilder {
pub fn with_initial_balance(mut self, balance: Balance) -> Self {
self.initial_balance = balance;
self
}

pub fn build(self) -> sp_io::TestExternalities {
let storage = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut storage = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();

pallet_faucet::GenesisConfig::<Test> { initial_balance: self.initial_balance }
.assimilate_storage(&mut storage)
.unwrap();

sp_io::TestExternalities::from(storage)
}
Expand Down
101 changes: 74 additions & 27 deletions pallets/faucet/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,109 @@
use crate::{mock::*, Error};
use frame_support::{assert_noop, assert_ok};
use frame_support::{assert_noop, assert_ok, traits::Currency};

#[test]
fn faucet_works() {
ExtBuilder::default().build_and_execute(|| {
fn faucet_works_with_initial_balance() {
ExtBuilder::default().with_initial_balance(200).build_and_execute(|| {
let balance = 200;
let receiver = 1;

let faucet_account = Faucet::account_id();
assert_eq!(Balances::free_balance(faucet_account), 200);

assert_eq!(Balances::free_balance(receiver), 0);
assert_ok!(Faucet::request_funds(RuntimeOrigin::none(), receiver, balance));
assert_eq!(Balances::free_balance(receiver), balance);
})

assert_eq!(Balances::free_balance(faucet_account), 0);
});
}

#[test]
fn faucet_fail_send_more_than_max() {
ExtBuilder::default().build_and_execute(|| {
let balance = 1000 + 5;
fn faucet_fails_without_initial_balance() {
ExtBuilder::default().with_initial_balance(0).build_and_execute(|| {
let receiver = 1;

let min_balance = Balances::minimum_balance();

let faucet_account = Faucet::account_id();
assert_eq!(Balances::free_balance(faucet_account), min_balance);

let request_amount = 200;
assert_noop!(
Faucet::request_funds(RuntimeOrigin::none(), 1, balance),
Error::<Test>::AmountTooHigh
Faucet::request_funds(RuntimeOrigin::none(), receiver, request_amount),
Error::<Test>::NoAmountToTransfer
);

assert_eq!(Balances::free_balance(faucet_account), min_balance);
});
}

#[test]
fn faucet_fail_exceed_max_amount_during_period() {
ExtBuilder::default().build_and_execute(|| {
let receiver = 1;
assert_ok!(Faucet::request_funds(RuntimeOrigin::none(), receiver, 100));
assert_eq!(Balances::free_balance(1), 100);
fn refill_faucet_works() {
ExtBuilder::default().with_initial_balance(0).build_and_execute(|| {
let faucet_account = Faucet::account_id();
let sender = 1;
let refill_amount = 500;
let min_balance = Balances::minimum_balance();

System::set_block_number(BLOCKS_PER_HOUR * 7);
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), sender, refill_amount));
let sender_initial_balance = Balances::free_balance(sender);
assert!(sender_initial_balance >= refill_amount);

assert_ok!(Faucet::request_funds(RuntimeOrigin::none(), receiver, 200));
assert_eq!(Balances::free_balance(1), 300);
assert_eq!(Balances::free_balance(faucet_account), Balances::minimum_balance());
assert_ok!(Faucet::refill_faucet(
RuntimeOrigin::signed(sender),
refill_amount - min_balance
));

System::set_block_number(BLOCKS_PER_HOUR * 20);
assert_eq!(Balances::free_balance(faucet_account), refill_amount);

assert_ok!(Faucet::request_funds(RuntimeOrigin::none(), receiver, 500));
assert_eq!(Balances::free_balance(1), 800);
assert_eq!(Balances::free_balance(sender), min_balance);
});
}

#[test]
fn request_fails_with_excess_in_period() {
ExtBuilder::default().with_initial_balance(1000).build_and_execute(|| {
let receiver = 1;

System::set_block_number(BLOCKS_PER_HOUR * 23);
assert_ok!(Faucet::request_funds(RuntimeOrigin::none(), receiver, 600));
assert_eq!(Balances::free_balance(receiver), 600);

assert_noop!(
Faucet::request_funds(RuntimeOrigin::none(), receiver, 210),
Faucet::request_funds(RuntimeOrigin::none(), receiver, 500),
Error::<Test>::RequestLimitExceeded
);
});
}

System::set_block_number(BLOCKS_PER_HOUR * 24 - 1);
#[test]
fn faucet_fails_with_insufficient_faucet_balance() {
ExtBuilder::default().with_initial_balance(300).build_and_execute(|| {
let receiver = 1;
let request_amount = 500;

assert_noop!(
Faucet::request_funds(RuntimeOrigin::none(), receiver, 210),
Error::<Test>::RequestLimitExceeded
Faucet::request_funds(RuntimeOrigin::none(), receiver, request_amount),
Error::<Test>::NoAmountToTransfer
);

let faucet_account = Faucet::account_id();
assert_eq!(Balances::free_balance(faucet_account), 300);
});
}

#[test]
fn request_works_after_accumulation_period() {
ExtBuilder::default().with_initial_balance(1000).build_and_execute(|| {
let receiver = 1;

assert_ok!(Faucet::request_funds(RuntimeOrigin::none(), receiver, 500));
assert_eq!(Balances::free_balance(receiver), 500);

System::set_block_number(BLOCKS_PER_HOUR * 24);

assert_ok!(Faucet::request_funds(RuntimeOrigin::none(), receiver, 210));
assert_eq!(Balances::free_balance(1), 1010);
assert_ok!(Faucet::request_funds(RuntimeOrigin::none(), receiver, 500));
assert_eq!(Balances::free_balance(receiver), 1000);
});
}
Loading