From a54cc86dba09916de259a585607be9b7943bee53 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Fri, 12 Apr 2024 18:57:00 -0500 Subject: [PATCH] fixup power redistribution mechanism --- ethereum/contracts/core/Forum.sol | 22 +++++++++------------- ethereum/test/Forum.js | 8 ++++++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/ethereum/contracts/core/Forum.sol b/ethereum/contracts/core/Forum.sol index cd8589d..8d2edd7 100644 --- a/ethereum/contracts/core/Forum.sol +++ b/ethereum/contracts/core/Forum.sol @@ -118,44 +118,40 @@ contract Forum is Reputation { return amount; } Post storage post = posts[postIndex]; - int totalOutboundAmount; // Propagate negative citations first for (uint i = 0; i < post.citations.length; i++) { if (post.citations[i].weightPercent < 0) { - int outboundAmount = _handleCitation( + amount -= _handleCitation( postIndex, post.citations[i], amount, initialNegative, depth ); - totalOutboundAmount += outboundAmount; } } // Now propagate positive citations for (uint i = 0; i < post.citations.length; i++) { if (post.citations[i].weightPercent > 0) { - int outboundAmount = _handleCitation( + amount -= _handleCitation( postIndex, post.citations[i], amount, initialNegative, depth ); - totalOutboundAmount += outboundAmount; } } - int remaining = amount - totalOutboundAmount; - if (remaining > 0) { - _update(address(this), post.author, uint(remaining)); - post.reputation += uint(remaining); + if (amount > 0) { + _update(address(this), post.author, uint(amount)); + post.reputation += uint(amount); } else { // Prevent reputation from being reduced below zero - if (int(post.reputation) + remaining >= 0) { - _update(post.author, address(this), uint(-remaining)); - post.reputation -= uint(-remaining); + if (int(post.reputation) + amount >= 0) { + _update(post.author, address(this), uint(-amount)); + post.reputation -= uint(-amount); } else { - refundToInbound = int(post.reputation) + remaining; + refundToInbound = int(post.reputation) + amount; _update(post.author, address(this), post.reputation); post.reputation = 0; } diff --git a/ethereum/test/Forum.js b/ethereum/test/Forum.js index 9aabe07..1afcf50 100644 --- a/ethereum/test/Forum.js +++ b/ethereum/test/Forum.js @@ -96,15 +96,18 @@ describe('Forum', () => { }); it('should be able to redistribute power via citations', async () => { + console.log('First post'); await dao.addPost(account1, 'content-id', []); await initiateValidationPool({ postIndex: 0 }); await dao.evaluateOutcome(0); expect(await dao.balanceOf(account1)).to.equal(100); + console.log('Second post'); await dao.addPost(account2, 'second-content-id', []); expect(await dao.balanceOf(account2)).to.equal(0); + console.log('Third post'); await dao.addPost(account3, 'third-content-id', [ - { weightPercent: -100, targetPostIndex: 0 }, { weightPercent: 100, targetPostIndex: 1 }, + { weightPercent: -100, targetPostIndex: 0 }, ]); await initiateValidationPool({ postIndex: 2 }); const pool = await dao.validationPools(1); @@ -112,7 +115,8 @@ describe('Forum', () => { await time.increase(POOL_DURATION + 1); await dao.evaluateOutcome(1); expect(await dao.balanceOf(account1)).to.equal(0); - expect(await dao.balanceOf(account2)).to.equal(100); + expect(await dao.balanceOf(account2)).to.equal(200); + expect(await dao.balanceOf(account3)).to.equal(0); }); it('should be able to reverse a negative citation with a negative citation', async () => {