forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
181 lines (165 loc) · 4.1 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package db
import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/jchavannes/btcd/wire"
"github.com/jchavannes/jgo/jerr"
"github.com/memocash/memo/app/bitcoin/wallet"
"time"
)
type Block struct {
Id uint `gorm:"primary_key"`
Height uint `gorm:"unique"`
Timestamp time.Time
Hash []byte `gorm:"unique"`
PrevBlock []byte
MerkleRoot []byte
Nonce uint32
TxnCount uint32
Version int32
Bits uint32
CreatedAt time.Time
UpdatedAt time.Time
}
func (b *Block) Save() error {
result := save(b)
if result.Error != nil {
return jerr.Get("error saving block", result.Error)
}
return nil
}
func (b Block) GetChainhash() *chainhash.Hash {
hash, _ := chainhash.NewHash(b.Hash)
return hash
}
func (b Block) GetPrevBlockChainhash() *chainhash.Hash {
hash, _ := chainhash.NewHash(b.PrevBlock)
return hash
}
func (b Block) GetMerkleRoot() *chainhash.Hash {
hash, _ := chainhash.NewHash(b.MerkleRoot)
return hash
}
func (b Block) NextHeight() uint {
return b.Height + 1
}
func (b Block) PrevHeight() uint {
if b.Height == 0 {
return 0
}
return b.Height - 1
}
func SaveBlocks(blocks []*Block) error {
for _, block := range blocks {
err := block.Save()
if err != nil {
return jerr.Get("error saving blocks", err)
}
}
return nil
}
func GetGenesis() (*Block, error) {
var block = Block{
Height: 0,
Timestamp: time.Unix(1231006505, 0),
Hash: wallet.GenesisBlock.Hash.CloneBytes(),
MerkleRoot: wallet.GenesisBlock.MerkleRoot.CloneBytes(),
Nonce: 2083236893,
TxnCount: 1,
Version: 1,
Bits: 0x1d00ffff,
}
err := find(&block, &block)
if err == nil {
return &block, nil
}
if ! IsRecordNotFoundError(err) {
return nil, jerr.Get("error finding genesis block", err)
}
err = create(&block)
if err != nil {
return nil, jerr.Get("error creating block", err)
}
return &block, nil
}
func ConvertMessageHeaderToBlock(header *wire.BlockHeader) (*Block) {
blockHash := header.BlockHash()
return &Block{
Timestamp: header.Timestamp,
Hash: blockHash.CloneBytes(),
PrevBlock: header.PrevBlock.CloneBytes(),
MerkleRoot: header.MerkleRoot.CloneBytes(),
Nonce: header.Nonce,
Version: header.Version,
Bits: header.Bits,
}
}
func GetBlockByHeight(height uint) (*Block, error) {
db, err := getDb()
if err != nil {
return nil, jerr.Get("error getting db", err)
}
var block = Block{}
result := db.Where("height = ?", height).Find(&block)
if result.Error != nil {
return nil, jerr.Get("error getting block", result.Error)
}
return &block, nil
}
func GetBlockByHash(hash chainhash.Hash) (*Block, error) {
var block = Block{
Hash: hash.CloneBytes(),
}
err := find(&block, &block)
if err != nil {
return nil, jerr.Get("error getting block", err)
}
return &block, nil
}
func AddBlock(block *Block) error {
parent, err := GetBlockByHash(*block.GetPrevBlockChainhash())
if err != nil {
return jerr.Get("error getting parent", err)
}
block.Height = parent.Height + 1
_, err = GetBlockByHash(*block.GetChainhash())
if err == nil {
return jerr.New("block already exists")
}
result := save(&block)
if result.Error != nil {
return jerr.Get("error saving block", result.Error)
}
return nil
}
func GetRecentBlock() (*Block, error) {
db, err := getDb()
if err != nil {
return nil, jerr.Get("error getting db", err)
}
var block Block
result := db.Order("height desc").First(&block)
if result.Error != nil {
if IsRecordNotFoundError(result.Error) {
return GetGenesis()
}
return nil, jerr.Get("error querying first block", result.Error)
}
return &block, nil
}
func GetBlocksInHeightRange(startHeight uint, endHeight uint) ([]*Block, error) {
query, err := getDb()
if err != nil {
return nil, jerr.Get("error getting db", err)
}
var blocks []*Block
if startHeight > endHeight {
query = query.Order("height desc")
startHeight, endHeight = endHeight, startHeight
}
query = query.Where("height >= ? AND height <= ?", startHeight, endHeight)
result := query.Find(&blocks)
if result.Error != nil {
return nil, jerr.Get("error querying blocks", result.Error)
}
return blocks, nil
}