update for beem: create posts more easily and draw pseudo-random numbers

in #hive-1395314 months ago

Repository

https://github.com/holgern/beem


beem-logo

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

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.23.10

  • get_node_answer_time added to NodeList
  • New node added
  • new stored parameter: default_canonical_url
  • beempy notifications sorting is reversed, a new parameter can be used to change the sorting
  • New beempy createpost command, it can be used to create an empty markdown file with YAML header for a new post
  • beempy post has now a canonical_url parameter, when not set, default_canonical_url is set
  • New beempy draw command, can be used to generate pseudo-random number from block identifiers using hashsums
  • remove enum34 dependency

Changelog for version 0.23.11

  • replace asn1 by asn1crypto

Setting canonical URLs when posting with beempy

The canonical_url field in the json_metadata of a post is part of an open standard that defines which canonical URL should be set by all hive front-ends.

The canonical URL is then parsed by the search engines and shown in search results.

A default canonical URL can now be set with

beemy set default_canonical_url https://hive.blog

When not modified, https://hive.blog is the default value. When posting now with beempy post this canonical URL is set in the json_metadata field.

It is also possible to set a canonical URL by:

beempy post --canonical-url https://hive.blog my_post.md

Create a new markdown file for posting

The following command

beempy createpost my_new_post.md

can be used to create an empty markdown file with a YAML header.
After executing the command, beempy asks the following questions:
beempy createpost
and creates the following YAML header in the created markdown file:
Resulting YAML header

Draw pseudo-random numbers and post the result as a comment

beempy draw can be used to generate pseudo-random numbers based on a seed which is generated as follows:

    if hashtype == "md5":
        seed = hashlib.md5((trx_id + block["block_id"] + block["previous"]).encode()).hexdigest()
    elif hashtype == "sha256":
        seed = hashlib.sha256((trx_id + block["block_id"] + block["previous"]).encode()).hexdigest()
    elif hashtype == "sha512":
        seed = hashlib.sha512((trx_id + block["block_id"] + block["previous"]).encode()).hexdigest()

beempy draw uses three entropy sources: the hash of the last block, the hash of the current block and the hash of a transaction inside the current block.

When using md5 as hashtype, the seed is used to initialize the random generator:

random.seed(a=seed, version=2)
number = int(random.random() * len(draw_list))

This is e.g. used in splinterlands to generate random numbers.

When using sha256 or sha512, ,<draw_number> is added to the seed and a pseudo-random number is generated by

bigRand = int.from_bytes(seed, 'big')
number = bigRand % (len(draw_list))

It is possible to choose the separator by using -s parameter.

The results for sha256 can be validated here.

Usage

The following command uses the latest block number and the last transaction for building the seed and draw three number of 1000 different ones.

beempy draw -d 3 -p 1000

creates the following output

+--------------+------------------------------------------+
| Key          | Value                                    |
+--------------+------------------------------------------+
| block number | 43892950                                 |
| trx id       | 38206e28dcbf15d8f9ce1be907fb60a99d427171 |
| block id     | 029dc0d6a1fe487b5f2907988ba72b3ba495589b |
| previous     | 029dc0d55023bdf2f6450083c52349c4cd838f65 |
| hash type    | sha256                                   |
| draws        | 3                                        |
| participants | 1000                                     |
| 1. draw      | 406                                      |
| 2. draw      | 568                                      |
| 3. draw      | 186                                      |
+--------------+------------------------------------------+

We can validate the results by adding the value of trx id, block id and previous in this order into the seed field of this pseudo-random generator:

We can see that the same numbers were generated.

The results can also be validated by

beempy draw -d 3 -p 1000 -b 43892950 -t 38206e28dcbf15d8f9ce1be907fb60a99d427171

Adding --markdown or -m creates a markdown output which can directly copied into a post. The command above with -m generates:

The following results can be checked with:

beempy draw -d 3 -p 1000 -b 43892950 -t 38206e28dcbf15d8f9ce1be907fb60a99d427171 -h sha256 -s ','
keyvalue
block number43892950
trx id38206e28dcbf15d8f9ce1be907fb60a99d427171
block id029dc0d6a1fe487b5f2907988ba72b3ba495589b
previous id029dc0d55023bdf2f6450083c52349c4cd838f65
hash typesha256
draws3
participants1000
1. draw406
2. draw568
3. draw186

Posting the results directly as reply

This can be used to directly publish results in a comment.

beempy draw -d 3 -p 10 --reply @user/my-post

@user/my-post is the parent post to which the comment is replied.

Prevent cheating

A user could rerun beempy draw until the results are fitting. This can be prevented by defining previously which future block number is used for generating the pseudo-random numbers.

Or a future broadcast is used as reference, e.g. the next post. After broadcasting a new post, the trx-id and the block number can be checked and provided to ´beempy draw´.

Creating a participants list to draw user from a list

beempy draw can be used to draw account names from a list of accounts. The participants can be stored in a text file where each line is one participant.

In order to draw one account, I can store all participating accounts in a file:

@holger80
@beembot
@beempy

A participant can now be selected by:

beempy draw -p account_list.txt

which results in

+--------------+------------------------------------------+
| Key          | Value                                    |
+--------------+------------------------------------------+
| block number | 43893720                                 |
| trx id       | c12b9911d6c9d950f02700ae70c066e105c40aba |
| block id     | 029dc3d8bc1518ad8a92c6e9fd2cce4e64095fb2 |
| previous     | 029dc3d7d7b53b31b7d389a0cc9a84e8352ddb19 |
| hash type    | sha256                                   |
| draws        | 1                                        |
| participants | 3                                        |
| 1. draw      | 2 - @beembot                             |
+--------------+------------------------------------------+

The order of the participants should be clear (e.g. ordered by date or ordered by letter) and defined previously.

When each participant should only be drawn once, the --without-replacement or -w parameter must be used.

Feedback

Do you have some new ideas or improvements for beem or beempy? Let me know.


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

Sort:  

Your hive draw looks a lot more sophisticated than the one I made.
It should proove to be very useful

I'll have to give it a try soon.

One thing I noticed is you don't add +1 after calculating the modulo.

So it looks like you are calculating from 0 to n-1 with position 1 actually being 0. That or it is impossible for the top number to be called. I know you called .len but I'm not sure exactly how it works (I'm still just really noob).

The following results can be checked with:

beempy draw -d 5 -p 2 -b 43903393 -t 15bc57129f215b93857eeea7935538d56b1a9223 -h sha256 -s ','
keyvalue
block number43903393
trx id15bc57129f215b93857eeea7935538d56b1a9223
block id029de9a1e968a91ef4819726354ca60f8c3fe4bc
previous id029de9a04d988326c0b85e79fd16b69b6817c299
hash typesha256
draws5
participants2
1. draw2
2. draw2
3. draw1
4. draw1
5. draw2

As you can see the last position will also be called.

I'm just added +1 to all numbers. So the number are going from 1 to participants.

The following results can be checked with:

beempy draw -d 3 -p 100 -b 43908154 -t 52840f5017464772a9a940a6ecfeb268d0a95def -h sha256 -s ','
keyvalue
block number43908154
trx id52840f5017464772a9a940a6ecfeb268d0a95def
block id029dfc3a5273dd08bd9ada6426205ac7c5d1dd0c
previous id029dfc39a4961c8b6dc21da20c7b8d598e963dc8
hash typesha256
draws3
participants100
1. draw79
2. draw82
3. draw97

That's an easy way to check and to flip a coin.

Everytime you post updates i feel like beem has been made for me to use. I need to start with the SE amd HE thing and will read all your documentation and will use it more frequently. Is there a way to "embed" beem in other apps?? If yes are there specific files i must include in my root folder from beem repo?

Beem is a python package, which means there is no need to copy files from the repo. Embedding beem in other python apps is normally done by adding beem as a dependency in the setup.py file.

I found what I was looking for. Every day I learn python and already within this community hive I set out to learn everything related. I didn't know about beem. Thank you!

@holger80 . I took the initial steps to get into the beem studio. And I have a question.

4.png

I did it all in my virtual environment, but I see at the end:

data_dir "C": Rafael Aquino "AppData" Local "beem".

It will bring me some trouble in the future. I'm a learner and I'm eager to learn.
thanks!


Excuse my English, I'm using the translator. In the future I want to make a publication about beem for the Spanish speaking community


great for this i like it

This is another interesting update on your been project, and we will feature this post on today's Gitplait elites post. Thanks for your awesome works!

You are an elite

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 day

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

Do not miss the last post from @hivebuzz:

Project Activity Update
Support the HiveBuzz project. Vote for our proposal!

This is wonderful

Good to know that! Thanks 🙏

I making extra $19k or more every month from home by doing very simple and easy job online from home. I have received e xactly $20845 last month from this home job. Join now this job and start making extra cash online by follow instruction on the given website...... www.jobnews3.com