Skip to content

Commit

Permalink
Merge pull request #1497 from 0chain/revert-1496-revert-1494-feat/ses…
Browse files Browse the repository at this point in the history
…sion-key

Session key
  • Loading branch information
dabasov authored Nov 1, 2024
2 parents b1ef274 + 84b7d56 commit 1c3fddd
Show file tree
Hide file tree
Showing 25 changed files with 207 additions and 113 deletions.
5 changes: 3 additions & 2 deletions code/go/0chain.net/blobbercore/allocation/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ type Allocation struct {
TimeUnit time.Duration `gorm:"column:time_unit;not null;default:172800000000000"`
StartTime common.Timestamp `gorm:"column:start_time;not null"`
// Ending and cleaning
CleanedUp bool `gorm:"column:cleaned_up;not null;default:false"`
Finalized bool `gorm:"column:finalized;not null;default:false"`
CleanedUp bool `gorm:"column:cleaned_up;not null;default:false"`
Finalized bool `gorm:"column:finalized;not null;default:false"`
OwnerSigningPublicKey string `gorm:"column:owner_signing_public_key;size:512;not null" json:"owner_signing_public_key"`

// FileOptions to define file restrictions on an allocation for third-parties
// default 00000000 for all crud operations suggesting only owner has the below listed abilities.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ type BaseFileChanger struct {
EncryptedKeyPoint string `json:"encrypted_key_point,omitempty"`
CustomMeta string `json:"custom_meta,omitempty"`

ChunkSize int64 `json:"chunk_size,omitempty"` // the size of achunk. 64*1024 is default
IsFinal bool `json:"is_final,omitempty"` // current chunk is last or not
ChunkSize int64 `json:"chunk_size,omitempty"` // the size of achunk. 64*1024 is default
IsFinal bool `json:"is_final,omitempty"` // current chunk is last or not
SignatureVersion int `json:"signature_version,omitempty"`

ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks.
ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of CompactMerkleTree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (nf *UpdateFileChanger) ApplyChange(ctx context.Context, rootRef *reference
fileRef.ChunkSize = nf.ChunkSize
fileRef.IsPrecommit = true
fileRef.FilestoreVersion = filestore.VERSION
fileRef.SignatureVersion = nf.SignatureVersion

return rootRef, nil
}
Expand Down Expand Up @@ -173,6 +174,7 @@ func (nf *UpdateFileChanger) ApplyChangeV2(ctx context.Context, allocationRoot,
PathLevel: len(strings.Split(strings.TrimRight(nf.Path, "/"), "/")),
NumBlocks: int64(math.Ceil(float64(nf.Size*1.0) / float64(nf.ChunkSize))),
NumUpdates: refResult.NumUpdates + 1,
SignatureVersion: nf.SignatureVersion,
}
nf.storageVersion = 1
newFile.FileMetaHash = encryption.Hash(newFile.GetFileMetaHashDataV2())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (nf *UploadFileChanger) applyChange(ctx context.Context, rootRef *reference
HashToBeComputed: true,
IsPrecommit: true,
FilestoreVersion: filestore.VERSION,
SignatureVersion: nf.SignatureVersion,
}

fileID, ok := fileIDMeta[newFile.Path]
Expand Down Expand Up @@ -197,6 +198,7 @@ func (nf *UploadFileChanger) ApplyChangeV2(ctx context.Context, allocationRoot,
PathLevel: len(strings.Split(strings.TrimRight(nf.Path, "/"), "/")),
NumBlocks: int64(math.Ceil(float64(nf.Size*1.0) / float64(nf.ChunkSize))),
NumUpdates: 1,
SignatureVersion: nf.SignatureVersion,
}
nf.storageVersion = 1
newFile.FileMetaHash = encryption.Hash(newFile.GetFileMetaHashDataV2())
Expand Down
30 changes: 17 additions & 13 deletions code/go/0chain.net/blobbercore/allocation/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
"github.com/0chain/blobber/code/go/0chain.net/core/node"
"github.com/0chain/blobber/code/go/0chain.net/core/transaction"
"go.uber.org/zap"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -110,8 +111,9 @@ func FetchAllocationFromEventsDB(ctx context.Context, allocationID string, alloc
a.TimeUnit = sa.TimeUnit
a.FileOptions = sa.FileOptions
a.StartTime = sa.StartTime
// Only for testing purpose
a.StorageVersion = uint8(sa.StorageVersion)
a.OwnerSigningPublicKey = sa.OwnerSigningPublicKey
logging.Logger.Info("OwnerSigningPublicKey", zap.String("OwnerSigningPublicKey", a.OwnerSigningPublicKey), zap.String("allocation_id", a.ID))

m := map[string]interface{}{
"allocation_id": a.ID,
Expand Down Expand Up @@ -139,23 +141,24 @@ func FetchAllocationFromEventsDB(ctx context.Context, allocationID string, alloc
return a, nil
}

logging.Logger.Info("Saving the allocation to DB")
logging.Logger.Info("Saving the allocation to DB", zap.String("allocation_id", a.ID))

if !isExist {
err = Repo.Save(ctx, a)
} else {
updateMap := map[string]interface{}{
"tx": a.Tx,
"expiration_date": a.Expiration,
"owner_id": a.OwnerID,
"owner_public_key": a.OwnerPublicKey,
"repairer_id": a.RepairerID,
"size": a.TotalSize,
"finalized": a.Finalized,
"time_unit": a.TimeUnit,
"file_options": a.FileOptions,
"start_time": a.StartTime,
"blobber_size": a.BlobberSize,
"tx": a.Tx,
"expiration_date": a.Expiration,
"owner_id": a.OwnerID,
"owner_public_key": a.OwnerPublicKey,
"repairer_id": a.RepairerID,
"size": a.TotalSize,
"finalized": a.Finalized,
"time_unit": a.TimeUnit,
"file_options": a.FileOptions,
"start_time": a.StartTime,
"blobber_size": a.BlobberSize,
"owner_signing_public_key": a.OwnerSigningPublicKey,
}

updateOption := func(alloc *Allocation) {
Expand All @@ -170,6 +173,7 @@ func FetchAllocationFromEventsDB(ctx context.Context, allocationID string, alloc
alloc.FileOptions = a.FileOptions
alloc.StartTime = a.StartTime
alloc.BlobberSize = a.BlobberSize
alloc.OwnerSigningPublicKey = a.OwnerSigningPublicKey
}
err = Repo.UpdateAllocation(ctx, a, updateMap, updateOption)
}
Expand Down
3 changes: 3 additions & 0 deletions code/go/0chain.net/blobbercore/allocation/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func updateAllocationInDB(ctx context.Context, a *Allocation, sa *transaction.St
a.Tx = sa.Tx
a.OwnerID = sa.OwnerID
a.OwnerPublicKey = sa.OwnerPublicKey
a.OwnerSigningPublicKey = sa.OwnerSigningPublicKey

// // update fields
a.Expiration = sa.Expiration
Expand All @@ -263,6 +264,7 @@ func updateAllocationInDB(ctx context.Context, a *Allocation, sa *transaction.St
updateMap["finalized"] = a.Finalized
updateMap["file_options"] = a.FileOptions
updateMap["blobber_size"] = a.BlobberSize
updateMap["owner_signing_public_key"] = a.OwnerSigningPublicKey

updateOption := func(alloc *Allocation) {
alloc.Tx = a.Tx
Expand All @@ -273,6 +275,7 @@ func updateAllocationInDB(ctx context.Context, a *Allocation, sa *transaction.St
alloc.Finalized = a.Finalized
alloc.FileOptions = a.FileOptions
alloc.BlobberSize = a.BlobberSize
alloc.OwnerSigningPublicKey = a.OwnerSigningPublicKey
}

// update terms
Expand Down
2 changes: 2 additions & 0 deletions code/go/0chain.net/blobbercore/allocation/zcn.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func SyncAllocation(allocationId string) (*Allocation, error) {
alloc.Finalized = sa.Finalized
alloc.TimeUnit = sa.TimeUnit
alloc.FileOptions = sa.FileOptions
alloc.StorageVersion = uint8(sa.StorageVersion)
alloc.OwnerSigningPublicKey = sa.OwnerSigningPublicKey

// related terms
terms := make([]*Terms, 0, len(sa.BlobberDetails))
Expand Down
6 changes: 4 additions & 2 deletions code/go/0chain.net/blobbercore/challenge/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"github.com/0chain/blobber/code/go/0chain.net/core/transaction"
coreTxn "github.com/0chain/gosdk/core/transaction"
"math/rand"
"strings"
"sync"
"time"

"github.com/0chain/blobber/code/go/0chain.net/core/transaction"
coreTxn "github.com/0chain/gosdk/core/transaction"

"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/blobbercore/filestore"
Expand Down Expand Up @@ -366,6 +367,7 @@ func (cr *ChallengeEntity) getPostDataV2(ctx context.Context, allocationObj *all
FixedMerkleRoot: ref.FixedMerkleRoot,
Size: ref.Size,
FileMetaHash: ref.FileMetaHash,
SignatureVersion: ref.SignatureVersion,
}
postData["meta"] = metaRef
}
Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/blobbercore/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func SetupDefaultConfig() {

viper.SetDefault("max_dirs_files", 50000)
viper.SetDefault("max_objects_dir", 1000)
viper.SetDefault("max_objects_per_gb", 100000)
viper.SetDefault("max_objects_per_gb", 1000)
viper.SetDefault("kv.pebble_dir", "/pebble/data")
viper.SetDefault("kv.pebble_wal_dir", "/pebble/wal")
viper.SetDefault("kv.pebble_cache", 4*1024*1024*1024)
Expand Down
10 changes: 9 additions & 1 deletion code/go/0chain.net/blobbercore/handler/file_command_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,15 @@ func (cmd *UpdateFileCommand) ProcessContent(ctx context.Context, allocationObj
hashData := fmt.Sprintf("%s:%s:%s:%s", cmd.fileChanger.ActualHash, cmd.fileChanger.ValidationRoot, cmd.fileChanger.FixedMerkleRoot, node.Self.ID)
hash = encryption.Hash(hashData)
}
verify, err := encryption.Verify(allocationObj.OwnerPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
var (
err error
verify bool
)
if cmd.fileChanger.SignatureVersion == reference.SignatureV2 {
verify, err = encryption.VerifyEd25519(allocationObj.OwnerSigningPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
} else {
verify, err = encryption.Verify(allocationObj.OwnerPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
}
if err != nil || !verify {
logging.Logger.Error("UpdateFileCommand.VerifySignature", zap.Error(err))
return result, common.NewError("update_error", "Failed to verify validation root signature. ")
Expand Down
14 changes: 11 additions & 3 deletions code/go/0chain.net/blobbercore/handler/file_command_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,18 @@ func (cmd *UploadFileCommand) ProcessContent(ctx context.Context, allocationObj
hashData := fmt.Sprintf("%s:%s:%s:%s", cmd.fileChanger.ActualHash, cmd.fileChanger.ValidationRoot, cmd.fileChanger.FixedMerkleRoot, node.Self.ID)
hash = encryption.Hash(hashData)
}
verify, err := encryption.Verify(allocationObj.OwnerPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
var (
err error
verify bool
)
if cmd.fileChanger.SignatureVersion == reference.SignatureV2 {
verify, err = encryption.VerifyEd25519(allocationObj.OwnerSigningPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
} else {
verify, err = encryption.Verify(allocationObj.OwnerPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
}
if err != nil || !verify {
logging.Logger.Error("UploadFileCommand.VerifySignature", zap.Error(err))
return result, common.NewError("upload_error", "Failed to verify validation root signature. ")
logging.Logger.Error("UploadFileCommand.VerifySignature", zap.Error(err), zap.Int("SignatureVersion", cmd.fileChanger.SignatureVersion), zap.String("Hash", hash), zap.String("ValidationRootSignature", cmd.fileChanger.ValidationRootSignature), zap.String("OwnerSigningPublicKey", allocationObj.OwnerSigningPublicKey), zap.String("OwnerPublicKey", allocationObj.OwnerPublicKey))
return result, common.NewError("upload_error", fmt.Sprintf("%s %d", "Failed to verify validation root signature ", cmd.fileChanger.SignatureVersion))
}
}

Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/blobbercore/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func setupHandlers(s *mux.Router) {
RateLimitByObjectRL(common.ToJSONResponse(WithReadOnlyConnection(ReferencePathV2Handler))))

s.HandleFunc("/v1/file/latestwritemarker/{allocation}",
RateLimitByObjectRL(common.ToJSONResponse(WithReadOnlyConnection(WriteMarkerHandler))))
RateLimitByObjectRL(common.ToJSONResponse(WithConnection(WriteMarkerHandler))))

s.HandleFunc("/v1/file/objecttree/{allocation}",
RateLimitByObjectRL(common.ToStatusCode(WithStatusReadOnlyConnection(ObjectTreeHandler)))).
Expand Down
53 changes: 24 additions & 29 deletions code/go/0chain.net/blobbercore/handler/object_operation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ func (fsh *StorageHandler) Rollback(ctx context.Context, r *http.Request) (*blob
err error
)

allocationObj, err = fsh.verifyAllocation(ctx, allocationId, allocationTx, false)
allocationObj, err = fsh.verifyAllocation(ctx, allocationId, allocationTx, true)
if err != nil {
Logger.Error("Error in verifying allocation", zap.Error(err))
return nil, common.NewError("invalid_parameters", "Invalid allocation id passed."+err.Error())
Expand Down Expand Up @@ -1972,46 +1972,41 @@ func (fsh *StorageHandler) Rollback(ctx context.Context, r *http.Request) (*blob
writemarkerEntity.ClientPublicKey = clientKey
Logger.Info("rollback_writemarker", zap.Any("writemarker", writemarkerEntity.WM))

alloc, err := allocation.Repo.GetByIdAndLock(c, allocationID)
Logger.Info("[rollback]Lock Allocation", zap.Bool("is_redeem_required", alloc.IsRedeemRequired), zap.String("allocation_root", alloc.AllocationRoot), zap.String("latest_wm_redeemed", alloc.LatestRedeemedWM))
if err != nil {
txn.Rollback()
return &result, common.NewError("allocation_read_error", "Error reading the allocation object")
}
Logger.Info("[rollback]Lock Allocation", zap.Bool("is_redeem_required", allocationObj.IsRedeemRequired), zap.String("allocation_root", allocationObj.AllocationRoot), zap.String("latest_wm_redeemed", allocationObj.LatestRedeemedWM))

alloc.BlobberSizeUsed -= latestWriteMarkerEntity.WM.Size
alloc.UsedSize -= latestWriteMarkerEntity.WM.Size
alloc.AllocationRoot = allocationRoot
alloc.FileMetaRoot = fileMetaRoot
alloc.IsRedeemRequired = false
alloc.NumObjects = alloc.PrevNumObjects
alloc.NumBlocks = alloc.PrevNumBlocks
allocationObj.BlobberSizeUsed -= latestWriteMarkerEntity.WM.Size
allocationObj.UsedSize -= latestWriteMarkerEntity.WM.Size
allocationObj.AllocationRoot = allocationRoot
allocationObj.FileMetaRoot = fileMetaRoot
allocationObj.IsRedeemRequired = false
allocationObj.NumObjects = allocationObj.PrevNumObjects
allocationObj.NumBlocks = allocationObj.PrevNumBlocks
updateMap := map[string]interface{}{
"blobber_size_used": alloc.BlobberSizeUsed,
"used_size": alloc.UsedSize,
"allocation_root": alloc.AllocationRoot,
"file_meta_root": alloc.FileMetaRoot,
"blobber_size_used": allocationObj.BlobberSizeUsed,
"used_size": allocationObj.UsedSize,
"allocation_root": allocationObj.AllocationRoot,
"file_meta_root": allocationObj.FileMetaRoot,
"is_redeem_required": false,
"num_objects": alloc.NumObjects,
"num_blocks": alloc.NumBlocks,
"num_objects": allocationObj.NumObjects,
"num_blocks": allocationObj.NumBlocks,
}

updateOption := func(a *allocation.Allocation) {
a.BlobberSizeUsed = alloc.BlobberSizeUsed
a.UsedSize = alloc.UsedSize
a.AllocationRoot = alloc.AllocationRoot
a.FileMetaRoot = alloc.FileMetaRoot
a.IsRedeemRequired = alloc.IsRedeemRequired
a.NumObjects = alloc.NumObjects
a.NumBlocks = alloc.NumBlocks
a.BlobberSizeUsed = allocationObj.BlobberSizeUsed
a.UsedSize = allocationObj.UsedSize
a.AllocationRoot = allocationObj.AllocationRoot
a.FileMetaRoot = allocationObj.FileMetaRoot
a.IsRedeemRequired = allocationObj.IsRedeemRequired
a.NumObjects = allocationObj.NumObjects
a.NumBlocks = allocationObj.NumBlocks
}
writemarkerEntity.Latest = true
err = txn.Create(writemarkerEntity).Error
if err != nil {
txn.Rollback()
return &result, common.NewError("write_marker_error", "Error persisting the write marker "+err.Error())
}
if err = allocation.Repo.UpdateAllocation(c, alloc, updateMap, updateOption); err != nil {
if err = allocation.Repo.UpdateAllocation(c, allocationObj, updateMap, updateOption); err != nil {
txn.Rollback()
return &result, common.NewError("allocation_write_error", "Error persisting the allocation object "+err.Error())
}
Expand All @@ -2024,7 +2019,7 @@ func (fsh *StorageHandler) Rollback(ctx context.Context, r *http.Request) (*blob
var node wmpt.Node
if len(fileMetaRoot) > 0 {
decodedRoot, _ := hex.DecodeString(fileMetaRoot)
node = wmpt.NewHashNode(decodedRoot, alloc.NumBlocks)
node = wmpt.NewHashNode(decodedRoot, allocationObj.NumBlocks)
}
trie.RollbackTrie(node)
}
Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/blobbercore/reference/dbCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (dc *dbCollector) Finalize(ctx context.Context, allocationID, allocationRoo
err := db.Create(&(dc.createdRefs)).Error
if err != nil {
for ind, ref := range dc.createdRefs {
logging.Logger.Error("create_ref_error", zap.String("lookup_hash", ref.LookupHash), zap.String("path", ref.Path), zap.Int("index", ind), zap.String("allocation_root", allocationRoot))
logging.Logger.Error("create_ref_error", zap.String("lookup_hash", ref.LookupHash), zap.String("path", ref.Path), zap.Int("index", ind), zap.String("allocation_root", allocationRoot), zap.Any("ref", ref))
}
return err
}
Expand Down
4 changes: 4 additions & 0 deletions code/go/0chain.net/blobbercore/reference/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (

DIR_LIST_TAG = "dirlist"
FILE_LIST_TAG = "filelist"
SignatureV2 = 1
)

var (
Expand Down Expand Up @@ -91,6 +92,7 @@ type Ref struct {
NumUpdates int64 `gorm:"column:num_of_updates" json:"num_of_updates"`
NumBlockDownloads int64 `gorm:"column:num_of_block_downloads" json:"num_of_block_downloads"`
FilestoreVersion int `gorm:"column:filestore_version" json:"-"`
SignatureVersion int `gorm:"column:signature_version" json:"signature_version" filelist:"signature_version"`
IsEmpty bool `gorm:"-" dirlist:"is_empty"`
HashToBeComputed bool `gorm:"-"`
prevID int64 `gorm:"-"`
Expand Down Expand Up @@ -148,6 +150,7 @@ type PaginatedRef struct { //Gorm smart select fields.
EncryptedKey string `gorm:"column:encrypted_key" json:"encrypted_key,omitempty"`
EncryptedKeyPoint string `gorm:"column:encrypted_key_point" json:"encrypted_key_point,omitempty"`
FileMetaHash string `gorm:"column:file_meta_hash;size:64;not null" dirlist:"file_meta_hash" filelist:"file_meta_hash"`
SignatureVersion int `gorm:"column:signature_version" json:"signature_version,omitempty" filelist:"signature_version"`

CreatedAt common.Timestamp `gorm:"column:created_at" json:"created_at,omitempty"`
UpdatedAt common.Timestamp `gorm:"column:updated_at" json:"updated_at,omitempty"`
Expand All @@ -166,6 +169,7 @@ type RefMeta struct {
FixedMerkleRoot string `json:"fixed_merkle_root"`
Size int64 `json:"size"`
FileMetaHash string `json:"file_meta_hash"`
SignatureVersion int `json:"signature_version"`
}

// GetReferenceLookup hash(allocationID + ":" + path)
Expand Down
Loading

0 comments on commit 1c3fddd

Please sign in to comment.