Skip to content

Commit

Permalink
Rename contract
Browse files Browse the repository at this point in the history
  • Loading branch information
gjgd committed Jul 11, 2018
1 parent 1452120 commit 8781188
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 86 deletions.
3 changes: 1 addition & 2 deletions contracts/ProviderRound.sol → contracts/TransmuteDPOS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ 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,
Expand Down
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
162 changes: 81 additions & 81 deletions test/TestProviderRound.spec.js → test/TestTransmuteDPOS.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const ProviderRound = artifacts.require('./ProviderRound.sol');
const TransmuteDPOS = artifacts.require('./TransmuteDPOS.sol');
const { blockMiner, assertFail } = require('./utils.js');
require('truffle-test-utils').init();

contract('ProviderRound', accounts => {
contract('TransmuteDPOS', accounts => {

let providerRound;
let tdpos;
let contractAddress;
const PROVIDER_POOL_SIZE = 5;
const PROVIDER_UNREGISTERED = 0;
Expand All @@ -16,40 +16,40 @@ contract('ProviderRound', accounts => {
// Step 3: Registering parameters with provider()
async function approveBondProvider(pricePerStorageMineral, pricePerComputeMineral, blockRewardCut, feeShare, amountBonded, providerAddress) {
// This approve function comes from the ERC20 Transmute Token contract
await providerRound.approve(contractAddress, amountBonded, {from: providerAddress});
await providerRound.bond(providerAddress, amountBonded, {from: providerAddress});
await providerRound.provider(pricePerStorageMineral, pricePerComputeMineral, blockRewardCut, feeShare, {from: providerAddress});
await tdpos.approve(contractAddress, amountBonded, {from: providerAddress});
await tdpos.bond(providerAddress, amountBonded, {from: providerAddress});
await tdpos.provider(pricePerStorageMineral, pricePerComputeMineral, blockRewardCut, feeShare, {from: providerAddress});
}

describe('provider', () => {

before(async () => {
providerRound = await ProviderRound.deployed();
contractAddress = providerRound.address;
tdpos = await TransmuteDPOS.deployed();
contractAddress = tdpos.address;
for(let i = 0; i < 5; i++) {
await providerRound.mint(accounts[i], 1000, {from: accounts[0]});
await tdpos.mint(accounts[i], 1000, {from: accounts[0]});
}
await providerRound.setMaxNumberOfProviders(PROVIDER_POOL_SIZE);
await tdpos.setMaxNumberOfProviders(PROVIDER_POOL_SIZE);
});

beforeEach(async () => {
await blockMiner.mineUntilEndOfElectionPeriod(providerRound);
await providerRound.initializeRound();
await blockMiner.mineUntilEndOfElectionPeriod(tdpos);
await tdpos.initializeRound();
});

it('should fail if the provider does not bond some tokens on himself first', async () => {
await assertFail( providerRound.provider(22, 10, 1, 25, {from: accounts[0]}) );
await assertFail( tdpos.provider(22, 10, 1, 25, {from: accounts[0]}) );
});

it('should initially set totalBondedAmount to the amount the provider bonded to himself', async () => {
await approveBondProvider(22, 10, 1, 25, 42, accounts[0]);
const provider = await providerRound.providers.call(accounts[0]);
const provider = await tdpos.providers.call(accounts[0]);
assert.equal(42, provider[5]); // [5] is totalBondedAmount
});

it("should register a provider's parameters", async () => {
await approveBondProvider(10, 20, 2, 35, 1, accounts[1]);
const provider = await providerRound.providers.call(accounts[1]);
const provider = await tdpos.providers.call(accounts[1]);
assert.equal(provider[0], PROVIDER_REGISTERED); // [0] is providerStatus
assert.equal(provider[1].toNumber(), 10); // [1] is pricePerStorageMineral
assert.equal(provider[2].toNumber(), 20); // [2] is pricePerComputeMineral
Expand All @@ -63,32 +63,32 @@ contract('ProviderRound', accounts => {
'provider should not be able to have a negative blockRewardCut'
);
await assertFail(
providerRound.provider(22, 10, 1, 125, {from: accounts[2]}),
tdpos.provider(22, 10, 1, 125, {from: accounts[2]}),
'provider should not be able to have more than 100% feeShare'
);
});

it('should fail if not called during an active round', async () => {
await blockMiner.mineUntilEndOfElectionPeriod(providerRound);
await assertFail( providerRound.provider(22, 10, 1, 25, {from: accounts[0]}) );
await blockMiner.mineUntilEndOfElectionPeriod(tdpos);
await assertFail( tdpos.provider(22, 10, 1, 25, {from: accounts[0]}) );
});

it('should work if called before the lock period of an active round', async () => {
await blockMiner.mineUntilLastBlockBeforeLockPeriod(providerRound);
await providerRound.provider(23, 10, 1, 25, {from: accounts[0]});
const provider = await providerRound.providers(accounts[0]);
await blockMiner.mineUntilLastBlockBeforeLockPeriod(tdpos);
await tdpos.provider(23, 10, 1, 25, {from: accounts[0]});
const provider = await tdpos.providers(accounts[0]);
assert.equal(23, provider[1]); // [1] is pricePerStorageMineral
});

it('should fail during the lock period of an active round', async () => {
await blockMiner.mineUntilLastBlockBeforeLockPeriod(providerRound);
await blockMiner.mineUntilLastBlockBeforeLockPeriod(tdpos);
// Enter lock period
await blockMiner.mine(1);
await assertFail( providerRound.provider(22, 10, 1, 25, {from: accounts[0]}) );
await assertFail( tdpos.provider(22, 10, 1, 25, {from: accounts[0]}) );
});

it('should send a ProviderAdded event for a new provider', async () => {
const result = await providerRound.provider(22, 10, 1, 25, {from: accounts[2]});
const result = await tdpos.provider(22, 10, 1, 25, {from: accounts[2]});
assert.web3Event(result, {
event: 'ProviderAdded',
args: {
Expand All @@ -102,7 +102,7 @@ contract('ProviderRound', accounts => {
});

it('should send a ProviderUpdated event for an existing provider', async () => {
const result = await providerRound.provider(21, 11, 2, 24, {from: accounts[2]});
const result = await tdpos.provider(21, 11, 2, 24, {from: accounts[2]});
assert.web3Event(result, {
event: 'ProviderUpdated',
args: {
Expand All @@ -118,66 +118,66 @@ contract('ProviderRound', accounts => {
// TODO: Doc about providers, pool and active pool
it('should add the provider to the pool if he is Unregistered and size < maxSize', async () => {
// Check that provider isn't registered yet
assert.equal(false, await providerRound.containsProvider(accounts[3]));
assert.equal(false, await tdpos.containsProvider(accounts[3]));
// Check the size of the pool increases by 1
let providerPool = await providerRound.providerPool.call();
let providerPool = await tdpos.providerPool.call();
const previousSize = providerPool[3].toNumber(); // [3] is current size of the pool
await approveBondProvider(21, 13, 3, 26, 1, accounts[3]);
providerPool = await providerRound.providerPool.call();
providerPool = await tdpos.providerPool.call();
assert.equal(previousSize + 1, providerPool[3]);
// Check that the provider is registered in the pool now
assert.equal(true, await providerRound.containsProvider(accounts[3]));
assert.equal(true, await tdpos.containsProvider(accounts[3]));
});

it('should fail if provider is Unregistered and size == maxSize', async () => {
let providerPool = await providerRound.providerPool.call();
let providerPool = await tdpos.providerPool.call();
const maxSize = providerPool[2].toNumber(); // [2] is maxSize
let currentSize = providerPool[3]; // [3] is current size
assert.isAbove(maxSize, currentSize.toNumber());
await approveBondProvider(20 ,10, 2, 25, 1, accounts[4]);
providerPool = await providerRound.providerPool.call();
providerPool = await tdpos.providerPool.call();
currentSize = providerPool[3];
assert.equal(maxSize, currentSize);
await assertFail( approveBondProvider(20 ,10, 2, 25, 1, accounts[5]) );
});

it('should update the value of totalBondedAmount in the providerPool if the provider is Registered and size < maxSize', async () => {
// Check that provider is Registered
assert.equal(true, await providerRound.containsProvider(accounts[3]));
assert.equal(true, await tdpos.containsProvider(accounts[3]));
// Check the size of the pool stays the same
let providerPool = await providerRound.providerPool.call();
let providerPool = await tdpos.providerPool.call();
const previousSize = providerPool[3]; // [3] is current size of the pool
await providerRound.provider(19, 10, 2, 20, {from: accounts[3]});
providerPool = await providerRound.providerPool.call();
await tdpos.provider(19, 10, 2, 20, {from: accounts[3]});
providerPool = await tdpos.providerPool.call();
assert.deepEqual(previousSize, providerPool[3]);
});

it('should work if provider is Registered and size == maxSize', async () => {
await providerRound.provider(21 ,10, 2, 25, {from: accounts[4]});
await tdpos.provider(21 ,10, 2, 25, {from: accounts[4]});
});
});

describe('resignAsProvider', () => {

before(async () => {
providerRound = await ProviderRound.new();
contractAddress = providerRound.address;
tdpos = await TransmuteDPOS.new();
contractAddress = tdpos.address;
for(let i = 0; i < 5; i++) {
await providerRound.mint(accounts[i], 1000, {from: accounts[0]});
await tdpos.mint(accounts[i], 1000, {from: accounts[0]});
}
await providerRound.setMaxNumberOfProviders(PROVIDER_POOL_SIZE);
await blockMiner.mineUntilEndOfElectionPeriod(providerRound);
await providerRound.initializeRound();
await tdpos.setMaxNumberOfProviders(PROVIDER_POOL_SIZE);
await blockMiner.mineUntilEndOfElectionPeriod(tdpos);
await tdpos.initializeRound();
await approveBondProvider(22, 10, 1, 25, 1, accounts[0]);
await approveBondProvider(22, 10, 1, 25, 1, accounts[1]);
await approveBondProvider(22, 10, 1, 25, 1, accounts[2]);
});

it('should remove the provider from the provider mapping', async () => {
const registeredProvider = await providerRound.providers.call(accounts[0]);
const registeredProvider = await tdpos.providers.call(accounts[0]);
assert.equal(PROVIDER_REGISTERED, registeredProvider[0]); // [0] is providerStatus
await providerRound.resignAsProvider({from: accounts[0]});
const resignedProvider = await providerRound.providers.call(accounts[0]);
await tdpos.resignAsProvider({from: accounts[0]});
const resignedProvider = await tdpos.providers.call(accounts[0]);
assert.equal(PROVIDER_UNREGISTERED, resignedProvider[0]); // [0] is providerStatus
assert.equal(0, resignedProvider[1]); // [1] is pricePerStorageMineral
assert.equal(0, resignedProvider[2]); // [2] is pricePerComputeMineral
Expand All @@ -187,13 +187,13 @@ contract('ProviderRound', accounts => {
});

it('should remove the provider from the providerPool', async () => {
assert.equal(true, await providerRound.containsProvider(accounts[1]));
await providerRound.resignAsProvider({from: accounts[1]});
assert.equal(false, await providerRound.containsProvider(accounts[1]));
assert.equal(true, await tdpos.containsProvider(accounts[1]));
await tdpos.resignAsProvider({from: accounts[1]});
assert.equal(false, await tdpos.containsProvider(accounts[1]));
});

it('should send a ProviderResigned event', async () => {
const result = await providerRound.resignAsProvider({from: accounts[2]});
const result = await tdpos.resignAsProvider({from: accounts[2]});
assert.web3Event(result, {
event: 'ProviderResigned',
args: {
Expand All @@ -203,75 +203,75 @@ contract('ProviderRound', accounts => {
});

it("should fail if the transaction's sender is not a provider", async () => {
await assertFail( providerRound.resignAsProvider({from: accounts[3]}) );
await assertFail( tdpos.resignAsProvider({from: accounts[3]}) );
});
});

describe('bond', () => {

before(async () => {
providerRound = await ProviderRound.new();
contractAddress = providerRound.address;
tdpos = await TransmuteDPOS.new();
contractAddress = tdpos.address;
for(let i = 0; i < 10; i++) {
await providerRound.mint(accounts[i], 1000, {from: accounts[0]});
await tdpos.mint(accounts[i], 1000, {from: accounts[0]});
}
await providerRound.setMaxNumberOfProviders(PROVIDER_POOL_SIZE);
await blockMiner.mineUntilEndOfElectionPeriod(providerRound);
await providerRound.initializeRound();
await tdpos.setMaxNumberOfProviders(PROVIDER_POOL_SIZE);
await blockMiner.mineUntilEndOfElectionPeriod(tdpos);
await tdpos.initializeRound();
await approveBondProvider(22, 10, 1, 25, 1, accounts[0]);
await approveBondProvider(10, 20, 2, 35, 1, accounts[1]);
});

it('should fail if the delegator does not approve to transfer at least the bonded amount', async () => {
await assertFail( providerRound.bond(accounts[0], 10, {from: accounts[5]}) );
await providerRound.approve(contractAddress, 9, {from: accounts[5]});
await assertFail( providerRound.bond(accounts[0], 10, {from: accounts[5]}) );
await assertFail( tdpos.bond(accounts[0], 10, {from: accounts[5]}) );
await tdpos.approve(contractAddress, 9, {from: accounts[5]});
await assertFail( tdpos.bond(accounts[0], 10, {from: accounts[5]}) );
});

it('should add a delegator to the delegators list', async () => {
await providerRound.approve(contractAddress, 10, {from: accounts[5]});
await providerRound.bond(accounts[0], 10, {from: accounts[5]});
const firstDelegator = await providerRound.delegators.call(accounts[5]);
await tdpos.approve(contractAddress, 10, {from: accounts[5]});
await tdpos.bond(accounts[0], 10, {from: accounts[5]});
const firstDelegator = await tdpos.delegators.call(accounts[5]);
assert.equal(accounts[0], firstDelegator[0]); // [0] is delegateAddress
assert.equal(10, firstDelegator[1]); // [1] is amountBonded
});

it('should increase the totalAmountBonded of the provider', async () => {
await providerRound.approve(contractAddress, 20, {from: accounts[6]});
await providerRound.bond(accounts[0], 20, {from: accounts[6]});
const provider = await providerRound.providers.call(accounts[0]);
await tdpos.approve(contractAddress, 20, {from: accounts[6]});
await tdpos.bond(accounts[0], 20, {from: accounts[6]});
const provider = await tdpos.providers.call(accounts[0]);
assert.equal(31, provider[5].toNumber()); // [5] is totalAmountBonded
});

it('should fail if the address is not a registered provider address', async () => {
await providerRound.approve(contractAddress, 15, {from: accounts[7]})
await assertFail( providerRound.bond(accounts[2], 15, {from: accounts[7]}) );
await tdpos.approve(contractAddress, 15, {from: accounts[7]})
await assertFail( tdpos.bond(accounts[2], 15, {from: accounts[7]}) );
});

it('should work if the address is not a registered provider address but the address is the sender address', async () => {
await providerRound.approve(contractAddress, 15, {from: accounts[3]})
await providerRound.bond(accounts[3], 15, {from: accounts[3]});
await tdpos.approve(contractAddress, 15, {from: accounts[3]})
await tdpos.bond(accounts[3], 15, {from: accounts[3]});
});

it('should fail if called twice by the same delegator', async () => {
await providerRound.approve(contractAddress, 40, {from: accounts[8]});
await providerRound.bond(accounts[1], 20, {from: accounts[8]});
await assertFail( providerRound.bond(accounts[1], 20, {from: accounts[8]}) );
await tdpos.approve(contractAddress, 40, {from: accounts[8]});
await tdpos.bond(accounts[1], 20, {from: accounts[8]});
await assertFail( tdpos.bond(accounts[1], 20, {from: accounts[8]}) );
});

it('should fail if TST balance is less than bonded amount', async () => {
await providerRound.approve(contractAddress, 1001, {from: accounts[9]});
await assertFail( providerRound.bond(accounts[1], 1001, {from: accounts[9]}) )
assert(1001 >= (await providerRound.providers(accounts[1]))[5]);
await tdpos.approve(contractAddress, 1001, {from: accounts[9]});
await assertFail( tdpos.bond(accounts[1], 1001, {from: accounts[9]}) )
assert(1001 >= (await tdpos.providers(accounts[1]))[5]);
});

it("should transfer amount from the delegator's balance to the contract's balance", async () => {
const contractBalance = (await providerRound.balanceOf(providerRound.address)).toNumber();
assert.equal(1000, await providerRound.balanceOf(accounts[9]));
await providerRound.approve(contractAddress, 300, {from: accounts[9]});
await providerRound.bond(accounts[1], 300, {from: accounts[9]});
assert.equal(700, await providerRound.balanceOf(accounts[9]));
assert.equal(contractBalance + 300, await providerRound.balanceOf(providerRound.address));
const contractBalance = (await tdpos.balanceOf(tdpos.address)).toNumber();
assert.equal(1000, await tdpos.balanceOf(accounts[9]));
await tdpos.approve(contractAddress, 300, {from: accounts[9]});
await tdpos.bond(accounts[1], 300, {from: accounts[9]});
assert.equal(700, await tdpos.balanceOf(accounts[9]));
assert.equal(contractBalance + 300, await tdpos.balanceOf(tdpos.address));
});
});
});
Expand Down

0 comments on commit 8781188

Please sign in to comment.