Skip to content

Commit

Permalink
move over to bt.VarInt datatype (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigh-latte authored Dec 14, 2021
1 parent 1ebf29d commit 5153ba3
Show file tree
Hide file tree
Showing 22 changed files with 386 additions and 112 deletions.
4 changes: 2 additions & 2 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (b *Block) Bytes() []byte {
bytes = append(bytes, b.BlockHeader.Bytes()...)

txCount := uint64(len(b.Txs))
bytes = append(bytes, bt.VarInt(txCount)...)
bytes = append(bytes, bt.VarInt(txCount).Bytes()...)

for _, tx := range b.Txs {
bytes = append(bytes, tx.Bytes()...)
Expand Down Expand Up @@ -73,7 +73,7 @@ func NewBlockFromBytes(b []byte) (*Block, error) {
}
offset += 80

txCount, size := bt.DecodeVarInt(b[offset:])
txCount, size := bt.NewVarIntFromBytes(b[offset:])
offset += size

var txs []*bt.Tx
Expand Down
10 changes: 5 additions & 5 deletions coinbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func makeCoinbaseOutputTransactions(coinbaseValue uint64, defaultWitnessCommitme

binary.LittleEndian.PutUint64(buf[0:], coinbaseValue)

buf = append(buf, bt.VarInt(uint64(len(*o.LockingScript)))...)
buf = append(buf, bt.VarInt(uint64(len(*o.LockingScript))).Bytes()...)
buf = append(buf, *o.LockingScript...)

numberOfTransactions := 1
Expand All @@ -101,7 +101,7 @@ func makeCoinbaseOutputTransactions(coinbaseValue uint64, defaultWitnessCommitme
log.Printf("Error decoding witness commitment: %+v", err)
return nil, err
}
wcl := bt.VarInt(uint64(len(wc)))
wcl := bt.VarInt(uint64(len(wc))).Bytes()
buf = append(buf, wcl...)
buf = append(buf, wc...)
}
Expand All @@ -111,11 +111,11 @@ func makeCoinbaseOutputTransactions(coinbaseValue uint64, defaultWitnessCommitme

byteArr := make([]byte, 8) // 8 bytes of 0 = 0 satoshis.
buf = append(buf, byteArr...)
buf = append(buf, bt.VarInt(uint64(len(minerIDBytes)))...)
buf = append(buf, bt.VarInt(uint64(len(minerIDBytes))).Bytes()...)
buf = append(buf, minerIDBytes...)
}

buf = append(bt.VarInt(uint64(numberOfTransactions)), buf...)
buf = append(bt.VarInt(uint64(numberOfTransactions)).Bytes(), buf...)
return buf, nil
}

Expand Down Expand Up @@ -143,7 +143,7 @@ func makeCoinbase1(height uint32, coinbaseText string) []byte {
buf = append(buf, make([]byte, 32)...) // Transaction hash - 4 bytes all bits are zero
buf = append(buf, []byte{0xff, 0xff, 0xff, 0xff}...) // Coinbase data size - 4 bytes - All bits are ones: 0xFFFFFFFF (ffffffff)

buf = append(buf, bt.VarInt(uint64(len(arbitraryData)+spaceForExtraNonce))...) // Length of the coinbase data, from 2 to 100 bytes
buf = append(buf, bt.VarInt(uint64(len(arbitraryData)+spaceForExtraNonce)).Bytes()...) // Length of the coinbase data, from 2 to 100 bytes
buf = append(buf, arbitraryData...)

return buf
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/libsv/go-bk v0.1.5
github.com/libsv/go-bt/v2 v2.1.0-beta.1
github.com/libsv/go-bt/v2 v2.1.0-beta.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/libsv/go-bk v0.1.5 h1:fqbWy8nwVM/ayM8Nxe+lM7fN0FaUMMD1w5MCpwit7XQ=
github.com/libsv/go-bk v0.1.5/go.mod h1:xbDkeFFpP0uyFaPLnP6TwaLpAsHaslZ0LftTdWlB6HI=
github.com/libsv/go-bt/v2 v2.1.0-beta.1 h1:3nSvhUnrCOM4Cel+zVlLjCoAITCJ7dbYJuPGj6x8958=
github.com/libsv/go-bt/v2 v2.1.0-beta.1/go.mod h1:u5g3GmVLffBV8sWvMqHR3JekC51OR9XYvmQp1h/XoiQ=
github.com/libsv/go-bt/v2 v2.1.0-beta.2 h1:oq6BQQtNeZiG/esfoY/7RyYF+dDj996xqNfvoQfH6n4=
github.com/libsv/go-bt/v2 v2.1.0-beta.2/go.mod h1:u5g3GmVLffBV8sWvMqHR3JekC51OR9XYvmQp1h/XoiQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
4 changes: 2 additions & 2 deletions merkleproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (mp *MerkleProof) Bytes() ([]byte, error) {
// set bit at index 0
flags |= 1 << 0

txLength = bt.VarInt(uint64(len(txOrID)))
txLength = bt.VarInt(uint64(len(txOrID))).Bytes()
}

if mp.TargetType == "header" {
Expand All @@ -86,7 +86,7 @@ func (mp *MerkleProof) Bytes() ([]byte, error) {

bytes := []byte{}
bytes = append(bytes, flags)
bytes = append(bytes, index...)
bytes = append(bytes, index.Bytes()...)
bytes = append(bytes, txLength...)
bytes = append(bytes, txOrID...)
bytes = append(bytes, target...)
Expand Down
18 changes: 9 additions & 9 deletions spv/ancestors_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ func NewAncestryFromBytes(b []byte) (*Ancestry, error) {
offset := uint64(1)
total := uint64(len(b))

l, size := bt.DecodeVarInt(b[offset:])
l, size := bt.NewVarIntFromBytes(b[offset:])
offset += uint64(size)
paymentTx, err := bt.NewTxFromBytes(b[offset : offset+l])
paymentTx, err := bt.NewTxFromBytes(b[offset : offset+uint64(l)])
if err != nil {
return nil, err
}
ancestry := &Ancestry{
PaymentTx: paymentTx,
Ancestors: make(map[[32]byte]*Ancestor),
}
offset += l
offset += uint64(l)

var TxID [32]byte

Expand Down Expand Up @@ -104,13 +104,13 @@ func parseChunk(b []byte, start uint64) (binaryChunk, uint64) {
offset := start
typeOfNextData := b[offset]
offset++
l, size := bt.DecodeVarInt(b[offset:])
l, size := bt.NewVarIntFromBytes(b[offset:])
offset += uint64(size)
chunk := binaryChunk{
ContentType: typeOfNextData,
Data: b[offset : offset+l],
Data: b[offset : offset+uint64(l)],
}
offset += l
offset += uint64(l)
return chunk, offset - start
}

Expand All @@ -128,10 +128,10 @@ func parseMapiCallbacks(b []byte) ([]*bc.MapiCallback, error) {

var responses = [][]byte{}
for allBinary > internalOffset {
l, size := bt.DecodeVarInt(b[internalOffset:])
l, size := bt.NewVarIntFromBytes(b[internalOffset:])
internalOffset += uint64(size)
response := b[internalOffset : internalOffset+l]
internalOffset += l
response := b[internalOffset : internalOffset+uint64(l)]
internalOffset += uint64(l)
responses = append(responses, response)
}

Expand Down
10 changes: 5 additions & 5 deletions spv/ancestors_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (j *AncestryJSON) Bytes() ([]byte, error) {
return nil, err
}
length := bt.VarInt(uint64(len(paymentTx)))
binaryTxContext = append(binaryTxContext, length...)
binaryTxContext = append(binaryTxContext, length.Bytes()...)
binaryTxContext = append(binaryTxContext, paymentTx...)

// follow with the list of ancestors, including their proof or mapi responses if present.
Expand All @@ -81,7 +81,7 @@ func (j *AncestryJSON) Bytes() ([]byte, error) {
}
length := bt.VarInt(uint64(len(rawTx)))
binaryTxContext = append(binaryTxContext, flagTx)
binaryTxContext = append(binaryTxContext, length...)
binaryTxContext = append(binaryTxContext, length.Bytes()...)
binaryTxContext = append(binaryTxContext, rawTx...)
if ancestor.Proof != nil {
rawProof, err := ancestor.Proof.Bytes()
Expand All @@ -90,20 +90,20 @@ func (j *AncestryJSON) Bytes() ([]byte, error) {
}
length := bt.VarInt(uint64(len(rawProof)))
binaryTxContext = append(binaryTxContext, flagProof)
binaryTxContext = append(binaryTxContext, length...)
binaryTxContext = append(binaryTxContext, length.Bytes()...)
binaryTxContext = append(binaryTxContext, rawProof...)
}
if ancestor.MapiResponses != nil && len(ancestor.MapiResponses) > 0 {
binaryTxContext = append(binaryTxContext, flagMapi)
numOfMapiResponses := bt.VarInt(uint64(len(ancestor.MapiResponses)))
binaryTxContext = append(binaryTxContext, numOfMapiResponses...)
binaryTxContext = append(binaryTxContext, numOfMapiResponses.Bytes()...)
for _, mapiResponse := range ancestor.MapiResponses {
mapiR, err := mapiResponse.Bytes()
if err != nil {
return nil, err
}
dataLength := bt.VarInt(uint64(len(mapiR)))
binaryTxContext = append(binaryTxContext, dataLength...)
binaryTxContext = append(binaryTxContext, dataLength.Bytes()...)
binaryTxContext = append(binaryTxContext, mapiR...)
}
}
Expand Down
5 changes: 1 addition & 4 deletions spv/ancestors_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,10 +46,8 @@ func TestAncestryBinaryToJSON(t *testing.T) {
j, err := spv.NewAncestoryJSONFromBytes(binary)
assert.NoError(t, err, "expected no error when transforming to json struct")

jsonBytes, err := json.Marshal(j)
_, err = json.Marshal(j)
assert.NoError(t, err, "expected no error when transforming to json bytes")
fmt.Println(string(jsonBytes))
assert.NoError(t, err)
}
})
}
Expand Down
16 changes: 8 additions & 8 deletions spv/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ func serializeParents(parents map[string]*Envelope, flake *[]byte, root bool) er
if !root {
*flake = append(*flake, flagTx) // first data will always be a rawTx.
}
*flake = append(*flake, dataLength...) // of this length.
*flake = append(*flake, currentTx...) // the data.
*flake = append(*flake, dataLength.Bytes()...) // of this length.
*flake = append(*flake, currentTx...) // the data.
if input.MapiResponses != nil && len(input.MapiResponses) > 0 {
*flake = append(*flake, flagMapi) // next data will be a mapi response.
numMapis := bt.VarInt(uint64(len(input.MapiResponses)))
*flake = append(*flake, numMapis...) // number of mapi reponses which follow
*flake = append(*flake, numMapis.Bytes()...) // number of mapi reponses which follow
for _, mapiResponse := range input.MapiResponses {
mapiR, err := mapiResponse.Bytes()
if err != nil {
return err
}
dataLength := bt.VarInt(uint64(len(mapiR)))
*flake = append(*flake, dataLength...) // of this length.
*flake = append(*flake, mapiR...) // the data.
*flake = append(*flake, dataLength.Bytes()...) // of this length.
*flake = append(*flake, mapiR...) // the data.
}
}
if input.Proof != nil {
Expand All @@ -95,9 +95,9 @@ func serializeParents(parents map[string]*Envelope, flake *[]byte, root bool) er
return errors.Wrap(err, "Failed to serialise this input's proof struct")
}
proofLength := bt.VarInt(uint64(len(proof)))
*flake = append(*flake, flagProof) // it's going to be a proof.
*flake = append(*flake, proofLength...) // of this length.
*flake = append(*flake, proof...) // the data.
*flake = append(*flake, flagProof) // it's going to be a proof.
*flake = append(*flake, proofLength.Bytes()...) // of this length.
*flake = append(*flake, proof...) // the data.
} else if input.HasParents() {
err = serializeParents(input.Parents, flake, false)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions spv/verifymerkleproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,18 @@ type merkleProofBinary struct {
func parseBinaryMerkleProof(proof []byte) (*merkleProofBinary, error) {
mpb := &merkleProofBinary{}

var offset, size int
var offset int

// flags is first byte
mpb.flags = proof[offset]
offset++

// index is the next varint after the 1st byte
mpb.index, size = bt.DecodeVarInt(proof[offset:])
index, size := bt.NewVarIntFromBytes(proof[offset:])
mpb.index = uint64(index)
offset += size

var txLength uint64
var txLength bt.VarInt
// if bit 1 of flags is NOT set, txOrId should contain txid (= 32 bytes)
if mpb.flags&1 == 0 {
txLength = 32
Expand All @@ -270,7 +271,7 @@ func parseBinaryMerkleProof(proof []byte) (*merkleProofBinary, error) {
// if bit 1 of flags is set, txOrId should contain tx hex (> 32 bytes)
if mpb.flags&1 == 1 {
// txLength is the next varint after the 1st byte + index size
txLength, size = bt.DecodeVarInt(proof[offset:])
txLength, size = bt.NewVarIntFromBytes(proof[offset:])
offset += size
if txLength <= 32 {
return nil, errors.New("invalid tx length (should be greater than 32 bytes)")
Expand All @@ -297,7 +298,7 @@ func parseBinaryMerkleProof(proof []byte) (*merkleProofBinary, error) {
return nil, ErrInvalidMerkleFlags
}

nodeCount, size := bt.DecodeVarInt(proof[offset:])
nodeCount, size := bt.NewVarIntFromBytes(proof[offset:])
offset += size

if mpb.index >= 1<<nodeCount {
Expand Down
9 changes: 5 additions & 4 deletions vendor/github.com/libsv/go-bt/v2/bscript/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 54 additions & 10 deletions vendor/github.com/libsv/go-bt/v2/bscript/script.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5153ba3

Please sign in to comment.