Code purge, start over with cache and test

This commit is contained in:
lash 2022-03-15 09:13:13 +00:00
parent 786e1a6029
commit b96542715d
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
13 changed files with 95 additions and 296 deletions

0
chaind/eth/__init__.py Normal file
View File

41
chaind/eth/cache.py Normal file
View File

@ -0,0 +1,41 @@
# external imports
from hexathon import strip_0x
from chainqueue.cache import (
CacheTx,
NoopNormalizer,
)
from chainlib.eth.tx import unpack
from chainlib.encode import TxHexNormalizer
class Normalizer(TxHexNormalizer, NoopNormalizer):
def __init__(self):
super(Normalizer, self).__init__()
self.address = self.wallet_address
self.hash = self.tx_hash
#self.value = self.noop
def value(self, v):
hexathon.to_int(v)
eth_normalizer = Normalizer()
class EthCacheTx(CacheTx):
def __init__(self, chain_spec):
super(EthCacheTx, self).__init__(chain_spec)
def deserialize(self, signed_tx):
signed_tx_bytes = bytes.fromhex(strip_0x(signed_tx))
tx = unpack(signed_tx_bytes, self.chain_spec)
self.hash = eth_normalizer.hash(tx['hash'])
self.sender = eth_normalizer.address(tx['from'])
self.recipient = eth_normalizer.address(tx['to'])
self.nonce = eth_normalizer.value(tx['nonce'])
self.value = eth_normalizer.value(tx['value'])

View File

@ -75,7 +75,7 @@ chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
dsn = dsn_from_config(config) dsn = dsn_from_config(config)
logg.debug('dns {}'.format(dsn)) logg.debug('dns {}'.format(dsn))
SQLBackend.setup(dsn, debug=config.true('DATABASE_DEBUG')) SQLBackend.setup(dsn, debug=config.true('DATABASE_DEBUG'))
rpc = EthHTTPConnection(url=config.get('RPC_HTTP_PROVIDER'), chain_spec=chain_spec) rpc = EthHTTPConnection(url=config.get('RPC_PROVIDER'), chain_spec=chain_spec)
def register_filter_tags(filters, session): def register_filter_tags(filters, session):
for f in filters: for f in filters:

View File

@ -1,4 +1,4 @@
chaind<=0.0.3,>=0.0.3a6 #chaind~=0.1.0
hexathon~=0.0.1a8 hexathon~=0.1.5
chainlib-eth<=0.1.0,>=0.0.10a10 chainlib-eth>=0.1.0b3,<=0.1.0
eth-token-index<=0.3.0,>=0.2.4a1 #eth-token-index~=0.2.4

View File

@ -3,7 +3,7 @@
set -e set -e
set -x set -x
set -a set -a
export PYTHONPATH=${PYTHONPATH:.} export PYTHONPATH=${PYTHONPATH:-.}
for f in `ls tests/*.py`; do for f in `ls tests/*.py`; do
python $f python $f
done done

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = chaind-eth name = chaind-eth
version = 0.0.3a5 version = 0.1.0
description = Queue server for ethereum description = Queue server for ethereum
author = Louis Holbrook author = Louis Holbrook
author_email = dev@holbrook.no author_email = dev@holbrook.no
@ -22,20 +22,20 @@ classifiers =
# Topic :: Blockchain :: EVM # Topic :: Blockchain :: EVM
license = GPL3 license = GPL3
licence_files = licence_files =
LICENSE.txt LICENSE
[options] [options]
python_requires = >= 3.6 python_requires = >= 3.6
include_package_data = True include_package_data = True
packages = packages =
chaind_eth chaind.eth
chaind_eth.runnable chaind.eth.runnable
chaind_eth.cli chaind.eth.cli
chainqueue.adapters # chainqueue.adapters
[options.entry_points] [options.entry_points]
console_scripts = console_scripts =
chaind-eth-server = chaind_eth.runnable.server:main chaind-eth-server = chaind_eth.runnable.server:main
chaind-eth-syncer = chaind_eth.runnable.syncer:main # chaind-eth-syncer = chaind_eth.runnable.syncer:main
chaind-eth-send = chaind_eth.runnable.send:main chaind-eth-send = chaind_eth.runnable.send:main
chaind-eth-resend = chaind_eth.runnable.resend:main chaind-eth-resend = chaind_eth.runnable.resend:main

View File

@ -1,51 +0,0 @@
# standard imports
import os
import unittest
# external imports
from chainsyncer.unittest.db import ChainSyncerDb
from chainqueue.unittest.db import ChainQueueDb
from chainlib.eth.unittest.ethtester import EthTesterCase
from chainqueue.adapters.eth import EthAdapter
from chainqueue.unittest.db import (
db_config,
dsn_from_config,
)
from chainqueue.sql.backend import SQLBackend
from chainlib.eth.address import to_checksum_address
from hexathon import add_0x
# local imports
from chaind_eth.chain import EthChainInterface
class TestBase(EthTesterCase):
def setUp(self):
super(TestBase, self).setUp()
self.db_chainsyncer = ChainSyncerDb(debug=bool(os.environ.get('DATABASE_DEBUG')))
self.session_chainsyncer = self.db_chainsyncer.bind_session()
self.db_chainqueue = ChainQueueDb(debug=bool(os.environ.get('DATABASE_DEBUG')))
self.session_chainqueue = self.db_chainqueue.bind_session()
self.interface = EthChainInterface()
def tearDown(self):
self.session_chainsyncer.commit()
self.db_chainsyncer.release_session(self.session_chainsyncer)
self.session_chainqueue.commit()
self.db_chainqueue.release_session(self.session_chainqueue)
super(TestBase, self).tearDown()
class TestSQLBase(TestBase):
example_tx_hex = 'f8650d8405f5e10082520894ee38d3a40e177608d41978778206831f60dd0fa88204008077a040adee2ad0a0e566bced4b76a8899549e86719eb8866b87674b6fdc88479c201a030b3ca061bb330f4d78bc9cb8144c8e570339496f56b7809387de2ffeaa585d5'
example_tx = bytes.fromhex(example_tx_hex)
example_tx_sender = add_0x(to_checksum_address('eb3907ecad74a0013c259d5874ae7f22dcbcc95c'))
dsn = dsn_from_config(db_config)
def setUp(self):
super(TestSQLBase, self).setUp()
self.backend = SQLBackend(self.dsn, debug=bool(os.environ.get('DATABASE_DEBUG')))
self.adapter = EthAdapter(self.backend)

View File

@ -1,26 +0,0 @@
# stanndard imports
import logging
import unittest
# external imports
from chainlib.chain import ChainSpec
# test imports
from tests.chaind_eth_base import TestSQLBase
logging.basicConfig(level=logging.DEBUG)
class TestAdapter(TestSQLBase):
def test_eth_adapter_translate(self):
self.adapter.translate(self.example_tx, self.chain_spec)
# succesful decode means translate is working, no further checks needed
def test_eth_adapter_add(self):
self.adapter.add(self.example_tx_hex, self.chain_spec, session=self.session_chainqueue)
if __name__ == '__main__':
unittest.main()

View File

@ -1,38 +0,0 @@
# standard imports
import unittest
# external imports
from chainlib.eth.gas import (
RPCGasOracle,
Gas,
)
from chainlib.eth.nonce import (
RPCNonceOracle,
)
from chainlib.eth.tx import (
TxFormat,
unpack,
)
from hexathon import (
strip_0x,
)
# test imports
from tests.chaind_eth_base import TestBase
class TestChain(TestBase):
def test_chain_interface(self):
gas_oracle = RPCGasOracle(conn=self.rpc)
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
c = Gas(self.chain_spec, signer=self.signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
(tx_hash, tx_raw_rlp_signed) = c.create(self.accounts[0], self.accounts[1], 1024, tx_format=TxFormat.RLP_SIGNED)
tx_raw_rlp_signed_bytes = bytes.fromhex(strip_0x(tx_raw_rlp_signed))
tx_src = unpack(tx_raw_rlp_signed_bytes, self.chain_spec)
tx_src = self.interface.src_normalize(tx_src)
assert tx_src['gas_price'] == tx_src['gasPrice']
if __name__ == '__main__':
unittest.main()

View File

@ -1,52 +0,0 @@
# stanndard imports
import logging
import unittest
# external imports
from hexathon import strip_0x
from chainlib.eth.tx import (
unpack,
TxFormat,
)
from chainqueue.sql.query import get_tx
from chainqueue.enum import StatusBits
from chainlib.eth.gas import (
RPCGasOracle,
Gas,
)
from chainlib.eth.nonce import (
RPCNonceOracle,
)
# local imports
from chaind_eth.dispatch import Dispatcher
# test imports
from tests.chaind_eth_base import TestSQLBase
logging.basicConfig(level=logging.DEBUG)
class TestDispatcher(TestSQLBase):
def test_dispatch_process(self):
gas_oracle = RPCGasOracle(conn=self.rpc)
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
c = Gas(self.chain_spec, signer=self.signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
(tx_hash, tx_raw_rlp_signed) = c.create(self.accounts[0], self.accounts[1], 1024, tx_format=TxFormat.RLP_SIGNED)
tx_raw_rlp_signed_bytes = bytes.fromhex(strip_0x(tx_raw_rlp_signed))
dispatcher = Dispatcher(self.chain_spec, self.adapter, 1)
self.adapter.add(tx_raw_rlp_signed, self.chain_spec, session=self.session_chainqueue)
#self.assertEqual(dispatcher.get_count(self.example_tx_sender, self.session_chainqueue), 1)
self.assertEqual(dispatcher.get_count(self.accounts[0], self.session_chainqueue), 1)
dispatcher.process(self.rpc, self.session_chainqueue)
tx_obj = unpack(tx_raw_rlp_signed_bytes, self.chain_spec)
o = get_tx(self.chain_spec, tx_obj['hash'], session=self.session_chainqueue)
self.assertGreater(o['status'] & StatusBits.IN_NETWORK, 0)
self.assertEqual(dispatcher.get_count(self.accounts[0], self.session_chainqueue), 0)
if __name__ == '__main__':
unittest.main()

View File

@ -1,101 +0,0 @@
# standard imports
import unittest
import logging
import os
# external imports
from potaahto.symbols import snake_and_camel
from hexathon import (
strip_0x,
)
from chainqueue.adapters.eth import EthAdapter
from chainqueue.unittest.db import (
db_config,
dsn_from_config,
)
from chainqueue.sql.backend import SQLBackend
from chainqueue.enum import is_alive
from chainqueue.sql.query import get_tx
from chainlib.eth.gas import (
RPCGasOracle,
Gas,
)
from chainlib.eth.nonce import (
RPCNonceOracle,
)
from chainlib.eth.tx import (
TxFormat,
raw,
unpack,
receipt,
Tx,
)
from chainlib.eth.block import (
block_by_hash,
Block,
)
from chainqueue.sql.state import (
set_sent,
set_reserved,
set_ready,
)
# local imports
from chaind_eth.filter import StateFilter
from chaind_eth.chain import EthChainInterface
# test imports
from tests.chaind_eth_base import TestBase
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestFilter(TestBase):
def test_filter(self):
gas_oracle = RPCGasOracle(conn=self.rpc)
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
c = Gas(self.chain_spec, signer=self.signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
(tx_hash, tx_raw_rlp_signed) = c.create(self.accounts[0], self.accounts[1], 1024, tx_format=TxFormat.RLP_SIGNED)
o = raw(tx_raw_rlp_signed)
self.rpc.do(o)
#o = receipt(tx_hash)
o = self.interface.tx_receipt(tx_hash)
rcpt = self.rpc.do(o)
#o = block_by_hash(rcpt['block_hash'])
o = self.interface.block_by_number(rcpt['block_number'])
block_src = self.rpc.do(o)
#block = Block(block_src)
block = self.interface.block_from_src(block_src)
dsn = dsn_from_config(db_config)
backend = SQLBackend(dsn, debug=bool(os.environ.get('DATABASE_DEBUG')))
adapter = EthAdapter(backend)
tx_raw_rlp_signed_bytes = bytes.fromhex(strip_0x(tx_raw_rlp_signed))
#adapter.add(tx_raw_rlp_signed_bytes, self.chain_spec, session=self.session_chainqueue)
adapter.add(tx_raw_rlp_signed, self.chain_spec, session=self.session_chainqueue)
set_ready(self.chain_spec, tx_hash, session=self.session_chainqueue)
set_reserved(self.chain_spec, tx_hash, session=self.session_chainqueue)
set_sent(self.chain_spec, tx_hash, session=self.session_chainqueue)
tx_src = unpack(tx_raw_rlp_signed_bytes, self.chain_spec)
tx_src = self.interface.src_normalize(tx_src)
tx = Tx(tx_src, block=block, rcpt=rcpt)
tx_repr = get_tx(self.chain_spec, tx_hash, session=self.session_chainqueue)
assert is_alive(tx_repr['status'])
fltr = StateFilter(self.chain_spec)
fltr.filter(self.rpc, block, tx, session=self.session_chainqueue)
tx_repr = get_tx(self.chain_spec, tx_hash, session=self.session_chainqueue)
assert not is_alive(tx_repr['status'])
if __name__ == '__main__':
unittest.main()

View File

@ -1,15 +0,0 @@
# standard imports
import unittest
# test imports
from tests.chaind_eth_base import TestBase
class TestHelo(TestBase):
def test_helo(self):
pass
if __name__ == '__main__':
unittest.main()

41
tests/test_tx.py Normal file
View File

@ -0,0 +1,41 @@
# standard imports
import os
import tempfile
import unittest
import shutil
import logging
import hashlib
# external imports
from chainlib.chain import ChainSpec
from chainqueue.cache import CacheTokenTx
from chainlib.error import RPCException
from chainlib.status import Status as TxStatus
from chaind.unittest.common import TestChaindFsBase
from chaind.driver import QueueDriver
from chaind.filter import StateFilter
from chainlib.eth.gas import Gas
# local imports
from chaind.eth.cache import EthCacheTx
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestEthChaindFs(TestChaindFsBase):
def setUp(self):
self.cache_adapter = EthCacheTx
super(TestEthChaindFs, self).setUp()
def test_deserialize(self):
data = "f8610d2a82520894eb3907ecad74a0013c259d5874ae7f22dcbcc95c8204008078a0ddbebd76701f6531e5ea42599f890268716e2bb38e3e125874f47595c2338049a00f5648d17b20efac8cb7ff275a510ebef6815e1599e29067821372b83eb1d28c"
hsh = self.adapter.put(data)
v = self.adapter.get(hsh)
self.assertEqual(data, v)
if __name__ == '__main__':
unittest.main()