/
utxos.go
154 lines (118 loc) · 2.7 KB
/
utxos.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package blockchain
import (
"bytes"
"github.com/EnsicoinDevs/eccd/network"
"io"
)
type UtxoEntry struct {
amount uint64
script []byte
blockHeight uint32
coinBase bool
}
func newUtxoEntry() *UtxoEntry {
return &UtxoEntry{}
}
func WriteUtxoEntry(writer io.Writer, entry *UtxoEntry) error {
err := network.WriteUint64(writer, entry.amount)
if err != nil {
return err
}
err = network.WriteVarUint(writer, uint64(len(entry.script)))
if err != nil {
return err
}
_, err = writer.Write(entry.script)
if err != nil {
return err
}
var coinBase byte
if entry.coinBase {
coinBase = 0x01
}
_, err = writer.Write([]byte{coinBase})
if err != nil {
return err
}
return nil
}
func ReadUtxoEntry(reader io.Reader) (*UtxoEntry, error) {
entry := new(UtxoEntry)
var err error
entry.amount, err = network.ReadUint64(reader)
if err != nil {
return nil, err
}
scriptLength, err := network.ReadVarUint(reader)
if err != nil {
return nil, err
}
entry.script = make([]byte, scriptLength)
_, err = io.ReadFull(reader, entry.script)
if err != nil {
return nil, err
}
coinBase := make([]byte, 1)
_, err = reader.Read(coinBase)
if err != nil {
return nil, err
}
if coinBase[0] != 0x00 {
entry.coinBase = true
}
return entry, nil
}
func (entry *UtxoEntry) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := WriteUtxoEntry(buf, entry)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (entry *UtxoEntry) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
newEntry, err := ReadUtxoEntry(buf)
*entry = *newEntry
return err
}
func (entry *UtxoEntry) Amount() uint64 {
return entry.amount
}
func (entry *UtxoEntry) Script() []byte {
return entry.script
}
func (entry *UtxoEntry) BlockHeight() uint32 {
return entry.blockHeight
}
func (entry *UtxoEntry) IsCoinBase() bool {
return entry.coinBase
}
type Utxos struct {
entries map[network.Outpoint]*UtxoEntry
}
func newUtxos() *Utxos {
return &Utxos{
entries: make(map[network.Outpoint]*UtxoEntry),
}
}
func (utxos *Utxos) AddEntry(outpoint *network.Outpoint, entry *UtxoEntry) {
utxos.entries[*outpoint] = entry
}
func (utxos *Utxos) AddEntryWithTx(outpoint *network.Outpoint, tx *Tx, blockHeight uint32) {
output := tx.Msg.Outputs[outpoint.Index]
utxos.AddEntry(outpoint, &UtxoEntry{
amount: output.Value,
script: output.Script,
blockHeight: blockHeight,
coinBase: tx.IsCoinBase(),
})
}
func (utxos *Utxos) FindEntry(outpoint *network.Outpoint) *UtxoEntry {
return utxos.entries[*outpoint]
}
func (utxos *Utxos) RemoveEntry(outpoint *network.Outpoint) *UtxoEntry {
entry := utxos.FindEntry(outpoint)
delete(utxos.entries, *outpoint)
return entry
}