Help with Return Type for Pure Service Method in Effect Typescript Library
need help defining a correct return type for a pure service method
Either<true, Error>Either<true, Error> seems more fitting here, since validateChainvalidateChain is not effectfulreadonly validateChain: (
state: BlockchainState
// `Effect.Either` instead ? since `R = never`
) => Effect.Effect<true, ValidationError>;
...
validateChain: (state) =>
Effect.gen(function* () {
const blocks = Chunk.toReadonlyArray(state.chain);
for (const [previous, current] of Array.window(blocks, 2)) {
const { hash, ...blockWithoutHash } = current;
const recalculatedHash = blockService.computeHash(blockWithoutHash);
if (hash !== recalculatedHash) {
return yield* Effect.fail(
new ValidationError({
blockNumber: current.blockNumber,
reason: "Hash mismatch",
})
);
}
if (current.previousHash !== previous.hash) {
return yield* Effect.fail(
new ValidationError({
blockNumber: current.blockNumber,
reason: "Chain link broken",
})
);
}
if (current.timestamp < previous.timestamp) {
return yield* Effect.fail(
new ValidationError({
blockNumber: current.blockNumber,
reason: "Invalid timestamp ordering",
})
);
}
}
return yield* Effect.succeed<true>(true);
}),readonly validateChain: (
state: BlockchainState
// `Effect.Either` instead ? since `R = never`
) => Effect.Effect<true, ValidationError>;
...
validateChain: (state) =>
Effect.gen(function* () {
const blocks = Chunk.toReadonlyArray(state.chain);
for (const [previous, current] of Array.window(blocks, 2)) {
const { hash, ...blockWithoutHash } = current;
const recalculatedHash = blockService.computeHash(blockWithoutHash);
if (hash !== recalculatedHash) {
return yield* Effect.fail(
new ValidationError({
blockNumber: current.blockNumber,
reason: "Hash mismatch",
})
);
}
if (current.previousHash !== previous.hash) {
return yield* Effect.fail(
new ValidationError({
blockNumber: current.blockNumber,
reason: "Chain link broken",
})
);
}
if (current.timestamp < previous.timestamp) {
return yield* Effect.fail(
new ValidationError({
blockNumber: current.blockNumber,
reason: "Invalid timestamp ordering",
})
);
}
}
return yield* Effect.succeed<true>(true);
}),