Add simple demurrage check, asserts for longer period checks

This commit is contained in:
lash 2022-12-15 07:25:04 +00:00
parent 3b87961d13
commit 97415dbed2
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 81 additions and 9 deletions

View File

@ -133,6 +133,13 @@ class TestDemurrage(EthTesterCase):
logg.debug('asserted within lower {} <= {} <= {}'.format(lower_target, v, target))
def assert_within_greater(self, v, target, tolerance_ppm):
higher_target = target + (target * (tolerance_ppm / 1000000))
self.assertLessEqual(v, higher_target)
self.assertGreaterEqual(v, target)
logg.debug('asserted within lower {} <= {} <= {}'.format(target, v, higher_target))
def tearDown(self):
pass

View File

@ -25,12 +25,16 @@ logg = logging.getLogger()
testdir = os.path.dirname(__file__)
TAX_LEVEL = 2
class TestBurn(TestDemurrage):
def setUp(self):
super(TestBurn, self).setUp()
# tax_level = ppm
def deploy(self, tax_level=None):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -39,14 +43,17 @@ class TestBurn(TestDemurrage):
self.mode = 'MultiNocap'
logg.debug('executing test setup default mode {}'.format(self.mode))
if tax_level != None:
self.deployer.settings.demurrage_level = tax_level * (10 ** 32)
self.deployer.settings.sink_address = self.accounts[9]
self.deployer.sink_address = self.accounts[9]
self.deploy(c, self.mode)
super(TestBurn, self).deploy(c, self.mode)
logg.info('deployed with mode {}'.format(self.mode))
def test_burn_basic(self):
self.deploy()
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -86,6 +93,7 @@ class TestBurn(TestDemurrage):
def test_burned_redistribution(self):
self.deploy()
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -143,6 +151,7 @@ class TestBurn(TestDemurrage):
def test_burned_other_redistribution(self):
self.deploy()
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -210,6 +219,7 @@ class TestBurn(TestDemurrage):
def test_burn_accumulate(self):
self.deploy(tax_level=2/1000)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -222,9 +232,10 @@ class TestBurn(TestDemurrage):
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.sink_address, self.default_supply)
r = self.rpc.do(o)
balance_share = int(self.default_supply / 2)
nonce_oracle = RPCNonceOracle(self.sink_address, self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[1], int(self.default_supply / 2))
(tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[1], balance_share)
r = self.rpc.do(o)
new_supply = None
@ -238,7 +249,9 @@ class TestBurn(TestDemurrage):
bob_bal = c.parse_balance(r)
prev_bob_bal = bob_bal
for i in range(1, 101):
iterations = 100
for i in range(1, iterations + 1):
nonce_oracle = RPCNonceOracle(self.sink_address, self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -269,12 +282,13 @@ class TestBurn(TestDemurrage):
o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
bob_bal = c.parse_balance(r)
bob_refund = prev_bob_bal - bob_bal
o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0])
r = self.rpc.do(o)
burner_bal = c.parse_balance(r)
sum_supply = bob_bal + burner_bal
o = c.total_burned(self.address, sender_address=self.accounts[0])
r = self.rpc.do(o)
@ -285,12 +299,20 @@ class TestBurn(TestDemurrage):
total_burned_base = c.parse_balance(r)
expected_supply = self.default_supply - (burn_rate * i)
logg.info('checking burn round {} balance burner {} bob {} supply {} expected {} burned {} base {}'.format(i, burner_bal, bob_bal, new_supply, expected_supply, total_burned, total_burned_base))
logg.info('checking burn round {} balance burner {} bob {} supply {} expected {} summed {} burned {} base {}'.format(i, burner_bal, bob_bal, new_supply, expected_supply, sum_supply, total_burned, total_burned_base))
self.assertEqual(new_supply, expected_supply)
sum_supply = burner_bal + bob_bal
logg.debug('balances sink {} bob {} total {} supply real {} original {}'.format(sink_bal, bob_bal, sum_supply, new_supply, self.default_supply))
self.assert_within_lower(sum_supply, new_supply, 0.00001)
self.assert_within_greater(burner_bal, balance_share - total_burned, 0.1)
bob_delta = self.default_supply * ((2 / 1000000) / 1000)
self.assert_within_lower(bob_bal, balance_share - bob_delta, 0.1)
self.assertEqual(total_burned, iterations * burn_rate)
if __name__ == '__main__':
unittest.main()

View File

@ -32,6 +32,7 @@ class TestDemurragePeriods(TestDemurrage):
self.mode = 'MultiNocap'
logg.debug('executing test setup default mode {}'.format(self.mode))
self.deployer.settings.demurrage_level = (2 / 1000) * (10 ** 32)
self.deployer.settings.sink_address = self.accounts[9]
self.deployer.sink_address = self.accounts[9]
self.deploy(c, self.mode)
@ -39,7 +40,39 @@ class TestDemurragePeriods(TestDemurrage):
logg.info('deployed with mode {}'.format(self.mode))
def test_overtime(self):
def test_ppm(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply)
r = self.rpc.do(o)
self.backend.time_travel(self.start_time + 60)
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
bob_bal = c.parse_balance(r)
o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0])
r = self.rpc.do(o)
sink_bal = c.parse_balance(r)
o = c.total_supply(self.address, sender_address=self.accounts[0])
r = self.rpc.do(o)
new_supply = c.parse_total_supply(r)
balance_delta = self.default_supply * ((2 / 1000000) / 1000)
self.assertEqual(bob_bal, self.default_supply - balance_delta)
def test_over_time(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -54,7 +87,9 @@ class TestDemurragePeriods(TestDemurrage):
nonce_oracle = RPCNonceOracle(self.sink_address, self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
for i in range(1, 1001):
iterations = 100
for i in range(1, iterations + 1):
self.backend.time_travel(self.start_time + (self.period_seconds * i))
(tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[1], prev_bob_bal - bob_bal)
@ -63,7 +98,7 @@ class TestDemurragePeriods(TestDemurrage):
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
(tx_hash, o) = c.change_period(self.address, self.sink_address)
(tx_hash, o) = c.apply_demurrage(self.address, self.sink_address)
self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
@ -83,6 +118,14 @@ class TestDemurragePeriods(TestDemurrage):
logg.info('round {} supply {} balance sink {} bob {}'.format(i, new_supply, sink_bal, bob_bal))
sum_supply = sink_bal + bob_bal
bob_delta = self.default_supply * ((2 / 1000000) / 100)
self.assert_within_lower(sum_supply, new_supply, 0.00001)
self.assert_within_greater(bob_bal, self.default_supply - bob_delta, 0.001)
self.assert_within_lower(sink_bal, bob_delta, 1000)
if __name__ == '__main__':
unittest.main()