Skip to content

Commit

Permalink
Add prefixes to merkle leaves to guard against a tree shrinking attack
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Dec 4, 2024
1 parent 8c9e84b commit 3bc005e
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/eigenState/avsOperators/avsOperators.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (a *AvsOperatorsModel) GenerateStateRoot(blockNumber uint64) (types.StateRo
return "", nil
}

fullTree, err := a.MerkleizeState(blockNumber, inputs)
fullTree, err := a.MerkleizeEigenState(blockNumber, inputs)
if err != nil {
a.logger.Sugar().Errorw("Failed to create merkle tree",
zap.Error(err),
Expand Down
10 changes: 6 additions & 4 deletions pkg/eigenState/base/baseEigenState.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ func (b *BaseEigenState) ParseLogOutput(log *storage.TransactionLog) (map[string
// 1. Ensures that the tree is always different for different blocks
// 2. Allows us to have at least 1 value if there are no model changes for a block.
func (b *BaseEigenState) InitializeMerkleTreeBaseStateWithBlock(blockNumber uint64) [][]byte {
blockNumberByte := []byte(fmt.Sprintf("%d", blockNumber))
return [][]byte{
[]byte(fmt.Sprintf("%d", blockNumber)),
types.MerkleLeafPrefix_EigenStateBlock,
blockNumberByte,
}
}

Expand Down Expand Up @@ -104,10 +106,10 @@ type MerkleTreeInput struct {
Value []byte
}

// MerkleizeState creates a merkle tree from the given inputs.
// MerkleizeEigenState creates a merkle tree from the given inputs.
//
// Each input includes a SlotID and a byte representation of the state that changed
func (b *BaseEigenState) MerkleizeState(blockNumber uint64, inputs []*MerkleTreeInput) (*merkletree.MerkleTree, error) {
func (b *BaseEigenState) MerkleizeEigenState(blockNumber uint64, inputs []*MerkleTreeInput) (*merkletree.MerkleTree, error) {
om := orderedmap.New[types.SlotID, []byte]()

for _, input := range inputs {
Expand Down Expand Up @@ -136,7 +138,7 @@ func (b *BaseEigenState) MerkleizeState(blockNumber uint64, inputs []*MerkleTree
}

func encodeMerkleLeaf(slotID types.SlotID, value []byte) []byte {
return append([]byte(slotID), value...)
return append(types.MerkleLeafPrefix_EigenStateChange, append([]byte(slotID), value...)...)
}

func NewSlotID(txHash string, logIndex uint64) types.SlotID {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (ddr *DisabledDistributionRootsModel) GenerateStateRoot(blockNumber uint64)
return "", nil
}

fullTree, err := ddr.MerkleizeState(blockNumber, sortedInputs)
fullTree, err := ddr.MerkleizeEigenState(blockNumber, sortedInputs)
if err != nil {
ddr.logger.Sugar().Errorw("Failed to create merkle tree",
zap.Error(err),
Expand Down
2 changes: 1 addition & 1 deletion pkg/eigenState/operatorShares/operatorShares.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (osm *OperatorSharesModel) GenerateStateRoot(blockNumber uint64) (types.Sta
return "", nil
}

fullTree, err := osm.MerkleizeState(blockNumber, inputs)
fullTree, err := osm.MerkleizeEigenState(blockNumber, inputs)
if err != nil {
osm.logger.Sugar().Errorw("Failed to create merkle tree",
zap.Error(err),
Expand Down
2 changes: 1 addition & 1 deletion pkg/eigenState/rewardSubmissions/rewardSubmissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (rs *RewardSubmissionsModel) GenerateStateRoot(blockNumber uint64) (types.S
return "", nil
}

fullTree, err := rs.MerkleizeState(blockNumber, inputs)
fullTree, err := rs.MerkleizeEigenState(blockNumber, inputs)
if err != nil {
rs.logger.Sugar().Errorw("Failed to create merkle tree",
zap.Error(err),
Expand Down
2 changes: 1 addition & 1 deletion pkg/eigenState/stakerDelegations/stakerDelegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (s *StakerDelegationsModel) GenerateStateRoot(blockNumber uint64) (types.St
return "", nil
}

fullTree, err := s.MerkleizeState(blockNumber, inputs)
fullTree, err := s.MerkleizeEigenState(blockNumber, inputs)
if err != nil {
s.logger.Sugar().Errorw("Failed to create merkle tree",
zap.Error(err),
Expand Down
2 changes: 1 addition & 1 deletion pkg/eigenState/stakerShares/stakerShares.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ func (ss *StakerSharesModel) GenerateStateRoot(blockNumber uint64) (types.StateR
return "", nil
}

fullTree, err := ss.MerkleizeState(blockNumber, inputs)
fullTree, err := ss.MerkleizeEigenState(blockNumber, inputs)
if err != nil {
ss.logger.Sugar().Errorw("Failed to create merkle tree",
zap.Error(err),
Expand Down
3 changes: 2 additions & 1 deletion pkg/eigenState/stateManager/stateManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (e *EigenStateManager) CleanupProcessedStateForBlock(blockNumber uint64) er
func (e *EigenStateManager) GenerateStateRoot(blockNumber uint64, blockHash string) (types.StateRoot, error) {
sortedIndexes := e.GetSortedModelIndexes()
roots := [][]byte{
types.MerkleLeafPrefix_Block,
[]byte(fmt.Sprintf("%d", blockNumber)),
[]byte(blockHash),
}
Expand Down Expand Up @@ -168,7 +169,7 @@ func (e *EigenStateManager) encodeModelLeaf(model types.IEigenStateModel, blockN
if root == "" {
return nil, nil
}
return append([]byte(model.GetModelName()), []byte(root)...), nil
return append(types.MerkleLeafPrefix_EigenStateRoot, append([]byte(model.GetModelName()), []byte(root)...)...), nil
}

func (e *EigenStateManager) GetSortedModelIndexes() []int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (sdr *SubmittedDistributionRootsModel) GenerateStateRoot(blockNumber uint64
return "", nil
}

fullTree, err := sdr.MerkleizeState(blockNumber, sortedInputs)
fullTree, err := sdr.MerkleizeEigenState(blockNumber, sortedInputs)
if err != nil {
sdr.logger.Sugar().Errorw("Failed to create merkle tree",
zap.Error(err),
Expand Down
9 changes: 9 additions & 0 deletions pkg/eigenState/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ type IEigenStateModel interface {
type StateTransitions[T any] map[uint64]func(log *storage.TransactionLog) (T, error)

type SlotID string

type MerkleLeafPrefix []byte

var (
MerkleLeafPrefix_Block MerkleLeafPrefix = []byte("0x00")
MerkleLeafPrefix_EigenStateRoot MerkleLeafPrefix = []byte("0x01")
MerkleLeafPrefix_EigenStateBlock MerkleLeafPrefix = []byte("0x02")
MerkleLeafPrefix_EigenStateChange MerkleLeafPrefix = []byte("0x03")
)

0 comments on commit 3bc005e

Please sign in to comment.