2525 ErrBlockNotIndexed = errors .New ("indexdb: block not indexed" )
2626)
2727
28+ var (
29+ accountPrefix = []byte ("a" )
30+ blockPrefix = []byte ("b" )
31+ hash2HeightPrefix = []byte ("c" )
32+ height2HashPrefix = []byte ("d" )
33+ isProxyPrefix = []byte ("p" )
34+ )
35+
2836// NOTE(tav): We store the blockchain data within Badger using the following
2937// key/value structure:
3038//
@@ -80,10 +88,9 @@ func (s *Store) Accounts() (map[[8]byte]bool, error) {
8088 err := s .db .View (func (txn * badger.Txn ) error {
8189 it := txn .NewIterator (badger.IteratorOptions {})
8290 defer it .Close ()
83- prefix := []byte ("a" )
84- it .Seek (prefix )
91+ it .Seek (accountPrefix )
8592 for {
86- if ! it .ValidForPrefix (prefix ) {
93+ if ! it .ValidForPrefix (accountPrefix ) {
8794 break
8895 }
8996 key := it .Item ().Key ()
@@ -94,10 +101,9 @@ func (s *Store) Accounts() (map[[8]byte]bool, error) {
94101 }
95102 it = txn .NewIterator (badger.IteratorOptions {})
96103 defer it .Close ()
97- prefix = []byte ("p" )
98- it .Seek (prefix )
104+ it .Seek (isProxyPrefix )
99105 for {
100- if ! it .ValidForPrefix (prefix ) {
106+ if ! it .ValidForPrefix (isProxyPrefix ) {
101107 break
102108 }
103109 key := it .Item ().Key ()
@@ -127,10 +133,9 @@ func (s *Store) AccountsInfo() (map[string]*AccountInfo, error) {
127133 err := s .db .View (func (txn * badger.Txn ) error {
128134 it := txn .NewIterator (badger.IteratorOptions {})
129135 defer it .Close ()
130- prefix := []byte ("a" )
131- it .Seek (prefix )
136+ it .Seek (accountPrefix )
132137 for {
133- if ! it .ValidForPrefix (prefix ) {
138+ if ! it .ValidForPrefix (accountPrefix ) {
134139 break
135140 }
136141 item := it .Item ()
@@ -163,10 +168,9 @@ func (s *Store) AccountsInfo() (map[string]*AccountInfo, error) {
163168 err = s .db .View (func (txn * badger.Txn ) error {
164169 it := txn .NewIterator (badger.IteratorOptions {})
165170 defer it .Close ()
166- prefix := []byte ("p" )
167- it .Seek (prefix )
171+ it .Seek (isProxyPrefix )
168172 for {
169- if ! it .ValidForPrefix (prefix ) {
173+ if ! it .ValidForPrefix (isProxyPrefix ) {
170174 break
171175 }
172176 key := it .Item ().Key ()
@@ -308,7 +312,7 @@ func (s *Store) Genesis() *model.BlockMeta {
308312// given height.
309313func (s * Store ) HasBalance (acct []byte , height uint64 ) (bool , error ) {
310314 key := make ([]byte , 1 + 8 + 8 )
311- key [0 ] = 'a'
315+ key [0 ] = 'a' // accountPrefix
312316 copy (key [1 :9 ], acct )
313317 binary .BigEndian .PutUint64 (key [9 :], height )
314318 ok := false
@@ -337,7 +341,7 @@ func (s *Store) HashForHeight(height uint64) ([]byte, error) {
337341 var hash []byte
338342 heightEnc := make ([]byte , 8 )
339343 binary .BigEndian .PutUint64 (heightEnc , height )
340- key := append ([] byte ( "d" ) , heightEnc ... )
344+ key := append (height2HashPrefix , heightEnc ... )
341345 err := s .db .View (func (txn * badger.Txn ) error {
342346 item , err := txn .Get (key )
343347 if err != nil {
@@ -363,7 +367,7 @@ func (s *Store) HashForHeight(height uint64) ([]byte, error) {
363367// HeightForHash returns the block height for the given hash.
364368func (s * Store ) HeightForHash (hash []byte ) (uint64 , error ) {
365369 height := uint64 (0 )
366- key := append ([] byte ( "c" ) , hash ... )
370+ key := append (hash2HeightPrefix , hash ... )
367371 err := s .db .View (func (txn * badger.Txn ) error {
368372 item , err := txn .Get (key )
369373 if err != nil {
@@ -423,7 +427,7 @@ func (s *Store) Index(ctx context.Context, height uint64, hash []byte, block *mo
423427 }
424428 if len (op .ProxyPublicKey ) > 0 {
425429 key := make ([]byte , 17 )
426- key [0 ] = 'p'
430+ key [0 ] = 'p' // isProxyPrefix
427431 copy (key [1 :], op .Account )
428432 binary .BigEndian .PutUint64 (key [9 :], height )
429433 proxyAccts = append (proxyAccts , key )
@@ -440,7 +444,7 @@ func (s *Store) Index(ctx context.Context, height uint64, hash []byte, block *mo
440444 updates := make ([]accountUpdate , len (accts ))
441445 for acct , diff := range accts {
442446 key := make ([]byte , 1 + 8 + 8 )
443- key [0 ] = 'a'
447+ key [0 ] = 'a' // accountPrefix
444448 copy (key [1 :], acct )
445449 copy (key [9 :], hval )
446450 updates [i ] = accountUpdate {
@@ -449,13 +453,13 @@ func (s *Store) Index(ctx context.Context, height uint64, hash []byte, block *mo
449453 }
450454 i ++
451455 }
452- blockKey := append ([] byte ( "b" ) , hval ... )
456+ blockKey := append (blockPrefix , hval ... )
453457 blockValue , err := proto .Marshal (block )
454458 if err != nil {
455459 log .Fatalf ("Failed to encode model.IndexedBlock: %s" , err )
456460 }
457- hash2heightKey := append ([] byte ( "c" ) , hash ... )
458- height2hashKey := append ([] byte ( "d" ) , hval ... )
461+ hash2heightKey := append (hash2HeightPrefix , hash ... )
462+ height2hashKey := append (height2HashPrefix , hval ... )
459463 latest = & model.BlockMeta {
460464 Hash : hash ,
461465 Height : height ,
@@ -576,10 +580,9 @@ func (s *Store) PurgeProxyAccounts() {
576580 err := s .db .View (func (txn * badger.Txn ) error {
577581 it := txn .NewIterator (badger.IteratorOptions {})
578582 defer it .Close ()
579- prefix := []byte ("p" )
580- it .Seek (prefix )
583+ it .Seek (isProxyPrefix )
581584 for {
582- if ! it .ValidForPrefix (prefix ) {
585+ if ! it .ValidForPrefix (isProxyPrefix ) {
583586 break
584587 }
585588 key := it .Item ().KeyCopy (nil )
@@ -595,7 +598,7 @@ func (s *Store) PurgeProxyAccounts() {
595598 err = s .db .View (func (txn * badger.Txn ) error {
596599 it := txn .NewIterator (badger.IteratorOptions {})
597600 defer it .Close ()
598- prefix := [] byte ( "a" )
601+ prefix := accountPrefix
599602 it .Seek (prefix )
600603 for {
601604 if ! it .ValidForPrefix (prefix ) {
@@ -640,7 +643,7 @@ func (s *Store) ResetTo(base uint64) error {
640643 delKeys := [][]byte {}
641644 err := s .db .View (func (txn * badger.Txn ) error {
642645 it := txn .NewIterator (badger.IteratorOptions {})
643- prefix := [] byte ( "a" )
646+ prefix := accountPrefix
644647 it .Seek (prefix )
645648 for {
646649 if ! it .ValidForPrefix (prefix ) {
@@ -662,7 +665,7 @@ func (s *Store) ResetTo(base uint64) error {
662665 }
663666 err = s .db .View (func (txn * badger.Txn ) error {
664667 it := txn .NewIterator (badger.IteratorOptions {})
665- prefix := [] byte ( "p" )
668+ prefix := isProxyPrefix
666669 it .Seek (prefix )
667670 for {
668671 if ! it .ValidForPrefix (prefix ) {
@@ -685,7 +688,7 @@ func (s *Store) ResetTo(base uint64) error {
685688 last := uint64 (0 )
686689 err = s .db .View (func (txn * badger.Txn ) error {
687690 it := txn .NewIterator (badger.IteratorOptions {})
688- prefix := [] byte ( "d" )
691+ prefix := height2HashPrefix
689692 it .Seek (prefix )
690693 for {
691694 if ! it .ValidForPrefix (prefix ) {
@@ -695,21 +698,21 @@ func (s *Store) ResetTo(base uint64) error {
695698 key := item .Key ()
696699 height := binary .BigEndian .Uint64 (key [1 :])
697700 if height > base {
698- key = item .KeyCopy (nil )
699- delKeys = append (delKeys , key )
700- key = make ([]byte , 9 )
701- key [0 ] = 'b'
702- binary .BigEndian .PutUint64 (key [1 :], height )
703- delKeys = append (delKeys , key )
701+ height2HashKey : = item .KeyCopy (nil )
702+ delKeys = append (delKeys , height2HashKey )
703+ blockKey : = make ([]byte , 9 )
704+ blockKey [0 ] = 'b' // blockPrefix
705+ binary .BigEndian .PutUint64 (blockKey [1 :], height )
706+ delKeys = append (delKeys , blockKey )
704707 hash , err := item .ValueCopy (nil )
705708 if err != nil {
706709 it .Close ()
707710 return err
708711 }
709- key = make ([]byte , len (hash )+ 1 )
710- key [0 ] = 'c'
711- copy (key [1 :], hash )
712- delKeys = append (delKeys , key )
712+ hash2HeightKey : = make ([]byte , len (hash )+ 1 )
713+ hash2HeightKey [0 ] = 'c' // hash2HeightPrefix
714+ copy (hash2HeightKey [1 :], hash )
715+ delKeys = append (delKeys , hash2HeightKey )
713716 } else {
714717 last = height
715718 }
@@ -775,15 +778,15 @@ func (s *Store) SetGenesis(val *model.BlockMeta) error {
775778 }
776779 hval := make ([]byte , 8 )
777780 binary .BigEndian .PutUint64 (hval , val .Height )
778- blockKey := append ([] byte ( "b" ) , hval ... )
781+ blockKey := append (blockPrefix , hval ... )
779782 blockValue , err := proto .Marshal (& model.IndexedBlock {
780783 Timestamp : val .Timestamp ,
781784 })
782785 if err != nil {
783786 log .Fatalf ("Failed to encode model.IndexedBlock: %s" , err )
784787 }
785- hash2heightKey := append ([] byte ( "c" ) , val .Hash ... )
786- height2hashKey := append ([] byte ( "d" ) , hval ... )
788+ hash2heightKey := append (hash2HeightPrefix , val .Hash ... )
789+ height2hashKey := append (height2HashPrefix , hval ... )
787790 err = s .db .Update (func (txn * badger.Txn ) error {
788791 if err := txn .Set ([]byte ("genesis" ), genesis ); err != nil {
789792 return err
0 commit comments