From d1b8944a3596e3cbd1ea292efa0de615d49318c2 Mon Sep 17 00:00:00 2001 From: andrewnguyen22 Date: Fri, 24 Jul 2020 18:02:48 -0400 Subject: [PATCH] enhanced proof entropy and flush to disk --- x/pocketcore/keeper/claim.go | 13 ++++++++----- x/pocketcore/keeper/proof.go | 2 +- x/pocketcore/types/cache.go | 1 + x/pocketcore/types/merkle.go | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/x/pocketcore/keeper/claim.go b/x/pocketcore/keeper/claim.go index 0949c1f08..e5479e672 100644 --- a/x/pocketcore/keeper/claim.go +++ b/x/pocketcore/keeper/claim.go @@ -20,7 +20,7 @@ func (k Keeper) SendClaimTx(ctx sdk.Ctx, n client.Client, claimTx func(pk crypto return } // retrieve the iterator to go through each piece of evidence in storage - iter := pc.EvidenceIterator() + iter := pc.EvidenceIterator() // TODO need to delete evidence in the case that unable to send proof or claim defer iter.Close() // loop through each evidence for ; iter.Valid(); iter.Next() { @@ -43,7 +43,11 @@ func (k Keeper) SendClaimTx(ctx sdk.Ctx, n client.Client, claimTx func(pk crypto // get the session context sessionCtx, er := ctx.PrevCtx(evidence.SessionHeader.SessionBlockHeight) if er != nil { - ctx.Logger().Error("could not get sessionCtx") + ctx.Logger().Error("could not get sessionCtx in auto send claim tx: " + er.Error()) + continue + } + if ctx.BlockHeight() <= evidence.SessionBlockHeight+k.BlocksPerSession(sessionCtx)-1 { // ensure session is over + ctx.Logger().Info("the session is ongoing, so will not send the claimtx yet") continue } // if the blockchain in the evidence is not supported then delete it because nodes don't get paid/challenged for unsupported blockchains @@ -60,7 +64,6 @@ func (k Keeper) SendClaimTx(ctx sdk.Ctx, n client.Client, claimTx func(pk crypto } // if the claim is mature, delete it because we cannot submit a mature claim if k.ClaimIsMature(ctx, evidence.SessionBlockHeight) { - fmt.Println("claim is mature @ ", ctx.BlockHeight(), evidence) if err := pc.DeleteEvidence(evidence.SessionHeader, evidenceType); err != nil { ctx.Logger().Debug(err.Error()) } @@ -93,11 +96,11 @@ func (k Keeper) ValidateClaim(ctx sdk.Ctx, claim pc.MsgClaim) (err sdk.Error) { return sdk.ErrInternal(er.Error()) } // ensure that session ended - sessionEndHeight := sessionContext.BlockHeight() + k.BlocksPerSession(sessionContext) - 1 + sessionEndHeight := claim.SessionBlockHeight + k.BlocksPerSession(sessionContext) - 1 if ctx.BlockHeight() <= sessionEndHeight { return pc.NewInvalidBlockHeightError(pc.ModuleName) } - if claim.TotalProofs <= k.MinimumNumberOfProofs(sessionContext) { + if claim.TotalProofs < k.MinimumNumberOfProofs(sessionContext) { return pc.NewInvalidProofsError(pc.ModuleName) } // if is not a pocket supported blockchain then return not supported error diff --git a/x/pocketcore/keeper/proof.go b/x/pocketcore/keeper/proof.go index d64881f07..6f1797592 100644 --- a/x/pocketcore/keeper/proof.go +++ b/x/pocketcore/keeper/proof.go @@ -95,7 +95,7 @@ func (k Keeper) ValidateProof(ctx sdk.Ctx, proof pc.MsgProof) (servicerAddr sdk. return nil, pc.MsgClaim{}, pc.NewInvalidProofsError(pc.ModuleName) } // validate number of proofs - if minProofs := k.MinimumNumberOfProofs(sessionCtx); claim.TotalProofs <= minProofs { + if minProofs := k.MinimumNumberOfProofs(sessionCtx); claim.TotalProofs < minProofs { return nil, pc.MsgClaim{}, pc.NewInvalidMerkleVerifyError(pc.ModuleName) } // validate the merkle proofs diff --git a/x/pocketcore/types/cache.go b/x/pocketcore/types/cache.go index b3dfb41a3..6bfab5541 100644 --- a/x/pocketcore/types/cache.go +++ b/x/pocketcore/types/cache.go @@ -136,6 +136,7 @@ func (cs *CacheStorage) Clear() { // "Iterator" - Returns an iterator for all of the items in the stores func (cs *CacheStorage) Iterator() db.Iterator { + _ = cs.FlushToDB() return cs.DB.Iterator(nil, nil) } diff --git a/x/pocketcore/types/merkle.go b/x/pocketcore/types/merkle.go index ddba49de3..4d9b43233 100644 --- a/x/pocketcore/types/merkle.go +++ b/x/pocketcore/types/merkle.go @@ -112,7 +112,7 @@ func (mp MerkleProof) Validate(root HashRange, leaf Proof, totalRelays int64) (i func sumFromHash(hash []byte) uint64 { hashCopy := make([]byte, len(hash)) copy(hashCopy, hash) - return binary.LittleEndian.Uint64(append(hashCopy[:3], make([]byte, 5)...)) + return binary.LittleEndian.Uint64(hash[:8]) } // "newLevelIsValid" - Ensure that the number of levels in the relayProof is valid