Add docstrings
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
from chainlib.eth.contract import (
|
||||
ABIContractEncoder,
|
||||
ABIContractType,
|
||||
)
|
||||
|
||||
|
||||
def test_abi_param():
|
||||
|
||||
e = ABIContractEncoder()
|
||||
e.uint256(42)
|
||||
e.bytes32('0x666f6f')
|
||||
e.address('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
|
||||
e.method('foo')
|
||||
e.typ(ABIContractType.UINT256)
|
||||
e.typ(ABIContractType.BYTES32)
|
||||
e.typ(ABIContractType.ADDRESS)
|
||||
|
||||
assert e.types[0] == ABIContractType.UINT256
|
||||
assert e.types[1] == ABIContractType.BYTES32
|
||||
assert e.types[2] == ABIContractType.ADDRESS
|
||||
assert e.contents[0] == '000000000000000000000000000000000000000000000000000000000000002a'
|
||||
assert e.contents[1] == '0000000000000000000000000000000000000000000000000000000000666f6f'
|
||||
assert e.contents[2] == '000000000000000000000000deadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
|
||||
|
||||
assert e.get() == 'a08f54bb000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000666f6f000000000000000000000000deadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_abi_param()
|
||||
@@ -1,35 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from chainlib.eth.address import (
|
||||
is_address,
|
||||
is_checksum_address,
|
||||
to_checksum,
|
||||
)
|
||||
|
||||
from tests.base import TestBase
|
||||
|
||||
|
||||
class TestChain(TestBase):
|
||||
|
||||
def test_chain_spec(self):
|
||||
checksum_address = '0xEb3907eCad74a0013c259D5874AE7f22DcBcC95C'
|
||||
plain_address = checksum_address.lower()
|
||||
|
||||
self.assertEqual(checksum_address, to_checksum(checksum_address))
|
||||
|
||||
self.assertTrue(is_address(plain_address))
|
||||
self.assertFalse(is_checksum_address(plain_address))
|
||||
self.assertTrue(is_checksum_address(checksum_address))
|
||||
|
||||
self.assertFalse(is_address(plain_address + "00"))
|
||||
self.assertFalse(is_address(plain_address[:len(plain_address)-2]))
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
to_checksum(plain_address + "00")
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
to_checksum(plain_address[:len(plain_address)-2])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
28
tests/test_interface.py
Normal file
28
tests/test_interface.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
from unittest.mock import Mock
|
||||
import logging
|
||||
|
||||
# local imports
|
||||
from chainlib.interface import ChainInterface
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
# replace with mocker
|
||||
def block_from_src(src):
|
||||
logg.debug('from src called with ' + src)
|
||||
|
||||
|
||||
class TestInterface(unittest.TestCase):
|
||||
|
||||
def test_interface_set(self):
|
||||
ifc = ChainInterface()
|
||||
block_from_src = Mock()
|
||||
ifc.set('block_from_src', block_from_src)
|
||||
ifc.block_from_src('foo')
|
||||
block_from_src.assert_called()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,26 +0,0 @@
|
||||
# standard imports
|
||||
import os
|
||||
import unittest
|
||||
|
||||
# local imports
|
||||
from chainlib.eth.address import to_checksum_address
|
||||
from chainlib.eth.nonce import OverrideNonceOracle
|
||||
from hexathon import add_0x
|
||||
|
||||
# test imports
|
||||
from tests.base import TestBase
|
||||
|
||||
|
||||
class TestNonce(TestBase):
|
||||
|
||||
def test_nonce(self):
|
||||
addr_bytes = os.urandom(20)
|
||||
addr = add_0x(to_checksum_address(addr_bytes.hex()))
|
||||
n = OverrideNonceOracle(addr, 42)
|
||||
self.assertEqual(n.get_nonce(), 42)
|
||||
self.assertEqual(n.next_nonce(), 42)
|
||||
self.assertEqual(n.next_nonce(), 43)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,119 +0,0 @@
|
||||
# standard imports
|
||||
import os
|
||||
import socket
|
||||
import unittest
|
||||
import unittest.mock
|
||||
import logging
|
||||
import json
|
||||
|
||||
# external imports
|
||||
from crypto_dev_signer.eth.transaction import EIP155Transaction
|
||||
from crypto_dev_signer.eth.signer.defaultsigner import ReferenceSigner
|
||||
from crypto_dev_signer.keystore.dict import DictKeystore
|
||||
|
||||
# local imports
|
||||
import chainlib
|
||||
from chainlib.eth.connection import EthUnixSignerConnection
|
||||
from chainlib.eth.sign import sign_transaction
|
||||
from chainlib.eth.tx import TxFactory
|
||||
from chainlib.eth.address import to_checksum_address
|
||||
from chainlib.jsonrpc import (
|
||||
jsonrpc_response,
|
||||
jsonrpc_error,
|
||||
)
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
)
|
||||
from chainlib.chain import ChainSpec
|
||||
|
||||
from tests.base import TestBase
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
keystore = DictKeystore()
|
||||
alice = keystore.new()
|
||||
bob = keystore.new()
|
||||
|
||||
|
||||
class Mocket(socket.socket):
|
||||
|
||||
req_id = None
|
||||
error = False
|
||||
tx = None
|
||||
signer = None
|
||||
|
||||
def connect(self, v):
|
||||
return self
|
||||
|
||||
|
||||
def send(self, v):
|
||||
o = json.loads(v)
|
||||
logg.debug('mocket received {}'.format(v))
|
||||
Mocket.req_id = o['id']
|
||||
params = o['params'][0]
|
||||
if to_checksum_address(params.get('from')) != alice:
|
||||
logg.error('from does not match alice {}'.format(params))
|
||||
Mocket.error = True
|
||||
if to_checksum_address(params.get('to')) != bob:
|
||||
logg.error('to does not match bob {}'.format(params))
|
||||
Mocket.error = True
|
||||
if not Mocket.error:
|
||||
Mocket.tx = EIP155Transaction(params, params['nonce'], params['chainId'])
|
||||
logg.debug('mocket {}'.format(Mocket.tx))
|
||||
return len(v)
|
||||
|
||||
|
||||
def recv(self, c):
|
||||
if Mocket.req_id != None:
|
||||
|
||||
o = None
|
||||
if Mocket.error:
|
||||
o = jsonrpc_error(Mocket.req_id)
|
||||
else:
|
||||
tx = Mocket.tx
|
||||
r = Mocket.signer.sign_transaction_to_rlp(tx)
|
||||
Mocket.tx = None
|
||||
o = jsonrpc_response(Mocket.req_id, add_0x(r.hex()))
|
||||
Mocket.req_id = None
|
||||
return json.dumps(o).encode('utf-8')
|
||||
|
||||
return b''
|
||||
|
||||
|
||||
class TestSign(TestBase):
|
||||
|
||||
|
||||
def setUp(self):
|
||||
super(TestSign, self).__init__()
|
||||
self.chain_spec = ChainSpec('evm', 'foo', 42)
|
||||
|
||||
|
||||
logg.debug('alice {}'.format(alice))
|
||||
logg.debug('bob {}'.format(bob))
|
||||
|
||||
self.signer = ReferenceSigner(keystore)
|
||||
|
||||
Mocket.signer = self.signer
|
||||
|
||||
|
||||
def test_sign_build(self):
|
||||
with unittest.mock.patch('chainlib.connection.socket.socket', Mocket) as m:
|
||||
rpc = EthUnixSignerConnection('foo', chain_spec=self.chain_spec)
|
||||
f = TxFactory(self.chain_spec, signer=rpc)
|
||||
tx = f.template(alice, bob, use_nonce=True)
|
||||
tx = f.build(tx)
|
||||
logg.debug('tx result {}'.format(tx))
|
||||
|
||||
|
||||
def test_sign_rpc(self):
|
||||
with unittest.mock.patch('chainlib.connection.socket.socket', Mocket) as m:
|
||||
rpc = EthUnixSignerConnection('foo')
|
||||
f = TxFactory(self.chain_spec, signer=rpc)
|
||||
tx = f.template(alice, bob, use_nonce=True)
|
||||
tx_o = sign_transaction(tx)
|
||||
rpc.do(tx_o)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,49 +0,0 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
import datetime
|
||||
|
||||
# external imports
|
||||
from chainlib.stat import ChainStat
|
||||
from chainlib.eth.block import Block
|
||||
|
||||
|
||||
class TestStat(unittest.TestCase):
|
||||
|
||||
def test_block(self):
|
||||
|
||||
s = ChainStat()
|
||||
|
||||
d = datetime.datetime.utcnow() - datetime.timedelta(seconds=30)
|
||||
block_a = Block({
|
||||
'timestamp': d.timestamp(),
|
||||
'hash': None,
|
||||
'transactions': [],
|
||||
'number': 41,
|
||||
})
|
||||
|
||||
d = datetime.datetime.utcnow()
|
||||
block_b = Block({
|
||||
'timestamp': d.timestamp(),
|
||||
'hash': None,
|
||||
'transactions': [],
|
||||
'number': 42,
|
||||
})
|
||||
|
||||
s.block_apply(block_a)
|
||||
s.block_apply(block_b)
|
||||
self.assertEqual(s.block_average(), 30.0)
|
||||
|
||||
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=10)
|
||||
block_c = Block({
|
||||
'timestamp': d.timestamp(),
|
||||
'hash': None,
|
||||
'transactions': [],
|
||||
'number': 43,
|
||||
})
|
||||
|
||||
s.block_apply(block_c)
|
||||
self.assertEqual(s.block_average(), 20.0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,30 +0,0 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
|
||||
# local imports
|
||||
from chainlib.eth.unittest.ethtester import EthTesterCase
|
||||
from chainlib.eth.nonce import RPCNonceOracle
|
||||
from chainlib.eth.gas import (
|
||||
RPCGasOracle,
|
||||
Gas,
|
||||
)
|
||||
from chainlib.eth.tx import (
|
||||
unpack,
|
||||
TxFormat,
|
||||
)
|
||||
from hexathon import strip_0x
|
||||
|
||||
class TxTestCase(EthTesterCase):
|
||||
|
||||
def test_tx_reciprocal(self):
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
gas_oracle = RPCGasOracle(self.rpc)
|
||||
c = Gas(signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, chain_spec=self.chain_spec)
|
||||
(tx_hash_hex, o) = c.create(self.accounts[0], self.accounts[1], 1024, tx_format=TxFormat.RLP_SIGNED)
|
||||
tx = unpack(bytes.fromhex(strip_0x(o)), self.chain_spec)
|
||||
self.assertEqual(tx['from'], self.accounts[0])
|
||||
self.assertEqual(tx['to'], self.accounts[1])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1 +0,0 @@
|
||||
{"address":"eb3907ecad74a0013c259d5874ae7f22dcbcc95c","crypto":{"cipher":"aes-128-ctr","ciphertext":"b0f70a8af4071faff2267374e2423cbc7a71012096fd2215866d8de7445cc215","cipherparams":{"iv":"9ac89383a7793226446dcb7e1b45cdf3"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"299f7b5df1d08a0a7b7f9c9eb44fe4798683b78da3513fcf9603fd913ab3336f"},"mac":"6f4ed36c11345a9a48353cd2f93f1f92958c96df15f3112a192bc994250e8d03"},"id":"61a9dd88-24a9-495c-9a51-152bd1bfaa5b","version":3}
|
||||
Reference in New Issue
Block a user