Skip to content

Commit

Permalink
Test session (#323)
Browse files Browse the repository at this point in the history
- contract changes
- flow fixes
- log level changes
  • Loading branch information
simonovic86 authored Jun 29, 2018
1 parent 91507e9 commit d9fe18e
Show file tree
Hide file tree
Showing 20 changed files with 2,345 additions and 2,499 deletions.
1,843 changes: 890 additions & 953 deletions modules/Blockchain/Ethereum/bidding-contract/abi.json

Large diffs are not rendered by default.

94 changes: 12 additions & 82 deletions modules/Blockchain/Ethereum/contracts/Bidding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,71 +44,25 @@ contract EscrowHolder {
function initiateEscrow(address DC_wallet, address DH_wallet, bytes32 import_id, uint token_amount, uint stake_amount, uint total_time_in_minutes) public;
}

/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;

event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable () public {
owner = msg.sender;
}

/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}

/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}

}

contract Bidding is Ownable{
contract Bidding {
using SafeMath for uint256;

ERC20 public token;
EscrowHolder public escrow;
address public reading;

uint replication_modifier;

modifier onlyContracts() {
require(EscrowHolder(msg.sender) == escrow || msg.sender == reading);
_;
}

function Bidding(address token_address, address escrow_address, address reading_address)
public{
require(msg.sender != address(0));
require ( token_address != address(0) && escrow_address != address(0) && reading_address != address(0));
token = ERC20(token_address);
escrow = EscrowHolder(escrow_address);
reading = reading_address;
active_nodes = 0;
replication_modifier = 1;
}

function setReplicationModifier(uint new_modifier) public onlyOwner{
replication_modifier = new_modifier;
}


Expand All @@ -132,7 +86,6 @@ contract Bidding is Ownable{
uint first_bid_index;

uint replication_factor;
uint replication_modifier;

bool active;
bool finalized;
Expand Down Expand Up @@ -202,14 +155,13 @@ contract Bidding is Ownable{
address[] predetermined_DH_wallet,
bytes32[] predetermined_DH_node_id)
public {
require(msg.sender != address(0));
OfferDefinition storage this_offer = offer[import_id];

require(max_token_amount_per_DH > 0 && total_escrow_time_in_minutes > 0 && data_size_in_bytes > 0);
require(this_offer.active == false);

require(profile[msg.sender].balance >= max_token_amount_per_DH.mul(predetermined_DH_wallet.length.mul(2).add(replication_modifier)));
profile[msg.sender].balance = profile[msg.sender].balance.sub(max_token_amount_per_DH.mul(predetermined_DH_wallet.length.mul(2).add(replication_modifier)));
require(profile[msg.sender].balance >= max_token_amount_per_DH.mul(predetermined_DH_wallet.length.mul(2).add(1)));
profile[msg.sender].balance = profile[msg.sender].balance.sub(max_token_amount_per_DH.mul(predetermined_DH_wallet.length.mul(2).add(1)));
emit BalanceModified(msg.sender, profile[msg.sender].balance);

this_offer.DC_wallet = msg.sender;
Expand All @@ -223,7 +175,6 @@ contract Bidding is Ownable{
this_offer.data_size_in_bytes = data_size_in_bytes;

this_offer.replication_factor = predetermined_DH_wallet.length;
this_offer.replication_modifier = replication_modifier;

this_offer.active = true;
this_offer.finalized = false;
Expand All @@ -244,20 +195,18 @@ contract Bidding is Ownable{
//TODO Decide when and under which conditions DC can cancel an offer
function cancelOffer(bytes32 import_id)
public{
require(msg.sender != address(0));
OfferDefinition storage this_offer = offer[import_id];
require(this_offer.active && this_offer.DC_wallet == msg.sender
&& this_offer.finalized == false);
this_offer.active = false;
uint max_total_token_amount = this_offer.max_token_amount_per_DH.mul(this_offer.replication_factor.mul(2).add(this_offer.replication_modifier));
uint max_total_token_amount = this_offer.max_token_amount_per_DH.mul(this_offer.replication_factor.mul(2).add(1));
profile[msg.sender].balance = profile[msg.sender].balance.add(max_total_token_amount);
emit BalanceModified(msg.sender, profile[msg.sender].balance);
emit OfferCanceled(import_id);
}

function activatePredeterminedBid(bytes32 import_id, bytes32 DH_node_id, uint bid_index)
public{
require(msg.sender != address(0));
require(offer[import_id].active && !offer[import_id].finalized);

OfferDefinition storage this_offer = offer[import_id];
Expand All @@ -280,15 +229,14 @@ contract Bidding is Ownable{

function getDistanceParameters(bytes32 import_id)
public view returns (bytes32 node_hash, bytes32 data_hash, uint256 distance, uint256 current_ranking, uint256 required_bid_amount, uint256 active_nodes_){
require(msg.sender != address(0));
OfferDefinition storage this_offer = offer[import_id];

node_hash = bytes32(uint128(keccak256(msg.sender)));
data_hash = bytes32(uint128(this_offer.data_hash));


distance = calculateDistance(import_id, msg.sender);
required_bid_amount = this_offer.replication_factor.mul(2).add(this_offer.replication_modifier);
required_bid_amount = this_offer.replication_factor.mul(2).add(1);
active_nodes_ = active_nodes;

if(this_offer.first_bid_index == uint(-1)){
Expand All @@ -306,7 +254,6 @@ contract Bidding is Ownable{

function addBid(bytes32 import_id, bytes32 DH_node_id)
public returns (uint distance){
require(msg.sender != address(0));
require(offer[import_id].active && !offer[import_id].finalized);

OfferDefinition storage this_offer = offer[import_id];
Expand Down Expand Up @@ -356,14 +303,13 @@ contract Bidding is Ownable{
}
}

if(this_offer.bid.length >= this_offer.replication_factor.mul(3).add(this_offer.replication_modifier)) emit FinalizeOfferReady(import_id);
if(this_offer.bid.length >= this_offer.replication_factor.mul(3).add(1)) emit FinalizeOfferReady(import_id);

emit AddedBid(import_id, msg.sender, DH_node_id, this_bid_index);
// return this_bid_index;
}

function getBidIndex(bytes32 import_id, bytes32 DH_node_id) public view returns(uint){
require(msg.sender != address(0));
OfferDefinition storage this_offer = offer[import_id];
uint256 i = 0;
while(i < this_offer.bid.length && (offer[import_id].bid[i].DH_wallet != msg.sender || offer[import_id].bid[i].DH_node_id != DH_node_id)) i = i + 1;
Expand All @@ -373,26 +319,24 @@ contract Bidding is Ownable{

function cancelBid(bytes32 import_id, uint bid_index)
public{
require(msg.sender != address(0));
require(offer[import_id].bid[bid_index].DH_wallet == msg.sender);
offer[import_id].bid[bid_index].active = false;
}

function chooseBids(bytes32 import_id) public returns (uint256[] chosen_data_holders){
require(msg.sender != address(0));

OfferDefinition storage this_offer = offer[import_id];
require(this_offer.active && !this_offer.finalized);
require(this_offer.replication_factor.mul(3).add(this_offer.replication_modifier) <= this_offer.bid.length);
require(this_offer.replication_factor.mul(3).add(1) <= this_offer.bid.length);
// require(this_offer.offer_creation_timestamp + 5 minutes < block.timestamp);

chosen_data_holders = new uint256[](this_offer.replication_factor.mul(2).add(this_offer.replication_modifier));
chosen_data_holders = new uint256[](this_offer.replication_factor.mul(2).add(1));

uint256 i;
uint256 current_index = 0;

uint256 token_amount_sent = 0;
uint256 max_total_token_amount = this_offer.max_token_amount_per_DH.mul(this_offer.replication_factor.mul(2).add(this_offer.replication_modifier));
uint256 max_total_token_amount = this_offer.max_token_amount_per_DH.mul(this_offer.replication_factor.mul(2).add(1));

//Sending escrow requests to predetermined bids
for(i = 0; i < this_offer.replication_factor; i = i + 1){
Expand All @@ -415,7 +359,7 @@ contract Bidding is Ownable{

//Sending escrow requests to network bids
uint256 bid_index = this_offer.first_bid_index;
while(current_index < this_offer.replication_factor.mul(2).add(this_offer.replication_modifier)) {
while(current_index < this_offer.replication_factor.mul(2).add(1)) {

while(bid_index != uint(-1) && !this_offer.bid[bid_index].active){
bid_index = this_offer.bid[bid_index].next_bid;
Expand Down Expand Up @@ -452,12 +396,10 @@ contract Bidding is Ownable{


function isBidChosen(bytes32 import_id, uint bid_index) public constant returns (bool _isBidChosen){
require(msg.sender != address(0));
return offer[import_id].bid[bid_index].chosen;
}

function getOfferStatus(bytes32 import_id) public constant returns (bool isOfferFinal){
require(msg.sender != address(0));
return offer[import_id].finalized;
}

Expand All @@ -468,11 +410,10 @@ contract Bidding is Ownable{
event ReputationModified(address wallet, uint new_balance);

function createProfile(bytes32 node_id, uint price_per_byte_minute, uint stake_per_byte_minute, uint read_stake_factor, uint max_time_in_minutes) public{
require(msg.sender != address(0));
ProfileDefinition storage this_profile = profile[msg.sender];
require(!this_profile.active);
// require(!this_profile.active);
this_profile.active = true;
active_nodes = active_nodes.add(1);
if(!this_profile.active) active_nodes = active_nodes.add(1);

this_profile.token_amount_per_byte_minute = price_per_byte_minute;
this_profile.stake_amount_per_byte_minute = stake_per_byte_minute;
Expand All @@ -484,22 +425,18 @@ contract Bidding is Ownable{
}

function setPrice(uint new_price_per_byte_minute) public {
require(msg.sender != address(0));
profile[msg.sender].token_amount_per_byte_minute = new_price_per_byte_minute;
}

function setStake(uint new_stake_per_byte_minute) public {
require(msg.sender != address(0));
profile[msg.sender].stake_amount_per_byte_minute = new_stake_per_byte_minute;
}

function setMaxTime(uint new_max_time_in_minutes) public {
require(msg.sender != address(0));
profile[msg.sender].max_escrow_time_in_minutes = new_max_time_in_minutes;
}

function depositToken(uint amount) public {
require(msg.sender != address(0));
require(token.balanceOf(msg.sender) >= amount && token.allowance(msg.sender, this) >= amount);
uint amount_to_transfer = amount;
amount = 0;
Expand All @@ -511,7 +448,6 @@ contract Bidding is Ownable{
}

function withdrawToken(uint amount) public {
require(msg.sender != address(0));
uint256 amount_to_transfer;
if(profile[msg.sender].balance >= amount){
amount_to_transfer = amount;
Expand All @@ -529,32 +465,27 @@ contract Bidding is Ownable{
}

function increaseBalance(address wallet, uint amount) public onlyContracts {
require(msg.sender != address(0));
profile[wallet].balance = profile[wallet].balance.add(amount);
emit BalanceModified(wallet, profile[wallet].balance);
}

function decreaseBalance(address wallet, uint amount) public onlyContracts {
require(msg.sender != address(0));
require(profile[wallet].balance >= amount);
profile[wallet].balance = profile[wallet].balance.sub(amount);
emit BalanceModified(wallet, profile[wallet].balance);
}

function increaseReputation(address wallet, uint amount) public onlyContracts {
require(msg.sender != address(0));
profile[wallet].reputation = profile[wallet].reputation.add(amount);
emit ReputationModified(wallet, profile[wallet].reputation);
}

function addEscrow(address wallet) public onlyContracts {
require(msg.sender != address(0));
profile[wallet].number_of_escrows = profile[wallet].number_of_escrows.add(1);
}

function getBalance(address wallet)
public view returns (uint256) {
require(msg.sender != address(0));
return profile[wallet].balance;
}

Expand Down Expand Up @@ -617,7 +548,6 @@ contract Bidding is Ownable{

function calculateDistance(bytes32 import_id, address DH_wallet)
public view returns (uint256 distance) {
require(msg.sender != address(0));
OfferDefinition storage this_offer = offer[import_id];
ProfileDefinition storage this_DH = profile[DH_wallet];

Expand Down
Loading

0 comments on commit d9fe18e

Please sign in to comment.