Skip to content

Commit

Permalink
fix: Bug in bump deserialization (#78)
Browse files Browse the repository at this point in the history
* recreation of the failure
* only parse the hash if it’s there


---------

Signed-off-by: Darren Kellenschwiler <[email protected]>
  • Loading branch information
sirdeggen authored Nov 20, 2023
1 parent ea2407e commit a31145d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ func NewBUMPFromBytes(bytes []byte) (*BUMP, error) {
txid := flags&2 > 0
if dup {
l.Duplicate = &dup
} else {
h := StringFromBytesReverse(bytes[skip : skip+32])
l.Hash = &h
skip += 32
}
if txid {
l.Txid = &txid
}
h := StringFromBytesReverse(bytes[skip : skip+32])
l.Hash = &h
skip += 32
bump.Path[lv][lf] = l
}
}
Expand Down
32 changes: 32 additions & 0 deletions bump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ const (
rootOfBlockTxExample = `1a1e779cd7dfc59f603b4e88842121001af822b2dc5d3b167ae66152e586a6b0`
fakeMadeUpNum = 814435
txidSmallBlock = `be7853d7685ed5b5ec61a0ca8e3f193a7b0632cb2db950528c2041ee5d7dd95d`

testnetHexExample = `0202ac5abbdc202bd6d50260825dd5465bd0798b052b2115c7abed277a41e403b21d0a61f6e257da3871657d324a31a449aa58a83b61967f1f2192eca71b3c21f68f`
testnetRootExample = `e4bbc8329d950749cb844ba2dac1e09bfc418d5c96d03fb688eff95bb90965b8`
testnetTxidExample = `8d15f64f8a35e94eb3cdbc07d9bc28952fdbdd13315fe3a94b393e635a9307aa`
)

var testnetBlockExample = []string{
"c615bb1649384ebe491151c2c1afd4d5f75bf8d81f8c2f76b48305e96f58c40e",
"8d15f64f8a35e94eb3cdbc07d9bc28952fdbdd13315fe3a94b393e635a9307aa",
"1db203e4417a27edabc715212b058b79d05b46d55d826002d5d62b20dcbb5aac",
}

var blockTxExample = []string{
"b6d4d13aa08bb4b6cdb3b329cef29b5a5d55d85a85c330d56fddbce78d99c7d6",
"426f65f6a6ce79c909e54d8959c874a767db3076e76031be70942b896cc64052",
Expand Down Expand Up @@ -84,3 +94,25 @@ func TestCalculateRootGivenTxid(t *testing.T) {
require.NoError(t, err)
require.Equal(t, rootExample, root)
}

func TestTestnetCalculateRootGivenTxid(t *testing.T) {
chainHashBlock := make([]*chainhash.Hash, 0)
for _, txid := range testnetBlockExample {
hash, err := chainhash.NewHashFromStr(txid)
require.NoError(t, err)
chainHashBlock = append(chainHashBlock, hash)
}
merkles, err := BuildMerkleTreeStoreChainHash(chainHashBlock)
require.NoError(t, err)
for txIndex, txid := range testnetBlockExample {
bump, err := NewBUMPFromMerkleTreeAndIndex(1575794, merkles, uint64(txIndex))
require.NoError(t, err)
b2, err := bump.String()
require.NoError(t, err)
b3, err := NewBUMPFromStr(b2)
require.NoError(t, err)
root, err := b3.CalculateRootGivenTxid(txid)
require.NoError(t, err)
require.Equal(t, testnetRootExample, root)
}
}

0 comments on commit a31145d

Please sign in to comment.