near-lake-framework-rs icon indicating copy to clipboard operation
near-lake-framework-rs copied to clipboard

refactor(high-level): Don't require `&mut Block` to improve the API for developers

Open khorolets opened this issue 7 months ago • 1 comments

Recently, I've received feedback about the usage of high-level updates.

It is impossible to use the framework like this:

for action in block.actions() {
        if action.receiver_id() == ctx.contract_id {
            let receipt = block.receipt_by_id(&action.receipt_id()).unwrap();
...

The reason is that we require the block to be &mut Block. I did it because I attempted to keep the Block size lower. I intended to empty some fields (receipts, transactions, state_changes, etc.) and set the value to them on demand. For example, a developer does block.transactions(). We extract the transactions from the underlying block if the value is empty.streamer_message: StreamerMessage. While I still believe that idea makes some sense, this adds inconveniences to other places. It does not make sense to attempt to save a small amount of bytes and ruin the DevEx.

In this PR, I made the Block structure to have all the data at the time of the conversion from the StreamerMessage. This allows me to eliminate the requirement to deal with &mut Block and makes the code above possible to use without redundant clone().

Feedback is welcome, meanwhile, I need to revisit the doc side for the leftover referrals to the &mut Block and correct them.

khorolets avatar Nov 23 '23 10:11 khorolets

I talked to @frol, and we agreed that parsing events and storing them in the Block is the heaviest operation that most developers might not need.

Instead of parsing all the logs for the events, we will do it on demand when Block.events() is called. Developers might build the cache layer for the events on top if needed, the framework is not going to handle it.

khorolets avatar Dec 08 '23 11:12 khorolets