0

I'm trying to test offchain workers in my local substrate node. It seems that by default the ocw is not enabled? I've only been able to see logs from ocw's when running the node with the --dev option (e.g. when using --chain local there are no ocw logs) - but AFAIU --dev will not persist the chain state nor the keystore when I run it multiple times.

I would definitely like the keys to persist, since my ocw needs to submit unsigned tx's with:

        let (_, result) = Signer::<T, T::AuthorityId>::any_account()
            .send_unsigned_transaction(
                |account| SomePayload {
                    value: value.clone(),
                    public: account.public.clone(),
                },
                |payload, signature| Call::submit_value_from_unsigned_transaction {
                    some_payload: payload,
                    signature,
                },
            )
            .ok_or("No local accounts accounts available.")?;

I also define the key for ocw to be pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"abcd");, so I will need to generate a key and add it to keystore, right? Well I don't want to be adding it to keystore everytime I make a change to my node and rerun it with --dev flag.

What's the best practice for testing whether my ocw functions as expected? I suppose that my question encompasses two questions really:

  • how to add keys to the keystore such that they persist
  • how to run the local node s.t. the ocw is enabled AND it picks up the added keys, without having to re-add them every time?

Many thanks for help on this one!

3 Answers 3

1

So go to node -> service and inside new_partial fn add:

if config.offchain_worker.enabled{
    sp_keystore::SyncCryptoStore::sr25519_generate_new(
        &*keystore,
        node_template_runtime::<your-pallet-name>::KEY_TYPE,
        Some("//Alice"),
    ).expect("Creating key with account Alice should work");
}

Example: https://github.com/MrishoLukamba/sub-harbour/blob/ml-offchain-workers/node/src/service.rs

Then every time an offchain-worker runs (e.g. with --dev option), the specified Alice key will be generated & added to the keystore.

1
  • With Polkadot-v1.0.0 and sp-keystore version = "0.27.0": there is no ` SyncCryptoStore` in sp_keystore. Use sp_keystore::Keystore::sr25519_generate_new Sep 11, 2023 at 7:49
1

For a more production-like environment, you can rely on the author_insertKey RPC. That is one possible way node admins can manage their OCW signing keys in a production environment.

For example:

curl --location --request POST 'http://localhost:9933' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "author_insertKey",
   "params": ["abcd","bread tongue spell stadium clean grief coin rent spend total practice document","0xb6a8b4b6bf796991065035093d3265e314c3fe89e75ccb623985e57b0c2e0c30"],
    "id": 1
}'

It's important to use the same KeyType that you declared in your OCW's crypto module. In your case, that's abcd.


Another path is to use the key insert subcommand on the node's executable. For example:

./target/release/node-template key insert \
--chain your_chainspec.json \
--scheme Sr25519\
--suri "bread tongue spell stadium clean grief coin rent spend total practice document" \
--key-type abcd \
--base-path same_base_path_of_the_node

The important detail here is the --base-path, which must match the running node's. That's where its keystore lives, and will persist after the node is restarted.

0

@Mrisho, I can't comment directly your answer due to StackExchange limitations, but OP asked explicitly to NOT have to use the "--dev" option for the offchain worker to run.

With your answer, as far as I've tested, the offchain worker would still need "--dev" flag to run; if not using "--dev" flag, the offchain worker doesn't print logs; even using "--offchain-worker Always" doesn't help

Following this thread, I'm also interested in the exact & concrete answers to OP's questions:

how to add keys to the keystore such that they persist

how to run the local node s.t. the ocw is enabled AND it picks up the added keys, without having to re-add them every time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.