Skip to content

Commit cc2f77e

Browse files
committed
Cross-chain variable decay
1 parent e21211a commit cc2f77e

18 files changed

Lines changed: 204 additions & 47 deletions

contracts/colony/Colony.sol

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ contract Colony is BasicMetaTransaction, Multicall, ColonyStorage, PatriciaTreeP
226226
bytes memory _skillCreationBefore,
227227
bytes memory _skillCreationAfter,
228228
bytes memory _setReputationRootHashBefore,
229-
bytes memory _setReputationRootHashAfter
229+
bytes memory _setReputationRootHashAfter,
230+
bytes memory _setColonyDecayRateBefore,
231+
bytes memory _setColonyDecayRateAfter
230232
)
231233
external
232234
stoppable
@@ -241,7 +243,9 @@ contract Colony is BasicMetaTransaction, Multicall, ColonyStorage, PatriciaTreeP
241243
_skillCreationBefore,
242244
_skillCreationAfter,
243245
_setReputationRootHashBefore,
244-
_setReputationRootHashAfter
246+
_setReputationRootHashAfter,
247+
_setColonyDecayRateBefore,
248+
_setColonyDecayRateAfter
245249
);
246250
}
247251

@@ -346,7 +350,7 @@ contract Colony is BasicMetaTransaction, Multicall, ColonyStorage, PatriciaTreeP
346350
ColonyAuthority colonyAuthority = ColonyAuthority(address(authority));
347351
bytes4 sig;
348352

349-
sig = bytes4(keccak256("setBridgeData(address,uint256,uint256,bytes,bytes,bytes,bytes,bytes,bytes)"));
353+
sig = bytes4(keccak256("setBridgeData(address,uint256,uint256,bytes,bytes,bytes,bytes,bytes,bytes,bytes,bytes)"));
350354
colonyAuthority.setRoleCapability(uint8(ColonyRole.Root), address(this), sig, true);
351355

352356
sig = bytes4(keccak256("setDefaultGlobalClaimDelay(uint256)"));

contracts/colony/ColonyAuthority.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ contract ColonyAuthority is CommonAuthority {
131131
addRoleCapability(ARBITRATION_ROLE, "setExpenditurePayout(uint256,uint256,uint256,uint256,address,uint256)");
132132

133133
// Added in colony vxxx
134-
addRoleCapability(ROOT_ROLE, "setBridgeData(address,uint256,uint256,bytes,bytes,bytes,bytes,bytes,bytes)");
134+
addRoleCapability(ROOT_ROLE, "setBridgeData(address,uint256,uint256,bytes,bytes,bytes,bytes,bytes,bytes,bytes,bytes)");
135135
addRoleCapability(ROOT_ROLE, "setDomainReputationScaling(uint256,uint256)");
136136
addRoleCapability(ROOT_ROLE, "setReputationDecayRate(uint256,uint256)");
137137
addRoleCapability(ROOT_ROLE, "setReputationMiningCycleRewardReputationScaling(uint256)");

contracts/colony/IMetaColony.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ interface IMetaColony is IColony {
8080
/// @param _skillCreationAfter The tx data after the dynamic part of the tx to brdige skill creation
8181
/// @param _setReputationRootHashBefore The tx data before the dynamic part of the tx to bridge a new reputation root hash
8282
/// @param _setReputationRootHashAfter The tx data after the dynamic part of the tx to bridge a new reputation root hash
83+
/// @param _setColonyDecayRateBefore The tx data before the dynamic part of the tx to set a colony's reputation decay rate
84+
/// @param _setColonyDecayRateAfter The tx data after the dynamic part of the tx to set a colony's reputation decay rate
8385
function setBridgeData(
8486
address _bridgeAddress,
8587
uint256 _chainId,
@@ -89,6 +91,8 @@ interface IMetaColony is IColony {
8991
bytes memory _skillCreationBefore,
9092
bytes memory _skillCreationAfter,
9193
bytes memory _setReputationRootHashBefore,
92-
bytes memory _setReputationRootHashAfter
94+
bytes memory _setReputationRootHashAfter,
95+
bytes memory _setColonyDecayRateBefore,
96+
bytes memory _setColonyDecayRateAfter
9397
) external;
9498
}

contracts/colonyNetwork/ColonyNetwork.sol

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,44 @@ contract ColonyNetwork is ColonyDataTypes, BasicMetaTransaction, ColonyNetworkSt
168168
return payoutWhitelist[_token];
169169
}
170170

171-
function setColonyReputationDecayRate(uint256 _numerator, uint256 _denominator) public calledByColony stoppable {
171+
function setColonyReputationDecayRate(uint256 _numerator, uint256 _denominator) public stoppable calledByColony {
172172
require(_numerator < 10**15, "colony-network-decay-numerator-too-big");
173173
require(_numerator <= _denominator, "colony-network-decay-rate-over-1");
174174

175-
ColonyDecayRate storage decayRate = colonyDecayRates[msgSender()];
175+
if (isMiningChain()){
176+
setColonyReputationDecayRateInternal(0, msgSender(), _numerator, _denominator);
177+
} else {
178+
bridgeColonyDecayRate(_numerator, _denominator);
179+
}
180+
}
181+
182+
function bridgeColonyDecayRate(uint256 _numerator, uint256 _denominator) internal {
183+
// Build the transaction we're going to send to the bridge to register the
184+
// creation of this skill on the home chain
185+
186+
bytes memory payload = abi.encodePacked(
187+
bridgeData[miningBridgeAddress].setColonyDecayRateBefore,
188+
abi.encodeWithSignature("setColonyReputationDecayRateFromBridge(address,uint256,uint256)", msgSender(), _numerator, _denominator),
189+
bridgeData[miningBridgeAddress].setColonyDecayRateAfter
190+
);
191+
192+
// Send bridge transaction
193+
// slither-disable-next-line unchecked-lowlevel
194+
(bool success, ) = miningBridgeAddress.call(payload);
195+
require(success, "colony-network-bridging-transaction-failed");
196+
}
197+
198+
function setColonyReputationDecayRateFromBridge(address _colony, uint256 _numerator, uint256 _denominator) public always
199+
onlyMiningChain
200+
{
201+
uint256 bridgeChainId = bridgeData[msgSender()].chainId;
202+
require(bridgeChainId != 0, "colony-network-not-known-bridge");
203+
204+
setColonyReputationDecayRateInternal(bridgeChainId, _colony, _numerator, _denominator);
205+
}
206+
207+
function setColonyReputationDecayRateInternal(uint256 _chainId, address _colony, uint256 _numerator, uint256 _denominator) internal {
208+
ColonyDecayRate storage decayRate = colonyDecayRates[_chainId][_colony];
176209

177210
if (activeReputationMiningCycle != decayRate.afterMiningCycle) {
178211
// Move the old-next values to current, as they are in effect
@@ -187,20 +220,25 @@ contract ColonyNetwork is ColonyDataTypes, BasicMetaTransaction, ColonyNetworkSt
187220
decayRate.nextNumerator = _numerator;
188221
decayRate.nextDenominator = _denominator;
189222

190-
emit ColonyReputationDecayRateToChange(msgSender(), activeReputationMiningCycle, _numerator, _denominator);
223+
emit ColonyReputationDecayRateToChange(_chainId, _colony, activeReputationMiningCycle, _numerator, _denominator);
191224
}
192225

193-
function getColonyReputationDecayRate(address _colony) public view returns (uint256, uint256) {
226+
function getColonyReputationDecayRate(uint256 _chainId, address _colony) public view returns (uint256, uint256) {
194227
uint256 numerator;
195228
uint256 denominator;
196229

197-
if (activeReputationMiningCycle != colonyDecayRates[_colony].afterMiningCycle) {
230+
uint256 chainId = _chainId;
231+
if (isMiningChainId(_chainId)){
232+
chainId = 0;
233+
}
234+
235+
if (activeReputationMiningCycle != colonyDecayRates[chainId][_colony].afterMiningCycle) {
198236
// Then the values of interest is whatever's in nextNumerator/nextDenominator
199-
numerator = colonyDecayRates[_colony].nextNumerator;
200-
denominator = colonyDecayRates[_colony].nextDenominator;
237+
numerator = colonyDecayRates[chainId][_colony].nextNumerator;
238+
denominator = colonyDecayRates[chainId][_colony].nextDenominator;
201239
} else {
202-
numerator = colonyDecayRates[_colony].currentNumerator;
203-
denominator = colonyDecayRates[_colony].currentDenominator;
240+
numerator = colonyDecayRates[chainId][_colony].currentNumerator;
241+
denominator = colonyDecayRates[chainId][_colony].currentDenominator;
204242
}
205243

206244
if (denominator == 0) {

contracts/colonyNetwork/ColonyNetworkDataTypes.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,13 @@ interface ColonyNetworkDataTypes {
186186
event ReputationUpdateStoredFromBridge(uint256 chainId, address colony, uint256 updateNumber);
187187

188188
/// @notice Event logged when a colony sets what its next decay rate is going to be
189+
/// @param chainId The chainId that the colony changing its decay rate is on
190+
/// @dev Note that in the case of the mining chain, this will be 0
189191
/// @param colony The colony changing its decay rate
190192
/// @param fromCycleCompleted When this mining cycle is completed, the new rate will be in effect
191193
/// @param numerator The new numerator of the decay rate
192194
/// @param denominator The new denominator of the decay rate
193-
event ColonyReputationDecayRateToChange(address colony, address fromCycleCompleted, uint256 numerator, uint256 denominator);
195+
event ColonyReputationDecayRateToChange(uint256 chainId, address colony, address fromCycleCompleted, uint256 numerator, uint256 denominator);
194196

195197
struct Skill {
196198
// total number of parent skills
@@ -235,6 +237,8 @@ interface ColonyNetworkDataTypes {
235237
bytes skillCreationAfter;
236238
bytes setReputationRootHashBefore;
237239
bytes setReputationRootHashAfter;
240+
bytes setColonyDecayRateBefore;
241+
bytes setColonyDecayRateAfter;
238242
}
239243

240244
struct PendingReputationUpdate {

contracts/colonyNetwork/ColonyNetworkSkills.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ contract ColonyNetworkSkills is ColonyNetworkStorage, Multicall, ColonyDataTypes
106106
bytes memory _skillCreationBefore,
107107
bytes memory _skillCreationAfter,
108108
bytes memory _setReputationRootHashBefore,
109-
bytes memory _setReputationRootHashAfter
109+
bytes memory _setReputationRootHashAfter,
110+
bytes memory _setColonyDecayRateBefore,
111+
bytes memory _setColonyDecayRateAfter
110112
)
111113
public
112114
always
@@ -125,7 +127,9 @@ contract ColonyNetworkSkills is ColonyNetworkStorage, Multicall, ColonyDataTypes
125127
_skillCreationBefore,
126128
_skillCreationAfter,
127129
_setReputationRootHashBefore,
128-
_setReputationRootHashAfter
130+
_setReputationRootHashAfter,
131+
_setColonyDecayRateBefore,
132+
_setColonyDecayRateAfter
129133
);
130134

131135
if (networkSkillCounts[_chainId] == 0) {

contracts/colonyNetwork/ColonyNetworkStorage.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ contract ColonyNetworkStorage is ColonyNetworkDataTypes, DSMath, CommonStorage,
125125
// networkId -> colonyAddress -> updateCount -> update
126126
mapping(uint256 => mapping( address => mapping(uint256 => PendingReputationUpdate))) pendingReputationUpdates; // Storage slot 48
127127

128-
mapping(address => ColonyDecayRate) colonyDecayRates; // Storage slot 49
128+
// networkId -> colonyAddress -> reputation decay rate
129+
// Note that a network Id of 0 here indicates the mining chain
130+
mapping(uint256 => mapping(address => ColonyDecayRate)) colonyDecayRates; // Storage slot 49
129131

130132
// Modifiers
131133

contracts/colonyNetwork/IColonyNetwork.sol

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ interface IColonyNetwork is ColonyNetworkDataTypes, IRecovery, IBasicMetaTransac
473473
/// @param _skillCreationAfter The tx data after the dynamic part of the tx to brdige skill creation
474474
/// @param _setReputationRootHashBefore The tx data before the dynamic part of the tx to bridge a new reputation root hash
475475
/// @param _setReputationRootHashAfter The tx data after the dynamic part of the tx to bridge a new reputation root hash
476+
/// @param _setColonyDecayRateBefore The tx data before the dynamic part of the tx to set a colony's reputation decay rate
477+
/// @param _setColonyDecayRateAfter The tx data after the dynamic part of the tx to set a colony's reputation decay rate
476478
function setBridgeData(
477479
address _bridgeAddress,
478480
uint256 _chainId,
@@ -482,7 +484,9 @@ interface IColonyNetwork is ColonyNetworkDataTypes, IRecovery, IBasicMetaTransac
482484
bytes memory _skillCreationBefore,
483485
bytes memory _skillCreationAfter,
484486
bytes memory _setReputationRootHashBefore,
485-
bytes memory _setReputationRootHashAfter
487+
bytes memory _setReputationRootHashAfter,
488+
bytes memory _setColonyDecayRateBefore,
489+
bytes memory _setColonyDecayRateAfter
486490
) external;
487491

488492
/// @notice Called to get the details about known bridge _bridgeAddress
@@ -571,11 +575,19 @@ interface IColonyNetwork is ColonyNetworkDataTypes, IRecovery, IBasicMetaTransac
571575
/// @param _denominator The denominator of the fraction reputation does down by every reputation cycle
572576
function setColonyReputationDecayRate(uint256 _numerator, uint256 _denominator) external;
573577

578+
/// @notice Called by a bridge to set the rate at which reputation in a colony on the chain corresponding
579+
/// to that bridge decays
580+
/// @param _colony The colony on the chain in question
581+
/// @param _numerator The numerator of the fraction reputation does down by every reputation cycle
582+
/// @param _denominator The denominator of the fraction reputation does down by every reputation cycle
583+
function setColonyReputationDecayRateFromBridge(address _colony, uint256 _numerator, uint256 _denominator) external;
584+
574585
/// @notice Called to get the rate at which reputation in a colony decays
586+
/// @param _chainId The chainId the colony is deployed on
575587
/// @param _colony The address of the colony in question
576588
/// @return numerator The numerator of the fraction reputation does down by every reputation cycle
577589
/// @return denominator The denominator of the fraction reputation does down by every reputation cycle
578-
function getColonyReputationDecayRate(address _colony) external view returns (uint256 numerator, uint256 denominator);
590+
function getColonyReputationDecayRate(uint256 _chainId, address _colony) external view returns (uint256 numerator, uint256 denominator);
579591

580592
/// @notice Called to get an array containing all parent skill ids of a skill
581593
/// @param _skillId The skill id being queried

contracts/reputationMiningCycle/ReputationMiningCycleRespond.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,9 @@ contract ReputationMiningCycleRespond is ReputationMiningCycleCommon {
534534
uint256 numerator;
535535
uint256 denominator;
536536

537-
(numerator, denominator) = IColonyNetwork(colonyNetworkAddress).getColonyReputationDecayRate(logEntry.colony);
537+
// logEntry.skillId >> 128 gives us the chain Id. It's 0 if it's on the mining chain, but that's expected by
538+
// the function.
539+
(numerator, denominator) = IColonyNetwork(colonyNetworkAddress).getColonyReputationDecayRate(logEntry.skillId >> 128, logEntry.colony);
538540
require(uint256(_disagreeStateReputationValue) == (uint256(_agreeStateReputationValue)*numerator)/denominator, "colony-reputation-mining-decay-incorrect");
539541
} else {
540542
if (logEntry.amount >= 0) {

docs/interfaces/icolonynetwork.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ Get the number of colonies in the network.
542542
|---|---|---|
543543
|_count|uint256|The colony count
544544

545-
### `getColonyReputationDecayRate(address _colony):uint256 numerator, uint256 denominator`
545+
### `getColonyReputationDecayRate(uint256 _chainId, address _colony):uint256 numerator, uint256 denominator`
546546

547547
Called to get the rate at which reputation in a colony decays
548548

@@ -551,6 +551,7 @@ Called to get the rate at which reputation in a colony decays
551551

552552
|Name|Type|Description|
553553
|---|---|---|
554+
|_chainId|uint256|The chainId the colony is deployed on
554555
|_colony|address|The address of the colony in question
555556

556557
**Return Parameters**
@@ -1097,7 +1098,7 @@ Used to track that a user is eligible to claim a reward
10971098
|_amount|uint256|The amount of CLNY to be awarded
10981099

10991100

1100-
### `setBridgeData(address _bridgeAddress, uint256 _chainId, uint256 _gas, bytes memory _updateLogBefore, bytes memory _updateLogAfter, bytes memory _skillCreationBefore, bytes memory _skillCreationAfter, bytes memory _setReputationRootHashBefore, bytes memory _setReputationRootHashAfter)`
1101+
### `setBridgeData(address _bridgeAddress, uint256 _chainId, uint256 _gas, bytes memory _updateLogBefore, bytes memory _updateLogAfter, bytes memory _skillCreationBefore, bytes memory _skillCreationAfter, bytes memory _setReputationRootHashBefore, bytes memory _setReputationRootHashAfter, bytes memory _setColonyDecayRateBefore, bytes memory _setColonyDecayRateAfter)`
11011102

11021103
Called to set the details about bridge _bridgeAddress
11031104

@@ -1115,6 +1116,8 @@ Called to set the details about bridge _bridgeAddress
11151116
|_skillCreationAfter|bytes|The tx data after the dynamic part of the tx to brdige skill creation
11161117
|_setReputationRootHashBefore|bytes|The tx data before the dynamic part of the tx to bridge a new reputation root hash
11171118
|_setReputationRootHashAfter|bytes|The tx data after the dynamic part of the tx to bridge a new reputation root hash
1119+
|_setColonyDecayRateBefore|bytes|The tx data before the dynamic part of the tx to set a colony's reputation decay rate
1120+
|_setColonyDecayRateAfter|bytes|The tx data after the dynamic part of the tx to set a colony's reputation decay rate
11181121

11191122

11201123
### `setColonyReputationDecayRate(uint256 _numerator, uint256 _denominator)`
@@ -1130,6 +1133,20 @@ Called by a colony to set the rate at which reputation in that colony decays
11301133
|_denominator|uint256|The denominator of the fraction reputation does down by every reputation cycle
11311134

11321135

1136+
### `setColonyReputationDecayRateFromBridge(address _colony, uint256 _numerator, uint256 _denominator)`
1137+
1138+
Called by a bridge to set the rate at which reputation in a colony on the chain corresponding to that bridge decays
1139+
1140+
1141+
**Parameters**
1142+
1143+
|Name|Type|Description|
1144+
|---|---|---|
1145+
|_colony|address|The colony on the chain in question
1146+
|_numerator|uint256|The numerator of the fraction reputation does down by every reputation cycle
1147+
|_denominator|uint256|The denominator of the fraction reputation does down by every reputation cycle
1148+
1149+
11331150
### `setFeeInverse(uint256 _feeInverse)`
11341151

11351152
Set the colony network fee to pay. e.g. if the fee is 1% (or 0.01), pass 100 as `_feeInverse`.

0 commit comments

Comments
 (0)