Skip to content

Commit

Permalink
fix: race in streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Feb 2, 2024
1 parent cb9048a commit 0f98e4d
Showing 1 changed file with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,67 @@ export class SRModulesOperatorsKeysService {
for (const stakingModule of stakingModules) {
const moduleInstance = this.stakingRouterService.getStakingRouterModuleImpl(stakingModule.type);

const keysGenerator = moduleInstance.getKeysStream(stakingModule.stakingModuleAddress, {});
const operatorsGenerator = moduleInstance.getOperatorsStream(stakingModule.stakingModuleAddress, {});
// const keysGenerator = moduleInstance.getKeysStream(stakingModule.stakingModuleAddress, {});
// const operatorsGenerator = moduleInstance.getOperatorsStream(stakingModule.stakingModuleAddress, {});

let nextKey = await keysGenerator.next();
let nextOperator = await operatorsGenerator.next();
// let nextKey = await keysGenerator.next();
// let nextOperator = await operatorsGenerator.next();

yield {
stakingModule: new StakingModuleResponse(stakingModule),
meta: !metaHasSent ? meta : null,
key: !nextKey.value ? null : new Key(nextKey.value),
operator: !nextOperator.value ? null : new Operator(nextOperator.value),
key: null, //!nextKey.value ? null : new Key(nextKey.value),
operator: null, //!nextOperator.value ? null : new Operator(nextOperator.value),
};

metaHasSent = true;

do {
if (!nextKey.done) {
nextKey = await keysGenerator.next();
}

if (!nextOperator.done) {
nextOperator = await operatorsGenerator.next();
}

if (!nextKey.value && !nextOperator.value) {
break;
}
const keysGenerator = moduleInstance.getKeysStream(stakingModule.stakingModuleAddress, {});
let nextKey = await keysGenerator.next();
while (!nextKey.done) {
// Yield all keys first
yield {
stakingModule: null, // Already yielded above
meta: null, // Already yielded above
key: nextKey.value ? new Key(nextKey.value) : null,
operator: null,
};
nextKey = await keysGenerator.next();
}

const operatorsGenerator = moduleInstance.getOperatorsStream(stakingModule.stakingModuleAddress, {});
let nextOperator = await operatorsGenerator.next();
while (!nextOperator.done) {
// After all keys, yield all operators
yield {
stakingModule: null,
meta: null,
key: !nextKey.value ? null : new Key(nextKey.value),
operator: !nextOperator.value ? null : new Operator(nextOperator.value),
stakingModule: null, // Already yielded above
meta: null, // Already yielded above
key: null,
operator: nextOperator.value ? new Operator(nextOperator.value) : null,
};
} while (!nextKey.done || !nextOperator.done);
nextOperator = await operatorsGenerator.next();
}

// do {
// if (!nextKey.done) {
// nextKey = await keysGenerator.next();
// }

// if (!nextOperator.done) {
// nextOperator = await operatorsGenerator.next();
// }

// if (!nextKey.value && !nextOperator.value) {
// break;
// }

// yield {
// stakingModule: null,
// meta: null,
// key: !nextKey.value ? null : new Key(nextKey.value),
// operator: !nextOperator.value ? null : new Operator(nextOperator.value),
// };
// } while (!nextKey.done || !nextOperator.done);
}
}
}

0 comments on commit 0f98e4d

Please sign in to comment.