Skip to content

Commit

Permalink
fix: renterd config naming and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Dec 15, 2023
1 parent b84074f commit 4fa92b8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 99 deletions.
60 changes: 29 additions & 31 deletions apps/renterd/contexts/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useConfigMain() {
isAutopilotEnabled,
autopilot,
contractSet,
configApp,
display,
gouging,
redundancy,
uploadPacking,
Expand Down Expand Up @@ -110,22 +110,22 @@ export function useConfigMain() {
)

const remoteValues: SettingsData = useMemo(() => {
const gougingData = buildGougingData(gouging.data)
const g = buildGougingData(gouging.data)
if (
(!isAutopilotEnabled || autopilot.data || autopilot.error) &&
gougingData &&
g &&
redundancy.data &&
uploadPacking.data &&
(contractSet.data || contractSet.error) &&
(configApp.data || configApp.error)
(display.data || display.error)
) {
return transformDown({
autopilotData: autopilot.data,
contractSetData: contractSet.data,
uploadPackingData: uploadPacking.data,
gougingData,
redundancyData: redundancy.data,
configAppData: configApp.data,
autopilot: autopilot.data,
contractSet: contractSet.data,
uploadPacking: uploadPacking.data,
gouging: g,
redundancy: redundancy.data,
display: display.data,
})
}
return null
Expand All @@ -139,34 +139,32 @@ export function useConfigMain() {
gouging.data,
buildGougingData,
redundancy.data,
configApp.data,
configApp.error,
display.data,
display.error,
])

const revalidate = useCallback(async (): Promise<SettingsData | null> => {
const autopilotData = isAutopilotEnabled
? await autopilot.mutate()
: undefined
const contractSetData = await contractSet.mutate()
const _gougingData = await gouging.mutate()
const redundancyData = await redundancy.mutate()
const uploadPackingData = await uploadPacking.mutate()
const configAppData = await configApp.mutate()
if (!_gougingData || !redundancyData) {
const a = isAutopilotEnabled ? await autopilot.mutate() : undefined
const cs = await contractSet.mutate()
const _g = await gouging.mutate()
const r = await redundancy.mutate()
const up = await uploadPacking.mutate()
const d = await display.mutate()
if (!_g || !r) {
triggerErrorToast('Error fetching settings.')
return null
} else {
const gougingData = buildGougingData(_gougingData)
if (!gougingData) {
const g = buildGougingData(_g)
if (!g) {
return null
}
return transformDown({
autopilotData,
contractSetData,
gougingData,
redundancyData,
uploadPackingData,
configAppData,
autopilot: a,
contractSet: cs,
gouging: g,
redundancy: r,
uploadPacking: up,
display: d,
})
}
}, [
Expand All @@ -177,7 +175,7 @@ export function useConfigMain() {
buildGougingData,
uploadPacking,
redundancy,
configApp,
display,
])

const { isUnlockedAndAuthedRoute } = useAppSettings()
Expand Down Expand Up @@ -258,7 +256,7 @@ export function useConfigMain() {
uploadPacking,
redundancy,
gouging,
configApp,
display,
})

const onInvalid = useOnInvalid(fields)
Expand Down
52 changes: 26 additions & 26 deletions apps/renterd/contexts/config/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('tansforms', () => {
describe('down', () => {
it('default works', () => {
expect(
transformDown(
{
transformDown({
autopilot: {
wallet: {
defragThreshold: 1000,
},
Expand All @@ -43,9 +43,9 @@ describe('tansforms', () => {
prune: true,
},
},
{ default: 'myset' },
{ enabled: true },
{
contractSet: { default: 'myset' },
uploadPacking: { enabled: true },
gouging: {
hostBlockHeightLeeway: 4,
maxContractPrice: '20000000000000000000000000',
maxDownloadPrice: '1004310000000000000000000000',
Expand All @@ -58,15 +58,15 @@ describe('tansforms', () => {
minPriceTableValidity: 300000000000,
migrationSurchargeMultiplier: 10,
},
{
redundancy: {
minShards: 10,
totalShards: 30,
},
{
display: {
includeRedundancyMaxStoragePrice: false,
includeRedundancyMaxUploadPrice: false,
}
)
},
})
).toEqual({
autopilotContractSet: 'autopilot',
allowanceMonth: new BigNumber('500'),
Expand Down Expand Up @@ -103,8 +103,8 @@ describe('tansforms', () => {

it('with include redundancy for storage and upload', () => {
expect(
transformDown(
{
transformDown({
autopilot: {
wallet: {
defragThreshold: 1000,
},
Expand All @@ -126,9 +126,9 @@ describe('tansforms', () => {
prune: true,
},
},
{ default: 'myset' },
{ enabled: true },
{
contractSet: { default: 'myset' },
uploadPacking: { enabled: true },
gouging: {
hostBlockHeightLeeway: 4,
maxContractPrice: '20000000000000000000000000',
maxDownloadPrice: '1004310000000000000000000000',
Expand All @@ -141,15 +141,15 @@ describe('tansforms', () => {
minPriceTableValidity: 300000000000,
migrationSurchargeMultiplier: 10,
},
{
redundancy: {
minShards: 10,
totalShards: 30,
},
{
display: {
includeRedundancyMaxStoragePrice: true,
includeRedundancyMaxUploadPrice: true,
}
)
},
})
).toEqual({
autopilotContractSet: 'autopilot',
allowanceMonth: new BigNumber('6006'),
Expand Down Expand Up @@ -506,8 +506,8 @@ describe('tansforms', () => {
redundancy,
display,
} = buildAllResponses()
let settings = transformDown(
{
let settings = transformDown({
autopilot: {
...autopilot,
contracts: {
...autopilot.contracts,
Expand All @@ -519,16 +519,16 @@ describe('tansforms', () => {
uploadPacking,
gouging,
redundancy,
display
)
display,
})
expect(settings.downloadTBMonth).toEqual(new BigNumber('92.72'))
// a little different due to rounding
expect(
transformUpAutopilot('Mainnet', settings, autopilot).contracts.download
).toEqual(91088814814815)

settings = transformDown(
{
settings = transformDown({
autopilot: {
...autopilot,
contracts: {
...autopilot.contracts,
Expand All @@ -540,8 +540,8 @@ describe('tansforms', () => {
uploadPacking,
gouging,
redundancy,
display
)
display,
})
expect(settings.downloadTBMonth).toEqual(new BigNumber('92.72'))
// using the rounded value results in same value
expect(
Expand Down
56 changes: 27 additions & 29 deletions apps/renterd/contexts/config/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import {
scDecimalPlaces,
SettingsData,
getAdvancedDefaultAutopilot,
ConfigAppData,
DisplayData,
ContractSetData,
defaultConfigApp,
defaultDisplay,
defaultContractSet,
GougingData,
RedundancyData,
Expand Down Expand Up @@ -184,8 +184,8 @@ export function transformUpRedundancy(
}
}

export function transformUpConfigApp(
values: ConfigAppData,
export function transformUpDisplay(
values: DisplayData,
existingValues: Record<string, unknown> | undefined
): ConfigDisplayOptions {
return {
Expand Down Expand Up @@ -276,11 +276,9 @@ export function transformDownUploadPacking(
}
}

export function transformDownConfigApp(
ca?: ConfigDisplayOptions
): ConfigAppData {
export function transformDownConfigApp(ca?: ConfigDisplayOptions): DisplayData {
if (!ca) {
return defaultConfigApp
return defaultDisplay
}
return {
includeRedundancyMaxStoragePrice: ca.includeRedundancyMaxStoragePrice,
Expand All @@ -291,7 +289,7 @@ export function transformDownConfigApp(
export function transformDownGouging(
g: GougingSettings,
r: RedundancyData,
ca: ConfigAppData
ca: DisplayData
): GougingData {
return {
maxStoragePriceTBMonth: toSiacoins(
Expand Down Expand Up @@ -344,37 +342,37 @@ export function transformDownRedundancy(r: RedundancySettings): RedundancyData {
}

export type RemoteData = {
autopilotData: AutopilotConfig | undefined
contractSetData: ContractSetSettings | undefined
uploadPackingData: UploadPackingSettings
gougingData: GougingSettings
redundancyData: RedundancySettings
configAppData: ConfigDisplayOptions | undefined
autopilot: AutopilotConfig | undefined
contractSet: ContractSetSettings | undefined
uploadPacking: UploadPackingSettings
gouging: GougingSettings
redundancy: RedundancySettings
display: ConfigDisplayOptions | undefined
}

export function transformDown({
autopilotData,
contractSetData,
uploadPackingData,
gougingData,
redundancyData,
configAppData,
autopilot,
contractSet,
uploadPacking,
gouging,
redundancy,
display,
}: RemoteData): SettingsData {
const configApp = transformDownConfigApp(configAppData)
const redundancy = transformDownRedundancy(redundancyData)
const d = transformDownConfigApp(display)
const r = transformDownRedundancy(redundancy)
return {
// autopilot
...transformDownAutopilot(autopilotData),
...transformDownAutopilot(autopilot),
// contractset
...transformDownContractSet(contractSetData),
...transformDownContractSet(contractSet),
// uploadpacking
...transformDownUploadPacking(uploadPackingData),
...transformDownUploadPacking(uploadPacking),
// gouging
...transformDownGouging(gougingData, redundancy, configApp),
...transformDownGouging(gouging, r, d),
// redundancy
...redundancy,
...r,
// config app
...configApp,
...d,
}
}

Expand Down
14 changes: 7 additions & 7 deletions apps/renterd/contexts/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const defaultUploadPacking = {
uploadPackingEnabled: true,
}

export const defaultConfigApp = {
export const defaultDisplay = {
includeRedundancyMaxStoragePrice: true,
includeRedundancyMaxUploadPrice: true,
}
Expand Down Expand Up @@ -65,14 +65,14 @@ export const defaultValues = {
...defaultGouging,
// redundancy
...defaultRedundancy,
// config app
...defaultConfigApp,
// config display
...defaultDisplay,
}

export type AutopilotData = typeof defaultAutopilot
export type ContractSetData = typeof defaultContractSet
export type UploadPackingData = typeof defaultUploadPacking
export type ConfigAppData = typeof defaultConfigApp
export type DisplayData = typeof defaultDisplay
export type GougingData = typeof defaultGouging
export type RedundancyData = typeof defaultRedundancy
export type SettingsData = typeof defaultValues
Expand Down Expand Up @@ -120,8 +120,8 @@ export const advancedDefaultContractSet: ContractSetData = {
defaultContractSet: 'autopilot',
}

export const advancedDefaultConfigApp: ConfigAppData = {
...defaultConfigApp,
export const advancedDefaultDisplay: DisplayData = {
...defaultDisplay,
}

export const advancedDefaultUploadPacking: UploadPackingData = {
Expand Down Expand Up @@ -152,7 +152,7 @@ export function getAdvancedDefaults(
return {
...getAdvancedDefaultAutopilot(network),
...advancedDefaultContractSet,
...advancedDefaultConfigApp,
...advancedDefaultDisplay,
...advancedDefaultUploadPacking,
...advancedDefaultGouging,
...getAdvancedDefaultRedundancy(network),
Expand Down
Loading

0 comments on commit 4fa92b8

Please sign in to comment.