How do I access the block height from inside a rust smart contract, please?


I would like to do some calculations regarding the current block height in my smart contract but I am struggling to find out how to locate the information.

Can anyone help, please?

You can get the current timestamp of the block within the smart contract code. That might be similar to the block height if you know the block-producing frequency.

1 Like

What happens if the block producing frequency is not knowable in this specific case?

You can observe the current blockchain to get an estimate of the block-producing frequency. Usually, I would suggest using the timestamp in smart contract logic. Maybe you can explain more in detail, why your current case needs block_height and cannot work with timeStamp? Concordium does not expose the block_height within the smart contract code.

1 Like

I do not think there is anything particularly elaborate in the system. We were asked to provide the option on our interface to limit interactions with our smart contract by block number.

However, it sounds like this might not be the standard way of doing things? If that is the case I might go back and query the requirements to ensure everyone understands what is involved.

cool, please feel free to reach out again if timestamp does not work for your use case. For now, timestamp would be the value that could be used out of the box in the smart contract.

1 Like

As @Doris stated already only timestamp is exposed.

We don’t expose block height since that should be deemed implementation detail of the chain, rather than something contracts should care about. Time seems to be the natural notion here, and note that time is “objective” and strictly increasing, in the sense that a parent of a block will have time strictly less than the current block.

The requirement “to limit by block number” sounds like it would be equally well covered by requirement to limit interaction to, e.g., at most once an hour.

This also allows flexibility in the future where block height might not be well-defined, e.g., if the underlying structure is no longer a “chain” of blocks.

1 Like

Great. Thanks all :slight_smile: