Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reduce contract sizes to fit 24kb HorizonStaking and SubgraphService #1088

Open
wants to merge 2 commits into
base: mde/horizon-migrate-scripts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,12 @@ interface IHorizonStakingMain {
* @param beneficiary The address where the tokens will be withdrawn after thawing
* @return The ID of the thaw request
*/
// function undelegateWithBeneficiary(
// address serviceProvider,
// address verifier,
// uint256 shares,
// address beneficiary
// ) external returns (bytes32);
function undelegateWithBeneficiary(
address serviceProvider,
address verifier,
uint256 shares,
address beneficiary
) external returns (bytes32);

/**
* @notice Withdraw undelegated tokens from a provision after thawing.
Expand Down Expand Up @@ -815,11 +815,11 @@ interface IHorizonStakingMain {
* @param verifier The verifier address
* @param nThawRequests The number of thaw requests to fulfill. Set to 0 to fulfill all thaw requests.
*/
// function withdrawDelegatedWithBeneficiary(
// address serviceProvider,
// address verifier,
// uint256 nThawRequests
// ) external;
function withdrawDelegatedWithBeneficiary(
address serviceProvider,
address verifier,
uint256 nThawRequests
) external;

/**
* @notice Re-delegate undelegated tokens from a provision after thawing to a `newServiceProvider` and `newVerifier`.
Expand Down Expand Up @@ -864,26 +864,6 @@ interface IHorizonStakingMain {
uint256 feeCut
) external;

/**
* @notice Delegate tokens to the subgraph data service provision.
* This function is for backwards compatibility with the legacy staking contract.
* It only allows delegating to the subgraph data service and DOES NOT have slippage protection.
* @dev See {delegate}.
* @param serviceProvider The service provider address
* @param tokens The amount of tokens to delegate
*/
function delegate(address serviceProvider, uint256 tokens) external;

/**
* @notice Undelegate tokens from the subgraph data service provision and start thawing them.
* This function is for backwards compatibility with the legacy staking contract.
* It only allows undelegating from the subgraph data service.
* @dev See {undelegate}.
* @param serviceProvider The service provider address
* @param shares The amount of shares to undelegate
*/
function undelegate(address serviceProvider, uint256 shares) external;

/**
* @notice Withdraw undelegated tokens from the subgraph data service provision after thawing.
* This function is for backwards compatibility with the legacy staking contract.
Expand Down
64 changes: 24 additions & 40 deletions packages/horizon/contracts/staking/HorizonStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,15 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain {
/**
* @notice See {IHorizonStakingMain-undelegate}.
*/
// function undelegateWithBeneficiary(
// address serviceProvider,
// address verifier,
// uint256 shares,
// address beneficiary
// ) external override notPaused returns (bytes32) {
// require(beneficiary != address(0), HorizonStakingInvalidBeneficiaryZeroAddress());
// return _undelegate(ThawRequestType.DelegationWithBeneficiary, serviceProvider, verifier, shares, beneficiary);
// }
function undelegateWithBeneficiary(
address serviceProvider,
address verifier,
uint256 shares,
address beneficiary
) external override notPaused returns (bytes32) {
require(beneficiary != address(0), HorizonStakingInvalidBeneficiaryZeroAddress());
return _undelegate(ThawRequestType.DelegationWithBeneficiary, serviceProvider, verifier, shares, beneficiary);
}

/**
* @notice See {IHorizonStakingMain-withdrawDelegated}.
Expand All @@ -342,21 +342,21 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain {
/**
* @notice See {IHorizonStakingMain-withdrawDelegatedWithBeneficiary}.
*/
// function withdrawDelegatedWithBeneficiary(
// address serviceProvider,
// address verifier,
// uint256 nThawRequests
// ) external override notPaused {
// _withdrawDelegated(
// ThawRequestType.DelegationWithBeneficiary,
// serviceProvider,
// verifier,
// address(0),
// address(0),
// 0,
// nThawRequests
// );
// }
function withdrawDelegatedWithBeneficiary(
address serviceProvider,
address verifier,
uint256 nThawRequests
) external override notPaused {
_withdrawDelegated(
ThawRequestType.DelegationWithBeneficiary,
serviceProvider,
verifier,
address(0),
address(0),
0,
nThawRequests
);
}

/**
* @notice See {IHorizonStakingMain-redelegate}.
Expand Down Expand Up @@ -396,22 +396,6 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain {
emit DelegationFeeCutSet(serviceProvider, verifier, paymentType, feeCut);
}

/**
* @notice See {IHorizonStakingMain-delegate}.
*/
function delegate(address serviceProvider, uint256 tokens) external override notPaused {
require(tokens != 0, HorizonStakingInvalidZeroTokens());
_graphToken().pullTokens(msg.sender, tokens);
_delegate(serviceProvider, SUBGRAPH_DATA_SERVICE_ADDRESS, tokens, 0);
}

/**
* @notice See {IHorizonStakingMain-undelegate}.
*/
function undelegate(address serviceProvider, uint256 shares) external override notPaused {
_undelegate(ThawRequestType.Delegation, serviceProvider, SUBGRAPH_DATA_SERVICE_ADDRESS, shares, msg.sender);
}

/**
* @notice See {IHorizonStakingMain-withdrawDelegated}.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/horizon/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ test = 'test'
cache_path = 'cache_forge'
fs_permissions = [{ access = "read", path = "./"}]
optimizer = true
optimizer_runs = 200
optimizer_runs = 50
1 change: 1 addition & 0 deletions packages/horizon/scripts/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async function main() {

console.log('Using deployer account:', deployer.address)
console.log('Using governor account:', governor.address)
console.log('==============================================')

const HorizonMigrateConfig = removeNFromBigInts(require('../ignition/configs/horizon-migrate.hardhat.json5'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,8 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest {
}

function _delegate(address serviceProvider, address verifier, uint256 tokens, uint256 minSharesOut) internal {
__delegate(serviceProvider, verifier, tokens, minSharesOut, false);
}

function _delegate(address serviceProvider, uint256 tokens) internal {
__delegate(serviceProvider, subgraphDataServiceLegacyAddress, tokens, 0, true);
bool legacy = verifier == subgraphDataServiceLegacyAddress;
__delegate(serviceProvider, verifier, tokens, minSharesOut, legacy);
}

function __delegate(
Expand Down Expand Up @@ -865,11 +862,7 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest {
token.approve(address(staking), tokens);
vm.expectEmit();
emit IHorizonStakingMain.TokensDelegated(serviceProvider, verifier, delegator, tokens);
if (legacy) {
staking.delegate(serviceProvider, tokens);
} else {
staking.delegate(serviceProvider, verifier, tokens, minSharesOut);
}
staking.delegate(serviceProvider, verifier, tokens, minSharesOut);

// after
DelegationPoolInternalTest memory afterPool = _getStorage_DelegationPoolInternal(
Expand Down Expand Up @@ -905,7 +898,8 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest {

function _undelegate(address serviceProvider, address verifier, uint256 shares) internal {
(, address caller, ) = vm.readCallers();
__undelegate(IHorizonStakingTypes.ThawRequestType.Delegation, serviceProvider, verifier, shares, false, caller);
bool legacy = verifier == subgraphDataServiceLegacyAddress;
__undelegate(IHorizonStakingTypes.ThawRequestType.Delegation, serviceProvider, verifier, shares, legacy, caller);
}

function _undelegateWithBeneficiary(address serviceProvider, address verifier, uint256 shares, address beneficiary) internal {
Expand All @@ -919,18 +913,6 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest {
);
}

function _undelegate(address serviceProvider, uint256 shares) internal {
(, address caller, ) = vm.readCallers();
__undelegate(
IHorizonStakingTypes.ThawRequestType.Delegation,
serviceProvider,
subgraphDataServiceLegacyAddress,
shares,
true,
caller
);
}

struct BeforeValues_Undelegate {
DelegationPoolInternalTest pool;
DelegationInternal delegation;
Expand Down Expand Up @@ -988,9 +970,7 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest {
);
vm.expectEmit();
emit IHorizonStakingMain.TokensUndelegated(serviceProvider, verifier, delegator, calcValues.tokens);
if (legacy) {
staking.undelegate(serviceProvider, shares);
} else if (thawRequestType == IHorizonStakingTypes.ThawRequestType.Delegation) {
if (thawRequestType == IHorizonStakingTypes.ThawRequestType.Delegation) {
staking.undelegate(serviceProvider, verifier, shares);
} else if (thawRequestType == IHorizonStakingTypes.ThawRequestType.DelegationWithBeneficiary) {
staking.undelegateWithBeneficiary(serviceProvider, verifier, shares, beneficiary);
Expand Down
2 changes: 1 addition & 1 deletion packages/horizon/test/staking/delegation/delegate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ contract HorizonStakingDelegateTest is HorizonStakingTest {
_createProvision(users.indexer, subgraphDataServiceLegacyAddress, amount, 0, 0);

resetPrank(users.delegator);
_delegate(users.indexer, delegationAmount);
_delegate(users.indexer, subgraphDataServiceLegacyAddress, delegationAmount, 0);
}

function testDelegate_RevertWhen_InvalidPool(
Expand Down
4 changes: 2 additions & 2 deletions packages/horizon/test/staking/delegation/undelegate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ contract HorizonStakingUndelegateTest is HorizonStakingTest {
_createProvision(users.indexer, subgraphDataServiceLegacyAddress, amount, 0, 0);

resetPrank(users.delegator);
_delegate(users.indexer, delegationAmount);
_delegate(users.indexer, subgraphDataServiceLegacyAddress, delegationAmount, 0);

DelegationInternal memory delegation = _getStorage_Delegation(
users.indexer,
subgraphDataServiceAddress,
users.delegator,
true
);
_undelegate(users.indexer, delegation.shares);
_undelegate(users.indexer, subgraphDataServiceLegacyAddress, delegation.shares);
}

function testUndelegate_RevertWhen_InvalidPool(
Expand Down
4 changes: 2 additions & 2 deletions packages/horizon/test/staking/delegation/withdraw.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ contract HorizonStakingWithdrawDelegationTest is HorizonStakingTest {
_createProvision(users.indexer, subgraphDataServiceLegacyAddress, 10_000_000 ether, 0, MAX_THAWING_PERIOD);

resetPrank(users.delegator);
_delegate(users.indexer, delegationAmount);
_delegate(users.indexer, subgraphDataServiceLegacyAddress, delegationAmount, 0);
DelegationInternal memory delegation = _getStorage_Delegation(
users.indexer,
subgraphDataServiceAddress,
users.delegator,
true
);
_undelegate(users.indexer, delegation.shares);
_undelegate(users.indexer, subgraphDataServiceLegacyAddress, delegation.shares);

LinkedList.List memory thawingRequests = staking.getThawRequestList(
IHorizonStakingTypes.ThawRequestType.Delegation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LegacyAllocation } from "../libraries/LegacyAllocation.sol";

abstract contract AllocationManagerV1Storage {
/// @notice Allocation details
mapping(address allocationId => Allocation.State allocation) public allocations;
mapping(address allocationId => Allocation.State allocation) internal allocations;

/// @notice Legacy allocation details
mapping(address allocationId => LegacyAllocation.State allocation) public legacyAllocations;
Expand All @@ -15,14 +15,14 @@ abstract contract AllocationManagerV1Storage {
mapping(address indexer => uint256 tokens) public allocationProvisionTracker;

/// @notice Maximum amount of time, in seconds, allowed between presenting POIs to qualify for indexing rewards
uint256 public maxPOIStaleness;
uint256 internal maxPOIStaleness;

/// @notice Destination of accrued indexing rewards
mapping(address indexer => address destination) public rewardsDestination;

/// @notice Track total tokens allocated per subgraph deployment
/// @dev Used to calculate indexing rewards
mapping(bytes32 subgraphDeploymentId => uint256 tokens) public subgraphAllocatedTokens;
mapping(bytes32 subgraphDeploymentId => uint256 tokens) internal subgraphAllocatedTokens;

/// @dev Gap to allow adding variables in future upgrades
uint256[50] private __gap;
Expand Down