# Blocks

For Terra, a new block is produced around every 6-8 seconds and includes all transactions and events that were processed by its proposing validator. Once the block is appended to the chain, it is considered finalized and all nodes connected to the network sync up and apply the state changes described in its body.

# Block Schema

# Fetching Blocks

You can access block information directly from the Terra instance using height as the identifier.

# Get block by height

Terra.block(height)
Terra.blocks.at(height)
block = terra.block(5)
block.height == 5 # -> True

# Iterate over blocks

Terra.blocks

You can process all the blocks by iterating over .blocks query.

for block in terra.blocks:
    print(block.height)
# 1
# 2
# ...
# block height at time of calling
Terra.blocks[int]

Using an integer index will let you get the block at height x + 1.

terra.blocks[0] # get first block, block height = 1
terra.blocks[250] # get 251st block, block height = 251
terra.blocks[-1] # get last block at time of calling
terra.blocks[-100] # get 100th last block at time of calling
Terra.blocks[slice]

You can use a slice to get a range of blocks. Note that the range specified will be [start, stop), following Python's conventions for ranges. This returns a BlocksRange, which is a generator and will not fetch the blocks until it is iterated over.

for block in terra.blocks[100:200]:
    print(block.height)
# 101
# 102
# ...
# 199

for block in terra.blocks[100:200:2]:
    print(block.height)
# 101
# 103
# ...
# 199

terra.blocks[:200] # -> BlocksRange for first 200 blocks

terra.blocks[-25:] # -> BlocksRange for last 25 blocks

# Parameters

int
height

Height of the block to query.


# Returns

Block

Block at the desired height.

Terra.blocks(start_height, stop_height, step=1, threads=1)

You can get the blocks within the specified inclusive range [start_height, stop_height], which will get all of the blocks in between, including the limits. Note that a BlocksRange generator will be returned, which will not be run until iterated over (lazy).



# Parameters

int
start_height

Height of the block to query.


int
stop_height

Height of the block to query.


int
step
optional

Incremental step amount for iteration.


DEFAULT 1
int
threads
optional

Amount of simultaneous threads to use to fetch blocks.


DEFAULT 1

# Returns

BlocksRange

A generator for the range of blocks specified.

Updated on: 3/11/2020, 8:43:58 PM