/
block_bl.go
187 lines (168 loc) · 5.04 KB
/
block_bl.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
182
183
184
185
186
187
package ldb
// // Deprecated
// import (
// "encoding/binary"
// "fmt"
// "github.com/fatzero/mass-core/database"
// "github.com/fatzero/mass-core/database/storage"
// "github.com/fatzero/mass-core/errors"
// "github.com/fatzero/mass-core/interfaces"
// "github.com/fatzero/mass-core/wire"
// )
// var (
// pubkblKeyPrefix = "PUBKBL"
// // uppkblKey = []byte("UPPKBL")
// // prefix + pk(compressed)
// pubkblKeyPrefixLen = len(pubkblKeyPrefix)
// // bl + blkHeight
// blHeightLen = 1 + 8
// )
// func makePubkblKey(publicKey interfaces.PublicKey) ([]byte, error) {
// pkbytes := publicKey.SerializeCompressed()
// switch len(pkbytes) {
// case PUBLICKEYLENGTH_MASS, PUBLICKEYLENGTH_CHIA:
// default:
// return nil, fmt.Errorf("invalid pk length %d", len(pkbytes))
// }
// key := make([]byte, pubkblKeyPrefixLen+len(pkbytes))
// copy(key, pubkblKeyPrefix)
// copy(key[pubkblKeyPrefixLen:], pkbytes)
// return key, nil
// }
// func serializeBLHeights(bitLength uint8, blkHeight uint64) []byte {
// buf := make([]byte, blHeightLen)
// buf[0] = bitLength
// binary.LittleEndian.PutUint64(buf[1:blHeightLen], blkHeight)
// return buf
// }
// func deserializeBLHeights(buf []byte) []*database.BLHeight {
// count := len(buf) / blHeightLen
// blhs := make([]*database.BLHeight, count)
// for i := 0; i < count; i++ {
// blhs[i] = &database.BLHeight{
// BitLength: int(buf[i*blHeightLen]),
// BlkHeight: binary.LittleEndian.Uint64(buf[i*blHeightLen+1 : (i+1)*blHeightLen]),
// }
// }
// return blhs
// }
// // func (db *ChainDb) insertPubkblToBatch(batch storage.Batch, publicKey interfaces.PublicKey, bitLengthOrK int, blkHeight uint64) error {
// func (db *ChainDb) insertPubkblToBatch(batch storage.Batch, block *wire.MsgBlock) error {
// // db key
// key, err := makePubkblKey(block.Header.PublicKey())
// if err != nil {
// return err
// }
// // bit_length
// bl := block.Header.Proof.BitLength()
// // height
// blkHeight := block.Header.Height
// buf := serializeBLHeights(uint8(bl), blkHeight)
// v, err := db.stor.Get(key)
// if err != nil {
// if err == storage.ErrNotFound {
// return batch.Put(key, buf)
// }
// return err
// }
// blhs := deserializeBLHeights(v)
// lastBl := blhs[len(blhs)-1].BitLength
// if bl < lastBl {
// return errors.New(fmt.Sprintf("insertPubkblToBatch: unexpected bl %d, last %d, height %d",
// bl, lastBl, blkHeight))
// }
// if bl > lastBl {
// v = append(v, buf...)
// return batch.Put(key, v)
// }
// return nil
// }
// func (db *ChainDb) removePubkblWithCheck(batch storage.Batch, block *wire.MsgBlock) error {
// key, err := makePubkblKey(block.Header.PublicKey())
// if err != nil {
// return err
// }
// buf, err := db.stor.Get(key)
// if err != nil {
// return err
// }
// l := len(buf)
// bl := buf[l-blHeightLen]
// h := binary.LittleEndian.Uint64(buf[l-blHeightLen+1:])
// if int(bl) == block.Header.Proof.BitLength() && h == block.Header.Height {
// if l == blHeightLen {
// return batch.Delete(key)
// }
// v := buf[:l-blHeightLen]
// return batch.Put(key, v)
// }
// return nil
// }
// func (db *ChainDb) GetPubkeyBlRecord(publicKey interfaces.PublicKey) ([]*database.BLHeight, error) {
// key, err := makePubkblKey(publicKey)
// if err != nil {
// return nil, err
// }
// v, err := db.stor.Get(key)
// if err != nil {
// if err == storage.ErrNotFound {
// empty := make([]*database.BLHeight, 0)
// return empty, nil
// }
// return nil, err
// }
// blh := deserializeBLHeights(v)
// return blh, nil
// }
//
// func (db *ChainDb) insertPubkbl(publicKey *pocec.PublicKey, bitLength int, blkHeight uint64) error {
// key := makePubkblKey(publicKey)
// buf := serializeBLHeights(uint8(bitLength), blkHeight)
// v, err := db.stor.Get(key)
// if err != nil {
// if err == storage.ErrNotFound {
// return db.stor.Put(key, buf)
// }
// return err
// }
// blhs := deserializeBLHeights(v)
// lastBl := blhs[len(blhs)-1].BitLength
// if bitLength < lastBl {
// return errors.New(fmt.Sprintf("insertPubkbl: unexpected bl %d, last %d, height %d",
// bitLength, lastBl, blkHeight))
// }
// if bitLength > lastBl {
// v = append(v, buf...)
// return db.stor.Put(key, v)
// }
// return nil
// }
// func (db *ChainDb) fetchPubkblIndexProgress() (uint64, error) {
// buf, err := db.stor.Get(uppkblKey)
// if err != nil {
// if err == storage.ErrNotFound {
// return 0, nil
// }
// return 0, err
// }
// return binary.LittleEndian.Uint64(buf), nil
// }
// func (db *ChainDb) updatePubkblIndexProgress(height uint64) error {
// value := make([]byte, 8)
// binary.LittleEndian.PutUint64(value, height)
// return db.stor.Put(uppkblKey, value)
// }
// func (db *ChainDb) deletePubkblIndexProgress() error {
// return db.stor.Delete(uppkblKey)
// }
// func (db *ChainDb) clearPubkbl() error {
// iter := db.stor.NewIterator(storage.BytesPrefix([]byte(pubkblKeyPrefix)))
// defer iter.Release()
// for iter.Next() {
// err := db.stor.Delete(iter.Key())
// if err != nil {
// return err
// }
// }
// return iter.Error()
// }