1
1
mirror of https://github.com/wader/fq.git synced 2024-10-04 07:27:08 +03:00

bitcoin_blkdat,bitcoin_block: Make sure there is a header if blkdat

Makes bitcoin_blkdat fails fast as it is part of probe group.
Speeds up reading a big JSON file etc.
This commit is contained in:
Mattias Wadman 2022-08-26 12:07:29 +02:00
parent 6bbfc66051
commit 004406de65
4 changed files with 30 additions and 12 deletions

View File

@ -188,11 +188,17 @@ out # Decode value as bitcoin_blkdat
out ... | bitcoin_blkdat
"help(bitcoin_block)"
out bitcoin_block: Bitcoin block decoder
out Options:
out has_header=false Has blkdat header
out Examples:
out # Decode file as bitcoin_block
out $ fq -d bitcoin_block . file
out # Decode value as bitcoin_block
out ... | bitcoin_block
out # Decode file using bitcoin_block options
out $ fq -d bitcoin_block -o has_header=false . file
out # Decode value as bitcoin_block
out ... | bitcoin_block({has_header:false})
"help(bitcoin_script)"
out bitcoin_script: Bitcoin script decoder
out Examples:

View File

@ -25,7 +25,7 @@ func init() {
func decodeBlkDat(d *decode.D, in interface{}) interface{} {
validBlocks := 0
for !d.End() {
d.FieldFormat("block", bitcoinBlockFormat, nil)
d.FieldFormat("block", bitcoinBlockFormat, format.BitCoinBlockIn{HasHeader: true})
validBlocks++
}

View File

@ -21,6 +21,9 @@ func init() {
{Names: []string{format.BITCOIN_TRANSACTION}, Group: &bitcoinTranscationFormat},
},
DecodeFn: decodeBitcoinBlock,
DecodeInArg: format.BitCoinBlockIn{
HasHeader: false,
},
})
}
@ -32,19 +35,24 @@ var rawHexReverse = scalar.Fn(func(s scalar.S) (scalar.S, error) {
})
func decodeBitcoinBlock(d *decode.D, in interface{}) interface{} {
bbi, _ := in.(format.BitCoinBlockIn)
size := d.BitsLeft()
// TODO: move to blkdat but how to model it?
switch d.PeekBits(32) {
case 0xf9beb4d9,
0x0b110907,
0xfabfb5da:
d.FieldU32("magic", scalar.UToSymStr{
0xf9beb4d9: "mainnet",
0x0b110907: "testnet3",
0xfabfb5da: "regtest",
}, scalar.ActualHex)
size = int64(d.FieldU32LE("size")) * 8
if bbi.HasHeader {
magic := d.PeekBits(32)
switch magic {
case 0xf9beb4d9,
0x0b110907,
0xfabfb5da:
d.FieldU32("magic", scalar.UToSymStr{
0xf9beb4d9: "mainnet",
0x0b110907: "testnet3",
0xfabfb5da: "regtest",
}, scalar.ActualHex)
size = int64(d.FieldU32LE("size")) * 8
default:
d.Fatalf("unknown magic %x", magic)
}
}
d.Endian = decode.LittleEndian

View File

@ -310,3 +310,7 @@ type CSVLIn struct {
Comma string `doc:"Separator character"`
Comment string `doc:"Comment line character"`
}
type BitCoinBlockIn struct {
HasHeader bool `doc:"Has blkdat header"`
}