diff --git a/contracts/communication/MessageTribute.sol b/contracts/communication/MessageTribute.sol index ea12141..ecb0844 100644 --- a/contracts/communication/MessageTribute.sol +++ b/contracts/communication/MessageTribute.sol @@ -18,12 +18,12 @@ contract MessageTribute is Controlled { struct Fee { uint256 amount; - bool tribute; bool permanent; } mapping(address => mapping(address => Fee)) public feeCatalog; mapping(address => uint256) public balances; + mapping(address => mapping(address => uint)) lastAudienceDeniedTimestamp; mapping(bytes32 => uint256) private friendIndex; address[] private friends; @@ -68,17 +68,16 @@ contract MessageTribute is Controlled { return friendIndex[keccak256(accountToCheck, sourceAccount)] > 0; } - function setRequiredTribute(address _to, uint _amount, bool _isTribute, bool _isPermanent) public { + function setRequiredTribute(address _to, uint _amount, bool _isPermanent) public { require(friendIndex[keccak256(msg.sender, _to)] == 0); - feeCatalog[msg.sender][_to] = Fee(_amount, _isTribute, _isPermanent); + feeCatalog[msg.sender][_to] = Fee(_amount, _isPermanent); } function getRequiredFee(address _from) public view - returns (uint256 fee, bool tribute) + returns (uint256 fee) { Fee memory f = getFee(_from); fee = f.amount; - tribute = f.tribute; } function deposit(uint256 _value) public { @@ -100,6 +99,7 @@ contract MessageTribute is Controlled { event AudienceRequested(address indexed from, address indexed to); event AudienceCancelled(address indexed from, address indexed to); + event AudienceTimeOut(address indexed from, address indexed to); event AudienceGranted(address indexed from, address indexed to, bool approve); function requestAudience(address _from, bytes32 hashedSecret) @@ -108,7 +108,8 @@ contract MessageTribute is Controlled { Fee memory f = getFee(_from); require(f.amount <= balances[msg.sender]); require(audienceRequested[_from][msg.sender].blockNum == 0); - + require(lastAudienceDeniedTimestamp[_from][msg.sender] + 3 days <= now); + AudienceRequested(_from, msg.sender); audienceRequested[_from][msg.sender] = Audience(block.number, now, f, hashedSecret); @@ -119,6 +120,14 @@ contract MessageTribute is Controlled { return audienceRequested[_from][_to].blockNum > 0; } + function timeOut(address _from, address _to) public { + require(audienceRequested[_from][_to].blockNum > 0); + require(audienceRequested[_from][_to].timestamp + 3 days <= now); + AudienceTimeOut(_from, _to); + balances[_to] += audienceRequested[_from][_to].fee.amount; + delete audienceRequested[_from][_to]; + } + function cancelAudienceRequest(address _from) public { require(audienceRequested[_from][msg.sender].blockNum > 0); require(audienceRequested[_from][msg.sender].timestamp + 2 hours <= now); @@ -127,24 +136,24 @@ contract MessageTribute is Controlled { delete audienceRequested[_from][msg.sender]; } - function grantAudience(address _to, bool _approve, bytes32 secret) public { - + function grantAudience(address _to, bool _approve, bool _waive, bytes32 secret) public { Audience storage aud = audienceRequested[msg.sender][_to]; require(aud.blockNum > 0); - require(aud.hashedSecret == keccak256(msg.sender, _to, secret)); AudienceGranted(msg.sender, _to, _approve); - bool isTribute = aud.fee.tribute; + if(!_approve) + lastAudienceDeniedTimestamp[msg.sender][_to] = block.timestamp; + uint256 amount = aud.fee.amount; delete audienceRequested[msg.sender][_to]; clearFee(msg.sender, _to); - if (isTribute) { + if (!_waive) { if (_approve) { require(SNT.transfer(msg.sender, amount)); } else { @@ -170,7 +179,7 @@ contract MessageTribute is Controlled { Fee memory specificFee = feeCatalog[_from][msg.sender]; if (friendIndex[keccak256(msg.sender, _from)] > 0) - return Fee(0, false, false); + return Fee(0, false); return specificFee.amount > 0 ? specificFee : generalFee; } @@ -178,8 +187,7 @@ contract MessageTribute is Controlled { function clearFee(address _from, address _to) private { if (!feeCatalog[_from][_to].permanent) { feeCatalog[_from][_to].amount = 0; - feeCatalog[_from][_to].tribute = false; feeCatalog[_from][_to].permanent = false; } } -} +} \ No newline at end of file diff --git a/test/messageTribute.js b/test/messageTribute.js index 66df0ce..2bd5fb5 100644 --- a/test/messageTribute.js +++ b/test/messageTribute.js @@ -168,7 +168,7 @@ describe('MessageTribute', function() { }); it("Requesting audience requiring deposit and no funds deposited", async () => { - await MessageTribute.methods.setRequiredTribute(accounts[9], 100, false, true).send({from: accounts[0]}); + await MessageTribute.methods.setRequiredTribute(accounts[9], 100, true).send({from: accounts[0]}); assert.equal( await MessageTribute.methods.balance().call({from: accounts[9]}), @@ -215,7 +215,7 @@ describe('MessageTribute', function() { it("Request audience requiring deposit, not having funds at the beginning, deposit funds and request audience", async () => { - await MessageTribute.methods.setRequiredTribute(accounts[8], 200, false, true).send({from: accounts[0]}); + await MessageTribute.methods.setRequiredTribute(accounts[8], 200, true).send({from: accounts[0]}); await SNT.methods.approve(MessageTribute.address, 100).send({from: accounts[8]}); await MessageTribute.methods.deposit(100).send({from: accounts[8]}); @@ -259,7 +259,7 @@ describe('MessageTribute', function() { it("Requesting tribute from specific account", async() => { - await MessageTribute.methods.setRequiredTribute(accounts[7], 100, true, true).send({from: accounts[0]}); + await MessageTribute.methods.setRequiredTribute(accounts[7], 100, true).send({from: accounts[0]}); await SNT.methods.approve(MessageTribute.address, 200).send({from: accounts[7]}); await MessageTribute.methods.deposit(200).send({from: accounts[7]}); @@ -323,7 +323,7 @@ describe('MessageTribute', function() { let initial0balance = (await SNT.methods.balanceOf(accounts[0]).call()).toString(); let initialCBalance = (await SNT.methods.balanceOf(MessageTribute.address).call()).toString(); let initial7DepBalance = (await MessageTribute.methods.balance().call({from: accounts[7]})).toString(); - let amount = (await MessageTribute.methods.getRequiredFee(accounts[0]).call({from: accounts[7]})).fee.toString(); + let amount = (await MessageTribute.methods.getRequiredFee(accounts[0]).call({from: accounts[7]})).toString(); assert.equal( await MessageTribute.methods.hasPendingAudience(accounts[0], accounts[7]).call(), @@ -332,7 +332,7 @@ describe('MessageTribute', function() { let hashedSecret = web3.utils.soliditySha3(accounts[0], accounts[7], secret); - let tx = await MessageTribute.methods.grantAudience(accounts[7], true, secret).send({from: accounts[0]}); + let tx = await MessageTribute.methods.grantAudience(accounts[7], true, false, secret).send({from: accounts[0]}); assert.notEqual(tx.events.AudienceGranted, undefined, "AudienceGranted wasn't triggered"); @@ -364,16 +364,16 @@ describe('MessageTribute', function() { it("Denying an audience", async() => { - let amount = (await MessageTribute.methods.getRequiredFee(accounts[0]).call({from: accounts[7]})).fee.toString(); + let amount = (await MessageTribute.methods.getRequiredFee(accounts[0]).call({from: accounts[7]})).toString(); let initial7DepBalance = (await MessageTribute.methods.balance().call({from: accounts[7]})).toString(); let hashedSecret = web3.utils.soliditySha3(accounts[0], accounts[7], secret); + console.log( await MessageTribute.methods.hasPendingAudience(accounts[0], accounts[7]).call()); let tx = await MessageTribute.methods.requestAudience(accounts[0], hashedSecret).send({from: accounts[7]}); let afterReq7DepBalance = (await MessageTribute.methods.balance().call({from: accounts[7]})).toString(); - assert.equal( afterReq7DepBalance, web3.utils.toBN(initial7DepBalance).sub(web3.utils.toBN(amount)).toString(), @@ -381,7 +381,7 @@ describe('MessageTribute', function() { assert.notEqual(tx.events.AudienceRequested, undefined, "AudienceRequested wasn't triggered"); - let tx2 = await MessageTribute.methods.grantAudience(accounts[7], false, secret).send({from: accounts[0]}); + let tx2 = await MessageTribute.methods.grantAudience(accounts[7], false, false, secret).send({from: accounts[0]}); assert.notEqual(tx2.events.AudienceGranted, undefined, "AudienceGranted wasn't triggered"); @@ -399,8 +399,8 @@ describe('MessageTribute', function() { }); it("Requesting a non permanent tribute from specific account", async() => { - await MessageTribute.methods.setRequiredTribute(accounts[6], 100, true, false).send({from: accounts[0]}); - let amount1 = (await MessageTribute.methods.getRequiredFee(accounts[0]).call({from: accounts[6]})).fee.toString(); + await MessageTribute.methods.setRequiredTribute(accounts[6], 100, false).send({from: accounts[0]}); + let amount1 = (await MessageTribute.methods.getRequiredFee(accounts[0]).call({from: accounts[6]})).toString(); await SNT.methods.approve(MessageTribute.address, 200).send({from: accounts[6]}); await MessageTribute.methods.deposit(200).send({from: accounts[6]}); @@ -416,10 +416,10 @@ describe('MessageTribute', function() { 100, "Deposited balance must be 100"); - let tx2 = await MessageTribute.methods.grantAudience(accounts[6], false, secret).send({from: accounts[0]}); + let tx2 = await MessageTribute.methods.grantAudience(accounts[6], true, false, secret).send({from: accounts[0]}); assert.notEqual(tx2.events.AudienceGranted, undefined, "AudienceGranted wasn't triggered"); - let amount = (await MessageTribute.methods.getRequiredFee(accounts[0]).call({from: accounts[6]})).fee.toString(); + let amount = (await MessageTribute.methods.getRequiredFee(accounts[0]).call({from: accounts[6]})).toString(); assert.equal( amount,