Skip to content

Commit

Permalink
Merge pull request #427 from DistributedCollective/staking-bug-fix-de…
Browse files Browse the repository at this point in the history
…legate-vp

Staking Contract Bug Fix - implementation for SIP-0043
  • Loading branch information
tjcloa authored Mar 31, 2022
2 parents 6819cf2 + 8a629ba commit cba5a0b
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion brownie-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ networks:
cmd_settings:
port: 443
gas_limit: 6800000
gas_price: 66000010 # 25000000000 25 GWEI
gas_price: 79000010 # 25000000000 25 GWEI
reverting_tx_gas_limit: false
default_contract_owner: false

Expand Down
2 changes: 1 addition & 1 deletion contracts/governance/Staking/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ contract Staking is IStaking, WeightedStaking, ApprovalReceiver {
* */
function extendStakingDuration(uint256 previousLock, uint256 until) public whenNotPaused {
until = timestampToLockDate(until);
require(previousLock <= until, "S04"); // cannot reduce staking duration
require(previousLock < until, "S04"); // must increase staking duration

/// @dev Do not exceed the max duration, no overflow possible.
uint256 latest = timestampToLockDate(block.timestamp + MAX_DURATION);
Expand Down
3 changes: 2 additions & 1 deletion scripts/contractInteraction/mainnet_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"StakingLogic3": "0xcAB5028619839dbb92a193f59026383973F04008",
"StakingLogic4": "0x4BaBb34189a9CDa8213D24DD3984058Fb8A955D2",
"StakingLogic5": "0xe31A938F5cf1C41747B5F20f9dD5d509B2ACd49c",

"StakingLogic6": "0x4769c04F2537C3b951Efa8a330A15716B5913Af6",

"FeeSharingProxyOld": "0x12B1B0C67d9A771EB5Db7726d23fdc6848fd93ef",
"FeeSharingProxy": "0x115cAF168c51eD15ec535727F64684D33B7b08D1",
"FeeSharingLogic": "0x8289AF920cA3d63245740a20116e13aAe0F978e3",
Expand Down
6 changes: 6 additions & 0 deletions scripts/contractInteraction/multisig.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ def replaceOwnerOnMultisig(multisig, oldOwner, newOwner):
def confirmWithMS(txId):
multisig = Contract.from_abi("MultiSig", address = conf.contracts['multisig'], abi=MultiSigWallet.abi, owner=conf.acct)
multisig.confirmTransaction(txId)
checkTx(txId)

def revokeConfirmationMS(txId):
multisig = Contract.from_abi("MultiSig", address = conf.contracts['multisig'], abi=MultiSigWallet.abi, owner=conf.acct)
multisig.revokeConfirmation(txId)

def confirmMultipleTxsWithMS(txIdFrom, txIdTo):
for i in range(txIdFrom, txIdTo + 1): # the right boundary processed to the value-1, so adding 1
confirmWithMS(i)
checkTx(i)

def checkTx(txId):
multisig = Contract.from_abi("MultiSig", address=conf.contracts['multisig'], abi=MultiSigWallet.abi, owner=conf.acct)
Expand Down
2 changes: 1 addition & 1 deletion scripts/contractInteraction/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from scripts.utils import *
import scripts.contractInteraction.config as conf

conf.loadConfig()
#conf.loadConfig()


def isProtocolPaused():
Expand Down
10 changes: 10 additions & 0 deletions scripts/contractInteraction/staking_vesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ def getStakes(address):
stakingProxy = Contract.from_abi("Staking", address=conf.contracts['Staking'], abi=Staking.abi, owner=conf.acct)
print(stakingProxy.getStakes(address))

def getStakingLogicAddess():
# Get the proxy contract instance
stakingProxy = Contract.from_abi("Staking", address=conf.contracts['Staking'], abi=StakingProxy.abi, owner=conf.acct)
print("Staking contract logic address:", stakingProxy.getImplementation())

def stakeTokens(sovAmount, stakeTime, acctAddress, delegateeAddress):
SOVtoken = Contract.from_abi("SOV", address=conf.contracts['SOV'], abi=SOV.abi, owner=acctAddress)
staking = Contract.from_abi("Staking", address=conf.contracts['Staking'], abi=Staking.abi, owner=acctAddress)
Expand All @@ -271,6 +276,11 @@ def pauseOrUnpauseStaking(flag):
data = staking.pauseUnpause.encode_input(flag)
sendWithMultisig(conf.contracts['multisig'], staking.address, data, conf.acct)

def isStakingPausedOrUnpaused():
# Get the proxy contract instance
staking = Contract.from_abi("Staking", address=conf.contracts['Staking'], abi=Staking.abi, owner=conf.acct)
print("isStakingPausedOrUnpaused", staking.paused())

def freezeOrUnfreezeStakingWithdawal(flag):
# Get the proxy contract instance
staking = Contract.from_abi("Staking", address=conf.contracts['Staking'], abi=Staking.abi, owner=conf.acct)
Expand Down
1 change: 1 addition & 0 deletions scripts/contractInteraction/testnet_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"StakingLogic": "0xeAd31Bdc8c777e85f2848B57E498f44839515854",
"StakingLogic4": "0x6EE4D162de10Dc83458E9CdeB54151d485df8562",
"StakingLogic5": "0xeAd31Bdc8c777e85f2848B57E498f44839515854",
"StakingLogic6": "0x78372F3a1Bdd9F341c819f903038e8aA1FDC3FC6",

"OldFeeSharingProxy": "0x740E6f892C0132D659Abcd2B6146D237A4B6b653",
"FeeSharingProxy": "0xedD92fb7C556E4A4faf8c4f5A90f471aDCD018f4",
Expand Down
31 changes: 27 additions & 4 deletions scripts/sip/sip_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def main():

# Call the function you want here

# createProposalSIP0038()
createProposalSIP0042()
# createProposalSIP0043()

balanceAfter = acct.balance()

Expand All @@ -38,11 +37,14 @@ def loadConfig():
acct = accounts[0]
configFile = open('./scripts/contractInteraction/testnet_contracts.json')
elif thisNetwork == "testnet":
acct = accounts.load("rskdeployerdev")
acct = accounts.load("rskdeployer")
configFile = open('./scripts/contractInteraction/testnet_contracts.json')
elif thisNetwork == "rsk-testnet":
acct = accounts.load("rskdeployerdev")
acct = accounts.load("rskdeployer")
configFile = open('./scripts/contractInteraction/testnet_contracts.json')
elif thisNetwork == "testnet-dev":
acct = accounts.load("rskdeployerdev")
configFile = open('./scripts/contractInteraction/testnet_contracts.json')
elif thisNetwork == "rsk-mainnet":
acct = accounts.load("rskdeployer")
configFile = open('./scripts/contractInteraction/mainnet_contracts.json')
Expand Down Expand Up @@ -337,4 +339,25 @@ def createProposalSIP0042():
description = "SIP-0042: Staking Security Update, Details: https://github.com/DistributedCollective/SIPS/blob/7c1a44b/SIP-0042.md, sha256: 522e1e65c49ec028d81c3a1f94a47354c2f6287c2d90c6eec8f06dcc17a1ebcc"

# Create Proposal
print(signatures)
print(datas)
print(description)
createProposal(contracts['GovernorOwner'], targets, values, signatures, datas, description)

def createProposalSIP0043():

staking = Contract.from_abi("StakingProxy", address=contracts['Staking'], abi=StakingProxy.abi, owner=acct)

# Action
targets = [contracts['Staking']]
values = [0]
signatures = ["setImplementation(address)"]
data = staking.setImplementation.encode_input(contracts['StakingLogic6'])
datas = ["0x" + data[10:]]
description = "SIP-0043 : Critical governance bug fix, Details: https://github.com/DistributedCollective/SIPS/blob/bdd346e/SIP-0043.md, sha256: 7a99f0862208d77e54f30f3c3759ca1d7efe9d1d1ec7df1ef1f83c649aa651a4"

# Create Proposal
print(signatures)
print(datas)
print(description)
# createProposal(contracts['GovernorOwner'], targets, values, signatures, datas, description)
31 changes: 23 additions & 8 deletions tests/staking/ExtendedStakingTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,26 +506,41 @@ contract("Staking", (accounts) => {
it("shouldn't extendStakingDuration when _getPriorUserStakeByDate == 0", async () => {
let duration = new BN(0);
let lockedTS = await getTimeFromKickoff(duration);
// console.log("lockedTS: ", lockedTS.toString());
//console.log("lockedTS: ", lockedTS.toString());
let newTime = await getTimeFromKickoff(TWO_WEEKS);
// console.log("newTime: ", newTime.toString());
//console.log("newTime: ", newTime.toString());

// Trying to extend the stake when previous stake is 0
await expectRevert(staking.extendStakingDuration(lockedTS, newTime), "S05"); // S05 : no stakes till the prev lock date
});

it("should fail when trying to extendStakingDuration when the new date equals to the previous", async () => {
let amount = "1000";
const BN_TWO_WEEKS = new BN(TWO_WEEKS);
let duration = BN_TWO_WEEKS.mul(new BN(2));
let lockedTS = await getTimeFromKickoff(duration);
//console.log("lockedTS: ", lockedTS.toString());
let newTime = lockedTS; //await getTimeFromKickoff(TWO_WEEKS);

// Trying to extend the stake when previous stake is 0
await expectRevert(staking.extendStakingDuration(lockedTS, newTime), "S04"); // S04 : no stakes till the prev lock date
});

it("extend to a date inside the next 2 weeks granularity bucket", async () => {
let amount = "1000";
let duration = new BN(TWO_WEEKS).mul(new BN(2));
const BN_TWO_WEEKS = new BN(TWO_WEEKS);
let duration = BN_TWO_WEEKS.mul(new BN(2));
//console.log("duration: ", duration.toString());
let lockedTS = await getTimeFromKickoff(duration);
// console.log("lockedTS: ", lockedTS.toString());
//console.log("lockedTS: ", lockedTS.toString());

// Extending for 13 days
let newDuration = duration.add(new BN(DAY).mul(new BN(13)));
// Extending for 14 days
let newDuration = duration.add(BN_TWO_WEEKS);
//console.log("newDuration: ", newDuration.toString());
let newTime = await getTimeFromKickoff(newDuration);
console.log("newTime: ", newTime.toString());
//console.log("newTime: ", newTime.toString());
let newTimeLockDate = await staking.timestampToLockDate(newTime);
console.log("newTimeLockDate: ", newTimeLockDate.toString());
//console.log("newTimeLockDate: ", newTimeLockDate.toString());

// Set delegate as account1
await staking.stake(amount, lockedTS, root, account1);
Expand Down

0 comments on commit cba5a0b

Please sign in to comment.