Ejemplo n.º 1
0
 def test_estimate_virtual_op_num(self):
     stm = self.bts
     account = Account("gtg", steem_instance=stm)
     block_num = 21248120
     block = Block(block_num, steem_instance=stm)
     op_num1 = account.estimate_virtual_op_num(block.time(),
                                               stop_diff=1,
                                               max_count=100)
     op_num2 = account.estimate_virtual_op_num(block_num,
                                               stop_diff=1,
                                               max_count=100)
     op_num3 = account.estimate_virtual_op_num(block_num,
                                               stop_diff=100,
                                               max_count=100)
     op_num4 = account.estimate_virtual_op_num(block_num,
                                               stop_diff=0.00001,
                                               max_count=100)
     self.assertTrue(abs(op_num1 - op_num2) < 2)
     self.assertTrue(abs(op_num1 - op_num4) < 2)
     self.assertTrue(abs(op_num1 - op_num3) < 200)
     block_diff1 = 0
     block_diff2 = 0
     for h in account.get_account_history(op_num4 - 1, 1):
         block_diff1 = (block_num - h["block"])
     for h in account.get_account_history(op_num4 + 1, 1):
         block_diff2 = (block_num - h["block"])
     self.assertTrue(block_diff1 > 0)
     self.assertTrue(block_diff1 < 1000)
     self.assertTrue(block_diff2 <= 0)
     self.assertTrue(block_diff2 > -1000)
Ejemplo n.º 2
0
 def test_estimate_block_num(self):
     bts = self.bts
     b = Blockchain(steem_instance=bts)
     last_block = b.get_current_block()
     num = last_block.identifier
     old_block = Block(num - 60, steem_instance=bts)
     date = old_block.time()
     est_block_num = b.get_estimated_block_num(date, accurate=False)
     self.assertTrue((est_block_num - (old_block.identifier)) < 10)
     est_block_num = b.get_estimated_block_num(date, accurate=True)
     self.assertTrue((est_block_num - (old_block.identifier)) < 2)
     est_block_num = b.get_estimated_block_num(date, estimateForwards=True, accurate=True)
     self.assertTrue((est_block_num - (old_block.identifier)) < 2)
     est_block_num = b.get_estimated_block_num(date, estimateForwards=True, accurate=False)
Ejemplo n.º 3
0
 def test_block(self, node_param):
     if node_param == "instance":
         stm = Steem(node="abc", autoconnect=False, num_retries=1)
         set_shared_steem_instance(self.bts)
         o = Block(1)
         self.assertIn(o.steem.rpc.url, self.urls)
         with self.assertRaises(NumRetriesReached):
             Block(1, steem_instance=stm)
     else:
         set_shared_steem_instance(
             Steem(node="abc", autoconnect=False, num_retries=1))
         stm = self.bts
         o = Block(1, steem_instance=stm)
         self.assertIn(o.steem.rpc.url, self.urls)
         with self.assertRaises(NumRetriesReached):
             Block(1)
Ejemplo n.º 4
0
 def test_verify_transaction(self):
     stm = self.stm
     block = Block(22005665, steem_instance=stm)
     trx = block.transactions[28]
     signed_tx = Signed_Transaction(trx)
     key = signed_tx.verify(chain=stm.chain_params, recover_parameter=False)
     public_key = format(Base58(key[0]), stm.prefix)
     self.assertEqual(public_key, "STM4tzr1wjmuov9ftXR6QNv7qDWsbShMBPQpuwatZsfSc5pKjRDfq")
Ejemplo n.º 5
0
    def test_export(self, node_param):
        if node_param == "normal":
            bts = self.bts
            block_num = 2000000
        else:
            bts = self.testnet
            block_num = 2

        if bts.rpc.get_use_appbase():
            block = bts.rpc.get_block({"block_num": block_num}, api="block")
            if block and "block" in block:
                block = block["block"]
        else:
            block = bts.rpc.get_block(block_num)

        b = Block(block_num, steem_instance=bts)
        keys = list(block.keys())
        json_content = b.json()

        for k in keys:
            if k not in "json_metadata":
                if isinstance(block[k], dict) and isinstance(
                        json_content[k], list):
                    self.assertEqual(list(block[k].values()), json_content[k])
                else:
                    self.assertEqual(block[k], json_content[k])

        if bts.rpc.get_use_appbase():
            block = bts.rpc.get_block_header({"block_num": block_num},
                                             api="block")
            if "header" in block:
                block = block["header"]
        else:
            block = bts.rpc.get_block_header(block_num)

        b = BlockHeader(block_num, steem_instance=bts)
        keys = list(block.keys())
        json_content = b.json()

        for k in keys:
            if k not in "json_metadata":
                if isinstance(block[k], dict) and isinstance(
                        json_content[k], list):
                    self.assertEqual(list(block[k].values()), json_content[k])
                else:
                    self.assertEqual(block[k], json_content[k])
Ejemplo n.º 6
0
 def test_block(self, node_param):
     if node_param == "instance":
         set_shared_steem_instance(self.bts)
         o = Block(1)
         self.assertIn(o.steem.rpc.url, self.urls)
         with self.assertRaises(
             RPCConnection
         ):
             Block(1, steem_instance=Steem(node="https://abc.d", autoconnect=False, num_retries=1))
     else:
         set_shared_steem_instance(Steem(node="https://abc.d", autoconnect=False, num_retries=1))
         stm = self.bts
         o = Block(1, steem_instance=stm)
         self.assertIn(o.steem.rpc.url, self.urls)
         with self.assertRaises(
             RPCConnection
         ):
             Block(1)
Ejemplo n.º 7
0
    def test_block_only_ops(self):
        bts = self.bts
        test_block_id = self.test_block_id
        block = Block(test_block_id, only_ops=True, steem_instance=bts)
        self.assertEqual(block.identifier, test_block_id)
        self.assertTrue(isinstance(block.time(), datetime))
        self.assertTrue(isinstance(block, dict))

        self.assertTrue(len(block.operations))
        self.assertTrue(isinstance(block.ops_statistics(), dict))

        block2 = Block(test_block_id + 1, steem_instance=bts)
        self.assertTrue(block2.time() > block.time())
        with self.assertRaises(exceptions.BlockDoesNotExistsException):
            Block(0, steem_instance=bts)
Ejemplo n.º 8
0
def uid_from_block(trx_id, block_num, qty, prefix):
    block = Block(block_num)
    seed = hashlib.md5(
        (trx_id + block["block_id"] + block["previous"]).encode()).hexdigest()
    random.seed(a=seed, version=2)
    uid_list = []
    for i in range(qty):
        uid = prefix + generateUid(10)
        uid_list.append(uid)
    return uid_list
Ejemplo n.º 9
0
    def test_block(self, node_param):
        if node_param == "non_appbase":
            bts = self.bts
        else:
            bts = self.appbase
        block = Block(self.test_block_id, steem_instance=bts)
        self.assertEqual(block.identifier, self.test_block_id)
        self.assertTrue(isinstance(block.time(), datetime))
        self.assertTrue(isinstance(block, dict))

        self.assertTrue(len(block.ops()))
        self.assertTrue(isinstance(block.ops_statistics(), dict))

        block2 = Block(self.test_block_id + 1, steem_instance=bts)
        self.assertTrue(block2.time() > block.time())
        with self.assertRaises(
            exceptions.BlockDoesNotExistsException
        ):
            Block(0, steem_instance=bts)
Ejemplo n.º 10
0
 def test_verify_transaction(self):
     stm = self.stm
     block = Block(22005665, steem_instance=stm)
     trx = block.transactions[28]
     signed_tx = Signed_Transaction(trx)
     key = signed_tx.verify(chain=stm.chain_params, recover_parameter=False)
     public_key = format(Base58(key[0]), stm.prefix)
     self.assertEqual(
         public_key,
         "STM4xA6aCu23rKxsEZWF2xVYJvJAyycuoFxBRQEuQ5Hc7UtFET7fT")
Ejemplo n.º 11
0
        else:
            _ids[ops["_id"]] = 1
    duplicate_indices = []
    _id_list = []
    for _id in sorted(list(ops_dict.keys())):
        ops = ops_dict[_id]
        if _ids[ops["_id"]] == 1:
            continue
        if ops["_id"] not in _id_list:
            _id_list.append(ops["_id"])
        else:
            trx_id = ops["trx_id"]
            if trx_id == "0000000000000000000000000000000000000000":
                duplicate_indices.append(ops["index"])
            else:
                block = Block(ops["block"], blockchain_instance=stm)
                count_ops = 0
                for t in block.transactions:
                    if t["transaction_id"] != trx_id:
                        continue
                    for o in t["operations"]:
                        count_ops += 1
                if count_ops < _ids[ops["_id"]]:
                    duplicate_indices.append(ops["index"])

    type_count = {}
    for _id in sorted(list(ops_dict.keys())):
        ops = ops_dict[_id]
        if ops["type"] in type_count:
            type_count[ops["type"]] += 1
        else:
Ejemplo n.º 12
0
def benchmark_node_blocks(node,
                          num_retries=10,
                          num_retries_call=10,
                          timeout=60,
                          how_many_seconds=30):
    block_count = 0
    sucessfull = False
    error_msg = None
    start_total = timer()
    start_time = timer()
    max_batch_size = None
    threading = False
    thread_num = 16

    last_block_id = 19273700
    try:
        stm = Steem(node=node,
                    num_retries=num_retries,
                    num_retries_call=num_retries_call,
                    timeout=timeout)
        blockchain = Blockchain(steem_instance=stm)

        last_block_id = int(blockchain.get_current_block_num() * 0.75)

        last_block = Block(last_block_id, steem_instance=stm)

        total_transaction = 0

        start_time = timer()
        for entry in blockchain.blocks(start=last_block_id,
                                       max_batch_size=max_batch_size,
                                       threading=threading,
                                       thread_num=thread_num):
            block_no = entry.identifier
            block_count += 1
            if not sucessfull:
                sucessfull = True
            if "block" in entry:
                trxs = entry["block"]["transactions"]
            else:
                trxs = entry["transactions"]

            for tx in trxs:
                for op in tx["operations"]:
                    total_transaction += 1
            if "block" in entry:
                block_time = (entry["block"]["timestamp"])
            else:
                block_time = (entry["timestamp"])

            if timer() - start_time > how_many_seconds or quit_thread:
                break
    except NumRetriesReached:
        error_msg = 'NumRetriesReached'
        sucessfull = False
    except KeyboardInterrupt:
        error_msg = 'KeyboardInterrupt'
        # quit = True
    except Exception as e:
        error_msg = str(e)
        sucessfull = False
    total_duration = float("{0:.2f}".format(timer() - start_time))
    return {
        'sucessfull': sucessfull,
        'node': node,
        'error': error_msg,
        'total_duration': total_duration,
        'count': block_count,
        'access_time': None
    }
Ejemplo n.º 13
0
 block_num = b.get_estimated_block_num(
     vote["time"])
 current_block_num = b.get_current_block_num()
 transaction = None
 block_search_list = [
     0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5
 ]
 block_cnt = 0
 while transaction is None and block_cnt < len(
         block_search_list):
     if block_num + block_search_list[
             block_cnt] > current_block_num:
         block_cnt += 1
         continue
     block = Block(block_num +
                   block_search_list[block_cnt],
                   steem_instance=stm)
     for tt in block.transactions:
         for op in tt["operations"]:
             if isinstance(
                     op, dict
             ) and op["type"][:4] == "vote":
                 if op["value"][
                         "voter"] == vote[
                             "voter"]:
                     transaction = tt
             elif isinstance(op, list) and len(
                     op
             ) > 1 and op[0][:4] == "vote":
                 if op[1]["voter"] == vote[
                         "voter"]:
Ejemplo n.º 14
0
 def test_block(self):
     o = Block(1)
     self.assertEqual(o.steem.rpc.url, self.url)
Ejemplo n.º 15
0
def main():
    checkPoint()

    startblock = 0
    try:
        conn = create_connection(database)
        if conn is not None:
            printX('###########################')
            printX('#### Database connected #####')
            printX('###########################')

        c = conn.cursor()

        c.execute("SELECT blockheight FROM last_check")
        last_check = c.fetchall()
        startblock = last_check[0][0]

    except Exception as error:
        conn.close()
        print(error)
        if startblock is None or startblock == 0:
            startblock = int(input("Please insert initial block number."))
            try:
                with conn:
                    c.execute(
                        '''INSERT OR IGNORE INTO last_check (blockheight) VALUES (?)''',
                        (startblock, ))

            except Exception as e:
                print('ERROR UPDATING BLOCKHEIGHT 1 ' + str(e))
            startblock = int(startblock)

    getblock = startblock

    go = True
    while go == True:
        # Block was read based on block number and always trying to catch the latest block.
        try:
            blockchain = Blockchain()
            print('Block : ' + str(getblock))
        except:
            print("Latest Block")
        try:
            block = Block(getblock)
        except:
            block = None
            printX('###########################')
            printX('#### End of Block #########')
            printX('###########################')

        try:
            if getblock % retrieveInterval == 0:
                retrieveBlackList()
                checkPoint()
                getConfig()
                setHouseFund()

            if getblock % 200 == 0:
                resetSoftBannedCount()
        except Exception as e:
            print('Error get blacklist ' + str(e))

        # Iterating each transaction on every block to check any transaction is send to @epicdice, then storing all crucial value into global variable which later will be stored into local database for processing.
        if block != None:
            for i, txs in enumerate(block.transactions):
                for (j, tx) in enumerate(txs['operations']):
                    if tx['type'] == 'transfer_operation' and tx['value'][
                            'to'] in [watching]:
                        if tx != None:
                            transId = txs['transaction_id']
                            precision = str(tx['value']['amount']['precision'])
                            nai = tx['value']['amount']['nai']
                            fromWho = tx['value']['from']
                            toWho = tx['value']['to']
                            amount = tx['value']['amount']['amount']
                            ref_block_num = txs['ref_block_num']
                            transaction_num = txs['transaction_num']
                            block_num = txs['block_num']
                            memo = tx['value']['memo']
                            transType = tx['type']

                            finalAmount = (float(amount) *
                                           0.1**float(precision)) * 100 / 100
                            finalAmount = float_round(finalAmount, 3, round)

                            if '@@000000013' in nai:
                                finalAmountWithAsset = str(
                                    finalAmount) + ' SBD'
                                asset = 'SBD'
                            else:
                                finalAmountWithAsset = str(
                                    finalAmount) + ' STEEM'
                                asset = 'STEEM'

                        else:
                            print('is None')

                        sender = fromWho
                        factor = 0

                        try:
                            details = memo
                            betType = 0

                            details = str(details).lower()
                            rate = 0
                            if 'over' in details:
                                indexOfSpace = memo.index(" ")
                                rate = int(memo[indexOfSpace:indexOfSpace + 3])
                                factor = 100 - rate
                                betType = 1
                            elif 'above' in details:
                                indexOfSpace = memo.index(" ")
                                rate = int(memo[indexOfSpace:indexOfSpace + 3])
                                factor = 100 - rate
                                betType = 1
                            elif 'below' in details or 'under' in details:
                                try:
                                    indexOfSpace = memo.index(" ")
                                    rate = int(memo[indexOfSpace:indexOfSpace +
                                                    3])
                                    printX("indexOfSpace " + str(indexOfSpace))
                                except Exception as e:
                                    print('failed ' + str(e))
                                factor = 0 + rate - 1
                                betType = 2
                            else:
                                factor = 0
                        except Exception as e:
                            print('Exception ' + str(e))
                            factor = 0

                        printX(
                            '**************************  Bet Found ***********************************'
                        )
                        printX('Transaction : ' + str(tx))
                        printX('transType : ' + transType)
                        printX('fromWho : ' + fromWho)
                        printX('toWho : ' + toWho)
                        printX('amount : ' + amount)
                        printX('finalAmount : ' + str(finalAmount))
                        printX('finalAmountWithAsset : ' +
                               finalAmountWithAsset)
                        printX('precision : ' + str(precision))
                        printX('nai : ' + nai)
                        printX('memo : ' + memo)
                        printX('rate ' + str(rate))
                        printX('factor ' + str(factor))
                        printX('ref_block_num ' + str(ref_block_num))
                        printX('block_num ' + str(block_num))
                        printX('transaction_num ' + str(transaction_num))
                        printX('transId ' + str(transId))

                        if factor > 0:
                            # Calculation of the possible winning payout and go through a series of validation checking.
                            win = (float(finalAmount) * 100 /
                                   factor) * (1 - houseEdge)
                            win = float_round(win, 3, round)

                            if sender in userList:
                                printX('Found in blacklisted*****')
                            else:
                                if float(finalAmount) < minbet:
                                    printX('Sorry, Minimum bet is ' +
                                           str(minbet))
                                    clientTransfer(
                                        watching, sender,
                                        str(finalAmountWithAsset),
                                        'Refund invalid bet. Minimum bet is ' +
                                        str(minbet) + ' . ' +
                                        str(potentialBlackListChecking(sender))
                                        +
                                        ' more invalid bets will get your account permanently blacklisted.'
                                    )
                                elif win > maxwin[asset]:
                                    clientTransfer(
                                        watching, sender,
                                        str(finalAmountWithAsset),
                                        'Refund invalid bet. The maximum amount you can win in one game is '
                                        + str(maxwin[asset]) + ' ' + asset +
                                        ' . ' +
                                        str(potentialBlackListChecking(sender))
                                        +
                                        ' more invalid bets will get your account permanently blacklisted.'
                                    )
                                    printX(
                                        'Sorry, the maximum amount you can win in one game is '
                                        + str(maxwin[asset]) + ' ' + asset)

                                elif not isWatchingAccountSufficient(
                                        finalAmount, asset):
                                    clientTransfer(
                                        watching, sender,
                                        str(finalAmountWithAsset),
                                        'Sorry, Host insufficient fund. Refund Your Bet. Refund initiated'
                                    )
                                    printX('Host insufficient fund.')
                                else:
                                    error = 0

                                    if betType == 1:
                                        if rate < minRate or rate >= 96:
                                            error = 1
                                            clientTransfer(
                                                watching, sender,
                                                str(finalAmountWithAsset),
                                                'Refund invalid Bet. Cannot bet below  6 or above 96. '
                                                + str(
                                                    potentialBlackListChecking(
                                                        sender)) +
                                                ' more invalid bets will get your account permanently blacklisted.'
                                            )
                                            printX(
                                                'Below min rate or over 100')
                                    elif betType == 2:
                                        if rate > maxRate or rate <= 1:
                                            error = 1
                                            clientTransfer(
                                                watching, sender,
                                                str(finalAmountWithAsset),
                                                'Refund invalid Bet. Cannot bet above 95 or below 1. '
                                                + str(
                                                    potentialBlackListChecking(
                                                        sender)) +
                                                ' more invalid bets will get your account permanently blacklisted.'
                                            )
                                            printX(
                                                'Exceeded max rate or below 1')
                                    if error == 0:
                                        # store valid bet into local database
                                        try:
                                            with conn:
                                                c.execute(
                                                    '''INSERT OR IGNORE INTO percentagebets (block, txid, user, amount, bet, asset, hashServerSeed, serverSeed , clientSeed) VALUES (?,?,?,?,?,?,?,?,?)''',
                                                    (getblock, transId, sender,
                                                     finalAmount, memo, asset,
                                                     "", "", ""))
                                                printX(
                                                    'Insert to database succesfully !!!'
                                                )
                                        except Exception as e:
                                            print('ERROR INSERTING BET ' +
                                                  str(e))
                                            print(
                                                'Sorry, Your bet is invalid 1')
                                            clientTransfer(
                                                watching, sender,
                                                str(finalAmountWithAsset),
                                                'Refund invalid Bet. ' + str(
                                                    potentialBlackListChecking(
                                                        sender)) +
                                                ' more invalid bets will get your account permanently blacklisted.'
                                            )

                        else:
                            print('Generic exception !!!')
            getblock = getblock + 1
        else:
            go = False
        try:
            with conn:
                c.execute('''UPDATE last_check SET blockheight=?''',
                          (getblock, ))
        except Exception as e:
            conn.close()
            print('ERROR UPDATING BLOCKHEIGHT 2 ' + str(e))
        processActiveBet(houseEdge, referralRate)
    return getblock
Ejemplo n.º 16
0
         new_acc += 1
         new_acc_names.append(b["new_account_name"])
         new_acc_index = b["new_account_id"]
 if trx_num_needed:
     if len(what_list) <= 1 and len(what_list) <= 1: # and new_acc <= 1:
         trx_num_needed = False
     elif len(follower_names_1) == 0 or len(follower_names_0) == 0: # and new_acc <= 1:
         trx_num_needed = False
     elif len(following_names_1 + following_names_0) == len(set(following_names_1 + following_names_0)):
         trx_num_needed = False
         
 if trx_num_needed:
     if len(blocks) < 2:
         trx_num_needed = False
 if trx_num_needed:
     block = Block(blocks[0]["block_num"])
     api_calls += 1
     trx_ids = block["transaction_ids"]
     # print("%d - %d blocks" % (last_block_num, len(blocks)))
     for i in range(len(blocks)):
         trx_num = trx_ids.index(blocks[i]["trx_id"])
         blocks[i]["trx_num"] = trx_num
         # blockTrxStorage.update_trx_num(block["block_num"], block["trx_id"], block["op_num"], trx_num)
 elif new_acc > 1 and new_acc_index < 965300:
     sorted_blocks = sorted(blocks, key=lambda x:  (x["new_account_id"] is None, x["new_account_id"]))
     trx_ids = []
     for block in sorted_blocks:
         trx_ids.append(block["trx_id"])
     for i in range(len(blocks)):
         trx_num = trx_ids.index(blocks[i]["trx_id"])
         blocks[i]["trx_num"] = trx_num            
Ejemplo n.º 17
0
def scan_history(
    hive: beem.Hive,
    block_num: Optional[int] = None,
    hours_ago: Optional[timedelta] = None,
    report_freq: int = 5,
    reports=True,
    use_test_node=False,
    quiet=False,
    include_unauthorized=False,
    include_non_podping=False,
    write_csv=False,
):
    """Scans back in history timed time delta ago, reporting with report_freq
    if timed is an int, treat it as hours, if report_freq is int, treat as min"""

    # Very first transaction from Dave Testing:
    """2021-05-10 13:51:58,353 INFO root MainThread
     : Feed Updated - 2021-05-07 20:58:33+00:00
     - f0affd194524a6e0171d65d29d5c501865f0bd72
     - https://feeds.transistor.fm/retail-remix"""

    scan_start_time = datetime.utcnow()

    report_timedelta = timedelta(minutes=report_freq)

    blockchain = Blockchain(mode="head", blockchain_instance=hive)
    if block_num:
        start_time = Block(block_num)["timestamp"].replace(tzinfo=None)
    elif hours_ago:
        start_time = datetime.utcnow() - hours_ago
        block_num = blockchain.get_estimated_block_num(start_time)
    else:
        raise ValueError(
            "scan_history: block_num or --old=<hours> required sto scan history"
        )

    allowed_accounts = get_allowed_accounts()

    count_posts = 0
    pings = 0

    if reports:
        logging.info("Started catching up")

    # beem type doesn't have type hints
    # noinspection PyTypeChecker
    stream = blockchain.stream(
        opNames=["custom_json"],
        start=block_num,
        max_batch_size=50,
        raw_ops=False,
        threading=False,
    )

    post = None

    for post in stream:
        post_time = post["timestamp"].replace(tzinfo=None)
        time_dif = post_time - start_time
        time_to_now = datetime.utcnow() - post_time
        count_posts += 1
        if reports:
            if time_dif > report_timedelta:
                timestamp = post["timestamp"]
                current_block_num = post["block_num"]
                output_status(
                    timestamp,
                    pings,
                    count_posts,
                    time_to_now,
                    current_block_num=current_block_num,
                    reports=reports,
                    quiet=quiet,
                )
                start_time = post["timestamp"].replace(tzinfo=None)
                count_posts = 0
                pings = 0

        if allowed_op_id(post["id"]):
            if set(post["required_posting_auths"]) & allowed_accounts:
                count = output(post, quiet, use_test_node, write_csv,
                               "data-podping")
                pings += count
                Pings.total_pings += count
            else:
                if include_unauthorized:
                    count = output(post, quiet, use_test_node, write_csv,
                                   "data-unauthorized")
                    pings += count
                    Pings.total_pings += count
        else:
            if include_non_podping:
                output(post, quiet, use_test_node, write_csv,
                       "data-not-podping_firehose")

        if time_to_now < timedelta(seconds=2):
            timestamp = post["timestamp"]
            current_block_num = post["block_num"]
            output_status(
                timestamp,
                pings,
                count_posts,
                time_to_now,
                current_block_num=current_block_num,
                reports=reports,
                quiet=quiet,
            )
            logging.info(f"block_num: {post['block_num']}")
            # Break out of the for loop we've caught up.
            break

    if post:
        scan_time = datetime.utcnow() - scan_start_time
        logging.info(
            f"Finished catching up at block_num: {post['block_num']} in {scan_time}"
        )
Ejemplo n.º 18
0
    owner_privkey = owner_key.get_private_key()
    memo_privkey = memo_key.get_private_key()
    if useWallet:
        stm.wallet.addPrivateKey(owner_privkey)
        stm.wallet.addPrivateKey(active_privkey)
        stm.wallet.addPrivateKey(memo_privkey)
        stm.wallet.addPrivateKey(posting_privkey)
    else:
        stm = Steem(node=testnet_node,
                    wif={'active': str(active_privkey),
                         'posting': str(posting_privkey),
                         'memo': str(memo_privkey)})
    account = Account(username, steem_instance=stm)
    if account["name"] == "beem":
        account.disallow("beem1", permission='posting')
        account.allow('beem1', weight=1, permission='posting', account=None)
        account.follow("beem1")
    elif account["name"] == "beem5":
        account.allow('beem4', weight=2, permission='active', account=None)
    if useWallet:
        stm.wallet.getAccountFromPrivateKey(str(active_privkey))

    # stm.create_account("beem1", creator=account, password=password1)

    account1 = Account("beem1", steem_instance=stm)
    b = Blockchain(steem_instance=stm)
    blocknum = b.get_current_block().identifier

    account.transfer("beem1", 1, "SBD", "test")
    b1 = Block(blocknum, steem_instance=stm)
Ejemplo n.º 19
0
s = Steem("https://steemd.minnowsupportproject.org")
s = Steem("https://api.steemit.com")

#读取日志
con = sqlite3.connect('steemdatabase.db')
cur = con.cursor()
f2 = open('block.txt', 'r')
txt = f2.read()
number_block=int(txt)
print("start:",number_block)

while p==1:
    try:
        #while True:
        block = Block(number_block,steem_instance=s)
        a=block.json_transactions
        print("Block:",number_block)
        write=0

        for i in a:
            timess=i["expiration"]
            operations=i["operations"][0]
            types=operations["type"]
            if types == "delegate_vesting_shares_operation":
                write=1
                print("find:",operations)
                name = operations["value"]["delegator"]
                towho = operations["value"]["delegatee"]
                vesting_shares=operations["value"]["vesting_shares"]["amount"]
                vesting = float(vesting_shares)/1000000
Ejemplo n.º 20
0
def treat_block(no):
    global last_idb, idb, blocks_checked, blocks_treat

    # Adding the number of this block to the currently treated list
    if no not in blocks_treat:
        blocks_treat.append(no)

        # If we're missing some blocks, we launch new threads
        while no != (last_idb + 1) and (last_idb + 1) <= idb:
            if (last_idb + 1) in blocks_treat:
                time.sleep(0.2)
            else:
                threading.Thread(target=treat_block,
                                 args=[last_idb + 1]).start()
                time.sleep(0.2)

        block_valid = 0
        tries = 0

        # Extracting operations from the block
        while block_valid == 0 and tries < 10:
            try:
                # print("Treating Block %s" % no)
                block_posts = Block(no).ops()
                blocks_checked += 1
                block_valid = 1
            except Exception as e:
                print("*** ERROR IN BLOCK %s (Try %s) : %s" % (no, tries, e))
                block_valid = 0
                tries += 1
                time.sleep(0.1)

                if tries == 10:
                    print("--- Master Error on Block %s" % no)

        # If the extraction worked
        if block_valid == 1:
            for b in block_posts:
                indic_tag = 0
                indic_mention = 0

                # If the operation is a comment
                if b[0] == 'comment':
                    if b[1]["title"] != "" and b[1]["parent_author"] == "":
                        try:
                            # Getting the json_metadata
                            if b[1]['json_metadata'] != '':
                                metadata = json.loads(b[1]['json_metadata'])
                                if target_tag in metadata["tags"]:
                                    print("-> Post with tag")
                                    indic_tag = 1
                        except Exception as err:
                            pass

                    if target_tagc in b[1]['body']:
                        print("-> Body with tag")
                        indic_tag = 1

                    if target_mention in b[1]['body']:
                        print("-> Body with mention")
                        indic_mention = 1

                if indic_tag == 1 or indic_mention == 1:
                    threading.Thread(target=treat_post, args=[b,
                                                              "TAG"]).start()

                # If we've been proxied votes
                if b[0] == 'account_witness_proxy' and b[1][
                        'proxy'] == account_name:
                    print("-> Been proxied")
                    threading.Thread(target=treat_post,
                                     args=[b, "PROXY"]).start()

            # Block is now treated, we remove it from the currently treated blocks
            blocks_treat = list(filter(lambda x: x != no, blocks_treat))
            # print(blocks_treat)

            # Connecting to the database
            indic_conn = 0
            while indic_conn == 0:
                try:
                    conn = mdb.connect(MySQL_Host,
                                       MySQL_User,
                                       MySQL_Pass,
                                       MySQL_Base,
                                       charset='utf8mb4')
                    cur = conn.cursor()
                    indic_conn = 1
                except (mdb.Error, mdb.Warning):
                    time.sleep(0.25)

            # Updating the last block number
            sql = "UPDATE LAST_BLOCK SET NUM = %s WHERE UID = 1"
            try:
                cur.execute(sql, (no, ))
                last_idb = no
            except mdb.Error as e:
                print("*SQL Error : %S" % e)

            # Closing connection
            cur.close()
            conn.commit()
            conn.close()

            # print("Last Block %s" % last_idb)

            del block_posts

    return
Ejemplo n.º 21
0
        b = Blockchain(steem_instance=stm)
        blocks_per_day = 20 * 60 * 24
        if newBlockTrxStorage:
            start_block = 1091
            trx_id_list = []
        else:
            start_block = blockTrxStorage.get_latest_block_num()
            trx_id_list = blockTrxStorage.get_block_trx_id(start_block)
        if start_block is None:
            start_block = 1091
            trx_id_list = []
        end_block = b.get_current_block_num()
        end_block_round = int(start_block) + blocks_per_day - 1

        cnt = 0
        sb = Block(start_block, steem_instance=stm)
        print("start: %d - %s" % (start_block, str(sb["timestamp"])))
        end_date = datetime(sb["timestamp"].year, sb["timestamp"].month, sb["timestamp"].day, 23, 59, 50) + timedelta(seconds = 60 * 60 * 24)
        if (addTzInfo(end_date) - sb["timestamp"]).total_seconds() / 60 / 60 / 24 > 1.1:
            end_date = datetime(sb["timestamp"].year, sb["timestamp"].month, sb["timestamp"].day, 23, 59, 50)
        #end_block_round = b.get_estimated_block_num(end_date) - 1
        print("end: %d - %s" % (end_block_round, str(end_date)))
        next_account_id = accountTrxStorage.get_latest_index() + 1
        
        if end_block_round > end_block:
            end_block_round = end_block
            data_available = False
        db_data = []
        db_account = []
        db_follow = []
        accounts = accountTrxStorage.get_accounts()
Ejemplo n.º 22
0
def main(args=None):
    
    args = parse_args(args)
    blockchain = args.blockchain
    
    nodelist = NodeList()
    nodelist.update_nodes(weights={"block": 1})
    
    if blockchain == "hive" or blockchain is None:
        max_batch_size = 50
        threading = False
        thread_num = 16
        block_debug = 1000
        
        nodes = nodelist.get_hive_nodes()
        blk_inst = Hive(node=nodes, num_retries=3, num_retries_call=3, timeout=30)
    elif blockchain == "blurt":
        max_batch_size = None
        threading = False
        thread_num = 8
        block_debug = 20
        nodes = ["https://rpc.blurt.buzz/", "https://api.blurt.blog", "https://rpc.blurtworld.com", "https://rpc.blurtworld.com"]
        blk_inst = Blurt(node=nodes, num_retries=3, num_retries_call=3, timeout=30)
    elif blockchain == "steem":
        max_batch_size = 50
        threading = False
        thread_num = 16
        block_debug = 1000
        nodes = nodelist.get_steem_nodes()
        blk_inst = Steem(node=nodes, num_retries=3, num_retries_call=3, timeout=30)
    else:
        raise Exception("Wrong parameter, can be hive, blurt or steem")
    print(blk_inst)
    block_count = 0
    total_ops = 0
    total_trx = 0
    duration_s = 60 * 60 * 1
    blocksperday = int(duration_s / 3)
    
    blockchain = Blockchain(blockchain_instance=blk_inst, )
    current_block_num = blockchain.get_current_block_num()
    last_block_id = current_block_num - blocksperday

    last_block = Block(last_block_id, blockchain_instance=blk_inst)

    stopTime = last_block.time() + timedelta(seconds=duration_s)

    start = timer()
    op_stats = {}
    for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num):
        if "block" in entry:
            block_time = parse_time(entry["block"]["timestamp"])
        else:
            block_time = entry["timestamp"]
        if block_time > stopTime:
            break
        block_count += 1
        if "block" in entry:
            trxs = entry["block"]["transactions"]
        else:
            trxs = entry["transactions"]
        for tx in trxs:
            total_trx += 1
            for op in tx["operations"]:
                if "_operation" in op["type"]:
                    op_type = op["type"][:-10]
                else:
                    op_type = op["type"]
                if op_type in op_stats:
                    op_stats[op_type] += 1
                else:
                    op_stats[op_type] = 1
                total_ops += 1

        ops_per_day = total_ops / block_count * blocksperday
        if block_count % (block_debug) == 0:
            print("%d blocks remaining... estimated ops per day: %.1f" % (blocksperday - block_count, ops_per_day))

    duration = timer() - start    
    t = PrettyTable(["Type", "Count", "percentage"])
    t.align = "l"
    op_list = []
    for o in op_stats:
        op_list.append({"type": o, "n": op_stats[o], "perc": op_stats[o] / total_ops * 100})
    op_list_sorted = sorted(op_list, key=lambda x: x['n'], reverse=True)
    for op in op_list_sorted:
        t.add_row([op["type"], op["n"], "%.2f %%" % op["perc"]])
    print(t)
Ejemplo n.º 23
0
def benchmark_node(node, how_many_minutes=10, how_many_seconds=30):
    block_count = 0
    history_count = 0
    access_time = 0
    follow_time = 0
    blockchain_version = u'0.0.0'
    successful = True
    error_msg = None
    start_total = timer()
    max_batch_size = None
    threading = False
    thread_num = 16

    authorpermvoter = u"@gtg/steem-pressure-4-need-for-speed|gandalf"
    [author, permlink, voter] = resolve_authorpermvoter(authorpermvoter)
    authorperm = construct_authorperm(author, permlink)
    last_block_id = 19273700
    try:
        stm = Steem(node=node, num_retries=3, num_retries_call=3, timeout=30)
        blockchain = Blockchain(steem_instance=stm)
        blockchain_version = stm.get_blockchain_version()

        last_block = Block(last_block_id, steem_instance=stm)

        stopTime = last_block.time() + timedelta(seconds=how_many_minutes * 60)
        total_transaction = 0

        start = timer()
        for entry in blockchain.blocks(start=last_block_id, max_batch_size=max_batch_size, threading=threading, thread_num=thread_num):
            block_no = entry.identifier
            block_count += 1
            if "block" in entry:
                trxs = entry["block"]["transactions"]
            else:
                trxs = entry["transactions"]

            for tx in trxs:
                for op in tx["operations"]:
                    total_transaction += 1
            if "block" in entry:
                block_time = parse_time(entry["block"]["timestamp"])
            else:
                block_time = parse_time(entry["timestamp"])

            if block_time > stopTime:
                last_block_id = block_no
                break
            if timer() - start > how_many_seconds or quit_thread:
                break
    except NumRetriesReached:
        error_msg = 'NumRetriesReached'
        block_count = -1
    except KeyboardInterrupt:
        error_msg = 'KeyboardInterrupt'
        # quit = True
    except Exception as e:
        error_msg = str(e)
        block_count = -1

    try:
        stm = Steem(node=node, num_retries=3, num_retries_call=3, timeout=30)
        account = Account("gtg", steem_instance=stm)
        blockchain_version = stm.get_blockchain_version()

        start = timer()
        for acc_op in account.history_reverse(batch_size=100):
            history_count += 1
            if timer() - start > how_many_seconds or quit_thread:
                break
    except NumRetriesReached:
        error_msg = 'NumRetriesReached'
        history_count = -1
        successful = False
    except KeyboardInterrupt:
        error_msg = 'KeyboardInterrupt'
        history_count = -1
        successful = False
        # quit = True
    except Exception as e:
        error_msg = str(e)
        history_count = -1
        successful = False

    try:
        stm = Steem(node=node, num_retries=3, num_retries_call=3, timeout=30)
        account = Account("gtg", steem_instance=stm)
        blockchain_version = stm.get_blockchain_version()

        start = timer()
        Vote(authorpermvoter, steem_instance=stm)
        stop = timer()
        vote_time = stop - start
        start = timer()
        Comment(authorperm, steem_instance=stm)
        stop = timer()
        comment_time = stop - start
        start = timer()
        Account(author, steem_instance=stm)
        stop = timer()
        account_time = stop - start
        start = timer()
        account.get_followers()
        stop = timer()
        follow_time = stop - start
        access_time = (vote_time + comment_time + account_time + follow_time) / 4.0
    except NumRetriesReached:
        error_msg = 'NumRetriesReached'
        access_time = -1
    except KeyboardInterrupt:
        error_msg = 'KeyboardInterrupt'
        # quit = True
    except Exception as e:
        error_msg = str(e)
        access_time = -1
    return {'successful': successful, 'node': node, 'error': error_msg,
            'total_duration': timer() - start_total, 'block_count': block_count,
            'history_count': history_count, 'access_time': access_time, 'follow_time': follow_time,
            'version': blockchain_version}
Ejemplo n.º 24
0
    nodes = get_node_list(appbase=False) + get_node_list(appbase=True)
    t = PrettyTable([
        "node", "10 blockchain minutes", "10000 virtual account op", "version"
    ])
    t.align = "l"
    for node in nodes:
        print("Current node:", node)
        try:
            stm = Steem(node=node, num_retries=3)
            blockchain = Blockchain(steem_instance=stm)
            account = Account("gtg", steem_instance=stm)
            virtual_op_count = account.virtual_op_count()
            blockchain_version = stm.get_blockchain_version()

            last_block_id = 19273700
            last_block = Block(last_block_id, steem_instance=stm)
            startTime = datetime.now()

            stopTime = last_block.time() + timedelta(seconds=how_many_minutes *
                                                     60)
            ltime = time.time()
            cnt = 0
            total_transaction = 0

            start_time = time.time()
            last_node = blockchain.steem.rpc.url

            for entry in blockchain.blocks(start=last_block_id,
                                           max_batch_size=max_batch_size,
                                           threading=threading,
                                           thread_num=thread_num):
Ejemplo n.º 25
0
            payload = json.loads(trx["payload"])
            contract = trx["contract"]
            action = trx["action"]

            if action == "sell":
                if "events" not in logs:
                    continue
                elif len(logs["events"]) == 1:
                    continue
                else:
                    token_found = False
                    for transfer in logs["events"]:
                        if transfer["data"]["symbol"] in scan_token:
                            token_found = True
                    if token_found:
                        steem_block = Block(block["refSteemBlockNumber"])
                        print(
                            "%d (%s) - %s:" % (block["blockNumber"],
                                               steem_block.json()["timestamp"],
                                               trx['transactionId']))
                        print("%s sold %s %s for %s" %
                              (trx["sender"], payload["quantity"],
                               payload["symbol"], payload["price"]))
                        for transfer in logs["events"]:
                            print("    - %s transfers %s %s to %s" %
                                  (transfer["data"]["from"],
                                   transfer["data"]["quantity"],
                                   transfer["data"]["symbol"],
                                   transfer["data"]["to"]))

            elif action == "buy":
Ejemplo n.º 26
0
def main(args=None):

    args = parse_args(args)
    blockchain = args.blockchain

    nodelist = NodeList()
    nodelist.update_nodes(weights={"block": 1})

    if blockchain == "hive" or blockchain is None:
        max_batch_size = 50
        threading = False
        thread_num = 16
        block_debug = 1000

        nodes = nodelist.get_hive_nodes()
        blk_inst = Hive(node=nodes,
                        num_retries=3,
                        num_retries_call=3,
                        timeout=30)
    elif blockchain == "blurt":
        max_batch_size = None
        threading = False
        thread_num = 8
        block_debug = 20
        nodes = [
            "https://api.blurt.blog", "https://rpc.blurtworld.com",
            "https://rpc.blurtworld.com"
        ]
        blk_inst = Blurt(node=nodes,
                         num_retries=3,
                         num_retries_call=3,
                         timeout=30)
    elif blockchain == "steem":
        max_batch_size = 50
        threading = False
        thread_num = 16
        block_debug = 1000
        nodes = nodelist.get_steem_nodes()
        blk_inst = Steem(node=nodes,
                         num_retries=3,
                         num_retries_call=3,
                         timeout=30)
    else:
        raise Exception("Wrong parameter, can be hive, blurt or steem")
    print(blk_inst)
    block_count = 0
    total_ops = 0
    total_virtual_ops = 0
    total_trx = 0
    blocksperday = 20 * 60 * 24

    blockchain = Blockchain(blockchain_instance=blk_inst, )
    current_block_num = blockchain.get_current_block_num()
    last_block_id = current_block_num - blocksperday

    last_block = Block(last_block_id, blockchain_instance=blk_inst)

    stopTime = last_block.time() + timedelta(seconds=60 * 60 * 24)

    start = timer()
    for entry in blockchain.blocks(start=last_block_id,
                                   max_batch_size=max_batch_size,
                                   threading=threading,
                                   thread_num=thread_num):
        if "block" in entry:
            block_time = parse_time(entry["block"]["timestamp"])
        else:
            block_time = entry["timestamp"]
        if block_time > stopTime:
            break
        block_count += 1
        if "block" in entry:
            trxs = entry["block"]["transactions"]
        else:
            trxs = entry["transactions"]
        for tx in trxs:
            total_trx += 1
            for op in tx["operations"]:
                total_ops += 1

        ops_per_day = total_ops / block_count * blocksperday
        if block_count % (block_debug) == 0:
            print("%d blocks remaining... estimated ops per day: %.1f" %
                  (blocksperday - block_count, ops_per_day))

    duration = timer() - start

    stopTime = last_block.time() + timedelta(seconds=60 * 60 * 24)
    start = timer()
    for entry in blockchain.blocks(start=last_block_id,
                                   max_batch_size=max_batch_size,
                                   threading=threading,
                                   thread_num=thread_num,
                                   only_virtual_ops=True):
        block_time = entry["timestamp"]
        if block_time > stopTime:
            break
        for tx in entry["operations"]:
            for op in tx["op"]:
                total_virtual_ops += 1

    duration = timer() - start

    print("Received %.2f blocks/s." % (block_count / duration))
    print("Bocks: %d, duration %.3f s" % (block_count, duration))
    print("Operations per day: %d" % total_ops)
    print("Trx per day: %d" % total_trx)
    print("Virtual Operations per day: %d" % total_virtual_ops)