JoshOrndorff / recipes

A Hands-On Cookbook for Aspiring Blockchain Chefs
GNU General Public License v3.0
378 stars 186 forks source link

OCW only triggers every third transaction #453

Open bmacer opened 2 years ago

bmacer commented 2 years ago

The offchain-worker functionality seems to only trigger every third time I send a transaction. This is without any substantial modification of the code.

Repro steps: cloned the recipes repo: git clone https://github.com/substrate-developer-hub/recipes.git cd recipes

edit nodes/kitchen-node/Cargo.toml: comment out line 51 (super-runtime configuration) uncomment out line 59 (ocw-runtime configuration)

then cd nodes/kitchen-node cargo build --release --features ocw cargo run -- --dev --tmp

then open Polkadot.js app explorer, switch to our Development > Local Node: 127.0.0.1:9944

Attempt Developer > Extrinsics > ocwDemo > submitNumberSigned > any number (1) > Submit Transaction

We'll see "No off-chain indexing data retrieved".

2021-07-20 16:27:24 submit_number_signed: (2, d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d (5GrwvaEF...))
2021-07-20 16:27:24 Number vector: [1, 2]
2021-07-20 16:27:24 🎁 Prepared block for proposing at 2 [hash: 0x9952ac81d5dfb09801644f4993ae941fab2581b47ff7d14da3835985070ee8d2; parent_hash: 0x54c8…7a13; extrinsics (2): [0xec97…827b, 0xdce4…344f]]
2021-07-20 16:27:24 Instant Seal success: CreatedBlock { hash: 0x9952ac81d5dfb09801644f4993ae941fab2581b47ff7d14da3835985070ee8d2, aux: ImportedAux { header_only: false, clear_justification_requests: false, needs_justification: false, bad_justification: false, is_new_best: true } }
2021-07-20 16:27:24 ✨ Imported #2 (0x9952…e8d2)
2021-07-20 16:27:24 Entering off-chain worker
2021-07-20 16:27:24 no off-chain indexing data retrieved.

The same will occur if we subimit the same extrinsic again.

However, if we submit it a third time, we'll see success:

2021-07-20 16:27:32 Instant Seal success: CreatedBlock { hash: 0x047aab0718146fd0f90d3b553142a74cf327e34827eb173066bacf5bb9dd5344, aux: ImportedAux { header_only: false, clear_justification_requests: false, needs_justification: false, bad_justification: false, is_new_best: true } }
2021-07-20 16:27:32 Entering off-chain worker
2021-07-20 16:27:32 sending request to: https://api.github.com/orgs/substrate-developer-hub
2021-07-20 16:27:32 💤 Idle (0 peers), best: #4 (0x047a…5344), finalized #0 (0x7d38…d515), ⬇ 0 ⬆ 0
2021-07-20 16:27:32 {"login":"substrate-developer-hub","id":47530779,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQ3NTMwNzc5","url":"https://api.github.com/orgs/substrate-developer-hub","repos_url":"https://api.github.com/orgs/substrate-developer-hub/repos","events_url":"https://api.github.com/orgs/substrate-developer-hub/events","hooks_url":"https://api.github.com/orgs/substrate-developer-hub/hooks","issues_url":"https://api.github.com/orgs/substrate-developer-hub/issues","members_url":"https://api.github.com/orgs/substrate-developer-hub/members{/member}","public_members_url":"https://api.github.com/orgs/substrate-developer-hub/public_members{/member}","avatar_url":"https://avatars.githubusercontent.com/u/47530779?v=4","description":"Documentation, samples, and tutorials for the Substrate framework for building blockchains.","name":"Substrate Developer Hub","company":null,"blog":"https://substrate.dev/","location":null,"email":null,"twitter_username":"substrate_io","is_verified":false,"has_organization_projects":true,"has_repository_projects":true,"public_repos":31,"public_gists":0,"followers":0,"following":0,"html_url":"https://github.com/substrate-developer-hub","created_at":"2019-02-11T14:59:31Z","updated_at":"2020-10-03T13:48:59Z","type":"Organization"}
2021-07-20 16:27:32 no off-chain indexing data retrieved.

If we submit it a fourth or fifth time, we'll return to the initial failures ("No local account available") .

On a sixth attempt, we'll get success again (this time, the results will be cached, so the success is slightly different):

2021-07-20 16:27:49 Instant Seal success: CreatedBlock { hash: 0x91ab713d564ec49055234c548a1385ce63b378b16004bca70f7c67f19608f1d5, aux: ImportedAux { header_only: false, clear_justification_requests: false, needs_justification: false, bad_justification: false, is_new_best: true } }
2021-07-20 16:27:49 ✨ Imported #8 (0x91ab…f1d5)
2021-07-20 16:27:49 Entering off-chain worker
2021-07-20 16:27:49 cached gh-info: { login: substrate-developer-hub, blog: https://substrate.dev/, public_repos: 31 }

This same cycle repeats. Only every third transaction appears to no trigger a "No local account available" debug line.

CocDap commented 2 years ago

Yeah! But What is your problem ? With my understanding, off chain worker executes when 1 block finalize, so in function fn offchain_worker(block_number: T::BlockNumber) has 1 parameter block_number. In recipes code, they set default that block_number % 4 that you can choice any function offchain execution. Function fetch_github_info execute in block_number 3 or 7,... (_Because frame_system::block_number is initialized with n+1 , actually block_number is n ) This is code from recipes that indicate your describe: image