Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #198 from ar-io/develop
Browse files Browse the repository at this point in the history
chore: devnet to testnet
  • Loading branch information
dtfiedler authored Feb 2, 2024
2 parents 087cfb8 + 58db2a2 commit dab7c68
Show file tree
Hide file tree
Showing 22 changed files with 2,174 additions and 1,942 deletions.
62 changes: 44 additions & 18 deletions src/actions/read/observers.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
import {
EPOCH_BLOCK_LENGTH,
EPOCH_DISTRIBUTION_DELAY,
GATEWAY_REGISTRY_SETTINGS,
TENURE_WEIGHT_PERIOD,
} from '../../constants';
import { getBaselineState, stubbedGatewayData } from '../../tests/stubs';
import {
getBaselineState,
stubbedGatewayData,
stubbedGateways,
stubbedPrescribedObservers,
} from '../../tests/stubs';
import { getEpoch, getPrescribedObservers } from './observers';

describe('getPrescribedObservers', () => {
it('should return the prescribed observers for the current epoch', async () => {
it('should return the prescribed observers for the current epoch from state', async () => {
const state = {
...getBaselineState(),
gateways: {
'a-test-gateway': stubbedGatewayData,
gateways: stubbedGateways,
prescribedObservers: {
[0]: stubbedPrescribedObservers,
},
// no distributions
};
const { result } = await getPrescribedObservers(state);
expect(result).toEqual([
{
compositeWeight: 1 / TENURE_WEIGHT_PERIOD, // gateway started at the same block as the epoch, so it gets the default value
gatewayAddress: 'a-test-gateway',
gatewayRewardRatioWeight: 1,
normalizedCompositeWeight: 1,
observerAddress: 'test-observer-wallet',
observerRewardRatioWeight: 1,
stake: 10000,
stakeWeight: 1,
start: 0,
tenureWeight: 1 / TENURE_WEIGHT_PERIOD, // gateway started at the same block as the epoch, so it gets the default value
},
]);
expect(result).toEqual(state.prescribedObservers[0]);
});
});

it('should return the current array of prescribed observer if not set in state yet', async () => {
const state = {
...getBaselineState(),
gateways: {
// only this gateway will be prescribed
'a-test-gateway': stubbedGatewayData,
},
prescribedObservers: {
// some other epochs prescribed observers
[1]: stubbedPrescribedObservers,
},
// no distributions
};
const { result } = await getPrescribedObservers(state);
expect(result).toEqual([
{
gatewayAddress: 'a-test-gateway',
observerAddress: stubbedGatewayData.observerWallet,
gatewayRewardRatioWeight: 1,
observerRewardRatioWeight: 1,
stake: stubbedGatewayData.operatorStake,
start: 0,
stakeWeight:
stubbedGatewayData.operatorStake /
GATEWAY_REGISTRY_SETTINGS.minOperatorStake,
tenureWeight: 1 / TENURE_WEIGHT_PERIOD, // the gateway started at the same time as the epoch
compositeWeight: 1 / TENURE_WEIGHT_PERIOD,
normalizedCompositeWeight: 1,
},
]);
});

describe('getEpoch', () => {
const state = getBaselineState();

Expand Down
25 changes: 11 additions & 14 deletions src/actions/read/observers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ import {
export const getPrescribedObservers = async (
state: IOState,
): Promise<ContractReadResult> => {
const { gateways, distributions } = state;

if (+SmartWeave.block.height < distributions.epochZeroStartHeight) {
return { result: [] };
}

const { prescribedObservers, distributions } = state;
const { epochStartHeight, epochEndHeight } = getEpochDataForHeight({
currentBlockHeight: new BlockHeight(+SmartWeave.block.height),
epochZeroStartHeight: new BlockHeight(distributions.epochZeroStartHeight),
epochBlockLength: new BlockHeight(EPOCH_BLOCK_LENGTH),
});

const prescribedObservers = await getPrescribedObserversForEpoch({
gateways,
epochStartHeight,
epochEndHeight,
distributions,
minOperatorStake: GATEWAY_REGISTRY_SETTINGS.minOperatorStake,
});
const existingOrComputedObservers =
prescribedObservers[epochStartHeight.valueOf()] ||
(await getPrescribedObserversForEpoch({
gateways: state.gateways,
distributions: state.distributions,
epochStartHeight,
epochEndHeight,
minOperatorStake: GATEWAY_REGISTRY_SETTINGS.minOperatorStake,
}));

return { result: prescribedObservers };
return { result: existingOrComputedObservers };
};

export async function getEpoch(
Expand Down
29 changes: 16 additions & 13 deletions src/actions/write/evolveState.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {
EPOCH_BLOCK_LENGTH,
GATEWAY_REGISTRY_SETTINGS,
NON_CONTRACT_OWNER_MESSAGE,
} from '../../constants';
import { getEpochDataForHeight } from '../../observers';
import {
getEpochDataForHeight,
getPrescribedObserversForEpoch,
} from '../../observers';
import {
BlockHeight,
ContractWriteResult,
Expand All @@ -21,25 +25,24 @@ export const evolveState = async (
throw new ContractError(NON_CONTRACT_OWNER_MESSAGE);
}

const {
epochStartHeight,
epochEndHeight,
epochPeriod,
epochDistributionHeight,
} = getEpochDataForHeight({
const { epochStartHeight, epochEndHeight } = getEpochDataForHeight({
currentBlockHeight: new BlockHeight(+SmartWeave.block.height),
epochZeroStartHeight: new BlockHeight(
state.distributions.epochZeroStartHeight,
),
epochBlockLength: new BlockHeight(EPOCH_BLOCK_LENGTH),
});

state.distributions = {
epochZeroStartHeight: state.distributions.epochZeroStartHeight,
epochStartHeight: epochStartHeight.valueOf(),
epochEndHeight: epochEndHeight.valueOf(),
epochPeriod: epochPeriod.valueOf(),
nextDistributionHeight: epochDistributionHeight.valueOf(),
const prescribedObservers = await getPrescribedObserversForEpoch({
gateways: state.gateways,
distributions: state.distributions,
epochStartHeight,
epochEndHeight,
minOperatorStake: GATEWAY_REGISTRY_SETTINGS.minOperatorStake,
});

state.prescribedObservers = {
[epochStartHeight.valueOf()]: prescribedObservers,
};

return { state };
Expand Down
60 changes: 60 additions & 0 deletions src/actions/write/saveObservations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getBaselineState,
stubbedArweaveTxId,
stubbedGatewayData,
stubbedPrescribedObserver,
} from '../../tests/stubs';
import { IOState, Observations } from '../../types';
import { saveObservations } from './saveObservations';
Expand Down Expand Up @@ -230,6 +231,15 @@ describe('saveObservations', () => {
},
},
observations: existingObservations,
prescribedObservers: {
[0]: [
{
...stubbedPrescribedObserver,
gatewayAddress: 'observer-address',
observerAddress: 'observer-address',
},
],
},
};
// set the current height to one that allows observations to be submitted
SmartWeave.block.height =
Expand Down Expand Up @@ -269,6 +279,15 @@ describe('saveObservations', () => {
observerWallet: 'observer-address',
},
},
prescribedObservers: {
[0]: [
{
...stubbedPrescribedObserver,
gatewayAddress: 'observer-address',
observerAddress: 'observer-address',
},
],
},
};
// set the current height to one that allows observations to be submitted
SmartWeave.block.height =
Expand Down Expand Up @@ -310,6 +329,15 @@ describe('saveObservations', () => {
observerWallet: stubbedArweaveTxId,
},
},
prescribedObservers: {
[0]: [
{
...stubbedPrescribedObserver,
gatewayAddress: 'observer-address',
observerAddress: 'observer-address',
},
],
},
};
const { state } = await saveObservations(initialState, {
caller: 'observer-address',
Expand Down Expand Up @@ -345,6 +373,15 @@ describe('saveObservations', () => {
status: NETWORK_LEAVING_STATUS,
},
},
prescribedObservers: {
[0]: [
{
...stubbedPrescribedObserver,
gatewayAddress: 'observer-address',
observerAddress: 'observer-address',
},
],
},
};
const { state } = await saveObservations(initialState, {
caller: 'observer-address',
Expand Down Expand Up @@ -380,6 +417,15 @@ describe('saveObservations', () => {
observerAddress: stubbedArweaveTxId,
},
},
prescribedObservers: {
[0]: [
{
...stubbedPrescribedObserver,
gatewayAddress: 'observer-address',
observerAddress: 'observer-address',
},
],
},
observations: {},
};
const { state } = await saveObservations(initialState, {
Expand Down Expand Up @@ -434,6 +480,20 @@ describe('saveObservations', () => {
},
},
observations: initialObservationsForEpoch,
prescribedObservers: {
[0]: [
{
...stubbedPrescribedObserver,
gatewayAddress: 'observer-address',
observerAddress: 'observer-address',
},
{
...stubbedPrescribedObserver,
gatewayAddress: 'a-second-observer-address',
observerAddress: 'a-second-observer-address',
},
],
},
};
const { state } = await saveObservations(initialState, {
caller: 'a-second-observer-address',
Expand Down
24 changes: 10 additions & 14 deletions src/actions/write/saveObservations.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {
EPOCH_BLOCK_LENGTH,
EPOCH_DISTRIBUTION_DELAY,
GATEWAY_REGISTRY_SETTINGS,
INVALID_OBSERVATION_CALLER_MESSAGE,
NETWORK_JOIN_STATUS,
} from '../../constants';
import {
getEpochDataForHeight,
getPrescribedObserversForEpoch,
} from '../../observers';
import { getEpochDataForHeight } from '../../observers';
import {
BlockHeight,
ContractWriteResult,
Expand Down Expand Up @@ -50,9 +46,14 @@ export const saveObservations = async (
{ caller, input }: PstAction,
): Promise<ContractWriteResult> => {
// get all other relevant state data
const { observations, gateways, distributions } = state;
const {
observations,
gateways,
distributions,
prescribedObservers: observersForEpoch,
} = state;
const { observerReportTxId, failedGateways } = new SaveObservations(input);
const { epochStartHeight, epochEndHeight } = getEpochDataForHeight({
const { epochStartHeight } = getEpochDataForHeight({
currentBlockHeight: new BlockHeight(+SmartWeave.block.height), // observations must be submitted within the epoch and after the last epochs distribution period (see below)
epochZeroStartHeight: new BlockHeight(distributions.epochZeroStartHeight),
epochBlockLength: new BlockHeight(EPOCH_BLOCK_LENGTH),
Expand All @@ -70,13 +71,8 @@ export const saveObservations = async (
);
}

const prescribedObservers = await getPrescribedObserversForEpoch({
gateways,
epochStartHeight,
epochEndHeight,
distributions,
minOperatorStake: GATEWAY_REGISTRY_SETTINGS.minOperatorStake,
});
const prescribedObservers =
observersForEpoch[epochStartHeight.valueOf()] || [];

// find the observer that is submitting the observation
const observer: WeightedObserver | undefined = prescribedObservers.find(
Expand Down
Loading

0 comments on commit dab7c68

Please sign in to comment.