Skip to content

Commit

Permalink
Merge branch 'master' into feature_cip22_addrindexrs
Browse files Browse the repository at this point in the history
  • Loading branch information
chiguireitor committed Sep 9, 2020
2 parents a28c822 + 2a01827 commit e2e0e91
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion counterpartylib/lib/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
48 changes: 42 additions & 6 deletions counterpartylib/lib/messages/issuance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
''')
Expand All @@ -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)
''')
Expand Down
5 changes: 4 additions & 1 deletion counterpartylib/lib/messages/sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?)
Expand All @@ -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,
Expand All @@ -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'],
Expand Down

0 comments on commit e2e0e91

Please sign in to comment.