-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
80 lines (71 loc) · 1.96 KB
/
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
package ombutil
import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btclog"
"github.com/btcsuite/btcutil"
"github.com/soapboxsys/ombudslib/ombwire"
"github.com/soapboxsys/ombudslib/ombwire/peg"
)
// A helper struct that contains all of the data relevant to ombuds in a
// Bitcoin block.
type UBlock struct {
Block *btcutil.Block
Bulletins []*Bulletin
Endorsements []*Endorsement
}
// CreateUBlock parses a btcutil block and parses out the relevant records. If
// logger is not nil, it is used to report strange problems as the functions
// parses through a block.
func CreateUBlock(blk *btcutil.Block, log btclog.Logger, net *chaincfg.Params) *UBlock {
ublk := &UBlock{
Block: blk,
Bulletins: []*Bulletin{},
Endorsements: []*Endorsement{},
}
wLog := func(s string, args ...interface{}) {
if log != nil {
log.Warnf(s, args)
}
}
for _, tx := range blk.Transactions() {
if !ombwire.HasMagic(tx.MsgTx()) {
continue
}
w, err := ombwire.ParseTx(tx.MsgTx())
if w == nil || err != nil {
wLog("Parsing wire threw: %s", err)
continue
}
switch w := w.(type) {
case *ombwire.Bulletin:
bltn, err := NewBltn(w, tx, blk, net)
if err != nil {
wLog("Creating bltn threw: %s", err)
continue
}
ublk.Bulletins = append(ublk.Bulletins, bltn)
case *ombwire.Endorsement:
endo, err := NewEndo(w, tx, blk, net)
if err != nil {
wLog("Creating endo threw: %s", err)
continue
}
ublk.Endorsements = append(ublk.Endorsements, endo)
default:
continue
}
}
return ublk
}
// PastPegDate determines if the passed block was created after the target peg
// date after which entries can be added to the public record.
func PastPegDate(blk *btcutil.Block, net *chaincfg.Params) bool {
if net.Net == wire.MainNet {
return blk.Height() > peg.StartHeight
} else if net.Net == wire.TestNet3 {
return blk.Height() > peg.TestStartHeight
} else {
return false
}
}