From ee7d2b4d627a4c0e10f2032c726ff3f119cdab62 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Fri, 26 Jul 2024 20:04:39 +0530 Subject: [PATCH] fix connection object size calculation --- .../handler/object_operation_handler.go | 28 ++++++++++++------- .../blobbercore/writemarker/protocol.go | 4 --- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go index 45c8921f2..968eaa2a1 100644 --- a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go +++ b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go @@ -656,11 +656,6 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b return nil, common.NewError("invalid_operation", "Operation needs to be performed by the owner of the allocation") } - if allocationObj.BlobberSizeUsed+connectionObj.Size > allocationObj.BlobberSize { - return nil, common.NewError("max_allocation_size", - "Max size reached for the allocation with this blobber") - } - writeMarkerString := r.FormValue("write_marker") if writeMarkerString == "" { return nil, common.NewError("invalid_parameters", "Invalid write marker passed") @@ -689,11 +684,6 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b "Latest write marker is in failed state") } - if latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size != writeMarker.ChainSize { - return nil, common.NewErrorf("invalid_chain_size", - "Invalid chain size. expected:%v got %v", latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size, writeMarker.ChainSize) - } - if latestWriteMarkerEntity.Status != writemarker.Committed { writeMarker.ChainLength = latestWriteMarkerEntity.WM.ChainLength } @@ -758,6 +748,24 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b if !rootRef.IsPrecommit { return nil, common.NewError("no_root_change", "No change in root ref") } + connectionObj.Size = rootRef.Size - allocationObj.BlobberSizeUsed + + if writemarkerEntity.WM.Size != connectionObj.Size { + return nil, common.NewError("write_marker_validation_failed", fmt.Sprintf("Write Marker size %v does not match the connection size %v", writemarkerEntity.WM.Size, connectionObj.Size)) + } + + if allocationObj.BlobberSizeUsed+connectionObj.Size > allocationObj.BlobberSize { + return nil, common.NewError("max_allocation_size", + "Max size reached for the allocation with this blobber") + } + + if latestWriteMarkerEntity != nil && latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size != writeMarker.ChainSize { + return nil, common.NewErrorf("invalid_chain_size", + "Invalid chain size. expected:%v got %v", latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size, writeMarker.ChainSize) + } else if latestWriteMarkerEntity == nil && connectionObj.Size != writeMarker.ChainSize { + return nil, common.NewErrorf("invalid_chain_size", + "Invalid chain size. expected:%v got %v", connectionObj.Size, writeMarker.ChainSize) + } elapsedApplyChanges := time.Since(startTime) - elapsedAllocation - elapsedGetLock - elapsedGetConnObj - elapsedVerifyWM - elapsedWritePreRedeem diff --git a/code/go/0chain.net/blobbercore/writemarker/protocol.go b/code/go/0chain.net/blobbercore/writemarker/protocol.go index 35c2c7c0b..c33c30d07 100644 --- a/code/go/0chain.net/blobbercore/writemarker/protocol.go +++ b/code/go/0chain.net/blobbercore/writemarker/protocol.go @@ -76,10 +76,6 @@ func (wme *WriteMarkerEntity) VerifyMarker(ctx context.Context, dbAllocation *al return common.NewError("write_marker_validation_failed", "Write Marker is not for the same allocation transaction") } - if wme.WM.Size != co.Size { - return common.NewError("write_marker_validation_failed", fmt.Sprintf("Write Marker size %v does not match the connection size %v", wme.WM.Size, co.Size)) - } - clientPublicKey := ctx.Value(constants.ContextKeyClientKey).(string) if clientPublicKey == "" { return common.NewError("write_marker_validation_failed", "Could not get the public key of the client")