Пример #1
0
def block(db):
    Block.create(
        **{
            'height':
            0,
            'hash':
            'cf7938a048f1442dd34f87ce56d3e25455b22a44f676325f1ae8c7a33d0731c7',
            'timestamp':
            '2018-06-17 00:50:36',
            'merkle_root':
            '3ead103523ad8f9bfc8365c7b5ddb6f10c731c6274730e88bcaa2c74606dd4bb',
            'tx': [
                "3ead103523ad8f9bfc8365c7b5ddb6f10c731c6274730e88bcaa2c74606dd4bb"
            ],
            'difficulty':
            0.000244141,
            'size':
            286,
            'nonce':
            2085541870,
            'version':
            b'01000000',
            'bits':
            b'f0ff0f1e',
            'coinbase':
            b'04ffff001d0104464e6f727468204b6f7265616e2066696c6d206f6e204b696d27732053696e6761706f726520747269702072657665616c73206e657720666f637573206f6e2065636f6e6f6d79',
            'tx_count':
            1
        })
Пример #2
0
    def checkblocks(self, height, force=False):
        if force or height % 300 == 0:
            self.log.debug('Commit blocks')
            blocks = []
            hashes = []
            with self.db.write_batch(transaction=True) as deleteBatch:
                for key, value in self.db.iterator(prefix=b'pg_block:'):
                    data = json.loads(value.decode('utf-8'))
                    hashes.append(data['hash'])
                    data['version'] = struct.pack('>i', data['version'])
                    data['bits'] = struct.pack('>i', data['bits'])
                    data['coinbase'] = base64.decodebytes(data['coinbase'].encode())
                    data['timestamp'] = datetime.fromtimestamp(data['timestamp'])
                    data['chainwork'] = int_to_bytes(data['chainwork'])
                    blocks.append(data)
                    deleteBatch.delete(key)
                if blocks:
                    if not self.initial_sync:
                        for block in blocks:
                            b = Block.create(**block)
                            external_sio.emit('block', b.to_json(), room='inv')
                    else:
                        Block.insert_many(blocks).execute(None)
            with self.db.write_batch(transaction=True) as deleteBatch:
                with self.db.snapshot() as sn:
                    wallets = []
                    walletsAddress = []
                    for key, value in sn.iterator(prefix=b'walletCreate:'):
                        addr = key.decode().split(':')[1]
                        uid = value.decode()
                        wallets.append({
                            'uid': uid
                        })
                        walletsAddress.append({
                            'wallet': uid,
                            'address': addr,
                        })
                        deleteBatch.delete(key)
                        self.log.info('PG wallet create %s for %s' % (uid, addr))
                    if wallets:
                        WalletGroup.insert_many(wallets).on_conflict_ignore().execute(None)

                    for key, value in sn.iterator(prefix=b'walletAdd:'):
                        addr = key.decode().split(':')[1]
                        walletsAddress.append({
                            'wallet': value.decode(),
                            'address': addr,
                        })
                        self.log.info('PG wallet add %s for %s' % (value.decode(), addr))
                        deleteBatch.delete(key)
                    if walletsAddress:
                        WalletGroupAddress.insert_many(walletsAddress).execute(None)
                    for key, value in sn.iterator(prefix=b'walletMerge:'):
                        existingWalletId = key.decode().split(':')[1]
                        newWalletId = value.decode()
                        deleteBatch.delete(key)
                        res = WalletGroupAddress.update(wallet = newWalletId).where(WalletGroupAddress.wallet == existingWalletId).execute(None)
                        self.log.info('PG wallet merge from %s to %s (%s)' % (existingWalletId, newWalletId, res))
Пример #3
0
    def checkblocks(self, height, force=False):
        if force or height % 300 == 0:
            self.log.debug('Commit blocks')
            blocks = []
            hashes = []
            with self.db.write_batch(transaction=True) as deleteBatch:
                for key, value in self.db.iterator(prefix=b'pg_block:'):
                    data = json.loads(value.decode('utf-8'))
                    hashes.append(data['hash'])
                    data['version'] = struct.pack('i', data['version'])
                    data['bits'] = struct.pack('i', data['bits'])
                    data['coinbase'] = base64.decodebytes(data['coinbase'].encode())
                    data['timestamp'] = datetime.fromtimestamp(data['timestamp'])
                    blocks.append(data)
                    deleteBatch.delete(key)
                if blocks:
                    if not self.initial_sync:
                        for block in blocks:
                            b = Block.create(**block)
#                            print(b.to_json())
                            external_sio.emit('block', b.to_json(), room='inv')
                    else:
                        Block.insert_many(blocks).execute(None)