Skip to content

Commit

Permalink
chore(BUX-296): refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Nov 2, 2023
1 parent 197d569 commit 7af76a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
20 changes: 16 additions & 4 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package paymail
import (
"errors"
"github.com/libsv/go-bc"
"github.com/libsv/go-bt/v2"
)

// BUMPs represents a slice of BUMPs - BSV Unified Merkle Paths
Expand All @@ -22,6 +23,13 @@ type BUMPLeaf struct {
offset uint64
}

// Flags which are used to determine the type of BUMPLeaf
const (
dataFlag bt.VarInt = iota
duplicateFlag
txIDFlag
)

func (b BUMP) calculateMerkleRoots() ([]string, error) {
merkleRoots := make([]string, 0)

Expand Down Expand Up @@ -50,10 +58,7 @@ func calculateMerkleRoot(baseLeaf BUMPLeaf, bump BUMP) (string, error) {
offset := baseLeaf.offset

for _, bLevel := range bump.path {
newOffset := offset - 1
if offset%2 == 0 {
newOffset = offset + 1
}
newOffset := getOffsetPair(offset)
leafInPair := findLeafByOffset(newOffset, bLevel)
if leafInPair == nil {
return "", errors.New("could not find pair")
Expand All @@ -78,6 +83,13 @@ func calculateMerkleRoot(baseLeaf BUMPLeaf, bump BUMP) (string, error) {
return calculatedHash, nil
}

func getOffsetPair(offset uint64) uint64 {
if offset%2 == 0 {
return offset + 1
}
return offset - 1
}

func prepareNodes(baseLeaf BUMPLeaf, offset uint64, leafInPair BUMPLeaf, newOffset uint64) (string, string) {
var txHash, tx2Hash string

Expand Down
46 changes: 19 additions & 27 deletions p2p_beef_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (dBeef *DecodedBEEF) GetMerkleRoots() ([]string, error) {
for _, bump := range dBeef.BUMPs {
partialMerkleRoots, err := bump.calculateMerkleRoots()
if err != nil {
fmt.Println(err)
return nil, err
}
merkleRoots = append(merkleRoots, partialMerkleRoots...)
Expand Down Expand Up @@ -89,7 +88,7 @@ func decodeBUMPs(beefBytes []byte) ([]BUMP, []byte, error) {
beefBytes = beefBytes[bytesUsed:]

for i := 0; i < int(nBump); i++ {
if len(beefBytes) < 1 {
if len(beefBytes) == 0 {
return nil, nil, errors.New("insufficient bytes to extract BUMP blockHeight")
}
blockHeight, bytesUsed := bt.NewVarIntFromBytes(beefBytes)
Expand All @@ -112,7 +111,7 @@ func decodeBUMPs(beefBytes []byte) ([]BUMP, []byte, error) {
}

func decodeBUMPPathsFromStream(hexBytes []byte) ([][]BUMPLeaf, []byte, error) {
if len(hexBytes) < 1 {
if len(hexBytes) == 0 {
return nil, nil, errors.New("cannot decode BUMP paths from stream - no bytes provided")
}

Expand All @@ -121,12 +120,12 @@ func decodeBUMPPathsFromStream(hexBytes []byte) ([][]BUMPLeaf, []byte, error) {
bumpPaths := make([][]BUMPLeaf, 0)

for i := 0; i < int(treeHeight); i++ {
if len(hexBytes) < 1 {
if len(hexBytes) == 0 {
return nil, nil, errors.New("cannot decode BUMP paths number of leaves from stream - no bytes provided")
}
nLeaves, bytesUsed := bt.NewVarIntFromBytes(hexBytes)
hexBytes = hexBytes[bytesUsed:]
bumpPath, remainingBytes, err := decodeBUMPPath(nLeaves, hexBytes)
bumpPath, remainingBytes, err := decodeBUMPLevel(nLeaves, hexBytes)
if err != nil {
return nil, nil, err
}
Expand All @@ -137,59 +136,52 @@ func decodeBUMPPathsFromStream(hexBytes []byte) ([][]BUMPLeaf, []byte, error) {
return bumpPaths, hexBytes, nil
}

func decodeBUMPPath(nLeaves bt.VarInt, hexBytes []byte) ([]BUMPLeaf, []byte, error) {
func decodeBUMPLevel(nLeaves bt.VarInt, hexBytes []byte) ([]BUMPLeaf, []byte, error) {
bumpPath := make([]BUMPLeaf, 0)
for i := 0; i < int(nLeaves); i++ {
if len(hexBytes) < 1 {
if len(hexBytes) == 0 {
return nil, nil, fmt.Errorf("insufficient bytes to extract offset for %d leaf of %d leaves", i, int(nLeaves))
}

offset, bytesUsed := bt.NewVarIntFromBytes(hexBytes)
hexBytes = hexBytes[bytesUsed:]

if len(hexBytes) < 1 {
if len(hexBytes) == 0 {
return nil, nil, fmt.Errorf("insufficient bytes to extract flag for %d leaf of %d leaves", i, int(nLeaves))
}

flag, bytesUsed := bt.NewVarIntFromBytes(hexBytes)
hexBytes = hexBytes[bytesUsed:]

if flag != 0 && flag != 1 && flag != 2 {
if flag != dataFlag && flag != duplicateFlag && flag != txIDFlag {
return nil, nil, fmt.Errorf("invalid flag: %d for %d leaf of %d leaves", flag, i, int(nLeaves))
}

if flag == 1 {
bumpPathElement := BUMPLeaf{
if flag == duplicateFlag {
bumpLeaf := BUMPLeaf{
offset: uint64(offset),
duplicate: true,
}
bumpPath = append(bumpPath, bumpPathElement)
bumpPath = append(bumpPath, bumpLeaf)
continue
}

if len(hexBytes) < hashBytesCount {
return nil, nil, errors.New("insufficient bytes to extract hash of path")
}

hash := hex.EncodeToString(hexBytes[:hashBytesCount])
hash := hex.EncodeToString(bt.ReverseBytes(hexBytes[:hashBytesCount]))
bytesUsed += hashBytesCount - 1
hexBytes = hexBytes[bytesUsed:]
hash = reverse(hash)

if flag == 0 {
bumpPathElement := BUMPLeaf{
hash: hash,
offset: uint64(offset),
}
bumpPath = append(bumpPath, bumpPathElement)
} else {
bumpPathElement := BUMPLeaf{
hash: hash,
txId: true,
offset: uint64(offset),
}
bumpPath = append(bumpPath, bumpPathElement)
bumpLeaf := BUMPLeaf{
hash: hash,
offset: uint64(offset),
}
if flag == txIDFlag {
bumpLeaf.txId = true
}
bumpPath = append(bumpPath, bumpLeaf)
}

return bumpPath, hexBytes, nil
Expand Down

0 comments on commit 7af76a2

Please sign in to comment.