Skip to content

Commit

Permalink
change chainhash verify
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitenjain14 committed Feb 16, 2024
1 parent 9189ecf commit b2772c7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
28 changes: 11 additions & 17 deletions code/go/0chain.net/blobbercore/handler/object_operation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b
if r.Method == "GET" {
return nil, common.NewError("invalid_method", "Invalid method used for the upload URL. Use POST instead")
}

var prevChainHash string
allocationId := ctx.Value(constants.ContextKeyAllocationID).(string)
allocationTx := ctx.Value(constants.ContextKeyAllocation).(string)
clientID := ctx.Value(constants.ContextKeyClient).(string)
Expand Down Expand Up @@ -593,19 +593,15 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b
if latestWriteMarkerEntity.Status == writemarker.Failed {
return nil, common.NewError("latest_write_marker_failed",
"Latest write marker is in failed state")
}
if latestWriteMarkerEntity.Status != writemarker.Committed {
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)
}
} else if latestWriteMarkerEntity.Status == writemarker.Committed {
writeMarker.ChainLength = latestWriteMarkerEntity.WM.ChainLength
} else {
if writeMarker.ChainSize != connectionObj.Size {
return nil, common.NewErrorf("invalid_chain_size",
"Invalid chain size. expected:%v got %v", connectionObj.Size, writeMarker.ChainSize)
}
}

if latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size != writeMarker.ChainSize {
return nil, common.NewError("invalid_chain_size",
"Invalid chain size")
}
prevChainHash = latestWriteMarkerEntity.WM.ChainHash
}

writemarkerEntity := &writemarker.WriteMarkerEntity{}
Expand Down Expand Up @@ -679,7 +675,7 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b
return &result, common.NewError("allocation_root_mismatch", result.ErrorMessage)
}

chainHash, err := writemarker.CalculateChainHash(ctx, allocationObj.ID, allocationRoot)
chainHash, err := writemarker.CalculateChainHash(ctx, allocationObj.ID, allocationRoot, prevChainHash)
if err != nil {
return nil, common.NewError("chain_hash_error", "Error calculating chain hash")
}
Expand Down Expand Up @@ -1426,17 +1422,15 @@ func (fsh *StorageHandler) Rollback(ctx context.Context, r *http.Request) (*blob

if allocationRoot != writeMarker.AllocationRoot {
result.AllocationRoot = allocationObj.AllocationRoot
if latestWriteMarkerEntity != nil {
result.WriteMarker = latestWriteMarkerEntity
}
result.WriteMarker = latestWriteMarkerEntity
result.Success = false
result.ErrorMessage = "Allocation root in the write marker does not match the calculated allocation root." +
" Expected hash: " + allocationRoot
txn.Rollback()
return &result, common.NewError("allocation_root_mismatch", result.ErrorMessage)
}

chainHash, err := writemarker.CalculateChainHash(ctx, allocationObj.ID, allocationRoot)
chainHash, err := writemarker.CalculateChainHash(ctx, allocationObj.ID, allocationRoot, latestWriteMarkerEntity.WM.ChainHash)
if err != nil {
txn.Rollback()
return nil, common.NewError("chain_hash_error", "Error calculating chain hash "+err.Error())
Expand Down
9 changes: 0 additions & 9 deletions code/go/0chain.net/blobbercore/handler/storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,6 @@ func (fsh *StorageHandler) GetLatestWriteMarker(ctx context.Context, r *http.Req

var result blobberhttp.LatestWriteMarkerResult
if latestWM != nil {
if latestWM.Status == writemarker.Committed {
latestWM.WM.ChainLength = 0 // start a new chain
}
result.LatestWM = &latestWM.WM
}
if prevWM != nil {
Expand Down Expand Up @@ -571,9 +568,6 @@ func (fsh *StorageHandler) getReferencePath(ctx context.Context, r *http.Request
var refPathResult blobberhttp.ReferencePathResult
refPathResult.ReferencePath = refPath
if latestWM != nil {
if latestWM.Status == writemarker.Committed {
latestWM.WM.ChainLength = 0 // start a new chain
}
refPathResult.LatestWM = &latestWM.WM
}
if _, ok := common.GetField(r, "chain_data"); ok {
Expand Down Expand Up @@ -650,9 +644,6 @@ func (fsh *StorageHandler) GetObjectTree(ctx context.Context, r *http.Request) (
var refPathResult blobberhttp.ReferencePathResult
refPathResult.ReferencePath = refPath
if latestWM != nil {
if latestWM.Status == writemarker.Committed {
latestWM.WM.ChainLength = 0 // start a new chain
}
refPathResult.LatestWM = &latestWM.WM
}
return &refPathResult, nil
Expand Down
30 changes: 19 additions & 11 deletions code/go/0chain.net/blobbercore/writemarker/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package writemarker

import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/allocation"
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
"github.com/0chain/blobber/code/go/0chain.net/core/common"
"github.com/0chain/blobber/code/go/0chain.net/core/encryption"
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
"go.uber.org/zap"
"gorm.io/gorm"
Expand All @@ -24,7 +24,7 @@ type WriteMarker struct {
Size int64 `gorm:"column:size" json:"size"`
ChainSize int64 `gorm:"column:chain_size" json:"chain_size"`
ChainHash string `gorm:"column:chain_hash;size:64" json:"chain_hash"`
ChainLength int `gorm:"column:chain_length" json:"chain_length"`
ChainLength int `gorm:"column:chain_length" json:"-"`
BlobberID string `gorm:"column:blobber_id;size:64" json:"blobber_id"`
Timestamp common.Timestamp `gorm:"column:timestamp;primaryKey" json:"timestamp"`
ClientID string `gorm:"column:client_id;size:64" json:"client_id"`
Expand Down Expand Up @@ -272,13 +272,6 @@ func GetMarkersForChain(ctx context.Context, allocationID string) ([]byte, error
return nil, err
}
markers := make([]byte, 0, len(unComittedMarkers)+1)
if commitedMarker != nil {
decodedHash, err := hex.DecodeString(commitedMarker.WM.AllocationRoot)
if err != nil {
return nil, err
}
markers = append(markers, decodedHash...)
}
for _, marker := range unComittedMarkers {
decodedHash, err := hex.DecodeString(marker.WM.AllocationRoot)
if err != nil {
Expand All @@ -289,7 +282,7 @@ func GetMarkersForChain(ctx context.Context, allocationID string) ([]byte, error
return markers, nil
}

func CalculateChainHash(ctx context.Context, allocationID, newRoot string) (string, error) {
func CalculateChainHash(ctx context.Context, allocationID, newRoot, prevChainHash string) (string, error) {
prevRoots, err := GetMarkersForChain(ctx, allocationID)
if err != nil {
return "", err
Expand All @@ -298,8 +291,23 @@ func CalculateChainHash(ctx context.Context, allocationID, newRoot string) (stri
if err != nil {
return "", err
}
prevChainBytes, err := hex.DecodeString(prevChainHash)
if err != nil {
return "", err
}
hasher := sha256.New()
if prevChainHash != "" {
_, err = hasher.Write(prevChainBytes)
if err != nil {
return "", err
}
}
prevRoots = append(prevRoots, decodedHash...)
return encryption.Hash(prevRoots), nil
_, err = hasher.Write(prevRoots)
if err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}

// client lock alloc -> commitMarker -> unlock alloc
Expand Down
17 changes: 13 additions & 4 deletions code/go/0chain.net/blobbercore/writemarker/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (wme *WriteMarkerEntity) VerifyMarker(ctx context.Context, dbAllocation *al
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))
}

if wme.WM.ChainSize < 0 {
return common.NewError("write_marker_validation_failed", fmt.Sprintf("Write Marker chain size %v is negative", wme.WM.ChainSize))
}

clientPublicKey := ctx.Value(constants.ContextKeyClientKey).(string)
if clientPublicKey == "" {
return common.NewError("write_marker_validation_failed", "Could not get the public key of the client")
Expand Down Expand Up @@ -210,16 +214,21 @@ func (wme *WriteMarkerEntity) VerifyRollbackMarker(ctx context.Context, dbAlloca
}

if wme.WM.Size != -latestWM.WM.Size {
return common.NewError("empty write_marker_validation_failed", fmt.Sprintf("Write Marker size is %v but should be 0", wme.WM.Size))
return common.NewError("write_marker_validation_failed", fmt.Sprintf("Write Marker size is %v but should be 0", wme.WM.Size))
}

if latestWM.Status != Committed {
if wme.WM.ChainSize != latestWM.WM.ChainSize+wme.WM.Size {
return common.NewError("empty write_marker_validation_failed", fmt.Sprintf("Write Marker chain size is %v but should be %v", wme.WM.ChainSize, latestWM.WM.ChainSize+wme.WM.Size))
}
wme.WM.ChainLength = latestWM.WM.ChainLength
}

if wme.WM.ChainSize != latestWM.WM.ChainSize+wme.WM.Size {
return common.NewError("write_marker_validation_failed", fmt.Sprintf("Write Marker chain size is %v but should be %v", wme.WM.ChainSize, latestWM.WM.ChainSize+wme.WM.Size))
}

if wme.WM.ChainSize < 0 {
return common.NewError("write_marker_validation_failed", fmt.Sprintf("Write Marker chain size %v is negative", wme.WM.ChainSize))
}

if wme.WM.AllocationRoot == dbAllocation.AllocationRoot {
return common.NewError("write_marker_validation_failed", "Write Marker allocation root is the same as the allocation root on record")
}
Expand Down

0 comments on commit b2772c7

Please sign in to comment.