adding cic-eth as sub dir

This commit is contained in:
2021-02-01 09:12:51 -08:00
parent ed3991e997
commit a4587deac5
317 changed files with 819441 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
# standard imports
import logging
# local imports
from cic_eth.db.models.otx import OtxSync
logg = logging.getLogger()
def test_db_block_sync(
init_database,
):
s = OtxSync('eip155:8995:bloxberg')
s.head(666, 12)
assert s.head() == (666, 12)
s.session(42, 13)
assert s.session() == (42, 13)
s.backlog(13, 2)
assert s.backlog() == (13, 2)
assert not s.synced
s.backlog(42, 13)
assert s.backlog() == (42, 13)
assert s.synced

View File

@@ -0,0 +1,29 @@
import os
import logging
import pytest
from cic_eth.db import TxConvertTransfer
from cic_eth.db.error import UnknownConvertError
logg = logging.getLogger()
def test_convert_transfer(
init_database,
default_chain_spec,
):
tx_hash_hex = '0x' + os.urandom(32).hex()
recipient = '0x' + os.urandom(20).hex()
txct = TxConvertTransfer(tx_hash_hex, recipient, str(default_chain_spec))
init_database.add(txct)
init_database.commit()
txct = TxConvertTransfer.get(tx_hash_hex)
assert txct.convert_tx_hash == tx_hash_hex
tx_hash_bogus_hex = '0x' + os.urandom(32).hex()
with pytest.raises(UnknownConvertError):
TxConvertTransfer.get(tx_hash_bogus_hex)

View File

@@ -0,0 +1,5 @@
def test_db_init(
init_database,
):
pass

View File

@@ -0,0 +1,67 @@
# standard imports
import os
# local imports
from cic_eth.db.models.lock import Lock
from cic_eth.db.enum import LockEnum
def test_lock(
init_database,
default_chain_spec,
):
chain_str = str(default_chain_spec)
# Check matching flag for global (zero-address) lock
a = 0xffffffffffffffff & LockEnum.CREATE
r = Lock.set(chain_str, a)
assert r == a
assert Lock.check(chain_str, a) > 0
# Check matching flag for address specific lock
address = '0x' + os.urandom(20).hex()
b = 0xffffffffffffffff & (LockEnum.QUEUE | LockEnum.SEND)
Lock.set(chain_str, b, address=address)
assert Lock.check(chain_str, b, address=address) == b
assert Lock.check(chain_str, a, address=address) == 0
assert Lock.check(chain_str, b & LockEnum.QUEUE, address=address) == LockEnum.QUEUE
assert Lock.check(chain_str, b) == 0
# Reset single flag
r = Lock.reset(chain_str, LockEnum.QUEUE, address=address)
assert r == LockEnum.SEND
# Reset to 0
r = Lock.reset(chain_str, LockEnum.SEND, address=address)
assert r == 0
# Row should be deleted when flags value reaches 0
q = init_database.query(Lock)
q = q.filter(Lock.address==address)
assert q.first() == None
def test_lock_merge_check(
init_database,
default_chain_spec,
):
chain_str = str(default_chain_spec)
foo_address = '0x' + os.urandom(20).hex()
bar_address = '0x' + os.urandom(20).hex()
Lock.set(chain_str, LockEnum.CREATE)
assert Lock.check_aggregate(chain_str, LockEnum.CREATE, address=foo_address) > 0
assert Lock.check_aggregate(chain_str, LockEnum.CREATE, address=bar_address) > 0
Lock.set(chain_str, LockEnum.CREATE, address=foo_address)
assert Lock.check_aggregate(chain_str, LockEnum.CREATE, address=foo_address) > 0
assert Lock.check_aggregate(chain_str, LockEnum.CREATE, address=bar_address) > 0
Lock.reset(chain_str, LockEnum.CREATE)
assert Lock.check_aggregate(chain_str, LockEnum.CREATE, address=foo_address) > 0
assert Lock.check_aggregate(chain_str, LockEnum.CREATE, address=bar_address) == 0

View File

@@ -0,0 +1,20 @@
# third-party imports
import pytest
# local imports
from cic_eth.db.models.nonce import Nonce
def test_nonce_increment(
init_database,
eth_empty_accounts,
database_engine,
):
# if database_engine[:6] == 'sqlite':
# pytest.skip('sqlite cannot lock tables which is required for this test, skipping')
nonce = Nonce.next(eth_empty_accounts[0], 3)
assert nonce == 3
nonce = Nonce.next(eth_empty_accounts[0], 3)
assert nonce == 4

View File

@@ -0,0 +1,84 @@
# standard imports
import os
import logging
# third-party imports
import pytest
# local imports
from cic_eth.db.models.base import SessionBase
from cic_eth.db.models.otx import OtxStateLog
from cic_eth.db.models.otx import Otx
from cic_eth.db.enum import StatusEnum
logg = logging.getLogger()
@pytest.mark.skip()
def test_get(
init_w3,
init_database,
):
tx_def = {
'from': init_w3.eth.accounts[0],
'to': init_w3.eth.accounts[1],
'nonce': 0,
'value': 101,
'gasPrice': 2000000000,
'gas': 21000,
'data': '',
'chainId': 1,
}
session = init_database
txs = []
for i in range(10):
nonce = init_w3.eth.getTransactionCount(init_w3.eth.accounts[0], 'pending')
tx_def['nonce'] = nonce
tx = init_w3.eth.sign_transaction(tx_def)
tx_hash = init_w3.eth.send_raw_transaction(tx['raw'])
logg.debug('tx {}'.format(tx))
address = init_w3.eth.accounts[i%3]
otx = Otx(int((i/3)+1), address, '0x'+tx_hash.hex(), tx['raw'])
txs.append(otx)
session.add(otx)
session.flush()
logg.debug(txs)
session.commit()
txs[0].status = 0
session.add(txs[0])
session.commit()
session.close()
get_txs = Otx.get()
logg.debug(get_txs)
def test_state_log(
init_database,
):
Otx.tracing = True
address = '0x' + os.urandom(20).hex()
tx_hash = '0x' + os.urandom(32).hex()
signed_tx = '0x' + os.urandom(128).hex()
otx = Otx.add(0, address, tx_hash, signed_tx, session=init_database)
otx.waitforgas(session=init_database)
otx.sent(session=init_database)
otx.success(1024, session=init_database)
q = init_database.query(OtxStateLog)
q = q.filter(OtxStateLog.otx_id==otx.id)
q = q.order_by(OtxStateLog.date.asc())
logs = q.all()
assert logs[0].status == StatusEnum.PENDING
assert logs[1].status == StatusEnum.WAITFORGAS
assert logs[2].status == StatusEnum.SENT
assert logs[3].status == StatusEnum.SUCCESS

View File

@@ -0,0 +1,55 @@
# standard imports
import logging
# third-party imports
import pytest
# local imports
from cic_eth.db import Otx
from cic_eth.db.error import TxStateChangeError
logg = logging.getLogger()
# Check that invalid transitions throw exceptions
# sent
def test_db_queue_states(
init_database,
):
session = init_database
# these values are completely arbitary
tx_hash = '0xF182DFA3AD48723E7E222FE7B4C2C44C23CD4D7FF413E8999DFA15ECE53F'
address = '0x38C5559D6EDDDA1F705D3AB1A664CA1B397EB119'
signed_tx = '0xA5866A5383249AE843546BDA46235A1CA1614F538FB486140693C2EF1956FC53213F6AEF0F99F44D7103871AF3A12B126DCF9BFB7AF11143FAB3ECE2B452EE35D1320C4C7C6F999C8DF4EB09E729715B573F6672ED852547F552C4AE99D17DCD14C810'
o = Otx(
nonce=42,
address=address[2:],
tx_hash=tx_hash[2:],
signed_tx=signed_tx[2:],
)
session.add(o)
session.commit()
o.sent(session=session)
session.commit()
# send after sent is ok
o.sent(session=session)
session.commit()
o.sendfail(session=session)
session.commit()
with pytest.raises(TxStateChangeError):
o.sendfail(session=session)
o.sent(session=session)
session.commit()
o.minefail(1234, session=session)
session.commit()
with pytest.raises(TxStateChangeError):
o.sent(session=session)

View File

@@ -0,0 +1,29 @@
# local imports
from cic_eth.db.models.role import AccountRole
def test_db_role(
init_database,
eth_empty_accounts,
):
foo = AccountRole.set('foo', eth_empty_accounts[0])
init_database.add(foo)
init_database.commit()
assert AccountRole.get_address('foo') == eth_empty_accounts[0]
bar = AccountRole.set('bar', eth_empty_accounts[1])
init_database.add(bar)
init_database.commit()
assert AccountRole.get_address('bar') == eth_empty_accounts[1]
foo = AccountRole.set('foo', eth_empty_accounts[2])
init_database.add(foo)
init_database.commit()
assert AccountRole.get_address('foo') == eth_empty_accounts[2]
assert AccountRole.get_address('bar') == eth_empty_accounts[1]
tag = AccountRole.role_for(eth_empty_accounts[2])
assert tag == 'foo'
tag = AccountRole.role_for(eth_empty_accounts[3])
assert tag == None

View File

@@ -0,0 +1,124 @@
# standard imports
import os
# third-party imports
import pytest
from cic_registry import zero_address
# local imports
from cic_eth.db.models.tx import TxCache
from cic_eth.db.models.otx import Otx
from cic_eth.eth.task import sign_tx
def test_set(
init_w3,
init_database,
):
tx_def = {
'from': init_w3.eth.accounts[0],
'to': init_w3.eth.accounts[1],
'nonce': 0,
'value': 500000000000000000000,
'gasPrice': 2000000000,
'gas': 21000,
'data': '',
'chainId': 1,
}
(tx_hash, tx_signed) = sign_tx(tx_def, 'Foo:1')
otx = Otx(
tx_def['nonce'],
tx_def['from'],
tx_hash,
tx_signed,
)
init_database.add(otx)
init_database.commit()
bogus_from_token = '0x' + os.urandom(20).hex()
to_value = int(tx_def['value'] / 2)
tx = TxCache(
tx_hash,
tx_def['from'],
tx_def['to'],
bogus_from_token,
zero_address,
tx_def['value'],
to_value,
666,
13,
)
init_database.add(tx)
init_database.commit()
tx_stored = init_database.query(TxCache).first()
assert (tx_stored.sender == tx_def['from'])
assert (tx_stored.recipient == tx_def['to'])
assert (tx_stored.source_token_address == bogus_from_token)
assert (tx_stored.destination_token_address == zero_address)
assert (tx_stored.from_value == '1b1ae4d6e2ef500000')
assert (tx_stored.to_value == '0d8d726b7177a80000')
assert (tx_stored.values() == (tx_def['value'], to_value))
assert (tx_stored.block_number == 666)
assert (tx_stored.tx_index == 13)
def test_clone(
init_database,
init_w3,
):
txs = []
for i in range(2):
tx_def = {
'from': init_w3.eth.accounts[0],
'to': init_w3.eth.accounts[1],
'nonce': 0,
'value': 500000000000000000000,
'gasPrice': 2000000000 + i,
'gas': 21000,
'data': '',
'chainId': 1,
}
(tx_hash, tx_signed) = sign_tx(tx_def, 'Foo:1')
otx = Otx(
tx_def['nonce'],
tx_def['from'],
tx_hash,
tx_signed,
)
init_database.add(otx)
tx_def['hash'] = tx_hash
txs.append(tx_def)
init_database.commit()
txc = TxCache(
txs[0]['hash'],
txs[0]['from'],
txs[0]['to'],
zero_address,
zero_address,
txs[0]['value'],
txs[0]['value'],
)
init_database.add(txc)
init_database.commit()
TxCache.clone(txs[0]['hash'], txs[1]['hash'])
q = init_database.query(TxCache)
q = q.join(Otx)
q = q.filter(Otx.tx_hash==txs[1]['hash'])
txc_clone = q.first()
assert txc_clone != None
assert txc_clone.sender == txc.sender
assert txc_clone.recipient == txc.recipient
assert txc_clone.source_token_address == txc.source_token_address
assert txc_clone.destination_token_address == txc.destination_token_address
assert txc_clone.from_value == txc.from_value
assert txc_clone.to_value == txc.to_value