Skip to content

Commit

Permalink
stores: update usability enum
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Nov 8, 2024
1 parent a081202 commit 7f2d604
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 33 deletions.
1 change: 0 additions & 1 deletion api/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
)

const (
ContractUsabilityInvalid = "invalid"
ContractUsabilityBad = "bad"
ContractUsabilityGoodForRenew = "goodforrenew"
)
Expand Down
4 changes: 2 additions & 2 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func (b *Bus) addContract(ctx context.Context, rev rhpv2.ContractRevision, contr
HostKey: rev.HostKey(),
StartHeight: startHeight,
State: state,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
WindowStart: rev.Revision.WindowStart,
WindowEnd: rev.Revision.WindowEnd,
ContractPrice: contractPrice,
Expand Down Expand Up @@ -576,7 +576,7 @@ func (b *Bus) addRenewal(ctx context.Context, renewedFrom types.FileContractID,
RenewedFrom: renewedFrom,
StartHeight: startHeight,
State: state,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
WindowStart: rev.Revision.WindowStart,
WindowEnd: rev.Revision.WindowEnd,
ContractPrice: contractPrice,
Expand Down
2 changes: 1 addition & 1 deletion internal/test/e2e/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ func TestUnconfirmedContractArchival(t *testing.T) {
HostKey: c.HostKey,
StartHeight: cs.BlockHeight,
State: api.ContractStatePending,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
WindowStart: math.MaxUint32,
WindowEnd: math.MaxUint32 + 10,
ContractPrice: types.NewCurrency64(1),
Expand Down
12 changes: 6 additions & 6 deletions stores/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func TestSQLContractStore(t *testing.T) {
Size: 4,
StartHeight: 5,
State: api.ContractStateActive,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
WindowStart: 6,
WindowEnd: 7,

Expand Down Expand Up @@ -659,7 +659,7 @@ func newTestContract(fcid types.FileContractID, hk types.PublicKey) api.Contract
ID: fcid,
HostKey: hk,
State: api.ContractStatePending,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
ContractPrice: types.NewCurrency64(1),
InitialRenterFunds: types.NewCurrency64(2),
}
Expand Down Expand Up @@ -837,7 +837,7 @@ func TestSQLMetadataStore(t *testing.T) {
ID: fcid1,
HostKey: hk1,
State: api.ContractStatePending,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
ContractPrice: types.NewCurrency64(1),
InitialRenterFunds: types.NewCurrency64(2),
}
Expand All @@ -860,7 +860,7 @@ func TestSQLMetadataStore(t *testing.T) {
ID: fcid2,
HostKey: hk2,
State: api.ContractStatePending,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
ContractPrice: types.NewCurrency64(1),
InitialRenterFunds: types.NewCurrency64(2),
}
Expand Down Expand Up @@ -4787,7 +4787,7 @@ func TestPutContract(t *testing.T) {
Size: 6,
StartHeight: 7,
State: api.ContractStateComplete,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
WindowStart: 8,
WindowEnd: 9,

Expand Down Expand Up @@ -4827,7 +4827,7 @@ func TestPutContract(t *testing.T) {
Size: 21,
StartHeight: 22,
State: api.ContractStateFailed,
Usability: api.ContractUsabilityInvalid,
Usability: api.ContractUsabilityGoodForRenew,
WindowStart: 23,
WindowEnd: 24,

Expand Down
20 changes: 3 additions & 17 deletions stores/sql/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,40 +81,26 @@ const (
contractUsabilityGoodForRenew
)

func ContractUsabilityFromString(usability string) ContractUsability {
switch strings.ToLower(usability) {
case api.ContractUsabilityBad:
return contractUsabilityBad
case api.ContractUsabilityGoodForRenew:
return contractUsabilityGoodForRenew
default:
return contractUsabilityInvalid
}
}

func (s *ContractUsability) LoadString(usability string) error {
switch strings.ToLower(usability) {
case api.ContractUsabilityInvalid:
*s = contractUsabilityInvalid
case api.ContractUsabilityBad:
*s = contractUsabilityBad
case api.ContractUsabilityGoodForRenew:
*s = contractUsabilityGoodForRenew
default:
return ErrInvalidContractUsability
*s = contractUsabilityInvalid
return ErrInvalidContractState
}
return nil
}

func (s ContractUsability) String() string {
switch s {
case contractUsabilityInvalid:
return api.ContractUsabilityInvalid
case contractUsabilityBad:
return api.ContractUsabilityBad
case contractUsabilityGoodForRenew:
return api.ContractUsabilityGoodForRenew
default:
return api.ContractUsabilityInvalid
return "invalid"
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ALTER TABLE contracts ADD COLUMN usability tinyint unsigned NOT NULL DEFAULT '0';
CREATE INDEX `idx_contracts_usability` ON contracts (`usability`);
ALTER TABLE contracts ADD COLUMN usability tinyint unsigned NOT NULL;
CREATE INDEX `idx_contracts_usability` ON contracts (`usability`);
UPDATE contracts SET usability = 1 WHERE 1=1;
2 changes: 1 addition & 1 deletion stores/sql/mysql/migrations/main/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ CREATE TABLE `contracts` (
`size` bigint unsigned DEFAULT NULL,
`start_height` bigint unsigned NOT NULL,
`state` tinyint unsigned NOT NULL DEFAULT '0',
`usability` tinyint unsigned NOT NULL DEFAULT '0',
`usability` tinyint unsigned NOT NULL,
`window_start` bigint unsigned NOT NULL DEFAULT '0',
`window_end` bigint unsigned NOT NULL DEFAULT '0',

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ALTER TABLE contracts ADD COLUMN usability INTEGER NOT NULL DEFAULT 0;
CREATE INDEX idx_contracts_usability ON contracts (usability);
ALTER TABLE contracts ADD COLUMN usability INTEGER NOT NULL;
CREATE INDEX idx_contracts_usability ON contracts (usability);
UPDATE contracts SET usability = 1 WHERE 1=1;
2 changes: 1 addition & 1 deletion stores/sql/sqlite/migrations/main/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE INDEX `idx_hosts_public_key` ON `hosts`(`public_key`);
CREATE INDEX `idx_hosts_net_address` ON `hosts`(`net_address`);

-- dbContract
CREATE TABLE contracts (`id` integer PRIMARY KEY AUTOINCREMENT, `created_at` datetime, `fcid` blob NOT NULL UNIQUE, `host_id` integer, `host_key` blob NOT NULL,`v2` INTEGER NOT NULL, `archival_reason` text DEFAULT NULL, `proof_height` integer DEFAULT 0, `renewed_from` blob, `renewed_to` blob, `revision_height` integer DEFAULT 0, `revision_number` text NOT NULL DEFAULT "0", `size` integer, `start_height` integer NOT NULL, `state` integer NOT NULL DEFAULT 0, `usability` integer NOT NULL DEFAULT 0, `window_start` integer NOT NULL DEFAULT 0, `window_end` integer NOT NULL DEFAULT 0, `contract_price` text, `initial_renter_funds` text, `delete_spending` text, `fund_account_spending` text, `sector_roots_spending` text, `upload_spending` text, CONSTRAINT `fk_contracts_host` FOREIGN KEY (`host_id`) REFERENCES `hosts`(`id`));
CREATE TABLE contracts (`id` integer PRIMARY KEY AUTOINCREMENT, `created_at` datetime, `fcid` blob NOT NULL UNIQUE, `host_id` integer, `host_key` blob NOT NULL,`v2` INTEGER NOT NULL, `archival_reason` text DEFAULT NULL, `proof_height` integer DEFAULT 0, `renewed_from` blob, `renewed_to` blob, `revision_height` integer DEFAULT 0, `revision_number` text NOT NULL DEFAULT "0", `size` integer, `start_height` integer NOT NULL, `state` integer NOT NULL DEFAULT 0, `usability` integer NOT NULL, `window_start` integer NOT NULL DEFAULT 0, `window_end` integer NOT NULL DEFAULT 0, `contract_price` text, `initial_renter_funds` text, `delete_spending` text, `fund_account_spending` text, `sector_roots_spending` text, `upload_spending` text, CONSTRAINT `fk_contracts_host` FOREIGN KEY (`host_id`) REFERENCES `hosts`(`id`));
CREATE INDEX `idx_contracts_archival_reason` ON `contracts`(`archival_reason`);
CREATE INDEX `idx_contracts_fcid` ON `contracts`(`fcid`);
CREATE INDEX `idx_contracts_host_id` ON `contracts`(`host_id`);
Expand Down

0 comments on commit 7f2d604

Please sign in to comment.