From b139f295f89856d0b73a86de263e06e76a2079c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=ADghearn=C3=A1n=20Carroll?= Date: Fri, 10 Dec 2021 09:28:58 +0000 Subject: [PATCH 1/5] added readfrom in txs type. added example --- .../read_txs_from_block.go | 42 +++++++++++++++++++ tx.go | 28 +++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 examples/read_txs_from_block/read_txs_from_block.go diff --git a/examples/read_txs_from_block/read_txs_from_block.go b/examples/read_txs_from_block/read_txs_from_block.go new file mode 100644 index 00000000..a3f0baa8 --- /dev/null +++ b/examples/read_txs_from_block/read_txs_from_block.go @@ -0,0 +1,42 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + + "github.com/libsv/go-bt/v2" +) + +// In this example, all txs from a block are being read in via chunking, so at no point +// does the entire block have to be held in memory, and instead can be streamed. +// +// We represent the block by interatively reading a file, however it could be any data +// stream that satisfies the io.Reader interface. + +func main() { + // Open file container block data. + f, err := os.Open("testing/data/tx/bin/block.bin") + if err != nil { + panic(err) + } + + // Create buffered reader for this file. + r := bufio.NewReader(f) + + // Read file header. This is step is specific to file reading and + // may need omitted or modified for other implentations. + _, err = io.ReadFull(f, make([]byte, 80)) + if err != nil { + panic(err) + } + + txs := bt.Txs{} + if _, err = txs.ReadFrom(r); err != nil { + panic(err) + } + for _, tx := range txs { + fmt.Println(tx.TxID()) + } +} diff --git a/tx.go b/tx.go index 22928760..fbc427a0 100644 --- a/tx.go +++ b/tx.go @@ -189,6 +189,34 @@ func (tx *Tx) ReadFrom(r io.Reader) (int64, error) { return bytesRead, nil } +// ReadFrom txs from a block in a `bt.Txs`. This assumes a preceeding varint detailing +// the total number of txs that the reader will provide. +func (tt *Txs) ReadFrom(r io.Reader) (int64, error) { + var bytesRead int64 + + var txCount VarInt + n, err := txCount.ReadFrom(r) + bytesRead += n + if err != nil { + return bytesRead, err + } + + *tt = make([]*Tx, txCount) + + for i := int64(0); i < int64(txCount); i++ { + tx := new(Tx) + n, err := tx.ReadFrom(r) + bytesRead += n + if err != nil { + return bytesRead, err + } + + (*tt)[i] = tx + } + + return bytesRead, nil +} + // HasDataOutputs returns true if the transaction has // at least one data (OP_RETURN) output in it. func (tx *Tx) HasDataOutputs() bool { From 8b7cfab226a92a8f0d62787bb7dc183430783640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=ADghearn=C3=A1n=20Carroll?= Date: Fri, 10 Dec 2021 09:39:50 +0000 Subject: [PATCH 2/5] fixed linter --- tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tx.go b/tx.go index fbc427a0..eb8d4387 100644 --- a/tx.go +++ b/tx.go @@ -189,7 +189,7 @@ func (tx *Tx) ReadFrom(r io.Reader) (int64, error) { return bytesRead, nil } -// ReadFrom txs from a block in a `bt.Txs`. This assumes a preceeding varint detailing +// ReadFrom txs from a block in a `bt.Txs`. This assumes a preceding varint detailing // the total number of txs that the reader will provide. func (tt *Txs) ReadFrom(r io.Reader) (int64, error) { var bytesRead int64 From 8bb409630213ed000b49d7b3b59abe45acccc55a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=ADghearn=C3=A1n=20Carroll?= Date: Fri, 10 Dec 2021 09:48:23 +0000 Subject: [PATCH 3/5] change type cast to uin64 --- tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tx.go b/tx.go index eb8d4387..232034f4 100644 --- a/tx.go +++ b/tx.go @@ -203,7 +203,7 @@ func (tt *Txs) ReadFrom(r io.Reader) (int64, error) { *tt = make([]*Tx, txCount) - for i := int64(0); i < int64(txCount); i++ { + for i := uint64(0); i < uint64(txCount); i++ { tx := new(Tx) n, err := tx.ReadFrom(r) bytesRead += n From 62827609f4caae9f198bc44fe91ceb9d734a9b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=ADghearn=C3=A1n=20Carroll?= Date: Fri, 10 Dec 2021 09:51:12 +0000 Subject: [PATCH 4/5] added test for bt.Txs.ReadFrom --- tx_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tx_test.go b/tx_test.go index 2e4cf397..cc9dd813 100644 --- a/tx_test.go +++ b/tx_test.go @@ -1167,3 +1167,26 @@ func TestTx_ReadFrom(t *testing.T) { assert.Equal(t, "b7c59d7fa17a74bbe0a05e5381f42b9ac7fe23b8a1ca40005a74802fe5b8bb5a", tx.TxID()) assert.Equal(t, int64(340216), bytesRead) } + +func TestTxs_ReadFrom(t *testing.T) { + f, err := data.TxBinData.Open("block.bin") + defer func() { + if f != nil { + f.Close() + } + }() + assert.NoError(t, err) + + r := bufio.NewReader(f) + + header := make([]byte, 80) + _, err = io.ReadFull(f, header) + assert.NoError(t, err) + + txs := bt.Txs{} + bytesRead, err := txs.ReadFrom(r) + assert.NoError(t, err) + + assert.Equal(t, "b7c59d7fa17a74bbe0a05e5381f42b9ac7fe23b8a1ca40005a74802fe5b8bb5a", txs[len(txs)-1].TxID()) + assert.Equal(t, int64(340219), bytesRead) +} From d52c4ad6daa982696589e6046b1d0ca3f0a0b141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=ADghearn=C3=A1n=20Carroll?= Date: Fri, 10 Dec 2021 14:03:23 +0000 Subject: [PATCH 5/5] fixed typo --- examples/read_txs_from_block/read_txs_from_block.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/read_txs_from_block/read_txs_from_block.go b/examples/read_txs_from_block/read_txs_from_block.go index a3f0baa8..afedaa40 100644 --- a/examples/read_txs_from_block/read_txs_from_block.go +++ b/examples/read_txs_from_block/read_txs_from_block.go @@ -25,7 +25,7 @@ func main() { // Create buffered reader for this file. r := bufio.NewReader(f) - // Read file header. This is step is specific to file reading and + // Read file header. This step is specific to file reading and // may need omitted or modified for other implentations. _, err = io.ReadFull(f, make([]byte, 80)) if err != nil {