Skip to content

Commit 2a384b8

Browse files
committed
Add BlockIndexes for building indexes of blocks
1 parent 522e52d commit 2a384b8

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

lib/block/block.go

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package block
22

33
import (
44
"encoding/json"
5-
"fmt"
65

76
"github.com/btcsuite/btcutil/base58"
87

@@ -61,30 +60,16 @@ func getTransactionRoot(txs []string) string {
6160
return common.MustMakeObjectHashString(txs) // TODO make root
6261
}
6362

64-
func getBlockKey(hash string) string {
65-
return fmt.Sprintf("%s%s", common.BlockPrefixHash, hash)
66-
}
67-
68-
func getBlockKeyPrefixHeight(height uint64) string {
69-
return fmt.Sprintf("%s%020d", common.BlockPrefixHeight, height)
70-
}
71-
7263
//GetBlockKeyPrefixHeight returns index key by height. It is used to make cursor as height.
7364
func GetBlockKeyPrefixHeight(height uint64) string {
74-
return getBlockKeyPrefixHeight(height)
75-
}
76-
77-
func (b Block) NewBlockKeyConfirmed() string {
78-
return fmt.Sprintf(
79-
"%s%s-%s%s",
80-
common.BlockPrefixConfirmed, b.ProposedTime,
81-
common.EncodeUint64ToString(b.Height),
82-
common.GetUniqueIDFromUUID(),
83-
)
65+
i := &BlockIndexes{}
66+
return i.KeyHeight(height)
8467
}
8568

8669
func (b *Block) Save(st *storage.LevelDBBackend) (err error) {
87-
key := getBlockKey(b.Hash)
70+
indexes := &BlockIndexes{}
71+
72+
key := indexes.KeyBlock(b.Hash)
8873
if b.Confirmed == "" {
8974
b.Confirmed = common.NowISO8601()
9075
}
@@ -101,10 +86,10 @@ func (b *Block) Save(st *storage.LevelDBBackend) (err error) {
10186
return
10287
}
10388

104-
if err = st.New(b.NewBlockKeyConfirmed(), b.Hash); err != nil {
89+
if err = st.New(indexes.KeyConfirmed(b), b.Hash); err != nil {
10590
return
10691
}
107-
if err = st.New(getBlockKeyPrefixHeight(b.Height), b.Hash); err != nil {
92+
if err = st.New(indexes.KeyHeight(b.Height), b.Hash); err != nil {
10893
return
10994
}
11095

@@ -125,22 +110,26 @@ func (b Block) NextBlock(st *storage.LevelDBBackend) (Block, error) {
125110
}
126111

127112
func GetBlock(st *storage.LevelDBBackend, hash string) (bt Block, err error) {
128-
err = st.Get(getBlockKey(hash), &bt)
113+
i := &BlockIndexes{}
114+
err = st.Get(i.KeyBlock(hash), &bt)
129115
return
130116
}
131117

132118
func GetBlockHeader(st *storage.LevelDBBackend, hash string) (bt Header, err error) {
133-
err = st.Get(getBlockKey(hash), &bt)
119+
i := &BlockIndexes{}
120+
err = st.Get(i.KeyBlock(hash), &bt)
134121
return
135122
}
136123

137124
func ExistsBlock(st *storage.LevelDBBackend, hash string) (exists bool, err error) {
138-
exists, err = st.Has(getBlockKey(hash))
125+
i := &BlockIndexes{}
126+
exists, err = st.Has(i.KeyBlock(hash))
139127
return
140128
}
141129

142130
func ExistsBlockByHeight(st *storage.LevelDBBackend, height uint64) (exists bool, err error) {
143-
exists, err = st.Has(getBlockKeyPrefixHeight(height))
131+
i := &BlockIndexes{}
132+
exists, err = st.Has(i.KeyHeight(height))
144133
return
145134
}
146135

@@ -222,7 +211,8 @@ func GetBlockHeadersByConfirmed(st *storage.LevelDBBackend, options storage.List
222211

223212
func GetBlockByHeight(st *storage.LevelDBBackend, height uint64) (bt Block, err error) {
224213
var hash string
225-
if err = st.Get(getBlockKeyPrefixHeight(height), &hash); err != nil {
214+
i := &BlockIndexes{}
215+
if err = st.Get(i.KeyHeight(height), &hash); err != nil {
226216
return
227217
}
228218

@@ -231,7 +221,8 @@ func GetBlockByHeight(st *storage.LevelDBBackend, height uint64) (bt Block, err
231221

232222
func GetBlockHeaderByHeight(st *storage.LevelDBBackend, height uint64) (bt Header, err error) {
233223
var hash string
234-
if err = st.Get(getBlockKeyPrefixHeight(height), &hash); err != nil {
224+
i := &BlockIndexes{}
225+
if err = st.Get(i.KeyHeight(height), &hash); err != nil {
235226
return
236227
}
237228

@@ -252,7 +243,8 @@ func GetLatestBlock(st *storage.LevelDBBackend) Block {
252243
}
253244

254245
func WalkBlocks(st *storage.LevelDBBackend, option *storage.WalkOption, walkFunc func(*Block, []byte) (bool, error)) error {
255-
err := st.Walk(common.BlockPrefixHeight, option, func(key, value []byte) (bool, error) {
246+
i := &BlockIndexes{}
247+
err := st.Walk(i.PrefixHeight(), option, func(key, value []byte) (bool, error) {
256248
var hash string
257249
if err := json.Unmarshal(value, &hash); err != nil {
258250
return false, err

lib/block/block_indexes.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package block
2+
3+
import (
4+
"fmt"
5+
6+
"boscoin.io/sebak/lib/common"
7+
"boscoin.io/sebak/lib/storage"
8+
)
9+
10+
type BlockIndexes struct {
11+
}
12+
13+
func (BlockIndexes) KeyHeight(height uint64) string {
14+
idx := storage.NewIndex()
15+
idx.WritePrefix(common.BlockPrefixHeight, common.EncodeUint64ToString(height))
16+
return idx.String()
17+
}
18+
19+
func (BlockIndexes) KeyBlock(hash string) string {
20+
idx := storage.NewIndex()
21+
idx.WritePrefix(common.BlockPrefixHash, hash)
22+
return idx.String()
23+
}
24+
25+
func (BlockIndexes) KeyConfirmed(b *Block) string {
26+
idx := storage.NewIndex()
27+
idx.WritePrefix(common.BlockPrefixConfirmed, b.ProposedTime)
28+
idx.WritePrefix(common.EncodeUint64ToString(b.Height))
29+
idx.WriteOrder(common.GetUniqueIDFromUUID())
30+
return idx.String()
31+
}
32+
33+
func (BlockIndexes) PrefixHeight() string {
34+
return fmt.Sprintf("%s", common.BlockPrefixHeight)
35+
}
36+
37+
func (BlockIndexes) PrefixHash() string {
38+
return fmt.Sprintf("%s", common.BlockPrefixHash)
39+
}

lib/node/runner/api_block.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func (nh NetworkHandlerNode) GetBlocksHandler(w http.ResponseWriter, r *http.Req
4141
http.Error(w, err.Error(), http.StatusInternalServerError)
4242
return
4343
}
44-
options.SetCursor([]byte(cursorBlock.NewBlockKeyConfirmed()))
44+
indexes := &block.BlockIndexes{}
45+
options.SetCursor([]byte(indexes.KeyConfirmed(&cursorBlock)))
4546
}
4647

4748
var bs []*block.Block

0 commit comments

Comments
 (0)