Skip to content

Commit

Permalink
chore(BUX-322): export all fields in BUMP (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 authored Nov 28, 2023
1 parent fabf348 commit 8476d6e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
14 changes: 7 additions & 7 deletions beef/beef_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (d *DecodedBEEF) GetLatestTx() *bt.Tx {
return d.Transactions[len(d.Transactions)-1].Transaction // get the last transaction as the processed transaction - it should be the last one because of khan's ordering
}

func decodeBUMPs(beefBytes []byte) ([]BUMP, []byte, error) {
func decodeBUMPs(beefBytes []byte) ([]*BUMP, []byte, error) {
if len(beefBytes) == 0 {
return nil, nil, errors.New("cannot decode BUMP - no bytes provided")
}
Expand All @@ -88,7 +88,7 @@ func decodeBUMPs(beefBytes []byte) ([]BUMP, []byte, error) {

beefBytes = beefBytes[bytesUsed:]

bumps := make([]BUMP, 0, uint64(nBump))
bumps := make([]*BUMP, 0, uint64(nBump))
for i := uint64(0); i < uint64(nBump); i++ {
if len(beefBytes) == 0 {
return nil, nil, errors.New("insufficient bytes to extract BUMP blockHeight")
Expand All @@ -108,7 +108,7 @@ func decodeBUMPs(beefBytes []byte) ([]BUMP, []byte, error) {
}
beefBytes = remainingBytes

bump := BUMP{
bump := &BUMP{
BlockHeight: uint64(blockHeight),
Path: bumpPaths,
}
Expand Down Expand Up @@ -162,8 +162,8 @@ func decodeBUMPLevel(nLeaves bt.VarInt, hexBytes []byte) ([]BUMPLeaf, []byte, er

if flag == duplicateFlag {
bumpLeaf := BUMPLeaf{
offset: uint64(offset),
duplicate: true,
Offset: uint64(offset),
Duplicate: true,
}
bumpPath = append(bumpPath, bumpLeaf)
continue
Expand All @@ -178,10 +178,10 @@ func decodeBUMPLevel(nLeaves bt.VarInt, hexBytes []byte) ([]BUMPLeaf, []byte, er

bumpLeaf := BUMPLeaf{
Hash: hash,
offset: uint64(offset),
Offset: uint64(offset),
}
if flag == txIDFlag {
bumpLeaf.txId = true
bumpLeaf.TxId = true
}
bumpPath = append(bumpPath, bumpLeaf)
}
Expand Down
18 changes: 9 additions & 9 deletions beef/beef_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ func TestDecodeBEEF_DecodeBEEF_HappyPaths(t *testing.T) {
hexStream: "0100beef01fe636d0c0007021400fe507c0c7aa754cef1f7889d5fd395cf1f785dd7de98eed895dbedfe4e5bc70d1502ac4e164f5bc16746bb0868404292ac8318bbac3800e4aad13a014da427adce3e010b00bc4ff395efd11719b277694cface5aa50d085a0bb81f613f70313acd28cf4557010400574b2d9142b8d28b61d88e3b2c3f44d858411356b49a28a4643b6d1a6a092a5201030051a05fc84d531b5d250c23f4f886f6812f9fe3f402d61607f977b4ecd2701c19010000fd781529d58fc2523cf396a7f25440b409857e7e221766c57214b1d38c7b481f01010062f542f45ea3660f86c013ced80534cb5fd4c19d66c56e7e8c5d4bf2d40acc5e010100b121e91836fd7cd5102b654e9f72f3cf6fdbfd0b161c53a9c54b12c841126331020100000001cd4e4cac3c7b56920d1e7655e7e260d31f29d9a388d04910f1bbd72304a79029010000006b483045022100e75279a205a547c445719420aa3138bf14743e3f42618e5f86a19bde14bb95f7022064777d34776b05d816daf1699493fcdf2ef5a5ab1ad710d9c97bfb5b8f7cef3641210263e2dee22b1ddc5e11f6fab8bcd2378bdd19580d640501ea956ec0e786f93e76ffffffff013e660000000000001976a9146bfd5c7fbe21529d45803dbcf0c87dd3c71efbc288ac0000000001000100000001ac4e164f5bc16746bb0868404292ac8318bbac3800e4aad13a014da427adce3e000000006a47304402203a61a2e931612b4bda08d541cfb980885173b8dcf64a3471238ae7abcd368d6402204cbf24f04b9aa2256d8901f0ed97866603d2be8324c2bfb7a37bf8fc90edd5b441210263e2dee22b1ddc5e11f6fab8bcd2378bdd19580d640501ea956ec0e786f93e76ffffffff013c660000000000001976a9146bfd5c7fbe21529d45803dbcf0c87dd3c71efbc288ac0000000000",
expectedDecodedBEEF: &DecodedBEEF{
BUMPs: BUMPs{
BUMP{
&BUMP{
BlockHeight: 814435,
Path: [][]BUMPLeaf{
{
BUMPLeaf{Hash: "0dc75b4efeeddb95d8ee98ded75d781fcf95d35f9d88f7f1ce54a77a0c7c50fe", offset: 20},
BUMPLeaf{Hash: "3ecead27a44d013ad1aae40038acbb1883ac9242406808bb4667c15b4f164eac", txId: true, offset: 21},
BUMPLeaf{Hash: "0dc75b4efeeddb95d8ee98ded75d781fcf95d35f9d88f7f1ce54a77a0c7c50fe", Offset: 20},
BUMPLeaf{Hash: "3ecead27a44d013ad1aae40038acbb1883ac9242406808bb4667c15b4f164eac", TxId: true, Offset: 21},
},
{
BUMPLeaf{Hash: "5745cf28cd3a31703f611fb80b5a080da55acefa4c6977b21917d1ef95f34fbc", offset: 11},
BUMPLeaf{Hash: "5745cf28cd3a31703f611fb80b5a080da55acefa4c6977b21917d1ef95f34fbc", Offset: 11},
},
{
BUMPLeaf{Hash: "522a096a1a6d3b64a4289ab456134158d8443f2c3b8ed8618bd2b842912d4b57", offset: 4},
BUMPLeaf{Hash: "522a096a1a6d3b64a4289ab456134158d8443f2c3b8ed8618bd2b842912d4b57", Offset: 4},
},
{
BUMPLeaf{Hash: "191c70d2ecb477f90716d602f4e39f2f81f686f8f4230c255d1b534dc85fa051", offset: 3},
BUMPLeaf{Hash: "191c70d2ecb477f90716d602f4e39f2f81f686f8f4230c255d1b534dc85fa051", Offset: 3},
},
{
BUMPLeaf{Hash: "1f487b8cd3b11472c56617227e7e8509b44054f2a796f33c52c28fd5291578fd", offset: 0},
BUMPLeaf{Hash: "1f487b8cd3b11472c56617227e7e8509b44054f2a796f33c52c28fd5291578fd", Offset: 0},
},
{
BUMPLeaf{Hash: "5ecc0ad4f24b5d8c7e6ec5669dc1d45fcb3405d8ce13c0860f66a35ef442f562", offset: 1},
BUMPLeaf{Hash: "5ecc0ad4f24b5d8c7e6ec5669dc1d45fcb3405d8ce13c0860f66a35ef442f562", Offset: 1},
},
{
BUMPLeaf{Hash: "31631241c8124bc5a9531c160bfddb6fcff3729f4e652b10d57cfd3618e921b1", offset: 1},
BUMPLeaf{Hash: "31631241c8124bc5a9531c160bfddb6fcff3729f4e652b10d57cfd3618e921b1", Offset: 1},
},
},
},
Expand Down
20 changes: 10 additions & 10 deletions beef/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// BUMPs represents a slice of BUMPs - BSV Unified Merkle Paths
type BUMPs []BUMP
type BUMPs []*BUMP

// BUMP is a struct that represents a whole BUMP format
type BUMP struct {
Expand All @@ -18,9 +18,9 @@ type BUMP struct {
// BUMPLeaf represents each BUMP path element
type BUMPLeaf struct {
Hash string
txId bool
duplicate bool
offset uint64
TxId bool
Duplicate bool
Offset uint64
}

// Flags which are used to determine the type of BUMPLeaf
Expand All @@ -34,7 +34,7 @@ func (b BUMP) CalculateMerkleRoot() (string, error) {
merkleRoot := ""

for _, bumpPathElement := range b.Path[0] {
if bumpPathElement.txId {
if bumpPathElement.TxId {
calcMerkleRoot, err := calculateMerkleRoot(bumpPathElement, b)
if err != nil {
return "", err
Expand All @@ -56,7 +56,7 @@ func (b BUMP) CalculateMerkleRoot() (string, error) {
// calculateMerkleRoots will calculate one merkle root for tx in the BUMPLeaf
func calculateMerkleRoot(baseLeaf BUMPLeaf, bump BUMP) (string, error) {
calculatedHash := baseLeaf.Hash
offset := baseLeaf.offset
offset := baseLeaf.Offset

for _, bLevel := range bump.Path {
newOffset := getOffsetPair(offset)
Expand All @@ -77,7 +77,7 @@ func calculateMerkleRoot(baseLeaf BUMPLeaf, bump BUMP) (string, error) {

baseLeaf = BUMPLeaf{
Hash: calculatedHash,
offset: offset,
Offset: offset,
}
}

Expand All @@ -93,7 +93,7 @@ func getOffsetPair(offset uint64) uint64 {

func findLeafByOffset(offset uint64, bumpLeaves []BUMPLeaf) *BUMPLeaf {
for _, bumpTx := range bumpLeaves {
if bumpTx.offset == offset {
if bumpTx.Offset == offset {
return &bumpTx
}
}
Expand All @@ -103,13 +103,13 @@ func findLeafByOffset(offset uint64, bumpLeaves []BUMPLeaf) *BUMPLeaf {
func prepareNodes(baseLeaf BUMPLeaf, offset uint64, leafInPair BUMPLeaf, newOffset uint64) (string, string) {
var baseLeafHash, pairLeafHash string

if baseLeaf.duplicate {
if baseLeaf.Duplicate {
baseLeafHash = leafInPair.Hash
} else {
baseLeafHash = baseLeaf.Hash
}

if leafInPair.duplicate {
if leafInPair.Duplicate {
pairLeafHash = baseLeaf.Hash
} else {
pairLeafHash = leafInPair.Hash
Expand Down

0 comments on commit 8476d6e

Please sign in to comment.