Skip to content

Commit

Permalink
Reorganized storage slots and variable types in BridgeState
Browse files Browse the repository at this point in the history
All `*Timeout` values in `BridgeState` are represented as `uint32` now.
All `*RewardMultiplier` values in `BridgeState` are represented as `uint32` now.
`__depositAlignmentGap` changed to `bytes16` to really move `movingFundsTxMaxTotalFee`
to the next storage slot (previous `bytes8` version was not doing it).
Added `bytes16 __movingFundsAlignmentGap` to keep as one slot:
`uint96 movingFundsTimeoutSlashingAmount | uint32 movingFundsTimeoutNotifierRewardMultiplier | bytes16 movingFundsTimeoutSlashingGap`

This change allows to make timeout and reward multiplier values use consistent
types as well as reduce gas consumption a bit. Before vs after:

|  Bridge  ·  revealDeposit               ·  103961  ·  108365  ·  104924  │
|  Bridge  ·  revealDeposit               ·  103961  ·  108365  ·  104925  │

|  Bridge  ·  submitDepositSweepProof     ·  191029  ·  346180  ·  267960  │
|  Bridge  ·  submitDepositSweepProof     ·  191007  ·  346158  ·  267938  │

|  Bridge  ·  submitRedemptionProof       ·  126351  ·  197632  ·  168338  │
|  Bridge  ·  submitRedemptionProof       ·  126278  ·  197535  ·  168253  │

|  Bridge  ·  requestRedemption           ·  113740  ·  131575  ·  121303  │
|  Bridge  ·  requestRedemption           ·  113734  ·  131563  ·  121292  │

|  Bridge  ·  submitMovedFundsSweepProof  ·  134172  ·  147375  ·  139461  │
|  Bridge  ·  submitMovedFundsSweepProof  ·  134172  ·  147375  ·  139461  │
  • Loading branch information
pdyraga committed May 26, 2022
1 parent a3716d0 commit 9d15d6e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
24 changes: 12 additions & 12 deletions solidity/contracts/bridge/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ contract Bridge is
uint64 redemptionDustThreshold,
uint64 redemptionTreasuryFeeDivisor,
uint64 redemptionTxMaxFee,
uint64 redemptionTimeout,
uint32 redemptionTimeout,
uint96 redemptionTimeoutSlashingAmount,
uint64 redemptionTimeoutNotifierRewardMultiplier
uint32 redemptionTimeoutNotifierRewardMultiplier
);

event MovingFundsParametersUpdated(
Expand All @@ -201,11 +201,11 @@ contract Bridge is
uint32 movingFundsTimeoutResetDelay,
uint32 movingFundsTimeout,
uint96 movingFundsTimeoutSlashingAmount,
uint256 movingFundsTimeoutNotifierRewardMultiplier,
uint32 movingFundsTimeoutNotifierRewardMultiplier,
uint64 movedFundsSweepTxMaxTotalFee,
uint32 movedFundsSweepTimeout,
uint96 movedFundsSweepTimeoutSlashingAmount,
uint64 movedFundsSweepTimeoutNotifierRewardMultiplier
uint32 movedFundsSweepTimeoutNotifierRewardMultiplier
);

event WalletParametersUpdated(
Expand Down Expand Up @@ -1228,9 +1228,9 @@ contract Bridge is
uint64 redemptionDustThreshold,
uint64 redemptionTreasuryFeeDivisor,
uint64 redemptionTxMaxFee,
uint64 redemptionTimeout,
uint32 redemptionTimeout,
uint96 redemptionTimeoutSlashingAmount,
uint64 redemptionTimeoutNotifierRewardMultiplier
uint32 redemptionTimeoutNotifierRewardMultiplier
) external onlyGovernance {
self.updateRedemptionParameters(
redemptionDustThreshold,
Expand Down Expand Up @@ -1311,11 +1311,11 @@ contract Bridge is
uint32 movingFundsTimeoutResetDelay,
uint32 movingFundsTimeout,
uint96 movingFundsTimeoutSlashingAmount,
uint256 movingFundsTimeoutNotifierRewardMultiplier,
uint32 movingFundsTimeoutNotifierRewardMultiplier,
uint64 movedFundsSweepTxMaxTotalFee,
uint32 movedFundsSweepTimeout,
uint96 movedFundsSweepTimeoutSlashingAmount,
uint64 movedFundsSweepTimeoutNotifierRewardMultiplier
uint32 movedFundsSweepTimeoutNotifierRewardMultiplier
) external onlyGovernance {
self.updateMovingFundsParameters(
movingFundsTxMaxTotalFee,
Expand Down Expand Up @@ -1607,9 +1607,9 @@ contract Bridge is
uint64 redemptionDustThreshold,
uint64 redemptionTreasuryFeeDivisor,
uint64 redemptionTxMaxFee,
uint64 redemptionTimeout,
uint32 redemptionTimeout,
uint96 redemptionTimeoutSlashingAmount,
uint64 redemptionTimeoutNotifierRewardMultiplier
uint32 redemptionTimeoutNotifierRewardMultiplier
)
{
redemptionDustThreshold = self.redemptionDustThreshold;
Expand Down Expand Up @@ -1672,11 +1672,11 @@ contract Bridge is
uint32 movingFundsTimeoutResetDelay,
uint32 movingFundsTimeout,
uint96 movingFundsTimeoutSlashingAmount,
uint256 movingFundsTimeoutNotifierRewardMultiplier,
uint32 movingFundsTimeoutNotifierRewardMultiplier,
uint64 movedFundsSweepTxMaxTotalFee,
uint32 movedFundsSweepTimeout,
uint96 movedFundsSweepTimeoutSlashingAmount,
uint256 movedFundsSweepTimeoutNotifierRewardMultiplier
uint32 movedFundsSweepTimeoutNotifierRewardMultiplier
)
{
movingFundsTxMaxTotalFee = self.movingFundsTxMaxTotalFee;
Expand Down
34 changes: 19 additions & 15 deletions solidity/contracts/bridge/BridgeState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ library BridgeState {
// This is a per-deposit input max fee for the sweep transaction.
uint64 depositTxMaxFee;
// Move movingFundsTxMaxTotalFee to the next storage slot for a more
// efficient variable layout
// efficient variable layout in the storage.
// slither-disable-next-line unused-state
bytes8 __depositAlignmentGap;
bytes16 __depositAlignmentGap;
// Maximum amount of the total BTC transaction fee that is acceptable in
// a single moving funds transaction.
//
Expand Down Expand Up @@ -100,7 +100,11 @@ library BridgeState {
// The percentage of the notifier reward from the staking contract
// the notifier of a moving funds timeout receives. The value is in the
// range [0, 100].
uint256 movingFundsTimeoutNotifierRewardMultiplier;
uint32 movingFundsTimeoutNotifierRewardMultiplier;
// Move movedFundsSweepTxMaxTotalFee to the next storage slot for a more
// efficient variable layout in the storage.
// slither-disable-next-line unused-state
bytes16 __movingFundsAlignmentGap;
// Maximum amount of the total BTC transaction fee that is acceptable in
// a single moved funds sweep transaction.
//
Expand All @@ -119,7 +123,7 @@ library BridgeState {
// The percentage of the notifier reward from the staking contract
// the notifier of a moved funds sweep timeout receives. The value is
// in the range [0, 100].
uint64 movedFundsSweepTimeoutNotifierRewardMultiplier;
uint32 movedFundsSweepTimeoutNotifierRewardMultiplier;
// The minimal amount that can be requested for redemption.
// Value of this parameter must take into account the value of
// `redemptionTreasuryFeeDivisor` and `redemptionTxMaxFee`
Expand All @@ -146,22 +150,22 @@ library BridgeState {
// transaction.
uint64 redemptionTxMaxFee;
// Move redemptionTimeout to the next storage slot for a more efficient
// variable layout
// variable layout in the storage.
// slither-disable-next-line unused-state
bytes8 __redemptionAlignmentGap;
// Time after which the redemption request can be reported as
// timed out. It is counted from the moment when the redemption
// request was created via `requestRedemption` call. Reported
// timed out requests are cancelled and locked TBTC is returned
// to the redeemer in full amount.
uint64 redemptionTimeout;
uint32 redemptionTimeout;
// The amount of stake slashed from each member of a wallet for a
// redemption timeout.
uint96 redemptionTimeoutSlashingAmount;
// The percentage of the notifier reward from the staking contract
// the notifier of a redemption timeout receives. The value is in the
// range [0, 100].
uint64 redemptionTimeoutNotifierRewardMultiplier;
uint32 redemptionTimeoutNotifierRewardMultiplier;
// The amount of ETH in wei the party challenging the wallet for fraud
// needs to deposit.
uint96 fraudChallengeDepositAmount;
Expand Down Expand Up @@ -291,9 +295,9 @@ library BridgeState {
uint64 redemptionDustThreshold,
uint64 redemptionTreasuryFeeDivisor,
uint64 redemptionTxMaxFee,
uint64 redemptionTimeout,
uint32 redemptionTimeout,
uint96 redemptionTimeoutSlashingAmount,
uint64 redemptionTimeoutNotifierRewardMultiplier
uint32 redemptionTimeoutNotifierRewardMultiplier
);

event MovingFundsParametersUpdated(
Expand All @@ -302,11 +306,11 @@ library BridgeState {
uint32 movingFundsTimeoutResetDelay,
uint32 movingFundsTimeout,
uint96 movingFundsTimeoutSlashingAmount,
uint256 movingFundsTimeoutNotifierRewardMultiplier,
uint32 movingFundsTimeoutNotifierRewardMultiplier,
uint64 movedFundsSweepTxMaxTotalFee,
uint32 movedFundsSweepTimeout,
uint96 movedFundsSweepTimeoutSlashingAmount,
uint64 movedFundsSweepTimeoutNotifierRewardMultiplier
uint32 movedFundsSweepTimeoutNotifierRewardMultiplier
);

event WalletParametersUpdated(
Expand Down Expand Up @@ -433,9 +437,9 @@ library BridgeState {
uint64 _redemptionDustThreshold,
uint64 _redemptionTreasuryFeeDivisor,
uint64 _redemptionTxMaxFee,
uint64 _redemptionTimeout,
uint32 _redemptionTimeout,
uint96 _redemptionTimeoutSlashingAmount,
uint64 _redemptionTimeoutNotifierRewardMultiplier
uint32 _redemptionTimeoutNotifierRewardMultiplier
) internal {
require(
_redemptionDustThreshold > self.movingFundsDustThreshold,
Expand Down Expand Up @@ -550,11 +554,11 @@ library BridgeState {
uint32 _movingFundsTimeoutResetDelay,
uint32 _movingFundsTimeout,
uint96 _movingFundsTimeoutSlashingAmount,
uint256 _movingFundsTimeoutNotifierRewardMultiplier,
uint32 _movingFundsTimeoutNotifierRewardMultiplier,
uint64 _movedFundsSweepTxMaxTotalFee,
uint32 _movedFundsSweepTimeout,
uint96 _movedFundsSweepTimeoutSlashingAmount,
uint64 _movedFundsSweepTimeoutNotifierRewardMultiplier
uint32 _movedFundsSweepTimeoutNotifierRewardMultiplier
) internal {
require(
_movingFundsTxMaxTotalFee > 0,
Expand Down
4 changes: 2 additions & 2 deletions solidity/test/bridge/Bridge.MovingFunds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ describe("Bridge - Moving funds", () => {
let movingFundsTimeoutResetDelay: number
let movingFundsTimeout: number
let movingFundsTimeoutSlashingAmount: BigNumber
let movingFundsTimeoutNotifierRewardMultiplier: BigNumber
let movingFundsTimeoutNotifierRewardMultiplier: number
let movedFundsSweepTimeout: number
let movedFundsSweepTimeoutSlashingAmount: BigNumber
let movedFundsSweepTimeoutNotifierRewardMultiplier: BigNumber
let movedFundsSweepTimeoutNotifierRewardMultiplier: number

before(async () => {
// eslint-disable-next-line @typescript-eslint/no-extra-semi
Expand Down
2 changes: 1 addition & 1 deletion solidity/test/bridge/Bridge.Redemption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("Bridge - Redemption", () => {

let redemptionTimeout: BigNumber
let redemptionTimeoutSlashingAmount: BigNumber
let redemptionTimeoutNotifierRewardMultiplier: BigNumber
let redemptionTimeoutNotifierRewardMultiplier: number

before(async () => {
// eslint-disable-next-line @typescript-eslint/no-extra-semi
Expand Down

0 comments on commit 9d15d6e

Please sign in to comment.