diff --git a/counterpartylib/lib/database.py b/counterpartylib/lib/database.py index 1a4ec07edf..6af84d7d49 100644 --- a/counterpartylib/lib/database.py +++ b/counterpartylib/lib/database.py @@ -45,7 +45,7 @@ def exectracer(cursor, sql, bindings): skip_tables = [ 'blocks', 'transactions', 'balances', 'messages', 'mempool', 'assets', - 'new_sends' # interim table for CIP10 activation + 'new_sends', 'new_issuances' # interim table for CIP10 activation ] skip_tables_block_messages = copy.copy(skip_tables) if command == 'update': diff --git a/counterpartylib/lib/messages/issuance.py b/counterpartylib/lib/messages/issuance.py index 2ffb5248cd..3034eada7e 100644 --- a/counterpartylib/lib/messages/issuance.py +++ b/counterpartylib/lib/messages/issuance.py @@ -45,6 +45,48 @@ def initialise(db): asset_longname TEXT, FOREIGN KEY (tx_index, tx_hash, block_index) REFERENCES transactions(tx_index, tx_hash, block_index)) ''') + + # Add asset_longname for sub-assets + # SQLite can’t do `ALTER TABLE IF COLUMN NOT EXISTS`. + columns = [column['name'] for column in cursor.execute('''PRAGMA table_info(issuances)''')] + if 'asset_longname' not in columns: + cursor.execute('''ALTER TABLE issuances ADD COLUMN asset_longname TEXT''') + + # If sweep_hotifx activated, Create issuances copy, copy old data, drop old table, rename new table, recreate indexes + # SQLite can’t do `ALTER TABLE IF COLUMN NOT EXISTS` nor can drop UNIQUE constraints + if 'msg_index' not in columns: + cursor.execute('''CREATE TABLE IF NOT EXISTS new_issuances( + tx_index INTEGER, + tx_hash TEXT, + msg_index INTEGER DEFAULT 0, + block_index INTEGER, + asset TEXT, + quantity INTEGER, + divisible BOOL, + source TEXT, + issuer TEXT, + transfer BOOL, + callable BOOL, + call_date INTEGER, + call_price REAL, + description TEXT, + fee_paid INTEGER, + locked BOOL, + status TEXT, + asset_longname TEXT, + PRIMARY KEY (tx_index, msg_index), + FOREIGN KEY (tx_index, tx_hash, block_index) REFERENCES transactions(tx_index, tx_hash, block_index), + UNIQUE (tx_hash, msg_index)) + ''') + cursor.execute('''INSERT INTO new_issuances(tx_index, tx_hash, msg_index, + block_index, asset, quantity, divisible, source, issuer, transfer, callable, + call_date, call_price, description, fee_paid, locked, status, asset_longname) + SELECT tx_index, tx_hash, 0, block_index, asset, quantity, divisible, source, + issuer, transfer, callable, call_date, call_price, description, fee_paid, + locked, status, asset_longname FROM issuances''', {}) + cursor.execute('DROP TABLE issuances') + cursor.execute('ALTER TABLE new_issuances RENAME TO issuances') + cursor.execute('''CREATE INDEX IF NOT EXISTS block_index_idx ON issuances (block_index) ''') @@ -58,12 +100,6 @@ def initialise(db): source_idx ON issuances (source) ''') - # Add asset_longname for sub-assets - # SQLite can’t do `ALTER TABLE IF COLUMN NOT EXISTS`. - columns = [column['name'] for column in cursor.execute('''PRAGMA table_info(issuances)''')] - if 'asset_longname' not in columns: - cursor.execute('''ALTER TABLE issuances ADD COLUMN asset_longname TEXT''') - cursor.execute('''CREATE INDEX IF NOT EXISTS asset_longname_idx ON issuances (asset_longname) ''') diff --git a/counterpartylib/lib/messages/sweep.py b/counterpartylib/lib/messages/sweep.py index 2db37cf150..2293ad53b3 100644 --- a/counterpartylib/lib/messages/sweep.py +++ b/counterpartylib/lib/messages/sweep.py @@ -167,6 +167,7 @@ def parse (db, tx, message): util.credit(db, destination, balance['asset'], balance['quantity'], action='sweep', event=tx['tx_hash']) if flags & FLAG_OWNERSHIP: + sweep_pos = 0 for balance in balances: cursor.execute('''SELECT * FROM issuances \ WHERE (status = ? AND asset = ?) @@ -178,6 +179,7 @@ def parse (db, tx, message): bindings= { 'tx_index': tx['tx_index'], 'tx_hash': tx['tx_hash'], + 'msg_index': sweep_pos, 'block_index': tx['block_index'], 'asset': balance['asset'], 'quantity': 0, @@ -194,8 +196,9 @@ def parse (db, tx, message): 'status': status, 'asset_longname': last_issuance['asset_longname'], } - sql='insert into issuances values(:tx_index, :tx_hash, :block_index, :asset, :quantity, :divisible, :source, :issuer, :transfer, :callable, :call_date, :call_price, :description, :fee_paid, :locked, :status, :asset_longname)' + sql='insert into issuances values(:tx_index, :tx_hash, :msg_index, :block_index, :asset, :quantity, :divisible, :source, :issuer, :transfer, :callable, :call_date, :call_price, :description, :fee_paid, :locked, :status, :asset_longname)' cursor.execute(sql, bindings) + sweep_pos += 1 bindings = { 'tx_index': tx['tx_index'],