Skip to content

Commit

Permalink
fix(SPV-000): BUMP verification changes (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmkowalski authored Nov 26, 2024
1 parent cc49612 commit 24f0ddf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
47 changes: 42 additions & 5 deletions beef/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
txIDFlag
)

// CalculateMerkleRoot will calculate the merkle root for the BUMP
func (b BUMP) CalculateMerkleRoot() (string, error) {
merkleRoot := ""

Expand Down Expand Up @@ -56,23 +57,34 @@ func calculateMerkleRoot(baseLeaf BUMPLeaf, bump BUMP) (string, error) {
calculatedHash := baseLeaf.Hash
offset := baseLeaf.Offset

for _, bLevel := range bump.Path {
for i := 0; i < len(bump.Path); i++ {
bLevel := bump.Path[i]
var previousBLevel []BUMPLeaf

if i-1 >= 0 {
previousBLevel = bump.Path[i-1]
}
newOffset := getOffsetPair(offset)
leafInPair := findLeafByOffset(newOffset, bLevel)
if leafInPair == nil {
return "", errors.New("could not find pair")
if previousBLevel == nil {
return "", errors.New("cannot compute leaf from children at base level")
}

var err error
leafInPair, err = calculateFromChildren(newOffset, previousBLevel)
if err != nil {
return "", err
}
}

leftNode, rightNode := prepareNodes(baseLeaf, offset, *leafInPair, newOffset)
str, err := merkleTreeParentStr(leftNode, rightNode)

if err != nil {
return "", err
}
calculatedHash = str

offset = offset / 2

baseLeaf = BUMPLeaf{
Hash: calculatedHash,
Offset: offset,
Expand All @@ -98,6 +110,31 @@ func findLeafByOffset(offset uint64, bumpLeaves []BUMPLeaf) *BUMPLeaf {
return nil
}

func calculateFromChildren(offset uint64, bumpLeaves []BUMPLeaf) (*BUMPLeaf, error) {
offsetChild := offset * 2
offsetChildPair := offsetChild + 1
leaf := findLeafByOffset(offsetChild, bumpLeaves)
if leaf == nil {
return nil, errors.New("could not find child")
}

leafInPair := findLeafByOffset(offsetChildPair, bumpLeaves)
if leafInPair == nil {
return nil, errors.New("could not find child")
}

leftNode, rightNode := prepareNodes(*leaf, offset, *leafInPair, offsetChildPair)
str, err := merkleTreeParentStr(leftNode, rightNode)
if err != nil {
return nil, errors.New("could not find pair")
}

return &BUMPLeaf{
Hash: str,
Offset: offset,
}, nil
}

func prepareNodes(baseLeaf BUMPLeaf, offset uint64, leafInPair BUMPLeaf, newOffset uint64) (string, string) {
var baseLeafHash, pairLeafHash string

Expand Down
Loading

0 comments on commit 24f0ddf

Please sign in to comment.