-
Notifications
You must be signed in to change notification settings - Fork 3
/
merkleBlock.go
119 lines (99 loc) · 2.54 KB
/
merkleBlock.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package types
import (
"encoding/hex"
"fmt"
"io"
"github.com/bytom/encoding/blockchain"
"github.com/bytom/encoding/bufpool"
"github.com/bytom/errors"
)
// serflag variables, start with 1
const (
_ = iota
//SerBlockHeader
//SerBlockTransactions
//SerBlockFull
)
// Block describes a complete block, including its header and the transactions
// it contains.
type MerkleBlock struct {
BlockHeader
Transactions []*Tx
TxHashes [][32]byte
StatusHashes [][32]byte
RawTxStatuses [][]byte
Flags []byte
}
// MarshalText fulfills the json.Marshaler interface. This guarantees that
// blocks will get deserialized correctly when being parsed from HTTP requests.
func (b *MerkleBlock) MarshalText() ([]byte, error) {
buf := bufpool.Get()
defer bufpool.Put(buf)
if _, err := b.WriteTo(buf); err != nil {
return nil, err
}
enc := make([]byte, hex.EncodedLen(buf.Len()))
hex.Encode(enc, buf.Bytes())
return enc, nil
}
// UnmarshalText fulfills the encoding.TextUnmarshaler interface.
func (b *MerkleBlock) UnmarshalText(text []byte) error {
decoded := make([]byte, hex.DecodedLen(len(text)))
if _, err := hex.Decode(decoded, text); err != nil {
return err
}
r := blockchain.NewReader(decoded)
if err := b.readFrom(r); err != nil {
return err
}
if trailing := r.Len(); trailing > 0 {
return fmt.Errorf("trailing garbage (%d bytes)", trailing)
}
return nil
}
func (b *MerkleBlock) readFrom(r *blockchain.Reader) error {
serflags, err := b.BlockHeader.readFrom(r)
if err != nil {
return err
}
if serflags == SerBlockHeader {
return nil
}
n, err := blockchain.ReadVarint31(r)
if err != nil {
return errors.Wrap(err, "reading number of transactions")
}
for ; n > 0; n-- {
data := TxData{}
if err = data.readFrom(r); err != nil {
return errors.Wrapf(err, "reading transaction %d", len(b.Transactions))
}
b.Transactions = append(b.Transactions, NewTx(data))
}
return nil
}
// WriteTo will write block to input io.Writer
func (b *MerkleBlock) WriteTo(w io.Writer) (int64, error) {
ew := errors.NewWriter(w)
if err := b.writeTo(ew, SerBlockFull); err != nil {
return 0, err
}
return ew.Written(), ew.Err()
}
func (b *MerkleBlock) writeTo(w io.Writer, serflags uint8) error {
if err := b.BlockHeader.writeTo(w, serflags); err != nil {
return err
}
if serflags == SerBlockHeader {
return nil
}
if _, err := blockchain.WriteVarint31(w, uint64(len(b.Transactions))); err != nil {
return err
}
for _, tx := range b.Transactions {
if _, err := tx.WriteTo(w); err != nil {
return err
}
}
return nil
}