Skip to content

Commit

Permalink
Merge 7578465 into a0a22ab
Browse files Browse the repository at this point in the history
  • Loading branch information
gjgd committed Jul 11, 2018
2 parents a0a22ab + 7578465 commit 0c00ff9
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 117 deletions.
4 changes: 4 additions & 0 deletions contracts/ProviderPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ contract ProviderPool is Ownable {
function updateProvider(address _provider, uint _newBondedAmount) internal {
providerPool.updateKey(_provider, _newBondedAmount, 0, 0);
}

function removeProvider(address _provider) internal {
providerPool.remove(_provider);
}
}
2 changes: 1 addition & 1 deletion contracts/RoundManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract RoundManager {
_;
}

constructor() {
constructor() public {
electionPeriodLength = 20;
rateLockDeadline = 5;
}
Expand Down
13 changes: 9 additions & 4 deletions contracts/TestProviderPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import "../contracts/ProviderPool.sol";
contract TestProviderPool is ProviderPool {

// @dev convenience method to be able to call addProvider from web3js (it has internal visibility)
function publicAddProvider(address _providerAddress, uint _bondedAmount) public {
addProvider(_providerAddress, _bondedAmount);
function publicAddProvider(address _provider, uint _bondedAmount) public {
addProvider(_provider, _bondedAmount);
}

// @dev convenience method to be able to call updateProvider from web3js (it has internal visibility)
function publicUpdateProvider(address _providerAddress, uint _bondedAmount) public {
updateProvider(_providerAddress, _bondedAmount);
function publicUpdateProvider(address _provider, uint _bondedAmount) public {
updateProvider(_provider, _bondedAmount);
}

// @dev convenience method to be able to call removeProvider from web3js (it has internal visibility)
function publicRemoveProvider(address _provider) public {
removeProvider(_provider);
}
}

46 changes: 23 additions & 23 deletions contracts/ProviderRound.sol → contracts/TransmuteDPOS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ import "./TransmuteToken.sol";
import "./RoundManager.sol";
import "./ProviderPool.sol";

// TODO: Change name to TransmuteDPOS
contract ProviderRound is TransmuteToken, RoundManager, ProviderPool {
contract TransmuteDPOS is TransmuteToken, RoundManager, ProviderPool {

event ProviderAdded (
address indexed _providerAddress,
address indexed _provider,
uint _pricePerStorageMineral,
uint _pricePerComputeMineral,
uint _blockRewardCut,
uint _feeShare
);

event ProviderUpdated (
address indexed _providerAddress,
address indexed _provider,
uint _pricePerStorageMineral,
uint _pricePerComputeMineral,
uint _blockRewardCut,
uint _feeShare
);

event ProviderResigned (
address indexed _providerAddress
address indexed _provider
);

struct Delegator {
Expand Down Expand Up @@ -54,40 +53,41 @@ contract ProviderRound is TransmuteToken, RoundManager, ProviderPool {
{
require(_blockRewardCut <= 100);
require(_feeShare <= 100);
Provider storage provider = providers[msg.sender];
Delegator storage delegator = delegators[msg.sender];
require(delegator.delegateAddress == msg.sender);
require(delegator.amountBonded > 0);
if (provider.status == ProviderStatus.Unregistered) {
Provider storage p = providers[msg.sender];
Delegator storage d = delegators[msg.sender];
require(d.delegateAddress == msg.sender);
require(d.amountBonded > 0);
if (p.status == ProviderStatus.Unregistered) {
numberOfProviders = numberOfProviders.add(1);
addProvider(msg.sender, provider.totalAmountBonded);
addProvider(msg.sender, p.totalAmountBonded);
emit ProviderAdded(msg.sender, _pricePerStorageMineral, _pricePerComputeMineral, _blockRewardCut, _feeShare);
} else {
updateProvider(msg.sender, provider.totalAmountBonded);
updateProvider(msg.sender, p.totalAmountBonded);
emit ProviderUpdated(msg.sender, _pricePerStorageMineral, _pricePerComputeMineral, _blockRewardCut, _feeShare);
}
provider.status = ProviderStatus.Registered;
provider.pricePerStorageMineral = _pricePerStorageMineral;
provider.pricePerComputeMineral = _pricePerComputeMineral;
provider.blockRewardCut = _blockRewardCut;
provider.feeShare = _feeShare;
p.status = ProviderStatus.Registered;
p.pricePerStorageMineral = _pricePerStorageMineral;
p.pricePerComputeMineral = _pricePerComputeMineral;
p.blockRewardCut = _blockRewardCut;
p.feeShare = _feeShare;
}

function resignAsProvider() public {
require(providers[msg.sender].status != ProviderStatus.Unregistered);
removeProvider(msg.sender);
delete providers[msg.sender];
emit ProviderResigned(msg.sender);
}

function bond(address _providerAddress, uint _amount) external {
Provider storage provider = providers[_providerAddress];
function bond(address _provider, uint _amount) external {
Provider storage p = providers[_provider];
// A delegator is only allowed to bond to an Unregistered provider if the provider is himself
// otherwise _providerAddress has to be associated with a Registered provider
require(_providerAddress == msg.sender || provider.status != ProviderStatus.Unregistered);
// otherwise _provider has to be associated with a Registered provider
require(_provider == msg.sender || p.status != ProviderStatus.Unregistered);
// Check if delegator has not already bonded to some address
require(delegators[msg.sender].delegateAddress == address(0));
this.transferFrom(msg.sender, this, _amount);
delegators[msg.sender] = Delegator(_providerAddress, _amount);
provider.totalAmountBonded = provider.totalAmountBonded.add(_amount);
delegators[msg.sender] = Delegator(_provider, _amount);
p.totalAmountBonded = p.totalAmountBonded.add(_amount);
}
}
6 changes: 3 additions & 3 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const TST = artifacts.require('./TransmuteToken.sol');
const ProviderRound = artifacts.require('./ProviderRound.sol');
const TransmuteDPOS = artifacts.require('./TransmuteDPOS.sol');
const RoundManager = artifacts.require('./RoundManager.sol');
const SortedDoublyLL = artifacts.require('./SortedDoublyLL.sol');
const TestProviderPool = artifacts.require('./TestProviderPool.sol');

module.exports = deployer => {
deployer.deploy(TST);
deployer.deploy(SortedDoublyLL);
deployer.link(SortedDoublyLL, ProviderRound);
deployer.deploy(ProviderRound);
deployer.link(SortedDoublyLL, TransmuteDPOS);
deployer.deploy(TransmuteDPOS);
deployer.deploy(RoundManager);
deployer.link(SortedDoublyLL, TestProviderPool);
deployer.deploy(TestProviderPool);
Expand Down
33 changes: 31 additions & 2 deletions test/TestProviderPool.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ contract('ProviderPool', accounts => {
let currentAddress = providerPool[0]; // [0] is head of the list
let node = await pp.getProvider.call(currentAddress);
let end = providerPool[1]; // [1] is tail of the list
do {
while(currentAddress != end) {
assert(bondedAmountOfPreviousAddress >= node[0]); // [0] is the bondedAmount
assert.equal(node[2], previousAddress); // [2] is previous address in the list
bondedAmountOfPreviousAddress = node[0].toNumber();
previousAddress = currentAddress;
currentAddress = node[1]; // [1] is next address in the list
node = await pp.getProvider.call(currentAddress);
}
while(currentAddress != end)
}

describe('setMaxNumberOfProviders', () => {
Expand Down Expand Up @@ -154,5 +153,35 @@ contract('ProviderPool', accounts => {
await assertFail( pp.publicUpdateProvider(accounts[5], 1) );
});
});

describe('removeProvider', () => {

it('should remove the provider from the pool', async () => {
assert.equal(true, await pp.containsProvider(accounts[0]));
await pp.publicRemoveProvider(accounts[0]);
assert.equal(false, await pp.containsProvider(accounts[0]));
});

it('should decrease the size of the pool by one', async () => {
let providerPool = await pp.providerPool.call();
const previousSize = providerPool[3].toNumber(); // [3] is the current size of the pool
await pp.publicRemoveProvider(accounts[1]);
providerPool = await pp.providerPool.call();
assert.equal(previousSize - 1, providerPool[3]);
});

it('should keep the list in decreasing order of bonded amounts', async () => {
await assertProvidersAreSortedByBondedAmount();
await pp.publicRemoveProvider(accounts[3]);
await assertProvidersAreSortedByBondedAmount();
await pp.publicRemoveProvider(accounts[4]);
await assertProvidersAreSortedByBondedAmount();
});

it('should fail if provider is not in the pool', async () => {
assert.equal(false, await pp.containsProvider(accounts[0]));
await assertFail( pp.publicRemoveProvider(accounts[0]) );
});
});
});

Loading

0 comments on commit 0c00ff9

Please sign in to comment.