Skip to content

Commit

Permalink
fix: inconsistent number formatting (#175)
Browse files Browse the repository at this point in the history
# Motivation

As part of the Rewards v2 SigmaPrime audit, we need to tackle the
following issues:

1.  ELSC2-04: Inconsistent Number Formatting In Merkle Tree Values

Specifically for the multiplier and amount fields in the Operator
Directed Rewards Submission Model.

# Modifications

* Handling as `%064x` and `%024x` instead of strings.

# Result
`ELSC2-04` SigmaPrime audit fix.
  • Loading branch information
0xrajath authored and seanmcgary committed Jan 8, 2025
2 parents 8ccec3a + a3c07bb commit c24f72d
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package operatorDirectedRewardSubmissions
import (
"encoding/json"
"fmt"
"math/big"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -291,7 +292,10 @@ func (odrs *OperatorDirectedRewardSubmissionsModel) GenerateStateRoot(blockNumbe
return nil, err
}

inputs := odrs.sortValuesForMerkleTree(inserts)
inputs, err := odrs.sortValuesForMerkleTree(inserts)
if err != nil {
return nil, err
}

if len(inputs) == 0 {
return nil, nil
Expand All @@ -309,11 +313,24 @@ func (odrs *OperatorDirectedRewardSubmissionsModel) GenerateStateRoot(blockNumbe
return fullTree.Root(), nil
}

func (odrs *OperatorDirectedRewardSubmissionsModel) sortValuesForMerkleTree(submissions []*OperatorDirectedRewardSubmission) []*base.MerkleTreeInput {
func (odrs *OperatorDirectedRewardSubmissionsModel) sortValuesForMerkleTree(submissions []*OperatorDirectedRewardSubmission) ([]*base.MerkleTreeInput, error) {
inputs := make([]*base.MerkleTreeInput, 0)
for _, submission := range submissions {
slotID := NewSlotID(submission.TransactionHash, submission.LogIndex, submission.RewardHash, submission.StrategyIndex, submission.OperatorIndex)
value := fmt.Sprintf("%s_%s_%s_%s_%s", submission.RewardHash, submission.Strategy, submission.Multiplier, submission.Operator, submission.Amount)

multiplierBig, success := new(big.Int).SetString(submission.Multiplier, 10)
if !success {
return nil, fmt.Errorf("failed to parse multiplier to BigInt: %s", submission.Multiplier)
}

amountBig, success := new(big.Int).SetString(submission.Amount, 10)
if !success {
return nil, fmt.Errorf("failed to parse amount to BigInt: %s", submission.Amount)
}

// Multiplier is a uint96 in the contracts, which translates to 24 hex characters
// Amount is a uint256 in the contracts, which translates to 64 hex characters
value := fmt.Sprintf("%s_%s_%024x_%s_%064x", submission.RewardHash, submission.Strategy, multiplierBig, submission.Operator, amountBig)
inputs = append(inputs, &base.MerkleTreeInput{
SlotID: slotID,
Value: []byte(value),
Expand All @@ -324,7 +341,7 @@ func (odrs *OperatorDirectedRewardSubmissionsModel) sortValuesForMerkleTree(subm
return strings.Compare(string(i.SlotID), string(j.SlotID))
})

return inputs
return inputs, nil
}

func (odrs *OperatorDirectedRewardSubmissionsModel) DeleteState(startBlockNumber uint64, endBlockNumber uint64) error {
Expand Down

0 comments on commit c24f72d

Please sign in to comment.