/
coin.go
144 lines (125 loc) · 3.14 KB
/
coin.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
package utxo
import (
"encoding/binary"
"errors"
"github.com/copernet/copernicus/log"
"github.com/copernet/copernicus/model/script"
"github.com/copernet/copernicus/model/txout"
"github.com/copernet/copernicus/util"
"github.com/copernet/copernicus/util/amount"
"io"
)
type Coin struct {
txOut txout.TxOut
height int32
isCoinBase bool
dirty bool //whether modified
fresh bool //whether add new
isMempoolCoin bool
}
func (coin *Coin) GetHeight() int32 {
return coin.height
}
func (coin *Coin) IsCoinBase() bool {
return coin.isCoinBase
}
func (coin *Coin) IsMempoolCoin() bool {
return coin.isMempoolCoin
}
func (coin *Coin) IsSpendable() bool {
return coin.txOut.IsSpendable()
}
func (coin *Coin) IsSpent() bool {
return coin.txOut.IsNull()
}
func (coin *Coin) Clear() {
coin.txOut.SetNull()
coin.height = 0
coin.isCoinBase = false
}
func (coin *Coin) GetTxOut() txout.TxOut {
return coin.txOut
}
func (coin *Coin) GetScriptPubKey() *script.Script {
txOut := coin.txOut
return txOut.GetScriptPubKey()
}
func (coin *Coin) GetAmount() amount.Amount {
return coin.txOut.GetValue()
}
func (coin *Coin) DeepCopy() *Coin {
newCoin := Coin{height: coin.height, isCoinBase: coin.isCoinBase, dirty: coin.dirty, fresh: coin.fresh, isMempoolCoin: coin.isMempoolCoin}
outScript := coin.txOut.GetScriptPubKey()
if coin.txOut.GetScriptPubKey() != nil {
newOutScript := script.NewScriptRaw(outScript.GetData())
newOut := txout.NewTxOut(coin.txOut.GetValue(), newOutScript)
newCoin.txOut = *newOut
}
return &newCoin
}
func (coin *Coin) DynamicMemoryUsage() int64 {
return int64(binary.Size(coin))
}
func (coin *Coin) Serialize(w io.Writer) error {
if coin.IsSpent() {
log.Debug("already spent")
return errors.New("already spent")
}
var bit int32
if coin.isCoinBase {
bit = 1
}
heightAndIsCoinBase := (coin.height << 1) | bit
if err := util.WriteVarLenInt(w, uint64(heightAndIsCoinBase)); err != nil {
return err
}
tc := txout.NewTxoutCompressor(&coin.txOut)
return tc.Serialize(w)
}
func (coin *Coin) Unserialize(r io.Reader) error {
//hicb means heightAndIsCoinBase
hicb, err := util.ReadVarLenInt(r)
if err != nil {
log.Error("Coin UnSerialize: the read count is: %d, error: %v", hicb, err)
return err
}
heightAndIsCoinBase := int32(hicb)
coin.height = heightAndIsCoinBase >> 1
if (heightAndIsCoinBase & 1) == 1 {
coin.isCoinBase = true
}
tc := txout.NewTxoutCompressor(&coin.txOut)
return tc.Unserialize(r)
}
// NewFreshCoin creates an confirmed coin
func NewFreshCoin(out *txout.TxOut, height int32, isCoinBase bool) *Coin {
return &Coin{
txOut: *out,
height: height,
isCoinBase: isCoinBase,
fresh: true,
}
}
// NewMempoolCoin creates an unconfirmed coin for mempool
func NewMempoolCoin(out *txout.TxOut) *Coin {
return &Coin{
txOut: *out,
isMempoolCoin: true,
fresh: true,
}
}
func NewEmptyCoin() *Coin {
return &Coin{
txOut: *txout.NewTxOut(0, nil),
height: 0,
isCoinBase: false,
}
}
func NewFreshEmptyCoin() *Coin {
return &Coin{
txOut: *txout.NewTxOut(0, nil),
height: 0,
isCoinBase: false,
fresh: true,
}
}