Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 28c675b
Author: Christian Langenbacher <[email protected]>
Date:   Tue Mar 5 12:18:07 2024 +0800

    [stf-raffle] fix merkle root display

commit 8735d64
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 21:35:11 2024 +0900

    [stf-raffle] update the raffle winning script

commit 5d392af
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 21:22:15 2024 +0900

    [stf-raffle] sending raffle winning event

commit f9d2de1
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 18:42:35 2024 +0900

    [stf-raffle] fix clippy v2

commit cf7b21c
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 17:37:16 2024 +0900

    [stf-raffle] fix no-std

commit 26ecfa3
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 13:42:28 2024 +0900

    [sgx-runtime] clippy

commit c68eaa0
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 12:39:03 2024 +0900

    [sgx-runtime] fix raffle index

commit 80ce2df
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 12:16:11 2024 +0900

    [sgx-runtime] fix evm compilation by adding the raffle to the evm build

commit 8cb5213
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 11:59:01 2024 +0900

    code style

commit 69dabe4
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 11:57:19 2024 +0900

    fix clippy

commit c1ca562
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 11:55:32 2024 +0900

    fix doc

commit ca972bc
Merge: 139c056 47a6de8
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 29 11:46:09 2024 +0900

    Merge branch 'master' into cl/raffle-demo

    # Conflicts:
    #	Cargo.lock
    #	app-libs/sgx-runtime/Cargo.toml
    #	core-primitives/binary-merkle-tree/Cargo.toml
    #	core-primitives/binary-merkle-tree/src/lib.rs
    #	enclave-runtime/Cargo.lock

commit 139c056
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 12:23:35 2024 +0900

    [raffle] fix publishing winners result

commit 74cb471
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 12:22:46 2024 +0900

    [raffle] fix index for first run

commit bad8ea1
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 12:03:11 2024 +0900

    [raffle] some better output in the demo

commit 29c337b
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 11:36:52 2024 +0900

    [raffle] minor export cleanup

commit d82c494
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 11:36:37 2024 +0900

    [raffle] remove obsolete lines of demo script

commit e3c92f9
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 11:29:34 2024 +0900

    [raffle] better prints in demo

commit aad96cf
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 11:29:16 2024 +0900

    [raffle] print if the proof is valid

commit 2b14776
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 11:25:29 2024 +0900

    [raffle] fix unused imports in cli

commit c82d4e5
Author: Christian Langenbacher <[email protected]>
Date:   Sat Feb 24 11:18:03 2024 +0900

    [raffle] add merkel proof validation cli

commit e5bfb56
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 17:42:09 2024 +0900

    [raffle] minor cleanup in trusted call

commit 1a21430
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 17:31:16 2024 +0900

    add draw-winners command

commit 35c1b5e
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 17:14:23 2024 +0900

    [add-raffle] remove unnecessary cmd

commit d820278
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 17:13:21 2024 +0900

    add register-for-raffle cli

commit b109655
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 16:51:45 2024 +0900

    Add public getter to show all ongoing raffles

commit edc67d8
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 13:10:39 2024 +0900

    [raffle] use more straight forward way to get the most recent raffle

commit fe11fa9
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 13:02:28 2024 +0900

    [raffle-std] correctly show event on parentchain

commit d9b0a5e
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 12:32:42 2024 +0900

    taplo fmt

commit 65d4776
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 12:13:35 2024 +0900

    [pallet-raffle] fix sgx build

commit a9b8358
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 12:13:24 2024 +0900

    [sgx-runtime] fix sgx build

commit 28565de
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 12:07:40 2024 +0900

    [raffle/stf/trusted_call] cleanup

commit 97cdea2
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 12:01:54 2024 +0900

    [sgx-runtime] fix sgx compilation

commit c192cea
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 11:55:57 2024 +0900

    cli compiles with add raffle

commit e1813c2
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 11:43:51 2024 +0900

    add raffle to overarching stf

commit a9e7860
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 11:14:49 2024 +0900

    minor fixes to make everything build

commit f496cee
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 11:14:22 2024 +0900

    [parentchain/light-client] fix unused import warning

commit 510cf1d
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 10:40:05 2024 +0900

    [ita-raffle-stf] implement create raffles trusted call

commit f769803
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 10:12:07 2024 +0900

    [ita-raffle-stf] update trusted call arguments to match dispatchables

commit 330e3e0
Author: Christian Langenbacher <[email protected]>
Date:   Fri Feb 23 10:05:48 2024 +0900

    add pallet-raffles to sgx-runtime

commit 924a82e
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 19:16:05 2024 +0900

    [raffles] minor fixes

commit d98fdf8
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 19:08:59 2024 +0900

    [raffles] add merkle proof test

commit 50308a1
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 18:58:26 2024 +0900

    [raffles] add merkle root to published event

commit 2fa6c48
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 17:29:32 2024 +0900

    [binary-merkle-tree] use from instead of try_from

commit eb4f14f
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 17:08:33 2024 +0900

    [app-lib] remove unused imports from pallet

commit 86a1267
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 17:07:17 2024 +0900

    [app-lib] remove unused imports from tests

commit b74a10d
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 17:02:45 2024 +0900

    [app-lib] add more tests to raffle pallet

commit 7d014e7
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 16:50:39 2024 +0900

    [app-lib] add draw winners extrinsic

commit 97d86f9
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 14:55:54 2024 +0900

    [app-lib] add basic pallet-raffles

commit 7276d2a
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 13:28:24 2024 +0900

    [core-primitives] add merkle proof crate

commit 9e2b2d2
Author: Christian Langenbacher <[email protected]>
Date:   Thu Feb 22 11:51:57 2024 +0900

    add getter stuff for raffle demo

commit c3f7236
Author: Christian Langenbacher <[email protected]>
Date:   Wed Feb 21 20:14:20 2024 +0900

    add draft of app-libs/raffle/stf

commit dc8f684
Author: Christian Langenbacher <[email protected]>
Date:   Wed Feb 21 20:11:38 2024 +0900

    [sgx-runtime] fix standalone std build
  • Loading branch information
clangenb committed Mar 9, 2024
1 parent 47a6de8 commit 9108feb
Show file tree
Hide file tree
Showing 29 changed files with 1,915 additions and 6 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,39 @@ dependencies = [
"substrate-api-client",
]

[[package]]
name = "ita-raffle-stf"
version = "0.9.0"
dependencies = [
"frame-support",
"frame-system",
"ita-sgx-runtime",
"itp-hashing",
"itp-node-api",
"itp-node-api-metadata",
"itp-sgx-externalities",
"itp-sgx-runtime-primitives",
"itp-stf-interface",
"itp-stf-primitives",
"itp-storage",
"itp-types",
"itp-utils",
"log 0.4.20",
"pallet-balances",
"pallet-parentchain",
"pallet-raffles",
"pallet-sudo",
"parity-scale-codec",
"rlp",
"sgx_tstd",
"sha3",
"sp-core",
"sp-io 7.0.0",
"sp-keyring",
"sp-runtime",
"sp-std",
]

[[package]]
name = "ita-sgx-runtime"
version = "0.9.0"
Expand All @@ -2803,11 +2836,13 @@ dependencies = [
"pallet-balances",
"pallet-evm",
"pallet-parentchain",
"pallet-raffles",
"pallet-sudo",
"pallet-timestamp",
"pallet-transaction-payment",
"parity-scale-codec",
"scale-info",
"sgx_rand",
"sp-api",
"sp-core",
"sp-runtime",
Expand All @@ -2821,6 +2856,7 @@ version = "0.9.0"
dependencies = [
"frame-support",
"frame-system",
"ita-raffle-stf",
"ita-sgx-runtime",
"itp-hashing",
"itp-node-api",
Expand Down Expand Up @@ -5177,6 +5213,26 @@ dependencies = [
"sp-std",
]

[[package]]
name = "pallet-raffles"
version = "0.11.0"
dependencies = [
"env_logger 0.9.3",
"frame-support",
"frame-system",
"itp-binary-merkle-tree",
"log 0.4.20",
"pallet-balances",
"parity-scale-codec",
"scale-info",
"serde 1.0.193",
"sp-core",
"sp-io 7.0.0 (git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42)",
"sp-keyring",
"sp-runtime",
"sp-std",
]

[[package]]
name = "pallet-sidechain"
version = "0.11.0"
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ resolver = "2"
members = [
"app-libs/oracle",
"app-libs/parentchain-interface",
"app-libs/raffle/stf",
"app-libs/raffle/pallet-raffle",
"app-libs/sgx-runtime",
"app-libs/stf",
"cli",
Expand Down
48 changes: 48 additions & 0 deletions app-libs/raffle/pallet-raffle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "pallet-raffles"
description = "The remote attestation registry and verification pallet for integritee blockchains and parachains"
version = "0.11.0"
authors = ["Integritee AG <[email protected]>"]
homepage = "https://integritee.network/"
repository = "https://github.com/integritee-network/pallets/"
license = "Apache-2.0"
edition = "2021"

[dependencies]
log = { version = "0.4", default-features = false }
parity-scale-codec = { version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }

itp-binary-merkle-tree = { default-features = false, path = "../../../core-primitives/binary-merkle-tree" }

# substrate dependencies
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

[dev-dependencies]
env_logger = "0.9.0"
pallet-balances = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

[features]
default = ["std"]
std = [
"frame-support/std",
"frame-system/std",
"log/std",
"itp-binary-merkle-tree/std",
"parity-scale-codec/std",
"scale-info/std",
"serde/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]

try-runtime = ["frame-support/try-runtime"]
232 changes: 232 additions & 0 deletions app-libs/raffle/pallet-raffle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{dispatch::DispatchResult, ensure};
use itp_binary_merkle_tree::{merkle_proof, merkle_root, MerkleProofWithCodec};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_core::{MaxEncodedLen, H256};
use sp_runtime::traits::Keccak256;
use sp_std::{fmt::Debug, vec::Vec};

pub use pallet::*;

pub mod merkle_tree {
pub use itp_binary_merkle_tree::{
merkle_proof, merkle_root, verify_proof, MerkleProofWithCodec,
};
pub use sp_runtime::traits::Keccak256;
}

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;

pub type RaffleIndex = u32;
pub type WinnerCount = u32;

#[derive(Debug, Clone, Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
pub struct RaffleMetadata<AccountId: Debug> {
index: RaffleIndex,
raffle: Raffle<AccountId>,
}

#[derive(Debug, Clone, Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
pub struct Raffle<AccountId: Debug> {
owner: AccountId,
winner_count: WinnerCount,
registration_open: bool,
}

#[frame_support::pallet]
pub mod pallet {
use crate::{weights::WeightInfo, Raffle, RaffleIndex, Shuffle, WinnerCount};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_core::H256;
use sp_std::vec::Vec;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);

/// Configuration trait.
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;

/// Implements random shuffling of values.
///
/// If you use this on-chain you need to make sure to have a deterministic seed based
/// on-chain values. If you use this in SGX, we want to make sure that the randomness
/// is as secure as possible, hence we use the SGX's randomness, which uses a hardware
/// secured randomness source: https://en.wikipedia.org/wiki/RDRAND.
type Shuffle: Shuffle;
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// A new raffle has been registered
RaffleAdded { index: RaffleIndex, raffle: Raffle<T::AccountId> },

/// Someone has registered for a raffle
RaffleRegistration { who: T::AccountId, index: RaffleIndex },

/// Winners were drawn of a raffle
WinnersDrawn { index: RaffleIndex, winners: Vec<T::AccountId>, registrations_root: H256 },
}

#[pallet::error]
pub enum Error<T> {
/// The raffle does not exist
NonexistentRaffle,
/// The registrations for that raffles are closed
RegistrationsClosed,
/// Only the raffle owner can draw the winners
OnlyRaffleOwnerCanDrawWinners,
}

/// Ongoing raffles.
#[pallet::storage]
#[pallet::getter(fn ongoing_raffles)]
pub type OnGoingRaffles<T: Config> =
StorageMap<_, Blake2_128Concat, RaffleIndex, Raffle<T::AccountId>, OptionQuery>;

/// Ongoing raffles.
#[pallet::storage]
#[pallet::getter(fn winners)]
pub type Winners<T: Config> =
StorageMap<_, Blake2_128Concat, RaffleIndex, Vec<T::AccountId>, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn registrations)]
pub type Registrations<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
RaffleIndex,
Blake2_128Concat,
T::AccountId,
(),
OptionQuery,
>;

#[pallet::storage]
#[pallet::getter(fn raffle_count)]
pub type RaffleCount<T> = StorageValue<_, RaffleIndex, ValueQuery>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::add_raffle())]
pub fn add_raffle(origin: OriginFor<T>, winner_count: WinnerCount) -> DispatchResult {
let sender = ensure_signed(origin)?;
let index = Self::raffle_count();

let raffle = Raffle { owner: sender, winner_count, registration_open: true };

OnGoingRaffles::<T>::insert(index, &raffle);
RaffleCount::<T>::put(index + 1);

Self::deposit_event(Event::RaffleAdded { index, raffle });
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::register_for_raffle())]
pub fn register_for_raffle(origin: OriginFor<T>, index: RaffleIndex) -> DispatchResult {
let sender = ensure_signed(origin)?;

ensure!(OnGoingRaffles::<T>::contains_key(index), Error::<T>::NonexistentRaffle);
ensure!(
OnGoingRaffles::<T>::get(index)
.expect("Asserted above that the key exists; qed")
.registration_open,
Error::<T>::RegistrationsClosed
);

Registrations::<T>::insert(index, &sender, ());

Self::deposit_event(Event::RaffleRegistration { who: sender, index });
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::draw_winners())]
pub fn draw_winners(origin: OriginFor<T>, index: RaffleIndex) -> DispatchResult {
let sender = ensure_signed(origin)?;
Self::try_draw_winners(sender, index)
}
}
}

impl<T: Config> Pallet<T> {
pub fn all_ongoing_raffles() -> Vec<RaffleMetadata<T::AccountId>> {
OnGoingRaffles::<T>::iter()
.map(|kv| RaffleMetadata { index: kv.0, raffle: kv.1 })
.collect()
}

pub fn raffle_registrations(index: RaffleIndex) -> Vec<T::AccountId> {
Registrations::<T>::iter_prefix(index).map(|kv| kv.0).collect()
}

fn try_draw_winners(owner: T::AccountId, index: RaffleIndex) -> DispatchResult {
let raffle = OnGoingRaffles::<T>::get(index).ok_or(Error::<T>::NonexistentRaffle)?;
ensure!(raffle.registration_open, Error::<T>::RegistrationsClosed);
ensure!(raffle.owner == owner, Error::<T>::OnlyRaffleOwnerCanDrawWinners);

let mut registrations = Self::raffle_registrations(index);
let registrations_root = Self::merkle_root(&registrations);
<T as Config>::Shuffle::shuffle(&mut registrations);

let count = core::cmp::min(registrations.len(), raffle.winner_count as usize);
let winners = registrations[..count].to_vec();

OnGoingRaffles::<T>::mutate(index, |r| r.as_mut().map(|r| r.registration_open = false));
Winners::<T>::insert(index, &winners);

Self::deposit_event(Event::WinnersDrawn { index, winners, registrations_root });
Ok(())
}

pub fn merkle_proof_for_registration(
index: RaffleIndex,
account: &T::AccountId,
) -> Option<MerkleProofWithCodec<H256, Vec<u8>>> {
let registrations = Self::raffle_registrations(index);
Self::merkle_proof(account, &registrations)
}

pub(crate) fn merkle_root(accounts: &[T::AccountId]) -> H256 {
merkle_root::<Keccak256, _>(accounts.iter().map(Encode::encode))
}

pub(crate) fn merkle_proof(
account: &T::AccountId,
registrations: &[T::AccountId],
) -> Option<MerkleProofWithCodec<H256, Vec<u8>>> {
let leaf_index = Self::merkle_leaf_index_for_registration(account, registrations)?;
let p =
merkle_proof::<Keccak256, _, _>(registrations.iter().map(Encode::encode), leaf_index);
Some(p.into())
}

pub(crate) fn merkle_leaf_index_for_registration(
account: &T::AccountId,
registrations: &[T::AccountId],
) -> Option<usize> {
registrations.iter().position(|a| a == account)
}
}

pub trait Shuffle {
fn shuffle<T>(values: &mut [T]);
}
Loading

0 comments on commit 9108feb

Please sign in to comment.