funga/test/test_helper.py

94 lines
2.6 KiB
Python
Raw Normal View History

2021-01-09 22:05:24 +01:00
# standard imports
import unittest
import logging
import os
2021-01-10 18:27:44 +01:00
# third-party imports
import web3
2021-01-09 22:05:24 +01:00
# local imports
from crypto_dev_signer.keystore import DictKeystore
from crypto_dev_signer.eth.signer import ReferenceSigner
from crypto_dev_signer.helper import TxExecutor
2021-01-10 18:27:44 +01:00
from crypto_dev_signer.eth.helper import EthTxExecutor
2021-01-09 22:05:24 +01:00
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
script_dir = os.path.realpath(os.path.dirname(__file__))
class MockEthTxBackend:
def dispatcher(self, tx):
logg.debug('sender {}'.format(tx))
return os.urandom(32)
def reporter(self, tx):
logg.debug('reporter {}'.format(tx))
2021-01-09 22:59:27 +01:00
def verifier(self, rcpt):
logg.debug('reporter {}'.format(rcpt))
2021-01-09 22:05:24 +01:00
def fee_price_helper(self):
return 21
2021-01-10 18:27:44 +01:00
def fee_helper(self, tx):
logg.debug('fee helper tx {}'.format(tx))
2021-01-09 22:05:24 +01:00
return 2
2021-01-09 22:59:27 +01:00
def builder(self, tx):
2021-01-10 18:27:44 +01:00
return tx
2021-01-09 22:05:24 +01:00
2021-01-09 22:59:27 +01:00
def builder_two(self, tx):
2021-01-10 18:27:44 +01:00
tx['value'] = 10243
tx['to'] = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
tx['data'] = ''
if tx.get('feePrice') != None:
tx['gasPrice'] = tx['feePrice']
del tx['feePrice']
if tx.get('feeUnits') != None:
tx['gas'] = tx['feeUnits']
del tx['feeUnits']
2021-01-09 22:59:27 +01:00
return tx
2021-01-09 22:05:24 +01:00
class TestHelper(unittest.TestCase):
def setUp(self):
logg.debug('setup')
self.db = DictKeystore()
2021-01-09 22:59:27 +01:00
keystore_filename = 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72'
keystore_filepath = os.path.join(script_dir, 'testdata', keystore_filename)
2021-01-09 22:05:24 +01:00
self.address_hex = self.db.import_keystore_file(keystore_filepath, '')
self.signer = ReferenceSigner(self.db)
def tearDown(self):
pass
def test_helper(self):
backend = MockEthTxBackend()
2021-01-10 18:27:44 +01:00
executor = TxExecutor(self.address_hex, self.signer, backend.builder, backend.dispatcher, backend.reporter, 666, 13, backend.fee_helper, backend.fee_price_helper, backend.verifier)
tx_ish = {'from': self.address_hex}
executor.sign_and_send([backend.builder_two])
def test_eth_helper(self):
backend = MockEthTxBackend()
w3 = web3.Web3(web3.Web3.HTTPProvider('http://localhost:8545'))
executor = EthTxExecutor(w3, self.address_hex, self.signer, 1337)
2021-01-09 22:05:24 +01:00
tx_ish = {'from': self.address_hex}
2021-01-10 18:27:44 +01:00
#executor.sign_and_send([backend.builder, backend.builder_two])
with self.assertRaises(ValueError):
executor.sign_and_send([backend.builder_two])
2021-01-09 22:05:24 +01:00
if __name__ == '__main__':
unittest.main()