@@ -2,7 +2,6 @@ package block
22
33import (
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.
7364func 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
8669func (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
127112func 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
132118func 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
137124func 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
142130func 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
223212func 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
232222func 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
254245func 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
0 commit comments