update for beem: transaction id is returned when broadcasting

in #hive-1395313 months ago (edited)

Repository

https://github.com/holgern/beem


beem-logo

beem is a python library and command line tool for HIVE. The current version is 0.24.2.

There is also a discord channel for beem: https://discord.gg/4HM592V

The newest beem version can be installed by:

pip install -U beem

Check that you are using hive nodes. The following command

beempy updatenodes --hive

updates the nodelist and uses only hive nodes. After setting hive as default_chain, beempy updatenodes can be used.

The list of nodes can be checked with

beempy config

and

beempy currentnode

shows the currently connected node.

Changelog for versions 0.24.2

  • New UnknownTransaction exception that is raised when using get_transaction with an unknown trx_id
  • New function is_transaction_existing which returns false, when a trx_id does not exists
  • beempy info does not show information for a trx_id
  • broadcast from TransactionBuilder can now return a trx_id, when set trx_id to True (default)
  • sign and finalizeOp from Hive and Steem return now the trx_id in a field
  • add export parameter to all broadcast commands in beempy
  • When setting unsigned in beempy, the default value of expires is changed to 3600
  • beempy history returns account history ops in a table or stored in a json file

Transaction_id is added to the returned dict when broadcasting

The transaction id is now returned in the returned dict on sign() and finalizeOp() in Hive and Steem.

This allows it to easily check if an operation could successfully send.

beempy upvote -w 25 @hivehustlers/hivehustlers-say-hello-to-usdhustlerm-power-up-sundays

returns now

{
    "expiration": "2020-06-21T19:38:48",
    "ref_block_num": 60836,
    "ref_block_prefix": 3155918752,
    "operations": [
        [
            "vote",
            {
                "voter": "holger80",
                "author": "hivehustlers",
                "permlink": "hivehustlers-say-hello-to-usdhustlerm-power-up-sundays",
                "weight": 2500
            }
        ]
    ],
    "extensions": [],
    "signatures": [
        "2024c3ab9ecd69b010a414114cc956f517d9d664dd24d728829fcebcf9f89b885571099549cb3f537ede0190c638bd0906acbd07994c1d289664236f2ee93b7765"
    ],
    "trx_id": "68db2522d1724f02430788e37b27694cbf7eb3f4"
}

The same result could be obtained by

from beem.comment import Comment
c= Comment("@dustsweeper/dustsweeper-s-faq")
c.blockchain.wallet.unlock("my_pass")
ret = c.upvote(weight=25, voter="holger80")
print(ret["trx_id"])

which returns

6056e3d1d26bc4c2c637984c21aa841b7b3ea1a4

We can now check both trx_ids and verify that my two votes were successfully broadcasted.

from beem.blockchain import Blockchain
b = Blockchain()
trx1 = b.get_transaction("68db2522d1724f02430788e37b27694cbf7eb3f4")
print("Vote was broadcasted at block %d" % trx1["block_num"])
trx2 = b.get_transaction("6056e3d1d26bc4c2c637984c21aa841b7b3ea1a4")
print("Vote was broadcasted at block %d" % trx2["block_num"])

returns

Vote was broadcasted at block 44494269
Vote was broadcasted at block 44494376

This allows it to check if a transaction was broadcasted without the need to parse all blocks.

The information about the trx_id was also available in previous versions, but only when the transaction was build and send by hand with TransactionBuilder.

beempy info can now be used to obtain information about a trx_id

The info command is now able to return information about a trx_id.

beempy info 68db2522d1724f02430788e37b27694cbf7eb3f4

Account history check with beempy

beempy history

can be used to check if a transaction could be broadcasted.

beempy history -n 2

returns now

+----+---------------------------------------------------------------------------+
| Nr | Hist op                                                                   |
+----+---------------------------------------------------------------------------+
| 1  | {                                                                         |
|    |     "voter": "holger80",                                                  |
|    |     "author": "dustsweeper",                                              |
|    |     "permlink": "dustsweeper-s-faq",                                      |
|    |     "weight": 2500,                                                       |
|    |     "trx_id": "6056e3d1d26bc4c2c637984c21aa841b7b3ea1a4",                 |
|    |     "block": 44494376,                                                    |
|    |     "trx_in_block": 11,                                                   |
|    |     "op_in_trx": 0,                                                       |
|    |     "virtual_op": 0,                                                      |
|    |     "timestamp": "2020-06-21T19:43:42",                                   |
|    |     "account": "holger80",                                                |
|    |     "type": "vote",                                                       |
|    |     "_id": "d442e4c0b703020f99e7b739f37527fa9477ffa7",                    |
|    |     "index": 173814                                                       |
|    | }                                                                         |
| 2  | {                                                                         |
|    |     "voter": "holger80",                                                  |
|    |     "author": "hivehustlers",                                             |
|    |     "permlink": "hivehustlers-say-hello-to-usdhustlerm-power-up-sundays", |
|    |     "weight": 2500,                                                       |
|    |     "trx_id": "68db2522d1724f02430788e37b27694cbf7eb3f4",                 |
|    |     "block": 44494269,                                                    |
|    |     "trx_in_block": 10,                                                   |
|    |     "op_in_trx": 0,                                                       |
|    |     "virtual_op": 0,                                                      |
|    |     "timestamp": "2020-06-21T19:38:18",                                   |
|    |     "account": "holger80",                                                |
|    |     "type": "vote",                                                       |
|    |     "_id": "6e20f474ff58dcd188763339820105bdbf59a1d4",                    |
|    |     "index": 173813                                                       |
|    | }                                                                         |
+----+---------------------------------------------------------------------------+

The ``history` command can also be used to store the entiry account history as json file:

beempy history --number -1 --order 1 --virtual-ops --json-file my_hist.json holger80

where --number -1 returns all ops (otherwise it is used to limit the number of returned ops), --order 1 returns the ops in the correct order, --virtual-ops returns also virtual ops and --json-file my_hist.json stores the ops in a json file.

The stored ops in a json file can then be loaded and processed, e.g. with excel or similar tools.

It is also possible to limit the returned ops by setting one ore more op types with for example--only-ops transfer,vote.

Transaction can be exported to a file with -e or --export in beempy

All beempy commands that broadcast something to the chain have now a --export parameter.

This can be used to do offline signing more easily.

beempy -dx upvote --export vote.json -w 25 @paragism/my-thoughts-on-yield-farming-and-bat-pump

The -dx parameter set the expiration time to a new default value of 3600 seconds, meaning I have 1 hour to sign and broadcast my transaction.

Now I can sign vote.json:

beempy sign -i vote.json -o vote_signed.json

(I do not need to enter my username, as I set holger80 as default account).

Due to the new changes, vote_signed.json has now a new field with the trx_id:

"trx_id": "c3306c892edf0528b09b1da9c003bca0a0dd7088"

I can now broadcast the trx:

beempy broadcast -f vote_signed.json

After a few seconds

beempy info c3306c892edf0528b09b1da9c003bca0a0dd7088

is returning details about the trx.

How to check if a transaction was successfully broadcasted and retry when not

That a transaction could be broadcasted without error message is no guarantee that the transaction is written into the hive chain. There is a small probability that a block could not be signed by a witness in time or is rejected because of an invalid operation inside the block. Thus it is always a good idea to check if the transaction was written into the chain.

The new trx field can be used to easily build a loop in which it is checked if the trx_id is valid and known. When this is not the case, the transaction is broadcasted again:

from beem.blockchain import Blockchain
from beem.comment import Comment
import time
b = Blockchain()
c= Comment("@eveuncovered/roadtrip-east-varkaus")
c.blockchain.wallet.unlock("my_pass")
trx_id_found = False
count = 0
while not trx_id_found and count < 5:
    count += 1
    delay = 0
    print("broadcasting trx")
    ret = c.upvote(weight=25, voter="holger80")
    trx_id = ret["trx_id"]
    print("Trx_id: %s" % trx_id)
    while not trx_id_found and delay < 120:
        print("checking trx_id...")
        trx_id_found = b.is_transaction_existing(trx_id)
        time.sleep(10)
        delay += 10
if not trx_id_found:
    print("Could not broadcast!")
else:
    print("Transaction is written into the chain!")

returns

broadcasting trx
Trx_id: d3df7b16b2e8e21a2084af9c21775ea697d0d8a1
checking trx_id...
checking trx_id...
checking trx_id...
checking trx_id...
checking trx_id...
checking trx_id...
Transaction is written into the chain!

With the help of the new is_transaction_existing function it is quite easy to check if a transaction was successfully broadcasted.


If you like what I do, consider casting a vote for me as witness on Hivesigner or on PeakD

Sort:  
Loading...

Great addition to the functionality, will be a great helper to make sure things run smoothly.

As always thanks for what you do.

Also the witness comment at the end worked! I went back and checked and did not have you voted. You are now!

Congratulations @holger80! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

Your post got the highest payout of the week

You can view your badges on your board And compare to others on the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Support the HiveBuzz project. Vote for our proposal!

Hi @holger80, you have received a small bonus upvote from MAXUV.
This is to inform you that you now have new MPATH tokens in your Hive-Engine wallet.
Please read this post for more information.
Thanks for being a member of both MAXUV and MPATH!