-
Notifications
You must be signed in to change notification settings - Fork 227
/
bitcoin_block.go
88 lines (74 loc) · 2.03 KB
/
bitcoin_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package bitcoin
// https://learnmeabitcoin.com/technical/blkdat
import (
"fmt"
"time"
"github.com/wader/fq/format"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
)
var bitcoinTranscationGroup decode.Group
func init() {
interp.RegisterFormat(
format.Bitcoin_Block,
&decode.Format{
Description: "Bitcoin block",
Dependencies: []decode.Dependency{
{Groups: []*decode.Group{format.Bitcoin_Transaction}, Out: &bitcoinTranscationGroup},
},
DecodeFn: decodeBitcoinBlock,
DefaultInArg: format.Bitcoin_Block_In{
HasHeader: false,
},
})
}
var rawHexReverse = scalar.BitBufFn(func(s scalar.BitBuf) (scalar.BitBuf, error) {
return scalar.RawSym(s, -1, func(b []byte) string {
decode.ReverseBytes(b)
return fmt.Sprintf("%x", b)
})
})
func decodeBitcoinBlock(d *decode.D) any {
var bbi format.Bitcoin_Block_In
d.ArgAs(&bbi)
size := d.BitsLeft()
if bbi.HasHeader {
magic := d.PeekUintBits(32)
switch magic {
case 0xf9beb4d9,
0x0b110907,
0xfabfb5da:
d.FieldU32("magic", scalar.UintMapSymStr{
0xf9beb4d9: "mainnet",
0x0b110907: "testnet3",
0xfabfb5da: "regtest",
}, scalar.UintHex)
size = int64(d.FieldU32LE("size")) * 8
default:
d.Fatalf("unknown magic %x", magic)
}
}
d.Endian = decode.LittleEndian
d.FramedFn(size, func(d *decode.D) {
d.FieldStruct("header", func(d *decode.D) {
d.FieldU32("version", scalar.UintHex)
d.FieldRawLen("previous_block_hash", 32*8, rawHexReverse)
d.FieldRawLen("merkle_root", 32*8, rawHexReverse)
d.FieldU32("time", scalar.UintActualUnixTimeDescription(time.Second, time.RFC3339))
d.FieldU32("bits", scalar.UintHex)
d.FieldU32("nonce", scalar.UintHex)
})
// TODO: remove? support header only decode this way?
if d.BitsLeft() == 0 {
return
}
txCount := d.FieldUintFn("tx_count", decodeVarInt)
d.FieldArray("transactions", func(d *decode.D) {
for i := uint64(0); i < txCount; i++ {
d.FieldFormat("transaction", &bitcoinTranscationGroup, nil)
}
})
})
return nil
}