Add dispatch, test

This commit is contained in:
lash 2022-03-15 09:32:20 +00:00
parent b96542715d
commit 441cb00404
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 47 additions and 1 deletions

View File

@ -39,3 +39,4 @@ class EthCacheTx(CacheTx):
self.recipient = eth_normalizer.address(tx['to']) self.recipient = eth_normalizer.address(tx['to'])
self.nonce = eth_normalizer.value(tx['nonce']) self.nonce = eth_normalizer.value(tx['nonce'])
self.value = eth_normalizer.value(tx['value']) self.value = eth_normalizer.value(tx['value'])
self.src = signed_tx

13
chaind/eth/dispatch.py Normal file
View File

@ -0,0 +1,13 @@
# external imports
from chainlib.eth.tx import raw
class EthDispatcher:
def __init__(self, conn):
self.conn = conn
def send(self, payload):
o = raw(payload)
self.conn.do(o)

View File

@ -15,27 +15,59 @@ from chaind.unittest.common import TestChaindFsBase
from chaind.driver import QueueDriver from chaind.driver import QueueDriver
from chaind.filter import StateFilter from chaind.filter import StateFilter
from chainlib.eth.gas import Gas from chainlib.eth.gas import Gas
from jsonrpc_std.parse import jsonrpc_validate_dict
from hexathon import strip_0x
# local imports # local imports
from chaind.eth.cache import EthCacheTx from chaind.eth.cache import EthCacheTx
from chaind.eth.dispatch import EthDispatcher
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
class MockConn:
def __init__(self):
self.fails = []
self.last = None
def add_fail(self, v):
self.fails.append(v)
def do(self, v):
if v in self.fails:
raise RuntimeError(v)
v = jsonrpc_validate_dict(v)
if v['method'] != 'eth_sendRawTransaction':
raise ValueError('unsupported method {}'.format(v['method']))
self.last = v['params'][0]
class TestEthChaindFs(TestChaindFsBase): class TestEthChaindFs(TestChaindFsBase):
def setUp(self): def setUp(self):
self.cache_adapter = EthCacheTx self.cache_adapter = EthCacheTx
self.conn = MockConn()
self.dispatcher = EthDispatcher(self.conn)
super(TestEthChaindFs, self).setUp() super(TestEthChaindFs, self).setUp()
def test_deserialize(self): def test_deserialize(self):
data = "f8610d2a82520894eb3907ecad74a0013c259d5874ae7f22dcbcc95c8204008078a0ddbebd76701f6531e5ea42599f890268716e2bb38e3e125874f47595c2338049a00f5648d17b20efac8cb7ff275a510ebef6815e1599e29067821372b83eb1d28c" data = "f8610d2a82520894eb3907ecad74a0013c259d5874ae7f22dcbcc95c8204008078a0ddbebd76701f6531e5ea42599f890268716e2bb38e3e125874f47595c2338049a00f5648d17b20efac8cb7ff275a510ebef6815e1599e29067821372b83eb1d28c" # valid RLP example data
hsh = self.adapter.put(data) hsh = self.adapter.put(data)
v = self.adapter.get(hsh) v = self.adapter.get(hsh)
self.assertEqual(data, v) self.assertEqual(data, v)
def test_dispatch(self):
data = "f8610d2a82520894eb3907ecad74a0013c259d5874ae7f22dcbcc95c8204008078a0ddbebd76701f6531e5ea42599f890268716e2bb38e3e125874f47595c2338049a00f5648d17b20efac8cb7ff275a510ebef6815e1599e29067821372b83eb1d28c" # valid RLP example data
hsh = self.adapter.put(data)
self.adapter.dispatch(hsh)
self.assertEqual(strip_0x(self.conn.last), strip_0x(data))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()