Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
Minor update to burn/fine API (#19)
Browse files Browse the repository at this point in the history
* Consolidated functions for setting epoch

* Centralized chain status, reduced build

* Removed commented code
  • Loading branch information
anthonygd authored Sep 8, 2021
1 parent 0e08534 commit 2984544
Show file tree
Hide file tree
Showing 21 changed files with 526 additions and 656 deletions.
892 changes: 349 additions & 543 deletions bindings/bridge.go

Large diffs are not rendered by default.

38 changes: 0 additions & 38 deletions dapp.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
"language": "Solidity",
"sources": {
"src/EthDKGDiamond.sol": { "urls": ["src/EthDKGDiamond.sol"] },
"src/CryptoLibrary.sol": { "urls": ["src/CryptoLibrary.sol"] },
"src/Registry.sol": { "urls": ["src/Registry.sol"] },
"src/SignatureLibrary.sol": { "urls": ["src/SignatureLibrary.sol"] },
"src/SimpleAuth.sol": { "urls": ["src/SimpleAuth.sol"] },
"src/AccusationManager.sol": { "urls": ["src/AccusationManager.sol"] },
"src/QueueLibrary.sol": { "urls": ["src/QueueLibrary.sol"] },
"src/Token.sol": { "urls": ["src/Token.sol"] },
"src/MerkleProof.sol": { "urls": ["src/MerkleProof.sol"] },
"src/TokenMigrator.sol": { "urls": ["src/TokenMigrator.sol"] },
Expand All @@ -30,28 +26,13 @@
"src/migrate/MigrateETHDKG.sol": {
"urls": ["src/migrate/MigrateETHDKG.sol"]
},
"src/facets/EthDKGLibrary.sol": {
"urls": ["src/facets/EthDKGLibrary.sol"]
},
"src/facets/StakingLibrary.sol": {
"urls": ["src/facets/StakingLibrary.sol"]
},
"src/facets/DiamondStorageLibrary.sol": {
"urls": ["src/facets/DiamondStorageLibrary.sol"]
},
"src/facets/ParticipantsLibrary.sol": {
"urls": ["src/facets/ParticipantsLibrary.sol"]
},
"src/facets/SnapshotsFacet.sol": {
"urls": ["src/facets/SnapshotsFacet.sol"]
},
"src/facets/StakingFacet.sol": { "urls": ["src/facets/StakingFacet.sol"] },
"src/facets/EthDKGInformationFacet.sol": {
"urls": ["src/facets/EthDKGInformationFacet.sol"]
},
"src/facets/AccessControlLibrary.sol": {
"urls": ["src/facets/AccessControlLibrary.sol"]
},
"src/facets/EthDKGInitializeFacet.sol": {
"urls": ["src/facets/EthDKGInitializeFacet.sol"]
},
Expand All @@ -73,13 +54,9 @@
"src/facets/EthDKGSubmitMPKFacet.sol": {
"urls": ["src/facets/EthDKGSubmitMPKFacet.sol"]
},
"src/facets/SnapshotsLibrary.sol": {
"urls": ["src/facets/SnapshotsLibrary.sol"]
},
"src/facets/EthDKGGroupAccusationFacet.sol": {
"urls": ["src/facets/EthDKGGroupAccusationFacet.sol"]
},
"src/facets/StopLibrary.sol": { "urls": ["src/facets/StopLibrary.sol"] },
"src/interfaces/StakingEvents.sol": {
"urls": ["src/interfaces/StakingEvents.sol"]
},
Expand All @@ -106,21 +83,6 @@
},
"src/interfaces/Snapshots.sol": {
"urls": ["src/interfaces/Snapshots.sol"]
},
"src/parsers/BaseParserLibrary.sol": {
"urls": ["src/parsers/BaseParserLibrary.sol"]
},
"src/parsers/RClaimsParserLibrary.sol": {
"urls": ["src/parsers/RClaimsParserLibrary.sol"]
},
"src/parsers/RCertParserLibrary.sol": {
"urls": ["src/parsers/RCertParserLibrary.sol"]
},
"src/parsers/BClaimsParserLibrary.sol": {
"urls": ["src/parsers/BClaimsParserLibrary.sol"]
},
"src/parsers/PClaimsParserLibrary.sol": {
"urls": ["src/parsers/PClaimsParserLibrary.sol"]
}
},
"settings": {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/MadBase/bridge

go 1.15

require github.com/ethereum/go-ethereum v1.10.5
require github.com/ethereum/go-ethereum v1.10.8
66 changes: 58 additions & 8 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion out/dapp.sol.json

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions src/AccusationDiamond.sol

This file was deleted.

3 changes: 2 additions & 1 deletion src/facets/AccusationLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity >= 0.5.15;

import "../parsers/PClaimsParserLibrary.sol";
import "./ChainStatusLibrary.sol";
import "./ParticipantsLibrary.sol";
import "./SnapshotsLibrary.sol";

Expand Down Expand Up @@ -57,7 +58,7 @@ library AccusationLibrary {
require(pClaims0.rCert.rClaims.chainId == pClaims1.rCert.rClaims.chainId, "Invalid multiple proposal accusation, the chainId between the proposals are different!");

// ensure the chainid of blob0 is correct for this chain using RCert sub object of PClaims
uint32 chainId = ParticipantsLibrary.participantsStorage().chainId;
uint32 chainId = ChainStatusLibrary.chainId();
require(pClaims0.rCert.rClaims.chainId == chainId, "Invalid multiple proposal accusation, the chainId is invalid for this chain!");

// ensure both accounts are applicable to a currently locked validator - Note<may be done in different layer?>
Expand Down
54 changes: 54 additions & 0 deletions src/facets/ChainStatusLibrary.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT-open-group
pragma solidity >=0.7.4;
pragma abicoder v2;

library ChainStatusLibrary {
bytes32 constant STORAGE_LOCATION = keccak256("chainstatus.storage");

struct ChainStatusStorage {
uint32 chainId;
uint256 epoch;
uint32 epochSize;
}

function chainStatusStorage() internal pure returns (ChainStatusStorage storage ss) {
bytes32 position = STORAGE_LOCATION;
assembly { // solium-disable-line
ss.slot := position
}
}

//
//
//
function setChainId(uint32 _chainId) internal {
chainStatusStorage().chainId = _chainId;
}

function chainId() internal view returns (uint32) {
return chainStatusStorage().chainId;
}

//
//
//
function setEpoch(uint256 _epoch) internal {
chainStatusStorage().epoch = _epoch;
}

function epoch() internal view returns (uint256) {
return chainStatusStorage().epoch;
}

//
//
//
function setEpochSize(uint32 _epochSize) internal {
chainStatusStorage().epochSize = _epochSize;
}

function epochSize() internal view returns (uint32) {
return chainStatusStorage().epochSize;
}

}
5 changes: 3 additions & 2 deletions src/facets/ParticipantsFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma abicoder v2;
import "../interfaces/Participants.sol";

import "./AccessControlLibrary.sol";
import "./ChainStatusLibrary.sol";
import "./ParticipantsLibrary.sol";
import "./StakingLibrary.sol";
import "./StopLibrary.sol";
Expand Down Expand Up @@ -57,10 +58,10 @@ contract ParticipantsFacet is AccessControlled, Constants, Participants, Stoppab
}

function getChainId() external view override returns (uint32) {
return ParticipantsLibrary.participantsStorage().chainId;
return ChainStatusLibrary.chainId();
}

function setChainId(uint32 _chainId) external override onlyOperator {
ParticipantsLibrary.participantsStorage().chainId = _chainId;
ChainStatusLibrary.setChainId(_chainId);
}
}
1 change: 0 additions & 1 deletion src/facets/ParticipantsLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ library ParticipantsLibrary {
uint8 validatorCount;
uint8 validatorMaxCount;
bool validatorsChanged;
uint32 chainId;
uint256 minimumStake;
QueueLibrary.Queue queue;
}
Expand Down
1 change: 1 addition & 0 deletions src/facets/Setup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ contract Setup is Constants {
update.addFacet(Staking.setMinimumStake.selector, stakingFacet);
update.addFacet(Staking.setRewardAmount.selector, stakingFacet);
update.addFacet(Staking.setRewardBonus.selector, stakingFacet);
update.addFacet(Staking.burn.selector, stakingFacet);

// Accusation Wiring
update.addFacet(Accusation.initializeAccusation.selector, accusationFacet);
Expand Down
5 changes: 3 additions & 2 deletions src/facets/SnapshotsFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma abicoder v2;
import "../interfaces/SnapshotsEvents.sol";

import "./AccessControlLibrary.sol";
import "./ChainStatusLibrary.sol";
import "./SnapshotsLibrary.sol";
import "./StopLibrary.sol";

Expand Down Expand Up @@ -35,11 +36,11 @@ contract SnapshotsFacet is AccessControlled, Constants, SnapshotsEvents, Stoppab
}

function setEpoch(uint256 ns) external onlyOperator {
SnapshotsLibrary.setEpoch(ns);
ChainStatusLibrary.setEpoch(ns);
}

function epoch() external view returns (uint256) {
return SnapshotsLibrary.epoch();
return ChainStatusLibrary.epoch();
}

function extractUint32(bytes calldata src, uint idx) external pure returns (uint32 val) {
Expand Down
25 changes: 9 additions & 16 deletions src/facets/SnapshotsLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity >=0.7.4;
pragma abicoder v2;

import "./ChainStatusLibrary.sol";
import "./ParticipantsLibrary.sol";
import "./StakingLibrary.sol";

Expand All @@ -24,7 +25,6 @@ library SnapshotsLibrary {

struct SnapshotsStorage {
mapping(uint256 => Snapshot) snapshots;
uint256 nextSnapshot;
bool validatorsChanged; // i.e. when we do nextSnapshot will there be different validators?
uint256 minEthSnapshotSize;
uint256 minMadSnapshotSize;
Expand All @@ -41,14 +41,6 @@ library SnapshotsLibrary {
//
//

function setEpoch(uint256 ns) internal {
snapshotsStorage().nextSnapshot = ns;
}

function epoch() internal view returns (uint256) {
return snapshotsStorage().nextSnapshot;
}

function extractUint32(bytes memory src, uint idx) internal pure returns (uint32 val) {
val = uint8(src[idx+3]);
val = (val << 8) | uint8(src[idx+2]);
Expand Down Expand Up @@ -126,6 +118,7 @@ library SnapshotsLibrary {

function snapshot(bytes calldata _signatureGroup, bytes calldata _bclaims) internal returns (bool) {

ChainStatusLibrary.ChainStatusStorage storage cs = ChainStatusLibrary.chainStatusStorage();
SnapshotsStorage storage ss = snapshotsStorage();

uint256[4] memory publicKey;
Expand All @@ -142,7 +135,7 @@ library SnapshotsLibrary {
uint32 height = extractUint32(_bclaims, 12);

// Store snapshot
Snapshot storage currentSnapshot = ss.snapshots[ss.nextSnapshot];
Snapshot storage currentSnapshot = ss.snapshots[cs.epoch];

currentSnapshot.saved = true;
currentSnapshot.rawBlockClaims = _bclaims;
Expand All @@ -151,8 +144,8 @@ library SnapshotsLibrary {
currentSnapshot.madHeight = height;
currentSnapshot.chainId = chainId;

if (ss.nextSnapshot > 1) {
Snapshot memory previousSnapshot = ss.snapshots[ss.nextSnapshot-1];
if (cs.epoch > 1) {
Snapshot memory previousSnapshot = ss.snapshots[cs.epoch-1];

require(
!previousSnapshot.saved || block.number >= previousSnapshot.ethHeight + ss.minEthSnapshotSize,
Expand All @@ -171,9 +164,9 @@ library SnapshotsLibrary {

for (uint idx=0; idx<ps.validators.length; idx++) {
if (msg.sender==ps.validators[idx]) {
StakingLibrary.lockRewardFor(ps.validators[idx], stakingS.rewardAmount + stakingS.rewardBonus, ss.nextSnapshot+2);
StakingLibrary.lockRewardFor(ps.validators[idx], stakingS.rewardAmount + stakingS.rewardBonus, cs.epoch+2);
} else {
StakingLibrary.lockRewardFor(ps.validators[idx], stakingS.rewardAmount, ss.nextSnapshot+2);
StakingLibrary.lockRewardFor(ps.validators[idx], stakingS.rewardAmount, cs.epoch+2);
}
}

Expand All @@ -183,9 +176,9 @@ library SnapshotsLibrary {
}
ss.validatorsChanged = false;

emit SnapshotTaken(chainId, ss.nextSnapshot, height, msg.sender, ss.validatorsChanged);
emit SnapshotTaken(chainId, cs.epoch, height, msg.sender, ss.validatorsChanged);

ss.nextSnapshot++;
cs.epoch++;

return reinitEthdkg;
}
Expand Down
26 changes: 10 additions & 16 deletions src/facets/StakingFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma abicoder v2;
import "../interfaces/StakingEvents.sol";

import "./AccessControlLibrary.sol";
import "./ChainStatusLibrary.sol";
import "./SnapshotsLibrary.sol";
import "./StakingLibrary.sol";
import "./StopLibrary.sol";
Expand Down Expand Up @@ -43,17 +44,20 @@ contract StakingFacet is AccessControlled, Constants, Stoppable {
}

function majorFine(address who) external {

StakingLibrary.StakingStorage storage ss = StakingLibrary.stakingStorage();
require(msg.sender == ss.ethdkgAddress, "only allowed from ethdkg");

StakingLibrary.fine(who, ss.majorStakeFine);
// TODO update signature and usage to identify why there's a fine
StakingLibrary.fine(who, bytes32("major"), ss.majorStakeFine);
}

function minorFine(address who) external {
StakingLibrary.StakingStorage storage ss = StakingLibrary.stakingStorage();
require(msg.sender == ss.ethdkgAddress, "only allowed from ethdkg");

StakingLibrary.fine(who, ss.minorStakeFine);
// TODO update signature and usage to identify why there's a fine
StakingLibrary.fine(who, bytes32("minor"), ss.minorStakeFine);
}

// Major Stake Fine getter/setter
Expand Down Expand Up @@ -91,15 +95,6 @@ contract StakingFacet is AccessControlled, Constants, Stoppable {
StakingLibrary.stakingStorage().rewardBonus = _rewardBonus;
}

// Setting and retrieving epoch / Snapshots "epoch()" is preferred method
function currentEpoch() external view returns (uint256) {
return SnapshotsLibrary.epoch();
}

function setCurrentEpoch(uint256 _epoch) external onlyOperator {
SnapshotsLibrary.setEpoch(_epoch);
}

function lockStake(uint256 amount) external stoppable returns (bool) {
return StakingLibrary.lockStakeFor(msg.sender, amount);
}
Expand Down Expand Up @@ -146,13 +141,12 @@ contract StakingFacet is AccessControlled, Constants, Stoppable {
return StakingLibrary.lockRewardFor(who, amountReward, unlockEpoch);
}

// Called by ETHDKG + Validation contracts to burn all stake of a malicious validator
function burnStake(address who, uint256 amount) external onlyOperator stoppable returns (bool) {
return StakingLibrary.burnStake(who, amount);
function fine(address who, bytes32 why, uint256 amount) external onlyOperator stoppable returns (bool) {
return StakingLibrary.fine(who, why, amount);
}

function fine(address who, uint256 amount) external onlyOperator stoppable returns (bool) {
return StakingLibrary.fine(who, amount);
function burn(address who) external onlyOperator stoppable returns (bool) {
return StakingLibrary.burn(who);
}

function unlockReward() external stoppable returns (bool) {
Expand Down
14 changes: 14 additions & 0 deletions src/facets/StakingFacet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,18 @@ contract StakingFacetTest is Constants, DSTest, Setup {
assertEq(staking.balanceRewardFor(me), 0); // no reward is still locked
}

function testFine() public {

}

function testBurn() public {
staking.lockStakeFor(me, 1_000_000);

assertEq(staking.balanceStakeFor(me), 1_000_000);

staking.burn(me);
assertEq(staking.balanceStakeFor(me), 0);
}


}
Loading

0 comments on commit 2984544

Please sign in to comment.