Rehabilitate last test

This commit is contained in:
nolash 2021-06-04 15:08:03 +02:00
parent 4b780c81ad
commit f299e25dcc
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 165 additions and 69 deletions

View File

@ -167,6 +167,21 @@ class DemurrageToken(ERC20):
return o return o
def account_period(self, contract_address, address, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('accountPeriod')
enc.typ(ABIContractType.ADDRESS)
enc.address(address)
data = add_0x(enc.get())
tx = self.template(sender_address, contract_address)
tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx))
o['params'].append('latest')
return o
def to_redistribution_period(self, contract_address, redistribution, sender_address=ZERO_ADDRESS): def to_redistribution_period(self, contract_address, redistribution, sender_address=ZERO_ADDRESS):
o = jsonrpc_template() o = jsonrpc_template()
o['method'] = 'eth_call' o['method'] = 'eth_call'
@ -190,6 +205,18 @@ class DemurrageToken(ERC20):
return self.transact_noarg('changePeriod', contract_address, sender_address) return self.transact_noarg('changePeriod', contract_address, sender_address)
def apply_redistribution_on_account(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
enc = ABIContractEncoder()
enc.method('applyRedistributionOnAccount')
enc.typ(ABIContractType.ADDRESS)
enc.address(address)
data = enc.get()
tx = self.template(sender_address, contract_address, use_nonce=True)
tx = self.set_code(tx, data)
tx = self.finalize(tx, tx_format)
return tx
def actual_period(self, contract_address, sender_address=ZERO_ADDRESS): def actual_period(self, contract_address, sender_address=ZERO_ADDRESS):
return self.call_noarg('actualPeriod', contract_address, sender_address=sender_address) return self.call_noarg('actualPeriod', contract_address, sender_address=sender_address)
@ -206,35 +233,46 @@ class DemurrageToken(ERC20):
return self.call_noarg('demurrageAmount', contract_address, sender_address=sender_address) return self.call_noarg('demurrageAmount', contract_address, sender_address=sender_address)
@classmethod
def parse_actual_period(self, v): def parse_actual_period(self, v):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_period_start(self, v): def parse_period_start(self, v):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_period_duration(self, v): def parse_period_duration(self, v):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_demurrage_amount(self, v): def parse_demurrage_amount(self, v):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_remainder(self, v): def parse_remainder(self, v):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_to_base_amount(self, v): def parse_to_base_amount(self, v):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_redistributions(self, v): def parse_redistributions(self, v):
return abi_decode_single(ABIContractType.BYTES32, v) return abi_decode_single(ABIContractType.BYTES32, v)
@classmethod
def parse_account_period(self, v):
return abi_decode_single(ABIContractType.ADDRESS, v)
@classmethod
def parse_to_redistribution_period(self, v): def parse_to_redistribution_period(self, v):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)

View File

@ -34,6 +34,7 @@ PERIOD = 1
class TestRedistribution(TestDemurrageDefault): class TestRedistribution(TestDemurrageDefault):
@unittest.skip('foo')
def test_debug_periods(self): def test_debug_periods(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -57,6 +58,7 @@ class TestRedistribution(TestDemurrageDefault):
# TODO: check receipt log outputs # TODO: check receipt log outputs
@unittest.skip('foo')
def test_redistribution_storage(self): def test_redistribution_storage(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -103,6 +105,7 @@ class TestRedistribution(TestDemurrageDefault):
self.assertEqual(strip_0x(r), '000000000000000000000000ef4200000000000000000000002dc6c000000002') self.assertEqual(strip_0x(r), '000000000000000000000000ef4200000000000000000000002dc6c000000002')
@unittest.skip('foo')
def test_redistribution_balance_on_zero_participants(self): def test_redistribution_balance_on_zero_participants(self):
supply = 1000000000000 supply = 1000000000000
@ -118,14 +121,15 @@ class TestRedistribution(TestDemurrageDefault):
(tx_hash, o) = c.change_period(self.address, self.accounts[0]) (tx_hash, o) = c.change_period(self.address, self.accounts[0])
self.rpc.do(o) self.rpc.do(o)
# tx_hash = self.contract.functions.changePeriod().transact() o = c.redistributions(self.address, 0, sender_address=self.accounts[0])
# rr = self.w3.eth.getTransactionReceipt(tx_hash) r = self.rpc.do(o)
# self.assertEqual(rr.status, 1) redistributions = c.parse_redistributions(r)
#
# redistribution = self.contract.functions.redistributions(0).call(); o = c.total_supply(self.address, sender_address=self.accounts[0])
# supply = self.contract.functions.totalSupply().call() r = self.rpc.do(o)
# supply = c.parse_total_supply(r)
# sink_increment = int(supply * (TAX_LEVEL / 1000000))
sink_increment = int(supply * (TAX_LEVEL / 1000000))
# for l in r['logs']: # for l in r['logs']:
# if l.topics[0].hex() == '0xa0717e54e02bd9829db5e6e998aec0ae9de796b8d150a3cc46a92ab869697755': # event Decayed(uint256,uint256,uint256,uint256) # if l.topics[0].hex() == '0xa0717e54e02bd9829db5e6e998aec0ae9de796b8d150a3cc46a92ab869697755': # event Decayed(uint256,uint256,uint256,uint256)
# period = int.from_bytes(l.topics[1], 'big') # period = int.from_bytes(l.topics[1], 'big')
@ -143,66 +147,120 @@ class TestRedistribution(TestDemurrageDefault):
# balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call() # balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
# self.assertEqual(balance, supply - sink_increment) # self.assertEqual(balance, supply - sink_increment)
# #
#
# def test_redistribution_two_of_ten(self): def test_redistribution_two_of_ten(self):
# mint_amount = 100000000 mint_amount = 100000000
# z = 0 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
# for i in range(10): c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
# self.contract.functions.mintTo(self.w3.eth.accounts[i], mint_amount).transact() z = 0
# z += mint_amount for i in range(10):
# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i], mint_amount)
# initial_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call() self.rpc.do(o)
# z += mint_amount
# spend_amount = 1000000
# external_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex()) o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
# self.contract.functions.transfer(external_address, spend_amount).transact({'from': self.w3.eth.accounts[1]}) r = self.rpc.do(o)
# tx_hash = self.contract.functions.transfer(external_address, spend_amount).transact({'from': self.w3.eth.accounts[2]}) initial_balance = c.parse_balance_of(r)
# r = self.w3.eth.getTransactionReceipt(tx_hash)
# # No cheating! spend_amount = 1000000
# self.contract.functions.transfer(self.w3.eth.accounts[3], spend_amount).transact({'from': self.w3.eth.accounts[3]}) external_address = to_checksum_address('0x' + os.urandom(20).hex())
# # No cheapskating!
# self.contract.functions.transfer(external_address, spend_amount-1).transact({'from': self.w3.eth.accounts[4]}) nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
# self.assertEqual(r.status, 1) (tx_hash, o) = c.transfer(self.address, self.accounts[1], external_address, spend_amount)
# self.rpc.do(o)
# self.eth_tester.time_travel(self.start_time + 61) o = receipt(tx_hash)
# r = self.rpc.do(o)
# self.contract.functions.applyDemurrage().transact() self.assertEqual(r['status'], 1)
# self.contract.functions.changePeriod().transact()
# nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
# bummer_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[3]).call() c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
# self.assertEqual(bummer_balance, mint_amount - (mint_amount * (TAX_LEVEL / 1000000))) (tx_hash, o) = c.transfer(self.address, self.accounts[2], external_address, spend_amount)
# logg.debug('bal {} '.format(bummer_balance)) self.rpc.do(o)
# o = receipt(tx_hash)
# bummer_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call() r = self.rpc.do(o)
# spender_balance = mint_amount - spend_amount self.assertEqual(r['status'], 1)
# spender_decayed_balance = int(spender_balance - (spender_balance * (TAX_LEVEL / 1000000)))
# self.assertEqual(bummer_balance, spender_decayed_balance) # No cheating!
# logg.debug('bal {} '.format(bummer_balance)) nonce_oracle = RPCNonceOracle(self.accounts[3], self.rpc)
# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
# tx_hash = self.contract.functions.applyRedistributionOnAccount(self.w3.eth.accounts[1]).transact() (tx_hash, o) = c.transfer(self.address, self.accounts[3], self.accounts[3], spend_amount)
# r = self.w3.eth.getTransactionReceipt(tx_hash) self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
# No cheapskating!
nonce_oracle = RPCNonceOracle(self.accounts[4], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.transfer(self.address, self.accounts[4], external_address, spend_amount-1)
self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
self.backend.time_travel(self.start_time + 61)
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[4])
self.rpc.do(o)
(tx_hash, o) = c.change_period(self.address, self.accounts[4])
self.rpc.do(o)
o = c.balance_of(self.address, self.accounts[3], sender_address=self.accounts[0])
r = self.rpc.do(o)
bummer_balance = c.parse_balance_of(r)
self.assertEqual(bummer_balance, mint_amount - (mint_amount * (TAX_LEVEL / 1000000)))
logg.debug('bal {} '.format(bummer_balance))
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
bummer_balance = c.parse_balance_of(r)
spender_balance = mint_amount - spend_amount
spender_decayed_balance = int(spender_balance - (spender_balance * (TAX_LEVEL / 1000000)))
self.assertEqual(bummer_balance, spender_decayed_balance)
logg.debug('bal {} '.format(bummer_balance))
(tx_hash, o) = c.apply_redistribution_on_account(self.address, self.accounts[4], self.accounts[1])
self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
# logg.debug('log {}'.format(r.logs)) # logg.debug('log {}'.format(r.logs))
# (tx_hash, o) = c.apply_redistribution_on_account(self.address, self.accounts[4], self.accounts[2])
# self.contract.functions.applyRedistributionOnAccount(self.w3.eth.accounts[2]).transact() self.rpc.do(o)
# o = receipt(tx_hash)
# redistribution_data = self.contract.functions.redistributions(0).call() r = self.rpc.do(o)
# logg.debug('redist data {}'.format(redistribution_data.hex())) self.assertEqual(r['status'], 1)
#
# account_period_data = self.contract.functions.accountPeriod(self.w3.eth.accounts[1]).call() o = c.redistributions(self.address, 0, sender_address=self.accounts[0])
# logg.debug('account period {}'.format(account_period_data)) r = self.rpc.do(o)
# redistribution_data = c.parse_redistributions(r)
# actual_period = self.contract.functions.actualPeriod().call() logg.debug('redist data {}'.format(redistribution_data))
# logg.debug('period {}'.format(actual_period))
# o = c.account_period(self.address, self.accounts[1], sender_address=self.accounts[0])
# redistribution = int((z / 2) * (TAX_LEVEL / 1000000)) r = self.rpc.do(o)
# spender_new_base_balance = ((mint_amount - spend_amount) + redistribution) account_period_data = c.parse_account_period(r)
# spender_new_decayed_balance = int(spender_new_base_balance - (spender_new_base_balance * (TAX_LEVEL / 1000000))) logg.debug('account period {}'.format(account_period_data))
#
# spender_actual_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call() o = c.actual_period(self.address, sender_address=self.accounts[0])
# logg.debug('rrr {} {}'.format(redistribution, spender_new_decayed_balance)) r = self.rpc.do(o)
# actual_period = c.parse_actual_period(r)
# self.assertEqual(spender_actual_balance, spender_new_decayed_balance) logg.debug('period {}'.format(actual_period))
redistribution = int((z / 2) * (TAX_LEVEL / 1000000))
spender_new_base_balance = ((mint_amount - spend_amount) + redistribution)
spender_new_decayed_balance = int(spender_new_base_balance - (spender_new_base_balance * (TAX_LEVEL / 1000000)))
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
spender_actual_balance = c.parse_balance_of(r)
logg.debug('rrr {} {}'.format(redistribution, spender_new_decayed_balance))
self.assertEqual(spender_actual_balance, spender_new_decayed_balance)
if __name__ == '__main__': if __name__ == '__main__':