Skip to content

Instantly share code, notes, and snippets.

@prozacchiwawa
Created November 10, 2021 04:56
Show Gist options
  • Save prozacchiwawa/2ee387e9287cdbf5a689f22b0883944e to your computer and use it in GitHub Desktop.
Save prozacchiwawa/2ee387e9287cdbf5a689f22b0883944e to your computer and use it in GitHub Desktop.
chia: copy block data from one wallet to another to speed up sync
#!/usr/bin/env python
import sys
import sqlite3
COPY_BLOCKS=1000
block_records_fields = [
'header_hash',
'prev_hash',
'height',
'weight',
'total_iters',
'block',
'sub_epoch_summary',
'is_peak'
]
header_blocks_fields = [
'header_hash',
'height',
'timestamp',
'block'
]
def db_copy(file1, file2):
con1 = sqlite3.connect(file1)
con2 = sqlite3.connect(file2)
have_blocks = True
height = 0
while have_blocks:
print(height)
in_cursor = con1.execute('select %s from block_records where height >= ? and height < ?' % ','.join(block_records_fields), (height, height + COPY_BLOCKS))
have_blocks = False
header_hashes = []
for row in in_cursor:
have_blocks = True
header_hashes.append(row[0])
con2.execute('insert into block_records (%s) values (%s)' % (','.join(block_records_fields), ','.join(map(lambda x: '?', block_records_fields))), row)
con2.commit()
in_cursor.close()
for h in header_hashes:
in_cursor = con1.execute('select %s from header_blocks where header_hash = ?' % (','.join(header_blocks_fields)), (h,))
for row in in_cursor:
con2.execute('insert into header_blocks (%s) values (%s)' % (','.join(header_blocks_fields), ','.join(map(lambda x: '?', header_blocks_fields))), row)
con2.commit()
in_cursor.close()
height += COPY_BLOCKS
if __name__ == '__main__':
if len(sys.argv) < 3:
print('usage: dbcopy.py wallet_db1.sqlite wallet_db2.sqlite')
sys.exit(1)
db_copy(sys.argv[1], sys.argv[2])
(base) arty@clotho:~$ cat copy_wallet_blocks.py
#!/usr/bin/env python
import sys
import sqlite3
COPY_BLOCKS=1000
block_records_fields = [
'header_hash',
'prev_hash',
'height',
'weight',
'total_iters',
'block',
'sub_epoch_summary',
'is_peak'
]
header_blocks_fields = [
'header_hash',
'height',
'timestamp',
'block'
]
def db_copy(file1, file2):
con1 = sqlite3.connect(file1)
con2 = sqlite3.connect(file2)
have_blocks = True
height = 0
while have_blocks:
print(height)
in_cursor = con1.execute('select %s from block_records where height >= ? and height < ?' % ','.join(block_records_fields), (height, height + COPY_BLOCKS))
have_blocks = False
header_hashes = []
for row in in_cursor:
have_blocks = True
header_hashes.append(row[0])
con2.execute('insert into block_records (%s) values (%s)' % (','.join(block_records_fields), ','.join(map(lambda x: '?', block_records_fields))), row)
con2.commit()
in_cursor.close()
for h in header_hashes:
in_cursor = con1.execute('select %s from header_blocks where header_hash = ?' % (','.join(header_blocks_fields)), (h,))
for row in in_cursor:
con2.execute('insert into header_blocks (%s) values (%s)' % (','.join(header_blocks_fields), ','.join(map(lambda x: '?', header_blocks_fields))), row)
con2.commit()
in_cursor.close()
height += COPY_BLOCKS
if __name__ == '__main__':
if len(sys.argv) < 3:
print('usage: dbcopy.py wallet_db1.sqlite wallet_db2.sqlite')
sys.exit(1)
db_copy(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment