Skip to content

Commit

Permalink
Add slashing lifecycle to the runtime (no slashing) (#794)
Browse files Browse the repository at this point in the history
* feat: Slashing Queue (#793)

* feat: Slashing Queue

* feat: Per Service restaking percentage exposure

* test: add more tests for the unapplied slash

* feat: slashing precompile (#795)

* feat: slashing precompile

* feat: Slashing and Dispute precompiles

* Update subxt

* test: add more tests to cover more cases

* doc: Update internal functions docs

* chore: Update toml formatting

* lint: fix clippy

* chore: update tangle-subxt version

---------

Co-authored-by: shekohex <[email protected]>
  • Loading branch information
drewstone and shekohex authored Oct 22, 2024
1 parent bee0039 commit 72783e6
Show file tree
Hide file tree
Showing 28 changed files with 28,963 additions and 18,724 deletions.
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.

24 changes: 12 additions & 12 deletions flake.lock

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

2 changes: 1 addition & 1 deletion forge/lib/openzeppelin-contracts
2 changes: 1 addition & 1 deletion forge/lib/tnt-core
97 changes: 3 additions & 94 deletions forge/src/cggmp21/CGGMP21Blueprint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
pragma solidity >=0.8.3;

import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "core/BlueprintServiceManager.sol";
import "core/BlueprintServiceManagerBase.sol";

contract CGGMP21Blueprint is BlueprintServiceManager {
contract CGGMP21Blueprint is BlueprintServiceManagerBase {
/// A Simple List of all Operator Ecdsa Public Keys
struct Operator {
address addr;
Expand Down Expand Up @@ -76,7 +76,7 @@ contract CGGMP21Blueprint is BlueprintServiceManager {
// Job 0 is the Keygen Job
if (job == KEYGEN_JOB) {
// The inputs are the DKG threshold
(uint8 t) = abi.decode(inputs, (uint8));
uint8 t = abi.decode(inputs, (uint8));
uint256 n = services[serviceId].operators.length;
// verify the DKG threshold is valid
if (t == 0 || t > n) {
Expand All @@ -100,94 +100,3 @@ contract CGGMP21Blueprint is BlueprintServiceManager {
return address(uint160(uint256(keccak256(publicKey))));
}
}

// contract CGGMP21JobResultVerifier is JobResultVerifier {
// uint8 constant KEYGEN_JOB = 0;
// uint8 constant SIGNING_JOB = 1;

// struct Keygen {
// uint64 jobCallId;
// uint8 t;
// bytes publicKey;
// }

// // A mapping of serviceId & JobCallId to Keygen
// mapping(uint64 => mapping(uint64 => Keygen)) public keygens;

// /// @dev Errors
// /// @dev InvalidJob The job is invalid
// error InvalidJob();
// /// @dev InvalidPublicKey The public key is invalid
// error InvalidPublicKey();
// /// @dev InvalidDKGThreshold The DKG threshold is invalid
// error InvalidDKGThreshold();
// /// @dev InvalidKeygenResult The keygen result is invalid
// error InvalidKeygenResult();
// /// @dev InvalidSignature The signature is invalid
// error InvalidSignature();
// /// @dev InvalidSigner The signer is invalid
// error InvalidSigner();

// function verify(
// uint64 serviceId,
// uint8 jobIndex,
// uint64 jobCallId,
// bytes calldata participant,
// bytes calldata inputs,
// bytes calldata outputs
// ) public override onlyRuntime {
// if (jobIndex == KEYGEN_JOB) {
// verifyKeygen(serviceId, jobCallId, inputs, outputs);
// } else if (jobIndex == SIGNING_JOB) {
// verifySigning(serviceId, jobCallId, inputs, outputs);
// } else {
// revert InvalidJob();
// }
// }

// function verifyKeygen(uint64 serviceId, uint64 jobCallId, bytes calldata inputs, bytes calldata outputs) internal {
// // The inputs are the DKG threshold
// (uint8 t) = abi.decode(inputs, (uint8));
// // The outputs are the public key
// bytes memory publicKey = outputs[0:33];
// // verify the public key is valid Ecdsa public key in the compressed format.
// if (publicKey.length != 33) {
// revert InvalidPublicKey();
// }
// // verify the DKG threshold is valid
// if (t == 0) {
// revert InvalidDKGThreshold();
// }
// // store the keygen
// keygens[serviceId][jobCallId] = Keygen(jobCallId, t, outputs);
// }

// function verifySigning(uint64 serviceId, uint64 jobCallId, bytes calldata inputs, bytes calldata outputs)
// internal
// view
// {
// // The inputs are the keygen result id (which is jobCallId) and the message hash.
// (uint64 keygenJobCallId, bytes32 message) = abi.decode(inputs, (uint64, bytes32));
// // The outputs are the signature
// bytes memory signature = outputs[0:65];
// // verify the signature is valid
// if (signature.length != 65) {
// revert InvalidSignature();
// }

// // get the keygen result
// Keygen memory keygen = keygens[serviceId][keygenJobCallId];
// // verify the keygen result exists
// if (keygen.jobCallId != keygenJobCallId) {
// revert InvalidKeygenResult();
// }
// // recover the public key from the signature
// address signer = ECDSA.recover(message, signature);
// // convert the public key to address format
// address keygenAddr = address(uint160(uint256(keccak256(keygen.publicKey))));
// // verify the public key is valid
// if (signer != keygenAddr) {
// revert InvalidSigner();
// }
// }
// }
13 changes: 13 additions & 0 deletions pallets/multi-asset-delegation/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use super::*;
use crate::types::{BalanceOf, OperatorStatus};
use sp_runtime::traits::Zero;
use sp_std::prelude::*;
use tangle_primitives::{traits::MultiAssetDelegationInfo, RoundIndex};

impl<T: crate::Config> MultiAssetDelegationInfo<T::AccountId, BalanceOf<T>> for crate::Pallet<T> {
Expand Down Expand Up @@ -50,4 +51,16 @@ impl<T: crate::Config> MultiAssetDelegationInfo<T::AccountId, BalanceOf<T>> for
.fold(Zero::zero(), |acc, stake| acc + stake.amount)
})
}

fn get_delegators_for_operator(
operator: &T::AccountId,
) -> Vec<(T::AccountId, BalanceOf<T>, Self::AssetId)> {
Operators::<T>::get(operator).map_or(Vec::new(), |metadata| {
metadata
.delegations
.iter()
.map(|stake| (stake.delegator.clone(), stake.amount, stake.asset_id))
.collect()
})
}
}
26 changes: 2 additions & 24 deletions pallets/services/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ fn mock_account_id<T: Config>(id: u8) -> T::AccountId {
}

fn operator_preferences<T: Config>() -> OperatorPreferences {
OperatorPreferences {
key: zero_key(),
approval: ApprovalPreference::default(),
price_targets: Default::default(),
}
OperatorPreferences { key: zero_key(), price_targets: Default::default() }
}

fn cggmp21_blueprint<T: Config>() -> ServiceBlueprint<T::Constraints> {
Expand Down Expand Up @@ -92,20 +88,6 @@ benchmarks! {

}: _(RawOrigin::Signed(bob.clone()), 0)


update_approval_preference {
let alice: T::AccountId = mock_account_id::<T>(1u8);
let blueprint = cggmp21_blueprint::<T>();
let _= Pallet::<T>::create_blueprint(RawOrigin::Signed(alice.clone()).into(), blueprint);

let bob: T::AccountId = mock_account_id::<T>(2u8);
let operator_preference = operator_preferences::<T>();

let _= Pallet::<T>::register(RawOrigin::Signed(bob.clone()).into(), 0, operator_preference, Default::default());

}: _(RawOrigin::Signed(bob.clone()), 0, ApprovalPreference::Required)


update_price_targets {
let alice: T::AccountId = mock_account_id::<T>(1u8);
let blueprint = cggmp21_blueprint::<T>();
Expand Down Expand Up @@ -162,8 +144,6 @@ benchmarks! {
Default::default()
);

let operator_preference = OperatorPreferences { approval: ApprovalPreference::Required, ..operator_preference };

let charlie: T::AccountId = mock_account_id::<T>(3u8);
let _= Pallet::<T>::register(RawOrigin::Signed(charlie.clone()).into(), 0, operator_preference, Default::default());

Expand All @@ -180,7 +160,7 @@ benchmarks! {
Default::default()
);

}: _(RawOrigin::Signed(charlie.clone()), 0)
}: _(RawOrigin::Signed(charlie.clone()), 0, Percent::from_percent(25))


reject {
Expand All @@ -197,8 +177,6 @@ benchmarks! {
Default::default()
);

let operator_preference = OperatorPreferences { approval: ApprovalPreference::Required, ..operator_preference };

let charlie: T::AccountId = mock_account_id::<T>(3u8);
let _= Pallet::<T>::register(RawOrigin::Signed(charlie.clone()).into(), 0, operator_preference, Default::default());

Expand Down
Loading

0 comments on commit 72783e6

Please sign in to comment.