substrate-node-template icon indicating copy to clipboard operation
substrate-node-template copied to clipboard

Runtime without Frame

Open JoshOrndorff opened this issue 3 years ago • 4 comments

This is a revival of a project I started a long time ago but never finished. (https://github.com/JoshOrndorff/recipes/pull/144)

This demonstrates a simple runtime written entirely without FRAME. It directly implements the necessary runtime APIs. The Runtime's logic is quite simple. It just maintains a single bit of state. The bit can be set, cleared, or toggled by anyone. Transactions require the user (or their wallet) to provide a salt to de-duplicate transactions.

# Run the node in the usual way
node-template --dev`


# In another terminal, check that the state starts cleared. (result: 0x00)
curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d   '{
       "jsonrpc":"2.0",
        "id":1,
        "method":"state_getStorage",
        "params": ["0x626F6F6C65616E"]
      }'
{"jsonrpc":"2.0","result":"0x00","id":1}

# Submit a Toggle transaction with salt 3 signed by alice
curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d   '{
       "jsonrpc":"2.0",
        "id":1,
        "method":"author_submitExtrinsic",
        "params": ["0x02032a51e39e68e0736b93a80d84d6d9fcb77f60d0d87e158e48191e3a00e1aa647d303b50c1fc15a89650a84af6273635697888fba039b53a38500935d6863d5c82d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"]
      }'
{"jsonrpc":"2.0","result":"0xa9b156e072f43817c923de3184a6f2934558ec3cdf81ec07b5160dca63633b2a","id":1}

# Check the state again and notice that the state has been set. (result: 0x01)
curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d   '{
       "jsonrpc":"2.0",
        "id":1,
        "method":"state_getStorage",
        "params": ["0x626F6F6C65616E"]                                                                                                                                                                                      
      }'
{"jsonrpc":"2.0","result":"0x01","id":1}

JoshOrndorff avatar May 20 '22 19:05 JoshOrndorff

Woah! I think it actually included a transaction!

# Submit the transaction `0x0004` via RPC
$ curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d   '{
       "jsonrpc":"2.0",
        "id":1,
        "method":"author_submitExtrinsic",
        "params": ["0x0004"]
      }'
{"jsonrpc":"2.0","result":"0x7324cf9181f377b38e2edeca7a080796554246690656566def41afedef911d0d","id":1}

# Node output in other terminal
2022-05-20 15:56:57 ✨ Imported #63 (0xd48c…7181)    
2022-05-20 15:57:00 šŸ™Œ Starting consensus session on top of parent 0xd48c750a262f4acc203a844cd5af5ecf97066de782053e1a4476af74b4b27181    
2022-05-20 15:57:00 šŸŽ Prepared block for proposing at 64 (0 ms) [hash: 0xba15a9830087d539ca0681bd229f9c6a6053df3117e7417eb042d2928ebdb442; parent_hash: 0xd48c…7181; extrinsics (1): [0x7324…1d0d]]    
2022-05-20 15:57:00 šŸ”– Pre-sealed block for proposal at 64. Hash now 0x204d979504f6eb866a00ec5630e544dcae7e44ec1c4008034935b4bb9d2db467, previously 0xba15a9830087d539ca0681bd229f9c6a6053df3117e7417eb042d2928ebdb442. 

I'm pretty sure that never worked before! cc @kianenigma

Next step is to see if the state actually updated.

JoshOrndorff avatar May 20 '22 19:05 JoshOrndorff

Wow, indeed the storage is updated! I can hardly believe it.

curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d   '{
       "jsonrpc":"2.0",
        "id":1,
        "method":"state_getStorage",
        "params": ["0x626F6F6C65616E"]
      }'
{"jsonrpc":"2.0","result":"0x01","id":1}

Next step: better provides tags or some kind of nonce or salt so transactions don't get temporarily banned.

Also, I think my set and clear variants might be swapped or something.

JoshOrndorff avatar May 20 '22 20:05 JoshOrndorff

A signed transaction actually worked!

JoshOrndorff avatar May 30 '22 07:05 JoshOrndorff

So I just noticed something weird here. I can start two nodes, and they will peer together. But the moment that one of the authors a block, the un-peer. No interesting logs from the non-authoring node. I would expect it to at least print the log that it was in the execute block function. But even that doesn't happen. I'm pretty sure I tested that before, but I don't see any notes about it above. :-/

I'm not sure how to debug that.

JoshOrndorff avatar Aug 02 '22 23:08 JoshOrndorff

@JoshOrndorff I think you had work for the https://polkadot.network/academy/ related to a substrate based frameless runtime that was a stand-alone repo... is that related to this work and also public? Can't find it if so, only in the private (planned public first half this year for sure) content repo. https://github.com/Polkadot-Blockchain-Academy/pba-content/tree/main/syllabus/4-Substrate/3-Runtime_and_Host_Functions/frameless-node-template

Asking as if work is concentrated in that priv repo, can this PR be closed? Or are they separate things?

nuke-web3 avatar Nov 19 '22 19:11 nuke-web3

I don't have this in a standalone repo. Someone copied it from here into the pba content repo. You may close this PR.

JoshOrndorff avatar Nov 20 '22 18:11 JoshOrndorff