Skip to content

Commit

Permalink
fix(redis): stamp alive check (#218)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark.B <[email protected]>
  • Loading branch information
nugaon and 0xCardinalError authored Oct 19, 2023
1 parent daa7171 commit ecb3069
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/PostageStamp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ contract PostageStamp is AccessControl, Pausable {
emit PriceUpdate(_price);
}

function setMinimumValidityBlocks(uint256 _value) external {
function setMinimumValidityBlocks(uint64 _value) external {
if (!hasRole(DEFAULT_ADMIN_ROLE, msg.sender)) {
revert AdministratorOnly();
}

minimumValidityBlocks = uint64(_value);
minimumValidityBlocks = _value;
}

/**
Expand Down
18 changes: 8 additions & 10 deletions src/Redistribution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ contract Redistribution is AccessControl, Pausable {
error SocCalcNotMatching(bytes32); // Soc address calculation does not match with the witness
error IndexOutsideSet(bytes32); // Stamp available: index resides outside of the valid index set
error SigRecoveryFailed(bytes32); // Stamp authorized: signature recovery failed for element
error BalanceValidationFailed(bytes32); // Stamp alive: batch remaining balance validation failed for attached stamp
error BatchDoesNotExist(bytes32); // Stamp alive: batch remaining balance validation failed for attached stamp
error BucketDiffers(bytes32); // Stamp aligned: postage bucket differs from address bucket
error InclusionProofFailed(uint8, bytes32);
// 1 = RC inclusion proof failed for element
Expand Down Expand Up @@ -1015,25 +1015,23 @@ contract Redistribution is AccessControl, Pausable {
}

function stampFunction(ChunkInclusionProof calldata entryProof) internal view {
// authentic
(address batchOwner, uint8 batchDepth, uint8 bucketDepth, , , ) = PostageContract.batches(
entryProof.postageProof.postageId
);
// authentic

// alive
if (batchOwner == address(0)) {
revert BatchDoesNotExist(entryProof.postageProof.postageId); // Batch does not exist or expired
}

uint32 postageIndex = getPostageIndex(entryProof.postageProof.index);
uint256 maxPostageIndex = postageStampIndexCount(batchDepth, bucketDepth);
// available
if (postageIndex >= maxPostageIndex) {
revert IndexOutsideSet(entryProof.postageProof.postageId);
}

// alive
if (
PostageContract.remainingBalance(entryProof.postageProof.postageId) <
PostageContract.minimumInitialBalancePerChunk()
) {
revert BalanceValidationFailed(entryProof.postageProof.postageId);
}

// aligned
uint64 postageBucket = getPostageBucket(entryProof.postageProof.index);
uint64 addressBucket = addressToBucket(entryProof.proveSegment, bucketDepth);
Expand Down
8 changes: 5 additions & 3 deletions test/Redistribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const errors = {
outOfDepth: 'OutOfDepth()',
reserveCheckFailed: 'ReserveCheckFailed()',
indexOutsideSet: 'IndexOutsideSet()',
balanceValidationFailed: 'BalanceValidationFailed()',
batchDoesNotExist: 'BatchDoesNotExist()',
bucketDiffers: 'BucketDiffers()',
sigRecoveryFailed: 'SigRecoveryFailed()',
inclusionProofFailed1: 'InclusionProofFailed',
Expand Down Expand Up @@ -1118,7 +1118,7 @@ describe('Redistribution', function () {
const postage = await ethers.getContract('PostageStamp', deployer);
const validityBlockTx = await postage.setMinimumValidityBlocks(1);
await validityBlockTx.wait();
const initialPaymentPerChunk = price1 * 2 - 1; // it works without substracting 1, the one block is the "createBatch"
const initialPaymentPerChunk = price1 * 2 - 1;
const batchSize = 2 ** batch.depth;
const transferAmount = initialPaymentPerChunk * batchSize;
await mintAndApprove(deployer, deployer, postage.address, transferAmount.toString());
Expand All @@ -1130,6 +1130,8 @@ describe('Redistribution', function () {
'0x00000000000000000000000000000000000000000000000000000000b0bafe77',
batch.immutable
);
await mineNBlocks(1); // in order to expire batch
await postage.expireLimited(1); // remove batch
const batchReceipt = await batchTx.wait();
const batchCreatedEvent = batchReceipt.events.filter((e: { event: string }) => e.event === 'BatchCreated');
const batchId = Buffer.from(arrayify(batchCreatedEvent[0].args[0]));
Expand All @@ -1143,7 +1145,7 @@ describe('Redistribution', function () {

await expect(
r_node_5.claim(proofParams.proof1, proofParams.proof2, proofParams.proofLast)
).to.be.revertedWith(errors.claim.balanceValidationFailed);
).to.be.revertedWith(errors.claim.batchDoesNotExist);
});

it('postage bucket and address bucket do not match', async function () {
Expand Down

0 comments on commit ecb3069

Please sign in to comment.