forked from libsv/go-bt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_txs_from_block.go
43 lines (36 loc) · 977 Bytes
/
read_txs_from_block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"bufio"
"fmt"
"io"
"github.com/libsv/go-bt/v2"
"github.com/libsv/go-bt/v2/testing/data"
)
// 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 interactively 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 := data.TxBinData.Open("block.bin")
if err != nil {
panic(err)
}
defer f.Close()
// Create buffered reader for this file.
r := bufio.NewReader(f)
// Read file header. This step is specific to file reading and
// may need omitted or modified for other implementations.
_, 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())
}
}