From c5b436605ad86bfd5f9889e3201fa4de508e56e7 Mon Sep 17 00:00:00 2001 From: Piers Shepperson Date: Thu, 24 Jun 2021 15:09:34 +0100 Subject: [PATCH 1/6] add immutable checks --- .../blobbercore/allocation/entity.go | 1 + .../blobbercore/allocation/protocol.go | 1 + .../handler/object_operation_handler.go | 21 +++++++++++++++++++ code/go/0chain.net/core/transaction/entity.go | 3 ++- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/code/go/0chain.net/blobbercore/allocation/entity.go b/code/go/0chain.net/blobbercore/allocation/entity.go index 62a11125a..4f0c37aed 100644 --- a/code/go/0chain.net/blobbercore/allocation/entity.go +++ b/code/go/0chain.net/blobbercore/allocation/entity.go @@ -32,6 +32,7 @@ type Allocation struct { LatestRedeemedWM string `gorm:"column:latest_redeemed_write_marker"` IsRedeemRequired bool `gorm:"column:is_redeem_required"` TimeUnit time.Duration `gorm:"column:time_unit"` + IsImmutable bool `gorm:"is_immutable"` // ending and cleaning CleanedUp bool `gorm:"column:cleaned_up"` Finalized bool `gorm:"column:finalized"` diff --git a/code/go/0chain.net/blobbercore/allocation/protocol.go b/code/go/0chain.net/blobbercore/allocation/protocol.go index 01c07bf26..eebbd939a 100644 --- a/code/go/0chain.net/blobbercore/allocation/protocol.go +++ b/code/go/0chain.net/blobbercore/allocation/protocol.go @@ -127,6 +127,7 @@ func VerifyAllocationTransaction(ctx context.Context, allocationTx string, a.Finalized = sa.Finalized a.PayerID = t.ClientID a.TimeUnit = sa.TimeUnit + a.IsImmutable = sa.IsImmutable // related terms a.Terms = make([]*Terms, 0, len(sa.BlobberDetails)) 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 59e6ddd26..eb3091ca4 100644 --- a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go +++ b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go @@ -424,6 +424,10 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*C return nil, common.NewError("invalid_parameters", "Invalid allocation id passed."+err.Error()) } + if allocationObj.IsImmutable { + return nil, common.NewError("immutable_allocation", "Cannot write to an immutable allocation") + } + allocationID := allocationObj.ID connectionID := r.FormValue("connection_id") @@ -590,6 +594,11 @@ func (fsh *StorageHandler) RenameObject(ctx context.Context, r *http.Request) (i if err != nil { return nil, common.NewError("invalid_parameters", "Invalid allocation id passed."+err.Error()) } + + if allocationObj.IsImmutable { + return nil, common.NewError("immutable_allocation", "Cannot rename data in an immutable allocation") + } + allocationID := allocationObj.ID clientID := ctx.Value(constants.CLIENT_CONTEXT_KEY).(string) @@ -688,6 +697,10 @@ func (fsh *StorageHandler) UpdateObjectAttributes(ctx context.Context, return nil, common.NewError("invalid_signature", "Invalid signature") } + if alloc.IsImmutable { + return nil, common.NewError("immutable_allocation", "Cannot update data in an immutable allocation") + } + // runtime type check _ = ctx.Value(constants.CLIENT_KEY_CONTEXT_KEY).(string) @@ -784,6 +797,10 @@ func (fsh *StorageHandler) CopyObject(ctx context.Context, r *http.Request) (int return nil, common.NewError("invalid_signature", "Invalid signature") } + if allocationObj.IsImmutable { + return nil, common.NewError("immutable_allocation", "Cannot copy data in an immutable allocation") + } + clientID := ctx.Value(constants.CLIENT_CONTEXT_KEY).(string) _ = ctx.Value(constants.CLIENT_KEY_CONTEXT_KEY).(string) @@ -916,6 +933,10 @@ func (fsh *StorageHandler) WriteFile(ctx context.Context, r *http.Request) (*Upl return nil, common.NewError("invalid_signature", "Invalid signature") } + if allocationObj.IsImmutable { + return nil, common.NewError("immutable_allocation", "Cannot write to an immutable allocation") + } + allocationID := allocationObj.ID if len(clientID) == 0 { diff --git a/code/go/0chain.net/core/transaction/entity.go b/code/go/0chain.net/core/transaction/entity.go index 0f17ae06e..4683b0df7 100644 --- a/code/go/0chain.net/core/transaction/entity.go +++ b/code/go/0chain.net/core/transaction/entity.go @@ -69,7 +69,7 @@ type StakePoolSettings struct { } type StorageNodeGeolocation struct { - Latitude float64 `json:"latitude"` + Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` } @@ -101,6 +101,7 @@ type StorageAllocation struct { Finalized bool `json:"finalized"` CCT time.Duration `json:"challenge_completion_time"` TimeUnit time.Duration `json:"time_unit"` + IsImmutable bool `json:"is_immutable"` } func (sa *StorageAllocation) Until() common.Timestamp { From b2744e476132f278072648e4e0e2dde2f658de11 Mon Sep 17 00:00:00 2001 From: Piers Shepperson Date: Thu, 24 Jun 2021 17:56:19 +0100 Subject: [PATCH 2/6] add immutable gorm --- code/go/0chain.net/blobbercore/allocation/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/go/0chain.net/blobbercore/allocation/entity.go b/code/go/0chain.net/blobbercore/allocation/entity.go index 4f0c37aed..410204cfe 100644 --- a/code/go/0chain.net/blobbercore/allocation/entity.go +++ b/code/go/0chain.net/blobbercore/allocation/entity.go @@ -32,7 +32,7 @@ type Allocation struct { LatestRedeemedWM string `gorm:"column:latest_redeemed_write_marker"` IsRedeemRequired bool `gorm:"column:is_redeem_required"` TimeUnit time.Duration `gorm:"column:time_unit"` - IsImmutable bool `gorm:"is_immutable"` + IsImmutable bool `gorm:"column:is_immutable"` // ending and cleaning CleanedUp bool `gorm:"column:cleaned_up"` Finalized bool `gorm:"column:finalized"` From 27153fa591de42e136113146b38046fd09b4374c Mon Sep 17 00:00:00 2001 From: Piers Shepperson Date: Thu, 24 Jun 2021 18:47:57 +0100 Subject: [PATCH 3/6] add immutable sql --- sql/01-create-table.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/sql/01-create-table.sql b/sql/01-create-table.sql index b8426aad1..b9704893d 100644 --- a/sql/01-create-table.sql +++ b/sql/01-create-table.sql @@ -21,6 +21,7 @@ CREATE TABLE allocations( blobber_size_used BIGINT NOT NULL DEFAULT 0, latest_redeemed_write_marker VARCHAR(255), is_redeem_required BOOLEAN, + is_immutable BOOLEAN, created_at TIMESTAMP NOT NULL DEFAULT NOW(), updated_at TIMESTAMP NOT NULL DEFAULT NOW() ); From c03f7dd57b94ad2ceaa5b82edb046778902c02b5 Mon Sep 17 00:00:00 2001 From: Piers Shepperson Date: Fri, 25 Jun 2021 15:42:24 +0100 Subject: [PATCH 4/6] update sql --- sql/01-create-table.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql/01-create-table.sql b/sql/01-create-table.sql index b9704893d..5250b7d79 100644 --- a/sql/01-create-table.sql +++ b/sql/01-create-table.sql @@ -15,6 +15,8 @@ CREATE TABLE allocations( used_size BIGINT NOT NULL DEFAULT 0, owner_id VARCHAR(64) NOT NULL, owner_public_key VARCHAR(256) NOT NULL, + repairer_id VARCHAR (64), + payer_id VARCHAR(64), expiration_date BIGINT NOT NULL, allocation_root VARCHAR(255) NOT NULL DEFAULT '', blobber_size BIGINT NOT NULL DEFAULT 0, From b5d77265b563c86a286cae18850ede7b0033da62 Mon Sep 17 00:00:00 2001 From: Piers Shepperson Date: Fri, 25 Jun 2021 17:11:38 +0100 Subject: [PATCH 5/6] update sql --- sql/01-create-table.sql | 3 --- sql/15-add-allocation-columns.sql | 6 ++++++ 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 sql/15-add-allocation-columns.sql diff --git a/sql/01-create-table.sql b/sql/01-create-table.sql index 5250b7d79..b8426aad1 100644 --- a/sql/01-create-table.sql +++ b/sql/01-create-table.sql @@ -15,15 +15,12 @@ CREATE TABLE allocations( used_size BIGINT NOT NULL DEFAULT 0, owner_id VARCHAR(64) NOT NULL, owner_public_key VARCHAR(256) NOT NULL, - repairer_id VARCHAR (64), - payer_id VARCHAR(64), expiration_date BIGINT NOT NULL, allocation_root VARCHAR(255) NOT NULL DEFAULT '', blobber_size BIGINT NOT NULL DEFAULT 0, blobber_size_used BIGINT NOT NULL DEFAULT 0, latest_redeemed_write_marker VARCHAR(255), is_redeem_required BOOLEAN, - is_immutable BOOLEAN, created_at TIMESTAMP NOT NULL DEFAULT NOW(), updated_at TIMESTAMP NOT NULL DEFAULT NOW() ); diff --git a/sql/15-add-allocation-columns.sql b/sql/15-add-allocation-columns.sql new file mode 100644 index 000000000..737495275 --- /dev/null +++ b/sql/15-add-allocation-columns.sql @@ -0,0 +1,6 @@ +\connect blobber_meta; + +BEGIN; + ALTER TABLE allocations ADD COLUMN repairer_id VARCHAR(64) NOT NULL; + ALTER TABLE allocations ADD COLUMN is_immutable BOOLEAN; +COMMIT; \ No newline at end of file From 05a310106748057fcd2e8ed46a160f7f33eac57c Mon Sep 17 00:00:00 2001 From: Piers Shepperson Date: Fri, 25 Jun 2021 17:12:53 +0100 Subject: [PATCH 6/6] update sql --- sql/15-add-allocation-columns.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/15-add-allocation-columns.sql b/sql/15-add-allocation-columns.sql index 737495275..c983cf73f 100644 --- a/sql/15-add-allocation-columns.sql +++ b/sql/15-add-allocation-columns.sql @@ -2,5 +2,5 @@ BEGIN; ALTER TABLE allocations ADD COLUMN repairer_id VARCHAR(64) NOT NULL; - ALTER TABLE allocations ADD COLUMN is_immutable BOOLEAN; + ALTER TABLE allocations ADD COLUMN is_immutable BOOLEAN NOT NULL; COMMIT; \ No newline at end of file