Rewrite to use timestamp instead of blocks

This commit is contained in:
nolash
2021-02-06 15:17:37 +01:00
parent 50c9cb61b5
commit 2ea28aacd1
5 changed files with 210 additions and 148 deletions

View File

@@ -3,6 +3,7 @@ import os
import unittest
import json
import logging
import datetime
# third-party imports
import web3
@@ -18,9 +19,11 @@ logging.getLogger('eth.vm').setLevel(logging.WARNING)
testdir = os.path.dirname(__file__)
#BLOCKTIME = 5 # seconds
TAX_LEVEL = 10000 * 2 # 2%
TAX_LEVEL = int(10000 * 2) # 2%
# calc "1-(0.98)^(1/518400)" <- 518400 = 30 days of blocks
# 0.00000003897127107225
#PERIOD = int(60/BLOCKTIME) * 60 * 24 * 30 # month
PERIOD = 10
PERIOD = 1
class Test(unittest.TestCase):
@@ -48,12 +51,15 @@ class Test(unittest.TestCase):
self.sink_address = self.w3.eth.accounts[9]
c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL, PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
self.start_block = self.w3.eth.blockNumber
b = self.w3.eth.getBlock(self.start_block)
self.start_time = b['timestamp']
def tearDown(self):
pass
@@ -61,10 +67,31 @@ class Test(unittest.TestCase):
def test_hello(self):
self.assertEqual(self.contract.functions.actualPeriod().call(), 1)
self.eth_tester.mine_blocks(PERIOD)
self.eth_tester.time_travel(self.start_time + 61)
self.assertEqual(self.contract.functions.actualPeriod().call(), 2)
def test_apply_demurrage(self):
modifier = 10 * (10 ** 37)
demurrage_modifier = self.contract.functions.demurrageModifier().call()
demurrage_modifier &= (1 << 128) - 1
self.assertEqual(modifier, demurrage_modifier)
self.eth_tester.time_travel(self.start_time + 59)
demurrage_modifier = self.contract.functions.demurrageModifier().call()
demurrage_modifier &= (1 << 128) - 1
self.assertEqual(modifier, demurrage_modifier)
self.eth_tester.time_travel(self.start_time + 61)
tx_hash = self.contract.functions.applyDemurrage().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
demurrage_modifier = self.contract.functions.demurrageModifier().call()
demurrage_modifier &= (1 << 128) - 1
self.assertEqual(int(98 * (10 ** 36)), demurrage_modifier)
def test_mint(self):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
@@ -80,6 +107,26 @@ class Test(unittest.TestCase):
balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance, 2000)
self.eth_tester.time_travel(self.start_time + 61)
balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance, int(2000 * 0.98))
def test_base_amount(self):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1000).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
self.eth_tester.time_travel(self.start_time + 61)
self.contract.functions.applyDemurrage().transact()
demurrage_modifier = self.contract.functions.demurrageModifier().call()
demurrage_amount = self.contract.functions.toDemurrageAmount(demurrage_modifier).call()
logg.debug('d {} {}'.format(demurrage_modifier.to_bytes(32, 'big').hex(), demurrage_amount))
a = self.contract.functions.toBaseAmount(1000).call();
self.assertEqual(a, 1020)
def test_transfer(self):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact()
@@ -128,62 +175,5 @@ class Test(unittest.TestCase):
self.assertEqual(balance_alice, 500)
def test_apply_tax(self):
self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact()
self.eth_tester.mine_blocks(PERIOD)
tx_hash = self.contract.functions.applyTax().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(self.contract.functions.redistributionCount().call(), 2)
self.assertEqual(self.contract.functions.demurrageModifier().call(), 980000)
self.eth_tester.mine_blocks(PERIOD)
tx_hash = self.contract.functions.applyTax().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(self.contract.functions.redistributionCount().call(), 3)
self.assertEqual(self.contract.functions.demurrageModifier().call(), 960400)
def test_tax_balance(self):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1000).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
self.eth_tester.mine_blocks(PERIOD)
tx_hash = self.contract.functions.applyTax().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance, 980)
def test_taxed_transfer(self):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1000000).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
self.eth_tester.mine_blocks(PERIOD)
tx_hash = self.contract.functions.applyTax().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
balance_alice = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance_alice, 980000)
tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500000).transact({'from': self.w3.eth.accounts[1]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
logg.debug('r {}'.format(r))
self.assertEqual(r.status, 1)
balance_alice = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
balance_alice_trunc = int(balance_alice/1000)*1000
self.assertEqual(balance_alice_trunc, 480000)
balance_bob = self.contract.functions.balanceOf(self.w3.eth.accounts[2]).call()
balance_bob_trunc = int(balance_bob/1000)*1000
self.assertEqual(balance_bob_trunc, 500000)
if __name__ == '__main__':
unittest.main()

View File

@@ -20,7 +20,7 @@ testdir = os.path.dirname(__file__)
#BLOCKTIME = 5 # seconds
TAX_LEVEL = 10000 * 2 # 2%
#PERIOD = int(60/BLOCKTIME) * 60 * 24 * 30 # month
PERIOD = 2
PERIOD = 1
class Test(unittest.TestCase):
@@ -47,12 +47,15 @@ class Test(unittest.TestCase):
self.sink_address = self.w3.eth.accounts[9]
c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL, PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
self.start_block = self.w3.eth.blockNumber
b = self.w3.eth.getBlock(self.start_block)
self.start_time = b['timestamp']
def tearDown(self):
pass
@@ -63,15 +66,14 @@ class Test(unittest.TestCase):
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
self.eth_tester.mine_blocks(PERIOD * 10)
tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500).transact({'from': self.w3.eth.accounts[1]})
self.eth_tester.time_travel(self.start_time + 61)
tx_hash = self.contract.functions.changePeriod().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
logg.debug('r {}'.format(r));
self.assertEqual(r.status, 1)
period = self.contract.functions.accountPeriod(self.w3.eth.accounts[1]).call()
self.assertEqual(period, 12)
redistribution = self.contract.functions.redistributions(1).call()
self.assertEqual(2, self.contract.functions.toRedistributionPeriod(redistribution).call())
self.assertEqual(2, self.contract.functions.actualPeriod().call())
if __name__ == '__main__':

View File

@@ -19,8 +19,7 @@ logging.getLogger('eth.vm').setLevel(logging.WARNING)
testdir = os.path.dirname(__file__)
#BLOCKTIME = 5 # seconds
TAX_LEVEL = 10000 * 2 # 2%
#PERIOD = int(60/BLOCKTIME) * 60 * 24 * 30 # month
TAX_LEVEL = int((10000 * 2) * (10 ** 32)) # 2%
PERIOD = 10
@@ -62,14 +61,20 @@ class Test(unittest.TestCase):
def test_tax_period(self):
t = self.contract.functions.taxLevel().call()
logg.debug('taxlevel {}'.format(t))
a = self.contract.functions.toTaxPeriodAmount(1000000, 0).call()
self.assertEqual(1000000, a)
self.assertEqual(a, 1000000)
a = self.contract.functions.toTaxPeriodAmount(1000000, 1).call()
self.assertEqual(980000, a)
self.assertEqual(a, 980000)
a = self.contract.functions.toTaxPeriodAmount(1000000, 2).call()
self.assertEqual(960400, a)
self.assertEqual(a, 960400)
a = self.contract.functions.toTaxPeriodAmount(980000, 1).call()
self.assertEqual(a, 960400)
def test_fractional_state(self):

View File

@@ -20,7 +20,7 @@ testdir = os.path.dirname(__file__)
#BLOCKTIME = 5 # seconds
TAX_LEVEL = 10000 * 2 # 2%
#PERIOD = int(60/BLOCKTIME) * 60 * 24 * 30 # month
PERIOD = 20
PERIOD = 1
class Test(unittest.TestCase):
@@ -47,13 +47,15 @@ class Test(unittest.TestCase):
self.sink_address = self.w3.eth.accounts[9]
c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL, PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
self.start_block = self.w3.eth.blockNumber
logg.debug('starting at block number {}'.format(self.start_block))
b = self.w3.eth.getBlock(self.start_block)
self.start_time = b['timestamp']
def tearDown(self):
pass
@@ -68,26 +70,28 @@ class Test(unittest.TestCase):
# TODO: check receipt log outputs
@unittest.skip('foo')
def test_redistribution_storage(self):
self.contract.functions.mintTo(self.w3.eth.accounts[1], 2000).transact()
self.contract.functions.mintTo(self.w3.eth.accounts[1], 1000000).transact()
self.contract.functions.mintTo(self.w3.eth.accounts[2], 1000000).transact()
tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500).transact({'from': self.w3.eth.accounts[1]})
external_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
tx_hash = self.contract.functions.transfer(external_address, 1000000).transact({'from': self.w3.eth.accounts[2]})
tx_hash = self.contract.functions.transfer(external_address, 999999).transact({'from': self.w3.eth.accounts[1]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
logg.debug('tx before {}'.format(r))
self.assertEqual(r.status, 1)
self.eth_tester.mine_blocks(PERIOD)
self.eth_tester.time_travel(self.start_time + 61)
redistribution = self.contract.functions.redistributions(0).call();
self.assertEqual(redistribution.hex(), '000000000100000000000000000000000000000000000007d000000000000001')
self.assertEqual(redistribution.hex(), '000000000100000000000000000000000000000000001e848000000000000001')
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[0], 1000000).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
redistribution = self.contract.functions.redistributions(1).call()
self.assertEqual(redistribution.hex(), '000000000000000000000000000000000000000000000f4a1000000000000002')
self.assertEqual(redistribution.hex(), '000000000000000000000000000000000000000000002dc6c000000000000002')
def test_redistribution_balance_on_zero_participants(self):
@@ -95,24 +99,27 @@ class Test(unittest.TestCase):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], supply).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.eth_tester.mine_blocks(PERIOD)
self.eth_tester.time_travel(self.start_time + 61)
tx_hash = self.contract.functions.applyTax().transact()
tx_hash = self.contract.functions.applyDemurrage().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
logg.debug('r {}'.format(r))
self.assertEqual(r.status, 1)
tx_hash = self.contract.functions.changePeriod().transact()
rr = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(rr.status, 1)
redistribution = self.contract.functions.redistributions(0).call();
supply = self.contract.functions.totalSupply().call()
sink_increment = int(supply * (TAX_LEVEL / 1000000))
for l in r['logs']:
if l.topics[0].hex() == '0x337db9c77a0769d770641c73e3282be23b15e2bddd830c219461dec832313389': # event Taxed(uint256,uint256)
if l.topics[0].hex() == '0xa0717e54e02bd9829db5e6e998aec0ae9de796b8d150a3cc46a92ab869697755': # event Decayed(uint256,uint256,uint256,uint256)
period = int.from_bytes(l.topics[1], 'big')
self.assertEqual(period, 1)
self.assertEqual(period, 2)
b = bytes.fromhex(l.data[2:])
remainder = int.from_bytes(b, 'big')
self.assertEqual(remainder, sink_increment)
self.assertEqual(remainder, int((1000000 - TAX_LEVEL) * (10 ** 32)))
logg.debug('period {} remainder {}'.format(period, remainder))
sink_balance = self.contract.functions.balanceOf(self.sink_address).call()
@@ -140,14 +147,15 @@ class Test(unittest.TestCase):
r = self.w3.eth.getTransactionReceipt(tx_hash)
# No cheating!
self.contract.functions.transfer(self.w3.eth.accounts[3], spend_amount).transact({'from': self.w3.eth.accounts[3]})
# Too low
# Cheapskate!
self.contract.functions.transfer(external_address, spend_amount-1).transact({'from': self.w3.eth.accounts[4]})
self.assertEqual(r.status, 1)
self.eth_tester.mine_blocks(PERIOD)
self.eth_tester.time_travel(self.start_time + 61)
self.contract.functions.applyTax().transact()
self.contract.functions.applyDemurrage().transact()
self.contract.functions.changePeriod().transact()
bummer_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[3]).call()
self.assertEqual(bummer_balance, mint_amount - (mint_amount * (TAX_LEVEL / 1000000)))