Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
beem
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hive
beem
Commits
9574ab6b
Commit
9574ab6b
authored
8 years ago
by
Fabian Schuh
Browse files
Options
Downloads
Patches
Plain Diff
[blockchain] ops, stream and blocks calls
parent
4a375a3f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bitshares/blockchain.py
+102
-8
102 additions, 8 deletions
bitshares/blockchain.py
with
102 additions
and
8 deletions
bitshares/blockchain.py
+
102
−
8
View file @
9574ab6b
import
time
from
.block
import
Block
from
.block
import
Block
from
.
import
bitshares
as
bts
from
.
import
bitshares
as
bts
from
.utils
import
parse_time
from
.utils
import
parse_time
...
@@ -31,6 +32,14 @@ class Blockchain(object):
...
@@ -31,6 +32,14 @@ class Blockchain(object):
"""
"""
return
self
.
bitshares
.
rpc
.
get_dynamic_global_properties
()
return
self
.
bitshares
.
rpc
.
get_dynamic_global_properties
()
def
chainParameters
(
self
):
return
self
.
config
()[
"
parameters
"
]
def
config
(
self
):
"""
Returns object 2.0.0
"""
return
self
.
bitshares
.
rpc
.
get_object
(
"
2.0.0
"
)
def
get_current_block_num
(
self
):
def
get_current_block_num
(
self
):
"""
This call returns the current block
"""
This call returns the current block
...
@@ -42,19 +51,104 @@ class Blockchain(object):
...
@@ -42,19 +51,104 @@ class Blockchain(object):
"""
"""
return
Block
(
self
.
get_current_block
(
self
.
mode
))
return
Block
(
self
.
get_current_block
(
self
.
mode
))
def
blocks
(
self
,
**
kwargs
):
def
block_time
(
self
,
block_num
):
"""
Yield Blocks as a generator
"""
Returns a datetime of the block with the given block
number.
:param int start: Start at this block
:param int block_num: Block number
"""
return
Block
(
block_num
).
time
()
def
block_timestamp
(
self
,
block_num
):
"""
Returns the timestamp of the block with the given block
number.
:param int block_num: Block number
"""
return
int
(
Block
(
block_num
).
time
().
timestamp
())
def
blocks
(
self
,
start
=
None
,
stop
=
None
):
"""
Yields blocks starting from ``start``.
:param int start: Starting block
:param int stop: Stop at this block
:param int stop: Stop at this block
:param str mode: We here have the choice between
*
"
head
"
: the last block
*
"
irreversible
"
: the block that is confirmed by 2/3 of all block producers and is thus irreversible!
"""
"""
return
self
.
bitshares
.
rpc
.
block_stream
(
**
kwargs
)
# Let's find out how often blocks are generated!
block_interval
=
self
.
chainParameters
().
get
(
"
block_interval
"
)
if
not
start
:
start
=
self
.
get_current_block_num
()
# We are going to loop indefinitely
while
True
:
# Get chain properies to identify the
head_block
=
self
.
get_current_block_num
()
# Blocks from start until head block
for
blocknum
in
range
(
start
,
head_block
+
1
):
# Get full block
block
=
self
.
bitshares
.
rpc
.
get_block
(
blocknum
)
yield
{
**
block
,
"
block_num
"
:
blocknum
}
# Set new start
start
=
head_block
+
1
if
stop
and
start
>
stop
:
break
# Sleep for one block
time
.
sleep
(
block_interval
)
def
ops
(
self
,
start
=
None
,
stop
=
None
,
**
kwargs
):
"""
Yields all operations (including virtual operations) starting from ``start``.
:param int start: Starting block
:param int stop: Stop at this block
:param str mode: We here have the choice between
*
"
head
"
: the last block
*
"
irreversible
"
: the block that is confirmed by 2/3 of all block producers and is thus irreversible!
:param bool only_virtual_ops: Only yield virtual operations
This call returns a list with elements that look like
this and carries only one operation each:::
{
'
block
'
: 8411453,
'
op
'
: [
'
vote
'
,
{
'
author
'
:
'
dana-edwards
'
,
'
permlink
'
:
'
church-encoding-numbers-defined-as-functions
'
,
'
voter
'
:
'
juanmiguelsalas
'
,
'
weight
'
: 6000}],
'
timestamp
'
:
'
2017-01-12T12:26:03
'
,
}
"""
for
block
in
self
.
blocks
(
start
=
start
,
stop
=
stop
,
**
kwargs
):
for
tx
in
block
[
"
transactions
"
]:
for
op
in
tx
[
"
operations
"
]:
yield
{
"
block
"
:
block
[
"
block_num
"
],
"
op
"
:
op
,
"
timestamp
"
:
block
[
"
timestamp
"
]
}
def
operations
(
self
,
**
kwargs
):
def
stream
(
self
,
opNames
=
[],
*
args
,
**
kwargs
):
"""
Yield specific operations
as a generator
"""
Yield specific operations
(e.g. comments) only
:param array opNames: List of operations to filter
by
:param array opNames: List of operations to filter
for
:param int start: Start at this block
:param int start: Start at this block
:param int stop: Stop at this block
:param int stop: Stop at this block
:param str mode: We here have the choice between
*
"
head
"
: the last block
*
"
irreversible
"
: the block that is confirmed by 2/3 of all block producers and is thus irreversible!
"""
"""
return
self
.
bitshares
.
rpc
.
stream
(
**
kwargs
)
for
op
in
self
.
ops
(
**
kwargs
):
if
not
opNames
or
op
[
"
op
"
][
0
]
in
opNames
:
yield
op
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment