code-423n4 / 2022-01-livepeer-findings

0 stars 0 forks source link

Gas Optimization: Struct layout #228

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

gzeon

Vulnerability details

Impact

Save storage slot by packing non-256 bit types.

./contracts/interfaces/IMigrator.sol:5

    struct MigrateDelegatorParams {
        // Address that is migrating from L1
        address l1Addr;
        // Address to use on L2
        // If null, l1Addr is used on L2
        address l2Addr;
        // Stake of l1Addr on L1
        uint256 stake;
        // Delegated stake of l1Addr on L1
        uint256 delegatedStake;
        // Fees of l1Addr on L1
        uint256 fees;
        // Delegate of l1Addr on L1
        address delegate;
    }

    struct MigrateUnbondingLocksParams {
        // Address that is migrating from L1
        address l1Addr;
        // Address to use on L2
        // If null, l1Addr is used on L2
        address l2Addr;
        // Total tokens in unbonding locks
        uint256 total;
        // IDs of unbonding locks being migrated
        uint256[] unbondingLockIds;
        // Delegate of l1Addr on L1
        address delegate;
    }

to

    struct MigrateDelegatorParams {
        // Address that is migrating from L1
        address l1Addr;
        // Address to use on L2
        // If null, l1Addr is used on L2
        address l2Addr;
        // Delegate of l1Addr on L1
        address delegate;
        // Stake of l1Addr on L1
        uint256 stake;
        // Delegated stake of l1Addr on L1
        uint256 delegatedStake;
        // Fees of l1Addr on L1
        uint256 fees;
    }

    struct MigrateUnbondingLocksParams {
        // Address that is migrating from L1
        address l1Addr;
        // Address to use on L2
        // If null, l1Addr is used on L2
        address l2Addr;
        // Delegate of l1Addr on L1
        address delegate;
        // Total tokens in unbonding locks
        uint256 total;
        // IDs of unbonding locks being migrated
        uint256[] unbondingLockIds;
    }
yondonfu commented 2 years ago

The suggested layout doesn't seem to support packing since the only types that are smaller than 32 bytes are addresses and two addresses cannot be packed into a single 32 byte slot.

0xleastwood commented 2 years ago

Agreed! address types always fill up an entire 32 byte slot.