Commit 0866c0a4 authored by didi's avatar didi
Browse files

started addressing circular dependencies (not yet correctly implemented), refs #2

parent 5a6340b7
// TODO: use the SafeMath lib of openzeppelin (implicit overflow checks etc)?
// TODO: check if this complies with the recommended style: http://solidity.readthedocs.io/en/develop/style-guide.html
pragma solidity ^0.4.11;
......@@ -32,6 +33,7 @@ contract Streem {
*/
mapping(address => uint) outStreamPtrs;
mapping(address => uint) inStreamPtrs;
// TODO: convert to mapping (better suited when elements are deleted)
Stream[] streams;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
......@@ -64,7 +66,7 @@ contract Streem {
// returns the settled balance and the outstanding balance (> 0 if underfunded)
function settleStream(Stream s) internal returns (uint, uint) {
var bal = streamBalance(s);
var bal = streamBalance(s, s, 1);
uint dt = uint(bal / s.perSecond);
// since we don't allow fractional seconds, the possible settleBalance may be lower than the actual streamBalance (TODO: sure?)
var settleBal = dt * s.perSecond;
......@@ -141,6 +143,11 @@ contract Streem {
delete streams[sid];
}
function equals(Stream s1, Stream s2) internal constant returns (bool) {
// TODO: not multi-stream ready
return s1.sender == s2.sender && s1.receiver == s2.receiver;
}
// returns the naive "should be" balance of a stream, ignoring the possibility of it running out of funds
function naiveStreamBalance(Stream s) internal constant returns (uint256) {
return (now - s.startTimestamp) * s.perSecond;
......@@ -152,12 +159,15 @@ contract Streem {
* Implements min(outgoingStreamBalance, staticBalance + incomingStreamBalance)
* TODO: due to the involved recursion, this will lead to an endless loop in circular relations, e.g. A -> B, B -> A
*/
function streamBalance(Stream s) internal constant returns (uint256) {
function streamBalance(Stream s, Stream origin, uint hops) internal constant returns (uint256) {
// naming: osb -> outgoingStreamBalance, isb -> incomingStreamBalance, sb -> static balance
uint256 osb = naiveStreamBalance(s);
if(equals(s, origin)) { // special case: break on circular dependency. TODO: proof correctness
return osb;
} else {
var inS = getInStreamOf(s.sender);
uint256 isb = exists(inS) ? streamBalance(inS) : 0;
uint256 isb = exists(inS) ? streamBalance(inS, origin, hops + 1) : 0;
int sb = settledBalances[s.sender];
......@@ -166,6 +176,7 @@ contract Streem {
return min(osb, uint(sb + int(isb)));
}
}
// this balance function can return a negative value if an outgoing stream went "under water"
// and a higher than real balance if an incoming stream went "under water".
......@@ -193,10 +204,10 @@ contract Streem {
function balanceOf(address _owner) constant returns (uint256) {
var inS = getInStreamOf(_owner);
// no prettier null check possible? https://ethereum.stackexchange.com/questions/871/what-is-the-zero-empty-or-null-value-of-a-struct
uint256 inStreamBal = exists(inS) ? streamBalance(inS) : 0;
uint256 inStreamBal = exists(inS) ? streamBalance(inS, inS, 1) : 0;
var outS = getOutStreamOf(_owner);
uint256 outStreamBal = exists(outS) ? streamBalance(outS) : 0;
uint256 outStreamBal = exists(outS) ? streamBalance(outS, outS, 1) : 0;
// TODO: check overflow before casting
assert(settledBalances[_owner] + int(inStreamBal) - int(outStreamBal) >= 0);
......
......@@ -317,7 +317,7 @@ const contract = {
"type": "event"
}
],
"unlinked_binary": "0x6060604052341561000f57600080fd5b60405160208061107f833981016040528080519150505b60018054600160a060020a03191633600160a060020a03169081178255600090815260026020526040812083905582905560058054909181016100698382610103565b916000526020600020906004020160005b60806040519081016040908152600080835260208301819052908201819052606082015291905081518154600160a060020a031916600160a060020a03919091161781556020820151600182018054600160a060020a031916600160a060020a039290921691909117905560408201518160020155606082015181600301555050505b5061017b565b81548183558181151161012f5760040281600402836000526020600020918201910161012f9190610135565b5b505050565b61017891905b80821115610174578054600160a060020a031990811682556001820180549091169055600060028201819055600382015560040161013b565b5090565b90565b610ef58061018a6000396000f300606060405236156100d85763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166306fdde0381146100dd57806318160ddd14610168578063313ce5671461018d5780635cc8ce33146101b657806364a80c0c146101db57806370a08231146101ff5780637ae2b5c71461023057806395d89b411461025b5780639dad9382146102e6578063a1365fda146102fb578063a9059cbb14610320578063e13e2ecf14610344578063e61b959e14610368578063e8f88890146103b1578063f408ebe9146103fa575b600080fd5b34156100e857600080fd5b6100f061040f565b60405160208082528190810183818151815260200191508051906020019080838360005b8381101561012d5780820151818401525b602001610114565b50505050905090810190601f16801561015a5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561017357600080fd5b61017b610446565b60405190815260200160405180910390f35b341561019857600080fd5b6101a061044c565b60405160ff909116815260200160405180910390f35b34156101c157600080fd5b61017b610451565b60405190815260200160405180910390f35b34156101e657600080fd5b6101fd600160a060020a036004351660243561046e565b005b341561020a57600080fd5b61017b600160a060020a03600435166105f8565b60405190815260200160405180910390f35b341561023b57600080fd5b61017b600435602435610794565b60405190815260200160405180910390f35b341561026657600080fd5b6100f06107ae565b60405160208082528190810183818151815260200191508051906020019080838360005b8381101561012d5780820151818401525b602001610114565b50505050905090810190601f16801561015a5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102f157600080fd5b6101fd6107e5565b005b341561030657600080fd5b61017b6108fc565b60405190815260200160405180910390f35b341561032b57600080fd5b6101fd600160a060020a0360043516602435610903565b005b341561034f57600080fd5b6101fd600160a060020a036004351660243561098d565b005b341561037357600080fd5b61037b6109d1565b6040518084600160a060020a0316600160a060020a03168152602001838152602001828152602001935050505060405180910390f35b34156103bc57600080fd5b61037b610a0b565b6040518084600160a060020a0316600160a060020a03168152602001838152602001828152602001935050505060405180910390f35b341561040557600080fd5b6101fd610a42565b005b60408051908101604052600681527f53747265656d0000000000000000000000000000000000000000000000000000602082015281565b60005481565b600081565b600160a060020a0333166000908152600260205260409020545b90565b60006104bc61047c33610b4e565b608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610b90565b156104c357fe5b60006104ce336105f8565b10156104d657fe5b6001600580548060010182816104ec9190610e2c565b916000526020600020906004020160005b60806040519081016040908152600160a060020a03338116835289166020830152810187905242606082015291905081518154600160a060020a031916600160a060020a03919091161781556020820151600182018054600160a060020a031916600160a060020a0392909216919091179055604082015181600201556060820151600391820155600160a060020a03338116600081815260209384526040808220979096039687905591891680835260049093529084902085905593945092917f4baaa557c21346b70bdc9482890b5d7e315f6a2123611e74004857ebecde0686915085905190815260200160405180910390a35b505050565b600080600080600061060986610ba1565b935061064f84608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610b90565b61065a57600061069e565b61069e84608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610be3565b5b92506106aa86610b4e565b91506106f082608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610b90565b6106fb57600061073f565b61073f82608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610be3565b5b600160a060020a038716600090815260026020526040812054919250908401829003121561076a57fe5b600160a060020a038616600090815260026020526040902054830181900394505b50505050919050565b60008183106107a357816107a5565b825b90505b92915050565b60408051908101604052600381527f5354520000000000000000000000000000000000000000000000000000000000602082015281565b60008060006107f333610b4e565b925061083983608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610b90565b151561084157fe5b61088583608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610cdf565b60018501546002860154929450909250600160a060020a039081169133909116907f96c5271ec05cb2683bdc50cf109341f5a4e45b02907df1c23a8855bddbe030a190858560405180848152602001838152602001828152602001935050505060405180910390a36105f333610dad565b5b505050565b6005545b90565b60008111801561091b575080610918336105f8565b10155b151561092357fe5b600160a060020a033381166000818152600260205260408082208054869003905592851680825290839020805485019055917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9084905190815260200160405180910390a35b5050565b60015433600160a060020a039081169116146109a857600080fd5b600160a060020a03821660009081526002602052604081208054830190558054820190555b5050565b6000806000806109e033610b4e565b600181015460028201546003830154600160a060020a03909216965094504203925090505b50909192565b600080600080610a1a33610ba1565b805460028201546003830154600160a060020a03909216965094504203925090505b50909192565b600160a060020a0333811660008181526002602090815260408083208390556003825280832083905560049091528120556001549091161415610b4b5760008054600160a060020a033316825260026020526040822055610aa590600590610e5e565b6005805460018101610ab78382610e2c565b916000526020600020906004020160005b60806040519081016040908152600080835260208301819052908201819052606082015291905081518154600160a060020a031916600160a060020a03919091161781556020820151600182018054600160a060020a031916600160a060020a039290921691909117905560408201518160020155606082015181600301555050505b5b565b600160a060020a038116600090815260036020526040812054600580549091908110610b7657fe5b906000526020600020906004020160005b5090505b919050565b60008160600151151590505b919050565b600160a060020a038116600090815260046020526040812054600580549091908110610b7657fe5b906000526020600020906004020160005b5090505b919050565b6000806000806000610bf486610e15565b9350610c008651610ba1565b9250610c4683608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610b90565b610c51576000610c95565b610c9583608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610be3565b5b9150600260008751600160a060020a03168152602081019190915260400160009081205491508282011215610cc757fe5b610cd384838301610794565b94505b50505050919050565b600080600080600080610cf187610be3565b9350866040015184811515610d0257fe5b049250866040015183029150610d1787610e15565b905081600260008951600160a060020a0316600160a060020a031681526020019081526020016000206000828254039250508190555081600260008960200151600160a060020a031681526020810191909152604001600020805490910190558260608801818151019052504260608801511115610d9157fe5b80821115610d9b57fe5b81828203955095505b50505050915091565b600160a060020a0381166000908152600360205260409020546005805482908110610dd457fe5b906000526020600020906004020160005b508054600160a060020a0319908116825560018201805490911690556000600282018190556003909101555b5050565b60008160400151826060015142030290505b919050565b8154818355818115116105f3576004028160040283600052602060002091820191016105f39190610e83565b5b505050565b5080546000825560040290600052602060002090810190610e7f9190610e83565b5b50565b61046b91905b80821115610ec2578054600160a060020a0319908116825560018201805490911690556000600282018190556003820155600401610e89565b5090565b905600a165627a7a72305820d00c5aac227632ff138d1a4fa835789fdf6e37adb194ce535e3394712417a6790029",
"unlinked_binary": "0x6060604052341561000f57600080fd5b604051602080611176833981016040528080519150505b60018054600160a060020a03191633600160a060020a03169081178255600090815260026020526040812083905582905560058054909181016100698382610103565b916000526020600020906004020160005b60806040519081016040908152600080835260208301819052908201819052606082015291905081518154600160a060020a031916600160a060020a03919091161781556020820151600182018054600160a060020a031916600160a060020a039290921691909117905560408201518160020155606082015181600301555050505b5061017b565b81548183558181151161012f5760040281600402836000526020600020918201910161012f9190610135565b5b505050565b61017891905b80821115610174578054600160a060020a031990811682556001820180549091169055600060028201819055600382015560040161013b565b5090565b90565b610fec8061018a6000396000f300606060405236156100d85763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166306fdde0381146100dd57806318160ddd14610168578063313ce5671461018d5780635cc8ce33146101b657806364a80c0c146101db57806370a08231146101ff5780637ae2b5c71461023057806395d89b411461025b5780639dad9382146102e6578063a1365fda146102fb578063a9059cbb14610320578063e13e2ecf14610344578063e61b959e14610368578063e8f88890146103b1578063f408ebe9146103fa575b600080fd5b34156100e857600080fd5b6100f061040f565b60405160208082528190810183818151815260200191508051906020019080838360005b8381101561012d5780820151818401525b602001610114565b50505050905090810190601f16801561015a5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561017357600080fd5b61017b610446565b60405190815260200160405180910390f35b341561019857600080fd5b6101a061044c565b60405160ff909116815260200160405180910390f35b34156101c157600080fd5b61017b610451565b60405190815260200160405180910390f35b34156101e657600080fd5b6101fd600160a060020a036004351660243561046e565b005b341561020a57600080fd5b61017b600160a060020a03600435166105f8565b60405190815260200160405180910390f35b341561023b57600080fd5b61017b60043560243561081c565b60405190815260200160405180910390f35b341561026657600080fd5b6100f0610836565b60405160208082528190810183818151815260200191508051906020019080838360005b8381101561012d5780820151818401525b602001610114565b50505050905090810190601f16801561015a5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102f157600080fd5b6101fd61086d565b005b341561030657600080fd5b61017b610984565b60405190815260200160405180910390f35b341561032b57600080fd5b6101fd600160a060020a036004351660243561098b565b005b341561034f57600080fd5b6101fd600160a060020a0360043516602435610a15565b005b341561037357600080fd5b61037b610a59565b6040518084600160a060020a0316600160a060020a03168152602001838152602001828152602001935050505060405180910390f35b34156103bc57600080fd5b61037b610a93565b6040518084600160a060020a0316600160a060020a03168152602001838152602001828152602001935050505060405180910390f35b341561040557600080fd5b6101fd610aca565b005b60408051908101604052600681527f53747265656d0000000000000000000000000000000000000000000000000000602082015281565b60005481565b600081565b600160a060020a0333166000908152600260205260409020545b90565b60006104bc61047c33610bd6565b608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610c18565b156104c357fe5b60006104ce336105f8565b10156104d657fe5b6001600580548060010182816104ec9190610f23565b916000526020600020906004020160005b60806040519081016040908152600160a060020a03338116835289166020830152810187905242606082015291905081518154600160a060020a031916600160a060020a03919091161781556020820151600182018054600160a060020a031916600160a060020a0392909216919091179055604082015181600201556060820151600391820155600160a060020a03338116600081815260209384526040808220979096039687905591891680835260049093529084902085905593945092917f4baaa557c21346b70bdc9482890b5d7e315f6a2123611e74004857ebecde0686915085905190815260200160405180910390a35b505050565b600080600080600061060986610c29565b935061064f84608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610c18565b61065a5760006106e2565b6106e284608060405190810160409081528254600160a060020a039081168352600184015416602083015260028301548183015260039092015460608201529086906080905190810160409081528254600160a060020a0390811683526001808501549091166020840152600284015491830191909152600390920154606082015290610c6b565b5b92506106ee86610bd6565b915061073482608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610c18565b61073f5760006107c7565b6107c782608060405190810160409081528254600160a060020a039081168352600184015416602083015260028301548183015260039092015460608201529084906080905190810160409081528254600160a060020a0390811683526001808501549091166020840152600284015491830191909152600390920154606082015290610c6b565b5b600160a060020a03871660009081526002602052604081205491925090840182900312156107f257fe5b600160a060020a038616600090815260026020526040902054830181900394505b50505050919050565b600081831061082b578161082d565b825b90505b92915050565b60408051908101604052600381527f5354520000000000000000000000000000000000000000000000000000000000602082015281565b600080600061087b33610bd6565b92506108c183608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610c18565b15156108c957fe5b61090d83608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610d8c565b60018501546002860154929450909250600160a060020a039081169133909116907f96c5271ec05cb2683bdc50cf109341f5a4e45b02907df1c23a8855bddbe030a190858560405180848152602001838152602001828152602001935050505060405180910390a36105f333610e5d565b5b505050565b6005545b90565b6000811180156109a35750806109a0336105f8565b10155b15156109ab57fe5b600160a060020a033381166000818152600260205260408082208054869003905592851680825290839020805485019055917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9084905190815260200160405180910390a35b5050565b60015433600160a060020a03908116911614610a3057600080fd5b600160a060020a03821660009081526002602052604081208054830190558054820190555b5050565b600080600080610a6833610bd6565b600181015460028201546003830154600160a060020a03909216965094504203925090505b50909192565b600080600080610aa233610c29565b805460028201546003830154600160a060020a03909216965094504203925090505b50909192565b600160a060020a0333811660008181526002602090815260408083208390556003825280832083905560049091528120556001549091161415610bd35760008054600160a060020a033316825260026020526040822055610b2d90600590610f55565b6005805460018101610b3f8382610f23565b916000526020600020906004020160005b60806040519081016040908152600080835260208301819052908201819052606082015291905081518154600160a060020a031916600160a060020a03919091161781556020820151600182018054600160a060020a031916600160a060020a039290921691909117905560408201518160020155606082015181600301555050505b5b565b600160a060020a038116600090815260036020526040812054600580549091908110610bfe57fe5b906000526020600020906004020160005b5090505b919050565b60008160600151151590505b919050565b600160a060020a038116600090815260046020526040812054600580549091908110610bfe57fe5b906000526020600020906004020160005b5090505b919050565b6000806000806000610c7c88610ec5565b9350610c888888610edc565b15610c9557839450610d80565b610c9f8851610c29565b9250610ce583608060405190810160409081528254600160a060020a03908116835260018401541660208301526002830154908201526003909101546060820152610c18565b610cf0576000610d3f565b610d3f83608060405190810160409081528254600160a060020a039081168352600180850154909116602084015260028401549183019190915260039092015460608201529089908901610c6b565b5b9150600260008951600160a060020a03168152602081019190915260400160009081205491508282011215610d7157fe5b610d7d8483830161081c565b94505b5b505050509392505050565b600080600080600080610da187886001610c6b565b9350866040015184811515610db257fe5b049250866040015183029150610dc787610ec5565b905081600260008951600160a060020a0316600160a060020a031681526020019081526020016000206000828254039250508190555081600260008960200151600160a060020a031681526020810191909152604001600020805490910190558260608801818151019052504260608801511115610e4157fe5b80821115610e4b57fe5b81828203955095505b50505050915091565b600160a060020a0381166000908152600360205260409020546005805482908110610e8457fe5b906000526020600020906004020160005b508054600160a060020a0319908116825560018201805490911690556000600282018190556003909101555b5050565b60008160400151826060015142030290505b919050565b60008151600160a060020a03168351600160a060020a031614801561082d57508160200151600160a060020a03168360200151600160a060020a0316145b90505b92915050565b8154818355818115116105f3576004028160040283600052602060002091820191016105f39190610f7a565b5b505050565b5080546000825560040290600052602060002090810190610f769190610f7a565b5b50565b61046b91905b80821115610fb9578054600160a060020a0319908116825560018201805490911690556000600282018190556003820155600401610f80565b5090565b905600a165627a7a72305820c51cb66e2d5a2a7215690fe6faed582be0203adebe118b7319e3572f864587660029",
"networks": {
"1500075197859": {
"events": {
......@@ -982,10 +982,10 @@ const contract = {
}
},
"links": {},
"address": "0xc5dc2ba3aee4b8c2cdc659a27118a5c5317705dd",
"updated_at": 1502119211001
"address": "0xaa6857c10e5d6acc37e2e7b0efc2749dfb213077",
"updated_at": 1502127244263
}
},
"schema_version": "0.0.5",
"updated_at": 1502119211001
"updated_at": 1502127244263
}
\ No newline at end of file
......@@ -213,6 +213,22 @@ function test3() {
streem.openStream(web3.eth.accounts[0], 1, {from: web3.eth.accounts[1], gas: 200000})
}
function test3a() {
console.log('open stream1: speed 1 from addr0 to addr1')
streem.openStream(web3.eth.accounts[1], 1, {from: web3.eth.accounts[0], gas: 200000})
console.log('open stream2: speed 2 from addr1 to addr0')
streem.openStream(web3.eth.accounts[0], 2, {from: web3.eth.accounts[1], gas: 200000})
}
function test3b() {
console.log('open stream1: speed 2 from addr0 to addr1')
streem.openStream(web3.eth.accounts[1], 2, {from: web3.eth.accounts[0], gas: 200000})
console.log('open stream2: speed 1 from addr1 to addr0')
streem.openStream(web3.eth.accounts[0], 1, {from: web3.eth.accounts[1], gas: 200000})
}
function test4() {
console.log('open stream1: speed 1 from addr0 to addr1')
streem.openStream(web3.eth.accounts[1], 1, {from: web3.eth.accounts[0], gas: 200000})
......
......@@ -39,6 +39,8 @@ Size of streams array: <span id="nrstreams">-</span><br>
<button id="test1a">Test1a: close A->B, wait 20, close B->C</button><br>
<button id="test2">Test2: open A->B (1), open B->C (2)</button><br>
<button id="test3">Test3: open A->B (1), open B->A (1)</button><br>
<button id="test3a">Test3a: open A->B (1), open B->A (2)</button><br>
<button id="test3b">Test3b: open A->B (2), open B->A (1)</button><br>
<button id="test4">Test4: open A->B (1), open B->C (1), open C->A (1)</button><br>
<script type="text/javascript" src="lib/web3.js"></script>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment