Introduce sim example
This commit is contained in:
@@ -97,6 +97,7 @@ class DemurrageTokenSimulation:
|
||||
self.period = 1
|
||||
self.period_txs = []
|
||||
self.period_tx_limit = self.period_seconds - 1
|
||||
self.sink_address = settings.sink_address
|
||||
|
||||
logg.info('intialized at block {} timestamp {} period {} demurrage level {} sink address {} (first address in keystore)'.format(
|
||||
self.last_block,
|
||||
@@ -126,6 +127,32 @@ class DemurrageTokenSimulation:
|
||||
logg.debug('tx {} block {} index {} verified'.format(tx_hash, self.last_block, rcpt['transaction_index']))
|
||||
|
||||
|
||||
def get_now(self):
|
||||
o = block_latest()
|
||||
r = self.rpc.do(o)
|
||||
o = block_by_number(r, include_tx=False)
|
||||
r = self.rpc.do(o)
|
||||
return r['timestamp']
|
||||
|
||||
|
||||
def get_minutes(self):
|
||||
t = self.get_now()
|
||||
return int((t - self.start_timestamp) / 60)
|
||||
|
||||
|
||||
def get_start(self):
|
||||
return self.start_timestamp
|
||||
|
||||
|
||||
def get_period(self):
|
||||
return self.period
|
||||
|
||||
def get_demurrage_modifier(self):
|
||||
o = self.caller_contract.demurrage_amount(self.address, sender_address=self.caller_address)
|
||||
r = self.rpc.do(o)
|
||||
return float(self.caller_contract.parse_demurrage_amount(r) / (10 ** 38))
|
||||
|
||||
|
||||
def from_units(self, v):
|
||||
return v * (10 ** self.decimals)
|
||||
|
||||
@@ -136,9 +163,10 @@ class DemurrageTokenSimulation:
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
|
||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], recipient, value)
|
||||
self.rpc.do(o)
|
||||
self.next_block()
|
||||
self.__next_block()
|
||||
self.__check_tx(tx_hash)
|
||||
self.period_txs.append(tx_hash)
|
||||
logg.info('mint {} tokens to {} - {}'.format(value, recipient, tx_hash))
|
||||
return tx_hash
|
||||
|
||||
|
||||
@@ -147,9 +175,10 @@ class DemurrageTokenSimulation:
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
|
||||
(tx_hash, o) = c.transfer(self.address, sender, recipient, value)
|
||||
self.rpc.do(o)
|
||||
self.next_block()
|
||||
self.__next_block()
|
||||
self.__check_tx(tx_hash)
|
||||
self.period_txs.append(tx_hash)
|
||||
logg.info('transfer {} tokens from {} to {} - {}'.format(value, sender, recipient, tx_hash))
|
||||
return tx_hash
|
||||
|
||||
|
||||
@@ -163,42 +192,51 @@ class DemurrageTokenSimulation:
|
||||
return self.caller_contract.parse_balance_of(r)
|
||||
|
||||
|
||||
def next_block(self):
|
||||
def __next_block(self):
|
||||
hsh = self.eth_helper.mine_block()
|
||||
o = block_by_hash(hsh)
|
||||
r = self.rpc.do(o)
|
||||
logg.info('now at block {} timestamp {}'.format(r['number'], r['timestamp']))
|
||||
|
||||
for tx_hash in r['transactions']:
|
||||
o = receipt(tx_hash)
|
||||
rcpt = self.rpc.do(o)
|
||||
if rcpt['status'] == 0:
|
||||
raise RuntimeError('tx {} (block {} index {}) failed'.format(tx_hash, self.last_block, rcpt['transaction_index']))
|
||||
logg.debug('tx {} (block {} index {}) verified'.format(tx_hash, self.last_block, rcpt['transaction_index']))
|
||||
|
||||
logg.debug('now at block {} timestamp {}'.format(r['number'], r['timestamp']))
|
||||
|
||||
|
||||
def next(self):
|
||||
target_timestamp = self.start_timestamp + (self.period * self.period_seconds) - 1
|
||||
target_timestamp = self.start_timestamp + (self.period * self.period_seconds)
|
||||
logg.debug('warping to {}, {} from start'.format(target_timestamp, target_timestamp - self.start_timestamp))
|
||||
self.last_timestamp = target_timestamp
|
||||
|
||||
self.eth_helper.time_travel(self.last_timestamp)
|
||||
self.next_block()
|
||||
self.__next_block()
|
||||
|
||||
o = block_by_number(self.last_block)
|
||||
r = self.rpc.do(o)
|
||||
self.last_block = r['number']
|
||||
block_base = self.last_block
|
||||
|
||||
|
||||
# for tx_hash in r['transactions']:
|
||||
# o = receipt(tx_hash)
|
||||
# rcpt = self.rpc.do(o)
|
||||
# if rcpt['status'] == 0:
|
||||
# raise RuntimeError('tx {} (block {} index {}) failed'.format(tx_hash, self.last_block, rcpt['transaction_index']))
|
||||
# logg.info('tx {} (block {} index {}) verified'.format(tx_hash, self.last_block, rcpt['transaction_index']))
|
||||
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[2], conn=self.rpc)
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
|
||||
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
|
||||
(tx_hash, o) = c.change_period(self.address, self.accounts[2])
|
||||
self.rpc.do(o)
|
||||
self.next_block()
|
||||
|
||||
(tx_hash, o) = c.change_period(self.address, self.accounts[0])
|
||||
for actor in self.actors:
|
||||
nonce_oracle = RPCNonceOracle(actor, conn=self.rpc)
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
|
||||
(tx_hash, o) = c.apply_redistribution_on_account(self.address, actor, actor)
|
||||
self.rpc.do(o)
|
||||
|
||||
nonce_oracle = RPCNonceOracle(self.sink_address, conn=self.rpc)
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=self.gas_oracle)
|
||||
(tx_hash, o) = c.apply_redistribution_on_account(self.address, self.sink_address, self.sink_address)
|
||||
self.rpc.do(o)
|
||||
self.next_block()
|
||||
self.__next_block()
|
||||
|
||||
o = block_latest()
|
||||
self.last_block = self.rpc.do(o)
|
||||
@@ -212,7 +250,7 @@ class DemurrageTokenSimulation:
|
||||
raise RuntimeError('demurrage step failed on block {}'.format(self.last_block))
|
||||
|
||||
self.last_timestamp = r['timestamp']
|
||||
logg.info('next concludes at block {} timestamp {}, {} after start'.format(self.last_block, self.last_timestamp, self.last_timestamp - self.start_timestamp))
|
||||
logg.debug('next concludes at block {} timestamp {}, {} after start'.format(self.last_block, self.last_timestamp, self.last_timestamp - self.start_timestamp))
|
||||
self.period += 1
|
||||
self.period_txs = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user