Skip to content

Commit

Permalink
chore(BUX-296): remove CMP methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Oct 24, 2023
1 parent 7c337b5 commit b23bb66
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 121 deletions.
25 changes: 23 additions & 2 deletions bump.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
package paymail

type BUMPTx struct {
hash string
txId bool
duplicate bool
}

type BUMP []map[uint64]string
type BUMPMap []map[string]BUMPTx

type BUMPSlice []BUMP
type BUMPSlice []BUMPMap

type BUMP struct {
blockHeight uint64
path BUMPSlice
}

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

for tx, offset := range cmp[len(cmp)-1] {
merkleRoot, err := calculateMerkleRoot(tx, offset, cmp)
if err != nil {
return nil, err
}
merkleRoots = append(merkleRoots, merkleRoot)
}
return merkleRoots, nil
}
172 changes: 53 additions & 119 deletions p2p_beef_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"strconv"

"github.com/libsv/go-bc"
"github.com/libsv/go-bt/v2"
Expand Down Expand Up @@ -31,7 +32,7 @@ type TxData struct {
}

type DecodedBEEF struct {
CMPSlice CMPSlice
BUMP BUMP
InputsTxData []TxData
ProcessedTxData TxData
}
Expand Down Expand Up @@ -98,11 +99,19 @@ func DecodeBEEF(beefHex string) (*DecodedBEEF, error) {
return nil, err
}

cmpSlice, remainingBytes, err := decodeCMPSliceFromStream(beefBytes)
blockHeight, bytesUsed := bt.NewVarIntFromBytes(beefBytes)
beefBytes = beefBytes[bytesUsed:]

bumpSlice, remainingBytes, err := decodeBUMPSliceFromStream(beefBytes)
if err != nil {
return nil, err
}

bump := BUMP{
blockHeight: uint64(blockHeight),
path: bumpSlice,
}

transactions, err := decodeTransactionsWithPathIndexes(remainingBytes)
if err != nil {
return nil, err
Expand All @@ -118,7 +127,7 @@ func DecodeBEEF(beefHex string) (*DecodedBEEF, error) {
transactions = transactions[:len(transactions)-1]

return &DecodedBEEF{
CMPSlice: cmpSlice,
BUMP: bump,
InputsTxData: transactions,
ProcessedTxData: processedTx,
}, nil
Expand All @@ -132,43 +141,20 @@ func DecodeBUMP(beefHex string) (*DecodedBEEF, error) {

blockHeight, bytesUsed := bt.NewVarIntFromBytes(beefBytes)
beefBytes = beefBytes[bytesUsed:]
fmt.Println("blockHeight", blockHeight)
fmt.Println("bytesUsed", bytesUsed)
fmt.Println()

bumpSlice, remainingBytes, err := decodeBUMPSliceFromStream(beefBytes)
bumpSlice, _, err := decodeBUMPSliceFromStream(beefBytes)
if err != nil {
return nil, err
}

fmt.Println("bumpSlice", bumpSlice)
fmt.Println("remainingBytes", remainingBytes)

return nil, nil
}

func decodeCMPSliceFromStream(hexBytes []byte) (CMPSlice, []byte, error) {
if len(hexBytes) == 0 {
return nil, nil, errors.New("cannot decode cmp slice from stream - no bytes provided")
}

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

var cmpPaths []CompoundMerklePath
for i := 0; i < int(nCMPs); i++ {
cmp, bytesUsedToDecodeCMP, err := NewCMPFromStream(hexBytes)
if err != nil {
return nil, nil, err
}

cmpPaths = append(cmpPaths, cmp)
hexBytes = hexBytes[bytesUsedToDecodeCMP:]
bump := BUMP{
blockHeight: uint64(blockHeight),
path: bumpSlice,
}

cmpSlice := CMPSlice(cmpPaths)
fmt.Println(bump)

return cmpSlice, hexBytes, nil
return nil, nil
}

func decodeBUMPSliceFromStream(hexBytes []byte) (BUMPSlice, []byte, error) {
Expand All @@ -178,128 +164,76 @@ func decodeBUMPSliceFromStream(hexBytes []byte) (BUMPSlice, []byte, error) {

treeHeight, bytesUsed := bt.NewVarIntFromBytes(hexBytes)
hexBytes = hexBytes[bytesUsed:]
fmt.Println("treeHeight", treeHeight)
fmt.Println("bytesUsed", bytesUsed)
fmt.Println()

decodeBUMP(treeHeight, hexBytes)

return nil, hexBytes, nil
}

func decodeBUMP(treeHeight bt.VarInt, hexBytes []byte) {
//var bumpSlice []BUMP
var bumpSlice BUMPSlice
for i := 0; i < int(treeHeight); i++ {
nLeaves, bytesUsed := bt.NewVarIntFromBytes(hexBytes)
hexBytes = hexBytes[bytesUsed:]
fmt.Println("nLeaves", nLeaves)
fmt.Println("bytesUsed", bytesUsed)
fmt.Println()

decodeBUMPLeaves(nLeaves, hexBytes)
bumpMap, remainingBytes := decodeBUMPLeaves(nLeaves, hexBytes)
hexBytes = remainingBytes
bumpSlice = append(bumpSlice, bumpMap)
}

return bumpSlice, hexBytes, nil
}

func decodeBUMPLeaves(nLeaves bt.VarInt, hexBytes []byte) {
fmt.Println("<--------- decodeBUMPLeaves ----------->")
fmt.Println("nLeaves", nLeaves)
fmt.Println("hexBytes", hexBytes)
fmt.Println()
func decodeBUMPLeaves(nLeaves bt.VarInt, hexBytes []byte) (BUMPMap, []byte) {
bumpMap := make(map[string]BUMPTx)
for i := 0; i < int(nLeaves); i++ {
if len(hexBytes) < 1 {
panic(fmt.Errorf("insufficient bytes to extract offset for %d leaf of %d leaves", i, int(nLeaves)))
}

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

if len(hexBytes[bytesUsed:]) < 1 {
panic(fmt.Errorf("insufficient bytes to extract flag for %d leaf of %d leaves", i, int(nLeaves)))
}

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

if len(hexBytes[bytesUsed:]) < hashBytesCount {
if flag == 1 {
key := strconv.FormatUint(uint64(offset), 10)
bumpMap[key] = BUMPTx{
duplicate: true,
}
continue
}

if len(hexBytes[bytesUsed:]) < hashBytesCount-1 {
panic("insufficient bytes to extract hash of path")
}

hash := hex.EncodeToString(hexBytes[:hashBytesCount])
bytesUsed += hashBytesCount
bytesUsed += hashBytesCount - 1
hexBytes = hexBytes[bytesUsed:]
fmt.Println("hash", hash)
fmt.Println("bytesUsed", bytesUsed)
fmt.Println("hexBytes", hexBytes)
fmt.Println()
}
}

func NewCMPFromStream(hexBytes []byte) (CompoundMerklePath, int, error) {
height, bytesUsed, err := extractHeight(hexBytes)
if err != nil {
return nil, 0, err
}
hexBytes = hexBytes[bytesUsed:]
hash = reverse(hash)

var cmp CompoundMerklePath
currentHeight := height
bytesUsedToDecodeCMP := bytesUsed

for currentHeight >= 0 {
var pathMap map[string]uint64

pathMap, bytesUsed, err = extractPathMap(hexBytes, currentHeight)
if err != nil {
return nil, 0, err
if flag == 0 {
bumpMap[strconv.FormatUint(uint64(offset), 10)] = BUMPTx{
hash: hash,
}
} else {
bumpMap[strconv.FormatUint(uint64(offset), 10)] = BUMPTx{
hash: hash,
txId: true,
}
}

cmp = append(CompoundMerklePath{pathMap}, cmp...)

hexBytes = hexBytes[bytesUsed:]

currentHeight--
bytesUsedToDecodeCMP += bytesUsed
}

return cmp, bytesUsedToDecodeCMP, nil
return BUMPMap{bumpMap}, hexBytes
}

func NewBUMPFromStream(hexBytes []byte) (CompoundMerklePath, int, error) {
height, bytesUsed, err := extractHeight(hexBytes)
if err != nil {
return nil, 0, err
}
hexBytes = hexBytes[bytesUsed:]

var cmp CompoundMerklePath
currentHeight := height
bytesUsedToDecodeCMP := bytesUsed

for currentHeight >= 0 {
var pathMap map[string]uint64

pathMap, bytesUsed, err = extractPathMap(hexBytes, currentHeight)
if err != nil {
return nil, 0, err
}

cmp = append(CompoundMerklePath{pathMap}, cmp...)

hexBytes = hexBytes[bytesUsed:]

currentHeight--
bytesUsedToDecodeCMP += bytesUsed
// reverse will reverse a hex string but it takes a pair of character at a time
func reverse(s string) string {
rns := []rune(s)
for i, j := 0, len(rns)-1; i < j; i, j = i+2, j-2 {
rns[i], rns[j], rns[i+1], rns[j-1] = rns[j-1], rns[i+1], rns[j], rns[i]
}

return cmp, bytesUsedToDecodeCMP, nil
return string(rns)
}

func decodeTransactionsWithPathIndexes(bytes []byte) ([]TxData, error) {
Expand Down

0 comments on commit b23bb66

Please sign in to comment.