node icon indicating copy to clipboard operation
node copied to clipboard

feat: MMD-1309 10-to-1 - Off-chain workers

Open ltfschoen opened this issue 4 years ago • 0 comments

  • [X] - i asked this question in room "Builders Program - Chains Track" in Element on 10th Nov 2021: i added the offchain_worker function and some new off-chain workers logic to a pallet where i had already setup other logic and had the mock configured so unit tests were passing.

i had it working in mock.rs with type AccountId = u128

impl frame_system::Config for Test {
    ...
    type AccountId = AccountId;
...

but now i have to update mock.rs so it works with off-chain workers, but i'm not sure how to get it to work.

in frame example-offchain-workers AccountId is:

impl frame_system::Config for Test {
        ...
	type AccountId = sp_core::sr25519::Public;
...

(as shown here: https://github.com/paritytech/substrate/blob/master/frame/example-offchain-worker/src/tests.rs#L67 )

where AccountId is:

type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

(as shown here: https://github.com/paritytech/substrate/blob/master/frame/example-offchain-worker/src/tests.rs#L83)

and it uses <Signature as Verify>::Signer in these two places:

  • https://github.com/paritytech/substrate/blob/master/frame/example-offchain-worker/src/tests.rs#L86
  • https://github.com/paritytech/substrate/blob/master/frame/example-offchain-worker/src/tests.rs#L104

so far the changes i've made are in this PR in the file pallets/mining/rewards-allowance/src/mock.rs here: https://github.com/DataHighway-DHX/node/pull/238/files#diff-b7ace9b0e8a1d800ea987e0e9b9e117f8fe6a52b923a9e654b65495c90c7ffd5R468

and when i run the tests with cargo test -p mining-rewards-allowance, i get the following error:

error[E0271]: type mismatch resolving `<sp_core::sr25519::Public as IdentifyAccount>::AccountId == u128`
   --> pallets/mining/rewards-allowance/src/mock.rs:449:2
    |
449 |     type Public = <Signature as Verify>::Signer;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u128`, found struct `sp_core::sr25519::Public`
    |
note: required by a bound in `frame_system::offchain::SigningTypes::Public`
   --> /Users/ls2/.cargo/git/checkouts/substrate-f10629da2328b525/f5dc02a/frame/system/src/offchain.rs:453:21
    |
453 |         + IdentifyAccount<AccountId = Self::AccountId>
    |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `frame_system::offchain::SigningTypes::Public`

SOLUTION: This was fixed in this commit 9ad33cdf615e54699613d608134cae5c89dcc3ab

  • [x] - need to move all on_initialize code into offchain_worker function, and need to figure out what date to use for storing data (i.e. in function set_mpower_of_account_for_date we're storing a next_start_date, but in offchain_workers we might be storing the current date for MPowerForAccountForDate... i.e. if there are a lot of registered miners and it takes more than a day to receive and process all of the offchain data received and we're storing that under a key of the current day it's being processed on on-chain, then if we don't finish going through all those registered miners in one day then they might be stored under a key for tomorrow's date incorrectly....

so instead we should be providing the date that the account id's mpower relates to from the API with a timestamp, and then in offchain_workers figure out the start of that date, and store it.

Note: See Jira mPower MMD-1584 for latest status

ltfschoen avatar Nov 06 '21 21:11 ltfschoen