Skip to content

Commit

Permalink
chore(BUM-296): rename BUMP structs
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Nov 2, 2023
1 parent 65a1b20 commit 6dc1e8f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
25 changes: 11 additions & 14 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import (
"github.com/libsv/go-bc"
)

// BUMPPaths represents BUMP format for all inputs
type BUMPPaths []BUMP
// BUMPs represents a slice of BUMPs - BSV Unified Merkle Paths
type BUMPs []BUMP

// BUMP is a struct that represents a whole BUMP format
type BUMP struct {
blockHeight uint64
path []BUMPPath
path [][]BUMPLeaf
}

// BUMPPath is a slice of BUMPLevel objects which represents a path
type BUMPPath []BUMPPathElement

// BUMPPathElement is a struct that represents a single BUMP transaction
type BUMPPathElement struct {
// BUMPLeaf represents each BUMP path element
type BUMPLeaf struct {
hash string
txId bool
duplicate bool
Expand All @@ -38,8 +35,8 @@ func (b BUMP) calculateMerkleRoots() ([]string, error) {
return merkleRoots, nil
}

func (bPath BUMPPath) findTxByOffset(offset uint64) *BUMPPathElement {
for _, bumpTx := range bPath {
func findTxByOffset(offset uint64, bumpLeaves []BUMPLeaf) *BUMPLeaf {
for _, bumpTx := range bumpLeaves {
if bumpTx.offset == offset {
return &bumpTx
}
Expand All @@ -48,7 +45,7 @@ func (bPath BUMPPath) findTxByOffset(offset uint64) *BUMPPathElement {
}

// calculateMerkleRoots will calculate one merkle root for tx in the BUMPPath
func calculateMerkleRoot(baseTx BUMPPathElement, bump BUMP) (string, error) {
func calculateMerkleRoot(baseTx BUMPLeaf, bump BUMP) (string, error) {
calculatedHash := baseTx.hash
offset := baseTx.offset

Expand All @@ -57,7 +54,7 @@ func calculateMerkleRoot(baseTx BUMPPathElement, bump BUMP) (string, error) {
if offset%2 == 0 {
newOffset = offset + 1
}
tx2 := bLevel.findTxByOffset(newOffset)
tx2 := findTxByOffset(newOffset, bLevel)
if &tx2 == nil {
return "", errors.New("could not find pair")
}
Expand All @@ -72,7 +69,7 @@ func calculateMerkleRoot(baseTx BUMPPathElement, bump BUMP) (string, error) {

offset = offset / 2

baseTx = BUMPPathElement{
baseTx = BUMPLeaf{
hash: calculatedHash,
offset: offset,
}
Expand All @@ -81,7 +78,7 @@ func calculateMerkleRoot(baseTx BUMPPathElement, bump BUMP) (string, error) {
return calculatedHash, nil
}

func prepareNodes(baseTx BUMPPathElement, offset uint64, tx2 BUMPPathElement, newOffset uint64) (string, string) {
func prepareNodes(baseTx BUMPLeaf, offset uint64, tx2 BUMPLeaf, newOffset uint64) (string, string) {
var txHash, tx2Hash string

if baseTx.duplicate {
Expand Down
17 changes: 0 additions & 17 deletions examples/client/bump/bump.go

This file was deleted.

16 changes: 8 additions & 8 deletions p2p_beef_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type TxData struct {
}

type DecodedBEEF struct {
BUMPs BUMPPaths
BUMPs BUMPs
InputsTxData []TxData
ProcessedTxData TxData
}
Expand Down Expand Up @@ -111,14 +111,14 @@ func decodeBUMPs(beefBytes []byte) ([]BUMP, []byte, error) {
return bumps, beefBytes, nil
}

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

treeHeight, bytesUsed := bt.NewVarIntFromBytes(hexBytes)
hexBytes = hexBytes[bytesUsed:]
bumpPaths := make([]BUMPPath, 0)
bumpPaths := make([][]BUMPLeaf, 0)

for i := 0; i < int(treeHeight); i++ {
if len(hexBytes) < 1 {
Expand All @@ -137,8 +137,8 @@ func decodeBUMPPathsFromStream(hexBytes []byte) ([]BUMPPath, []byte, error) {
return bumpPaths, hexBytes, nil
}

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

if flag == 1 {
bumpPathElement := BUMPPathElement{
bumpPathElement := BUMPLeaf{
offset: uint64(offset),
duplicate: true,
}
Expand All @@ -177,13 +177,13 @@ func decodeBUMPPath(nLeaves bt.VarInt, hexBytes []byte) (BUMPPath, []byte, error
hash = reverse(hash)

if flag == 0 {
bumpPathElement := BUMPPathElement{
bumpPathElement := BUMPLeaf{
hash: hash,
offset: uint64(offset),
}
bumpPath = append(bumpPath, bumpPathElement)
} else {
bumpPathElement := BUMPPathElement{
bumpPathElement := BUMPLeaf{
hash: hash,
txId: true,
offset: uint64(offset),
Expand Down

0 comments on commit 6dc1e8f

Please sign in to comment.