Compare commits

..

4 Commits

Author SHA1 Message Date
eb181c7f44
fixes for the inconsistency of the slow claps
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-11-04 17:43:36 +03:00
5307afe352
bump locked version
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-08-27 15:33:26 +03:00
7edc8935b6
optimize offenders gathering with a single pass over the data
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-08-27 15:31:32 +03:00
46aa18aafe
implement cross session claim via self_applause
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-08-27 14:41:46 +03:00
6 changed files with 261 additions and 72 deletions

2
Cargo.lock generated
View File

@ -3836,7 +3836,7 @@ dependencies = [
[[package]]
name = "ghost-slow-clap"
version = "0.3.40"
version = "0.3.42"
dependencies = [
"frame-benchmarking",
"frame-support",

126
README.md
View File

@ -150,8 +150,28 @@ rustup default stable
rustup update
```
Add the nightly release and the nightly WebAssembly (wasm) targets
to your development environment by running the following commands:
```bash
rustup target add wasm32-unknown-unknown --toolchain stable
rustup update nightly
```
```bash
rustup target add wasm32-unknown-unknown --toolchain nightly
```
```bash
rustup target add wasm32-unknown-unknown --toolchain stable-x86_64-unknown-linux-gnu
```
If you are trying to install WebAssembly (wasm) on an ARM-based processor like the ones in Raspberry Pi try this instead:
```bash
rustup target add wasm32-unknown-unknown --toolchain stable-aarch64-unknown-linux-gnu
```
**_IF error try_**
```bash
rustup target add wasm32-unknown-unknown --toolchain default
```
Verify the configuration of your development environment by running
@ -160,6 +180,10 @@ the following command:
rustup show
```
```bash
rustup +nightly show
```
You should see something like this:
![images/img_9.png](images/img_9.png)
@ -170,6 +194,13 @@ Now run:
rustup component add rust-src --toolchain stable
```
**_IF error try_**
```bash
rustup component add rust-src --toolchain default
```
## Install GHOST
Check if Git is already installed:
@ -180,7 +211,7 @@ git --version
Make a GHOST Directory and go to it:
```bash
mkdir ~/ghost && cd ~/ghost
mkdir ghost && cd ghost
```
Clone GHOST Node Git:
@ -195,7 +226,7 @@ cd ghost-node
Compile the node template by running the following command:
```bash
./scripts/starter.sh --release
cargo build --release
```
The build can take up to 20 minutes depending on the specifications of your machine.
@ -203,6 +234,7 @@ The build can take up to 20 minutes depending on the specifications of your mach
![images/img_12.png](images/img_12.png)
# Boot Node Qualification
## Boot Node Qualification Test
@ -288,7 +320,7 @@ Only `port 30333` should be opened.
Press _CTRL+C_ to stop the node.
# Launching GHOST TestNet
# Launching GHOST TestNet 3.1
Switch to main GIT branch:
```bash
@ -316,7 +348,66 @@ To generate the node key use the following command:
./target/release/ghost key generate-node-key --bin --file=/etc/ghost/node-key
```
Feel free to backup the file on a separate storage device.
Generate Wallet Key file with the following command:
```bash
./target/release/ghost key generate | grep "Secret seed" | awk '{$1=$2=""; sub(/^[ \t]+/, ""); print}' > /etc/ghost/wallet-key
```
Display the wallet-key on the screen by using cat command:
```bash
./target/release/ghost key inspect $(cat /etc/ghost/wallet-key)
```
![images/img_22.png](images/img_22.png)
Feel free to back the file on a separate storage device.
Generate Stash Key file with the following command:
```bash
./target/release/ghost key generate | grep "Secret seed" | awk '{$1=$2=""; sub(/^[ \t]+/, ""); print}' > /etc/ghost/stash-key
```
Display the stash-key on the screen by using cat command:
```bash
./target/release/ghost key inspect $(cat /etc/ghost/stash-key)
```
![images/img_23.png](images/img_23.png)
Generate Session Key file with the following command:
```bash
./target/release/ghost key generate | grep "Secret seed" | awk '{$1=$2=""; sub(/^[ \t]+/, ""); print}' > /etc/ghost/session-key
```
You have generates 4 types of Session Keys:
1. Session Key AUDI
2. Session Key BABE
3. Session Key SLOW
4. Session Key GRAN
Now let's display them!
Display the session-key//audi on the screen by using cat command:
```bash
./target/release/ghost key inspect "$(cat /etc/ghost/session-key)//audi"
```
![images/img_24.png](images/img_24.png)
Display the session-key//babe on the screen by using cat command:
```bash
./target/release/ghost key inspect "$(cat /etc/ghost/session-key)//babe"
```
![images/img_25.png](images/img_25.png)
Display the session-key//slow on the screen by using cat command:
```bash
./target/release/ghost key inspect "$(cat /etc/ghost/session-key)//slow"
```
![images/img_26.png](images/img_26.png)
Display the session-key//gran on the screen by using cat command:
```bash
./target/release/ghost key inspect "$(cat /etc/ghost/session-key)//gran" --scheme=ed25519
```
![images/img_27.png](images/img_27.png)
## Build and start the ghost-node
@ -346,7 +437,7 @@ sha256sum /etc/ghost/casper.json
You should see:
```
9da6045ed6d4fd844e3939573b31c013d0e564e542029330faa6d978cb4a915a
ad653233c978bfd00c5e4525b17628632bc4319c5f5f4b4dcc2f050ef3c3d145
```
Create running `ghost-node` service that starts on system boot using `--unit-file` flag.
@ -372,7 +463,10 @@ To choose default options press Enter here:
![images/img_30.png](images/img_30.png)
If you want to validate from this node press Enter:
Currently, you have to qualify to become a validator node through [GHOST Whales](https://ghostchain.io/whales-apply).
If you were NOT included in the [ghosties file](https://git.ghostchain.io/ghostchain/ghost-node/src/branch/main/service/ghosties) then you cannot be a validator node,
and you can only be a full node so for `disable validator mode? [y/N]` type `y`.
If you were included in the ghosties file you can press Enter:
```
disable validator mode? [y/N]: y
```
@ -417,6 +511,17 @@ Type `y` and press _Enter_ for create dedicated user for running `ghost-node`:
![images/img_37.png](images/img_37.png)
Now you can check whether or not the keys on your GHOST Node match those
in the [ghosties file](https://git.ghostchain.io/ghostchain/ghost-node/src/branch/main/service/ghosties) on GHOST Git.
If you are running a **Validator Node** and if you have followed
the Generating Keys Part you will see all `[+]`.
If you are a running a **Full Node** you will see errors:
(Press _Enter_ for prompts)
```bash
./scripts/starter.sh --check-keys
```
Full Node:
![images/img_38.png](images/img_38.png)
@ -430,7 +535,7 @@ Start `ghost-node`:
sudo systemctl start ghost-node
```
Wait ~60 seconds!
Wait 60 seconds!
Check node is started:
```bash
@ -440,6 +545,11 @@ sudo systemctl status ghost-node
For exit press _CTRL+C_
In order to insert keys run the following flags `--check-keys` `--insert-keys`:
```bash
./scripts/starter.sh --check-keys --insert-keys
```
Restart `ghost-node`:
```bash
sudo systemctl restart ghost-node

View File

@ -1,6 +1,6 @@
[package]
name = "ghost-slow-clap"
version = "0.3.40"
version = "0.3.43"
description = "Applause protocol for the EVM bridge"
license.workspace = true
authors.workspace = true

View File

@ -72,7 +72,7 @@ const LOG_TARGET: &str = "runtime::ghost-slow-clap";
const DB_PREFIX: &[u8] = b"slow_clap::";
const FETCH_TIMEOUT_PERIOD: u64 = 3_000;
const LOCK_BLOCK_EXPIRATION: u64 = 10;
const LOCK_BLOCK_EXPIRATION: u64 = 20;
pub type AuthIndex = u32;
@ -378,7 +378,8 @@ pub mod pallet {
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
if let Call::slow_clap { clap, signature } = call {
let authorities = Authorities::<T>::get(&clap.session_index);
let (session_index, _) = Self::mended_session_index(&clap);
let authorities = Authorities::<T>::get(&session_index);
let authority = match authorities.get(clap.authority_index as usize) {
Some(authority) => authority,
None => return InvalidTransaction::BadSigner.into(),
@ -459,27 +460,49 @@ impl<T: Config> Pallet<T> {
hex_str
}
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
let authorities = Authorities::<T>::get(&clap.session_index);
ensure!(
authorities.get(clap.authority_index as usize).is_some(),
Error::<T>::NotAnAuthority
fn mended_session_index(
clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>,
) -> (SessionIndex, H256) {
let prev_session_index = clap.session_index.saturating_sub(1);
let clap_unique_hash =
Self::generate_unique_hash(&clap.receiver, &clap.amount, &clap.network_id);
let received_claps_key = (
prev_session_index,
&clap.transaction_hash,
&clap_unique_hash,
);
let session_index = ReceivedClaps::<T>::get(&received_claps_key)
.is_empty()
.then(|| clap.session_index)
.unwrap_or(prev_session_index);
(session_index, clap_unique_hash)
}
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
let (session_index, clap_unique_hash) = Self::mended_session_index(&clap);
let mut claps_in_session = ClapsInSession::<T>::get(&session_index);
ensure!(
ClapsInSession::<T>::get(&clap.session_index)
claps_in_session
.get(&clap.authority_index)
.map(|info| !info.disabled)
.unwrap_or(true),
Error::<T>::CurrentValidatorIsDisabled
);
let clap_unique_hash =
Self::generate_unique_hash(&clap.receiver, &clap.amount, &clap.network_id);
let received_claps_key = (
clap.session_index,
&clap.transaction_hash,
&clap_unique_hash,
);
let disabled_authorites = claps_in_session
.values()
.filter(|info| info.disabled)
.count();
let active_authorities = Authorities::<T>::get(&session_index)
.len()
.saturating_sub(disabled_authorites);
let received_claps_key = (session_index, &clap.transaction_hash, &clap_unique_hash);
let number_of_received_claps =
ReceivedClaps::<T>::try_mutate(&received_claps_key, |tree_of_claps| {
@ -498,15 +521,15 @@ impl<T: Config> Pallet<T> {
}
})?;
ClapsInSession::<T>::mutate(&clap.session_index, |claps_details| {
(*claps_details)
.entry(clap.authority_index)
.and_modify(|individual| (*individual).claps.saturating_inc())
.or_insert(SessionAuthorityInfo {
claps: 1u32,
disabled: false,
});
});
claps_in_session
.entry(clap.authority_index)
.and_modify(|individual| individual.claps.saturating_inc())
.or_insert(SessionAuthorityInfo {
claps: 1u32,
disabled: false,
});
ClapsInSession::<T>::insert(&session_index, claps_in_session);
Self::deposit_event(Event::<T>::Clapped {
authority_id: clap.authority_index,
@ -517,7 +540,7 @@ impl<T: Config> Pallet<T> {
});
let enough_authorities =
Perbill::from_rational(number_of_received_claps as u32, authorities.len() as u32)
Perbill::from_rational(number_of_received_claps as u32, active_authorities as u32)
> Perbill::from_percent(T::ApplauseThreshold::get());
if enough_authorities {
@ -574,32 +597,55 @@ impl<T: Config> Pallet<T> {
fn applause_if_posible(
network_id: NetworkIdOf<T>,
session_index: SessionIndex,
prev_session_index: SessionIndex,
transaction_hash: H256,
receiver: T::AccountId,
amount: BalanceOf<T>,
) -> DispatchResult {
let curr_session_index = prev_session_index.saturating_add(1);
let clap_unique_hash = Self::generate_unique_hash(&receiver, &amount, &network_id);
let received_claps_key = (session_index, &transaction_hash, &clap_unique_hash);
let prev_received_claps_key = (prev_session_index, &transaction_hash, &clap_unique_hash);
let curr_received_claps_key = (curr_session_index, &transaction_hash, &clap_unique_hash);
let prev_authorities = Authorities::<T>::get(&prev_session_index);
let curr_authorities = Authorities::<T>::get(&curr_session_index);
let prev_received_claps = ReceivedClaps::<T>::get(&prev_received_claps_key).into_inner();
let curr_received_claps = ReceivedClaps::<T>::get(&curr_received_claps_key).into_inner();
let summary_authority_claps_length = curr_received_claps
.difference(&prev_received_claps)
.filter_map(|&index| {
curr_authorities
.get(index as usize)
.map(|curr_authority| {
prev_authorities
.iter()
.position(|prev_authority| curr_authority == prev_authority)
})
.flatten()
})
.count()
.saturating_add(curr_received_claps.len());
let clap = Clap {
authority_index: Default::default(),
block_number: Default::default(),
removed: false,
session_index,
removed: Default::default(),
session_index: Default::default(),
transaction_hash: Default::default(),
network_id,
receiver,
amount,
transaction_hash,
};
let enough_authorities = Perbill::from_rational(
ReceivedClaps::<T>::get(&received_claps_key).len() as u32,
Authorities::<T>::get(session_index).len() as u32,
summary_authority_claps_length as u32,
Authorities::<T>::get(prev_session_index).len() as u32,
) > Perbill::from_percent(T::ApplauseThreshold::get());
ensure!(enough_authorities, Error::<T>::NotEnoughClaps);
Self::try_applause(&clap, &received_claps_key)?;
Self::try_applause(&clap, &prev_received_claps_key)?;
Ok(())
}
@ -766,14 +812,17 @@ impl<T: Config> Pallet<T> {
Some(_) if from_block.le(&to_block) => {
let adjusted_to_block = estimated_block
.checked_sub(from_block)
.map(|current_distance| current_distance
.le(&max_block_distance)
.then(|| estimated_block)
)
.map(|current_distance| {
current_distance
.le(&max_block_distance)
.then(|| estimated_block)
})
.flatten()
.unwrap_or(from_block
.saturating_add(max_block_distance)
.min(estimated_block));
.unwrap_or(
from_block
.saturating_add(max_block_distance)
.min(estimated_block),
);
(from_block, adjusted_to_block)
}
_ => (to_block, to_block),
@ -1087,8 +1136,8 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
}
fn on_before_session_ending() {
let session_index = T::ValidatorSet::session_index();
let validators = T::ValidatorSet::validators();
let session_index = T::ValidatorSet::session_index().saturating_sub(1);
let authorities_len = Authorities::<T>::get(&session_index).len();
let claps_in_session = ClapsInSession::<T>::get(&session_index);
@ -1097,11 +1146,13 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
let offenders = validators
.into_iter()
.enumerate()
.filter(|(index, _)| !Self::is_good_actor(*index, median_claps, &claps_in_session))
.filter_map(|(_, id)| {
<T::ValidatorSet as ValidatorSetWithIdentification<T::AccountId>>::IdentificationOf::convert(
id.clone(),
).map(|full_id| (id, full_id))
.filter_map(|(index, id)| {
(!Self::is_good_actor(index, median_claps, &claps_in_session)).then(|| {
<T::ValidatorSet as ValidatorSetWithIdentification<T::AccountId>>::IdentificationOf::convert(
id.clone(),
).map(|full_id| (id, full_id))
})
.flatten()
})
.collect::<Vec<IdentificationTuple<T>>>();

View File

@ -991,7 +991,6 @@ fn should_self_applause_if_enough_received_claps() {
assert_ok!(do_clap_from(session_index, network_id, 0, false));
assert_ok!(do_clap_from(session_index, network_id, 1, false));
assert_ok!(do_clap_from(session_index, network_id, 2, false));
assert_eq!(
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
@ -999,14 +998,17 @@ fn should_self_applause_if_enough_received_claps() {
);
assert_eq!(Balances::balance(&receiver), 0);
assert_ok!(SlowClap::self_applause(
RuntimeOrigin::signed(receiver),
network_id,
session_index,
transaction_hash,
receiver,
amount,
));
assert_err!(
SlowClap::self_applause(
RuntimeOrigin::signed(receiver),
network_id,
session_index,
transaction_hash,
receiver,
amount,
),
Error::<Runtime>::NotEnoughClaps
);
assert_eq!(
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
false
@ -1015,6 +1017,26 @@ fn should_self_applause_if_enough_received_claps() {
Networks::on_finalize(System::block_number());
advance_session();
let next_session_index = Session::session_index();
let next_storage_key = (
next_session_index,
transaction_hash,
unique_transaction_hash,
);
assert_ok!(do_clap_from(next_session_index, network_id, 2, false));
assert_err!(
SlowClap::self_applause(
RuntimeOrigin::signed(receiver),
network_id,
next_session_index,
transaction_hash,
receiver,
amount,
),
Error::<Runtime>::NotEnoughClaps
);
assert_ok!(SlowClap::self_applause(
RuntimeOrigin::signed(receiver),
network_id,
@ -1027,6 +1049,10 @@ fn should_self_applause_if_enough_received_claps() {
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
true
);
assert_eq!(
pallet::ApplausesForTransaction::<Runtime>::get(&next_storage_key),
false
);
assert_eq!(Balances::balance(&receiver), amount);
});
}

View File

@ -81,18 +81,20 @@ extract_seed() {
}
downgrade_compiler_if_needed() {
echo "[+] fetching the latest ghost-node source code"
git switch main
git pull origin main
# TODO: uncomment later
# echo "[+] fetching the latest ghost-node source code"
# git switch main
# git pull origin main
# 1.86.0 works fine, tested with:
# 1.83.0 works fine, tested with:
# ...
# 1.87.0 throws errors during compilation
# 1.88.0 throws errors during compilation
LATEST_TESTED_VERSION=86
LATEST_TESTED_VERSION=83
cargo_version=$(cargo --version | cut -d'.' -f2)
if [ "$cargo_version" -gt "$LATEST_TESTED_VERSION" ]; then
echo "[+] downgrading rustc compiler version to 1.86.0"
rustup default 1.86.0
echo "[+] downgrading rustc compiler version to 1.83.0"
rustup default 1.83.0
toolchain_name=$(rustup show | grep default | head -n 1 | cut -d' ' -f1)
rustup target add wasm32-unknown-unknown --toolchain $toolchain_name
rustup component add rust-src --toolchain $toolchain_name