From 500f0c3a41441d50c0837b292d94858730d6fdac Mon Sep 17 00:00:00 2001 From: Louis Holbrook Date: Wed, 17 Feb 2021 08:30:10 +0000 Subject: [PATCH] cic-eth: Revert number as hex in tx-cache --- .../default/versions/e3b5330ee71c_.py | 4 +-- .../postgresql/versions/e3b5330ee71c_.py | 5 ++-- apps/cic-eth/cic_eth/db/models/tx.py | 30 +++++-------------- apps/cic-eth/cic_eth/queue/tx.py | 5 ++-- apps/cic-eth/tests/unit/db/test_tx.py | 5 ++-- apps/cic-eth/tests/unit/eth/test_bancor.py | 3 +- apps/cic-eth/tests/unit/eth/test_token.py | 3 +- 7 files changed, 19 insertions(+), 36 deletions(-) diff --git a/apps/cic-eth/cic_eth/db/migrations/default/versions/e3b5330ee71c_.py b/apps/cic-eth/cic_eth/db/migrations/default/versions/e3b5330ee71c_.py index 37cb3b81..6c9115b7 100644 --- a/apps/cic-eth/cic_eth/db/migrations/default/versions/e3b5330ee71c_.py +++ b/apps/cic-eth/cic_eth/db/migrations/default/versions/e3b5330ee71c_.py @@ -27,8 +27,8 @@ def upgrade(): sa.Column('destination_token_address', sa.String(42), nullable=False), sa.Column('sender', sa.String(42), nullable=False), sa.Column('recipient', sa.String(42), nullable=False), - sa.Column('from_value', sa.String(), nullable=False), - sa.Column('to_value', sa.String(), nullable=True), + sa.Column('from_value', sa.NUMERIC(), nullable=False), + sa.Column('to_value', sa.NUMERIC(), nullable=True), sa.Column('block_number', sa.BIGINT(), nullable=True), sa.Column('tx_index', sa.Integer, nullable=True), ) diff --git a/apps/cic-eth/cic_eth/db/migrations/postgresql/versions/e3b5330ee71c_.py b/apps/cic-eth/cic_eth/db/migrations/postgresql/versions/e3b5330ee71c_.py index ae53cf46..3abafb73 100644 --- a/apps/cic-eth/cic_eth/db/migrations/postgresql/versions/e3b5330ee71c_.py +++ b/apps/cic-eth/cic_eth/db/migrations/postgresql/versions/e3b5330ee71c_.py @@ -19,7 +19,6 @@ def upgrade(): op.create_table( 'tx_cache', sa.Column('id', sa.Integer, primary_key=True), -# sa.Column('tx_id', sa.Integer, sa.ForeignKey('tx.id'), nullable=True), sa.Column('otx_id', sa.Integer, sa.ForeignKey('otx.id'), nullable=True), sa.Column('date_created', sa.DateTime, nullable=False), sa.Column('date_updated', sa.DateTime, nullable=False), @@ -27,8 +26,8 @@ def upgrade(): sa.Column('destination_token_address', sa.String(42), nullable=False), sa.Column('sender', sa.String(42), nullable=False), sa.Column('recipient', sa.String(42), nullable=False), - sa.Column('from_value', sa.BIGINT(), nullable=False), - sa.Column('to_value', sa.BIGINT(), nullable=True), + sa.Column('from_value', sa.NUMERIC(), nullable=False), + sa.Column('to_value', sa.NUMERIC(), nullable=True), sa.Column('block_number', sa.BIGINT(), nullable=True), sa.Column('tx_index', sa.Integer, nullable=True), ) diff --git a/apps/cic-eth/cic_eth/db/models/tx.py b/apps/cic-eth/cic_eth/db/models/tx.py index 22c2252c..98a36baf 100644 --- a/apps/cic-eth/cic_eth/db/models/tx.py +++ b/apps/cic-eth/cic_eth/db/models/tx.py @@ -2,7 +2,7 @@ import datetime # third-party imports -from sqlalchemy import Column, String, Integer, DateTime, Enum, ForeignKey, Boolean +from sqlalchemy import Column, String, Integer, DateTime, Enum, ForeignKey, Boolean, NUMERIC from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property #from sqlalchemy.orm import relationship, backref #from sqlalchemy.ext.declarative import declarative_base @@ -55,8 +55,8 @@ class TxCache(SessionBase): destination_token_address = Column(String(42)) sender = Column(String(42)) recipient = Column(String(42)) - from_value = Column(String()) - to_value = Column(String()) + from_value = Column(NUMERIC()) + to_value = Column(NUMERIC()) block_number = Column(Integer()) tx_index = Column(Integer()) date_created = Column(DateTime, default=datetime.datetime.utcnow) @@ -64,16 +64,6 @@ class TxCache(SessionBase): date_checked = Column(DateTime, default=datetime.datetime.utcnow) - def values(self): - from_value_hex = bytes.fromhex(self.from_value) - from_value = int.from_bytes(from_value_hex, 'big') - - to_value_hex = bytes.fromhex(self.to_value) - to_value = int.from_bytes(to_value_hex, 'big') - - return (from_value, to_value) - - def check(self): """Update the "checked" timestamp to current time. @@ -116,15 +106,14 @@ class TxCache(SessionBase): if otx == None: raise NotLocalTxError('new {}'.format(tx_hash_new)) - values = txc.values() txc_new = TxCache( otx.tx_hash, txc.sender, txc.recipient, txc.source_token_address, txc.destination_token_address, - values[0], - values[1], + int(txc.from_value), + int(txc.to_value), ) localsession.add(txc_new) localsession.commit() @@ -141,17 +130,12 @@ class TxCache(SessionBase): raise FileNotFoundError('outgoing transaction record unknown {} (add a Tx first)'.format(tx_hash)) self.otx_id = tx.id -# if tx == None: -# session.close() -# raise ValueError('tx hash {} (outgoing: {}) not found'.format(tx_hash, outgoing)) -# session.close() - self.sender = sender self.recipient = recipient self.source_token_address = source_token_address self.destination_token_address = destination_token_address - self.from_value = num_serialize(from_value).hex() - self.to_value = num_serialize(to_value).hex() + self.from_value = from_value + self.to_value = to_value self.block_number = block_number self.tx_index = tx_index # not automatically set in sqlite, it seems: diff --git a/apps/cic-eth/cic_eth/queue/tx.py b/apps/cic-eth/cic_eth/queue/tx.py index 57ac3074..e3e13e46 100644 --- a/apps/cic-eth/cic_eth/queue/tx.py +++ b/apps/cic-eth/cic_eth/queue/tx.py @@ -378,7 +378,6 @@ def get_tx_cache(tx_hash): session.close() - values = txc.values() tx = { 'tx_hash': otx.tx_hash, 'signed_tx': otx.signed_tx, @@ -389,8 +388,8 @@ def get_tx_cache(tx_hash): 'destination_token': txc.destination_token_address, 'sender': txc.sender, 'recipient': txc.recipient, - 'from_value': values[0], - 'to_value': values[1], + 'from_value': int(txc.from_value), + 'to_value': int(txc.to_value), 'date_created': txc.date_created, 'date_updated': txc.date_updated, 'date_checked': txc.date_checked, diff --git a/apps/cic-eth/tests/unit/db/test_tx.py b/apps/cic-eth/tests/unit/db/test_tx.py index fc7841a6..775f7440 100644 --- a/apps/cic-eth/tests/unit/db/test_tx.py +++ b/apps/cic-eth/tests/unit/db/test_tx.py @@ -59,9 +59,8 @@ def test_set( 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.from_value == tx_def['value']) + assert (tx_stored.to_value == to_value) assert (tx_stored.block_number == 666) assert (tx_stored.tx_index == 13) diff --git a/apps/cic-eth/tests/unit/eth/test_bancor.py b/apps/cic-eth/tests/unit/eth/test_bancor.py index 3425786c..9fd8835c 100644 --- a/apps/cic-eth/tests/unit/eth/test_bancor.py +++ b/apps/cic-eth/tests/unit/eth/test_bancor.py @@ -126,4 +126,5 @@ def test_queue_cache_convert( assert txc.recipient == init_w3.eth.accounts[0] assert txc.source_token_address == bancor_tokens[0] assert txc.destination_token_address == bancor_tokens[1] - assert txc.values() == (amount, amount) + assert txc.from_value == amount + assert txc.to_value == amount diff --git a/apps/cic-eth/tests/unit/eth/test_token.py b/apps/cic-eth/tests/unit/eth/test_token.py index 6f8f509d..c65e7190 100644 --- a/apps/cic-eth/tests/unit/eth/test_token.py +++ b/apps/cic-eth/tests/unit/eth/test_token.py @@ -85,4 +85,5 @@ def test_queue_cache_transfer( assert txc.recipient == init_w3.eth.accounts[1] assert txc.source_token_address == bancor_tokens[0] assert txc.destination_token_address == bancor_tokens[0] - assert txc.values() == (value, value) + assert txc.from_value == value + assert txc.to_value == value