@@ -25,12 +25,12 @@ var (
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" )
28+ const (
29+ accountPrefix byte = 'a'
30+ blockPrefix byte = 'b'
31+ hash2HeightPrefix byte = 'c'
32+ height2HashPrefix byte = 'd'
33+ isProxyPrefix byte = 'p'
3434)
3535
3636// NOTE(tav): We store the blockchain data within Badger using the following
@@ -88,9 +88,10 @@ func (s *Store) Accounts() (map[[8]byte]bool, error) {
8888 err := s .db .View (func (txn * badger.Txn ) error {
8989 it := txn .NewIterator (badger.IteratorOptions {})
9090 defer it .Close ()
91- it .Seek (accountPrefix )
91+ prefix := []byte {accountPrefix }
92+ it .Seek (prefix )
9293 for {
93- if ! it .ValidForPrefix (accountPrefix ) {
94+ if ! it .ValidForPrefix (prefix ) {
9495 break
9596 }
9697 key := it .Item ().Key ()
@@ -101,9 +102,10 @@ func (s *Store) Accounts() (map[[8]byte]bool, error) {
101102 }
102103 it = txn .NewIterator (badger.IteratorOptions {})
103104 defer it .Close ()
104- it .Seek (isProxyPrefix )
105+ prefix = []byte {isProxyPrefix }
106+ it .Seek (prefix )
105107 for {
106- if ! it .ValidForPrefix (isProxyPrefix ) {
108+ if ! it .ValidForPrefix (prefix ) {
107109 break
108110 }
109111 key := it .Item ().Key ()
@@ -133,9 +135,10 @@ func (s *Store) AccountsInfo() (map[string]*AccountInfo, error) {
133135 err := s .db .View (func (txn * badger.Txn ) error {
134136 it := txn .NewIterator (badger.IteratorOptions {})
135137 defer it .Close ()
136- it .Seek (accountPrefix )
138+ prefix := []byte {accountPrefix }
139+ it .Seek (prefix )
137140 for {
138- if ! it .ValidForPrefix (accountPrefix ) {
141+ if ! it .ValidForPrefix (prefix ) {
139142 break
140143 }
141144 item := it .Item ()
@@ -168,9 +171,10 @@ func (s *Store) AccountsInfo() (map[string]*AccountInfo, error) {
168171 err = s .db .View (func (txn * badger.Txn ) error {
169172 it := txn .NewIterator (badger.IteratorOptions {})
170173 defer it .Close ()
171- it .Seek (isProxyPrefix )
174+ prefix := []byte {isProxyPrefix }
175+ it .Seek (prefix )
172176 for {
173- if ! it .ValidForPrefix (isProxyPrefix ) {
177+ if ! it .ValidForPrefix (prefix ) {
174178 break
175179 }
176180 key := it .Item ().Key ()
@@ -312,7 +316,7 @@ func (s *Store) Genesis() *model.BlockMeta {
312316// given height.
313317func (s * Store ) HasBalance (acct []byte , height uint64 ) (bool , error ) {
314318 key := make ([]byte , 1 + 8 + 8 )
315- key [0 ] = 'a' // accountPrefix
319+ key [0 ] = accountPrefix
316320 copy (key [1 :9 ], acct )
317321 binary .BigEndian .PutUint64 (key [9 :], height )
318322 ok := false
@@ -341,7 +345,7 @@ func (s *Store) HashForHeight(height uint64) ([]byte, error) {
341345 var hash []byte
342346 heightEnc := make ([]byte , 8 )
343347 binary .BigEndian .PutUint64 (heightEnc , height )
344- key := append (height2HashPrefix , heightEnc ... )
348+ key := append ([] byte { height2HashPrefix } , heightEnc ... )
345349 err := s .db .View (func (txn * badger.Txn ) error {
346350 item , err := txn .Get (key )
347351 if err != nil {
@@ -367,7 +371,7 @@ func (s *Store) HashForHeight(height uint64) ([]byte, error) {
367371// HeightForHash returns the block height for the given hash.
368372func (s * Store ) HeightForHash (hash []byte ) (uint64 , error ) {
369373 height := uint64 (0 )
370- key := append (hash2HeightPrefix , hash ... )
374+ key := append ([] byte { hash2HeightPrefix } , hash ... )
371375 err := s .db .View (func (txn * badger.Txn ) error {
372376 item , err := txn .Get (key )
373377 if err != nil {
@@ -427,7 +431,7 @@ func (s *Store) Index(ctx context.Context, height uint64, hash []byte, block *mo
427431 }
428432 if len (op .ProxyPublicKey ) > 0 {
429433 key := make ([]byte , 17 )
430- key [0 ] = 'p' // isProxyPrefix
434+ key [0 ] = isProxyPrefix
431435 copy (key [1 :], op .Account )
432436 binary .BigEndian .PutUint64 (key [9 :], height )
433437 proxyAccts = append (proxyAccts , key )
@@ -444,7 +448,7 @@ func (s *Store) Index(ctx context.Context, height uint64, hash []byte, block *mo
444448 updates := make ([]accountUpdate , len (accts ))
445449 for acct , diff := range accts {
446450 key := make ([]byte , 1 + 8 + 8 )
447- key [0 ] = 'a' // accountPrefix
451+ key [0 ] = accountPrefix
448452 copy (key [1 :], acct )
449453 copy (key [9 :], hval )
450454 updates [i ] = accountUpdate {
@@ -453,13 +457,13 @@ func (s *Store) Index(ctx context.Context, height uint64, hash []byte, block *mo
453457 }
454458 i ++
455459 }
456- blockKey := append (blockPrefix , hval ... )
460+ blockKey := append ([] byte { blockPrefix } , hval ... )
457461 blockValue , err := proto .Marshal (block )
458462 if err != nil {
459463 log .Fatalf ("Failed to encode model.IndexedBlock: %s" , err )
460464 }
461- hash2heightKey := append (hash2HeightPrefix , hash ... )
462- height2hashKey := append (height2HashPrefix , hval ... )
465+ hash2heightKey := append ([] byte { hash2HeightPrefix } , hash ... )
466+ height2hashKey := append ([] byte { height2HashPrefix } , hval ... )
463467 latest = & model.BlockMeta {
464468 Hash : hash ,
465469 Height : height ,
@@ -580,9 +584,10 @@ func (s *Store) PurgeProxyAccounts() {
580584 err := s .db .View (func (txn * badger.Txn ) error {
581585 it := txn .NewIterator (badger.IteratorOptions {})
582586 defer it .Close ()
583- it .Seek (isProxyPrefix )
587+ prefix := []byte {isProxyPrefix }
588+ it .Seek (prefix )
584589 for {
585- if ! it .ValidForPrefix (isProxyPrefix ) {
590+ if ! it .ValidForPrefix (prefix ) {
586591 break
587592 }
588593 key := it .Item ().KeyCopy (nil )
@@ -598,7 +603,7 @@ func (s *Store) PurgeProxyAccounts() {
598603 err = s .db .View (func (txn * badger.Txn ) error {
599604 it := txn .NewIterator (badger.IteratorOptions {})
600605 defer it .Close ()
601- prefix := accountPrefix
606+ prefix := [] byte { accountPrefix }
602607 it .Seek (prefix )
603608 for {
604609 if ! it .ValidForPrefix (prefix ) {
@@ -643,7 +648,7 @@ func (s *Store) ResetTo(base uint64) error {
643648 delKeys := [][]byte {}
644649 err := s .db .View (func (txn * badger.Txn ) error {
645650 it := txn .NewIterator (badger.IteratorOptions {})
646- prefix := accountPrefix
651+ prefix := [] byte { accountPrefix }
647652 it .Seek (prefix )
648653 for {
649654 if ! it .ValidForPrefix (prefix ) {
@@ -665,7 +670,7 @@ func (s *Store) ResetTo(base uint64) error {
665670 }
666671 err = s .db .View (func (txn * badger.Txn ) error {
667672 it := txn .NewIterator (badger.IteratorOptions {})
668- prefix := isProxyPrefix
673+ prefix := [] byte { isProxyPrefix }
669674 it .Seek (prefix )
670675 for {
671676 if ! it .ValidForPrefix (prefix ) {
@@ -688,7 +693,7 @@ func (s *Store) ResetTo(base uint64) error {
688693 last := uint64 (0 )
689694 err = s .db .View (func (txn * badger.Txn ) error {
690695 it := txn .NewIterator (badger.IteratorOptions {})
691- prefix := height2HashPrefix
696+ prefix := [] byte { height2HashPrefix }
692697 it .Seek (prefix )
693698 for {
694699 if ! it .ValidForPrefix (prefix ) {
@@ -701,7 +706,7 @@ func (s *Store) ResetTo(base uint64) error {
701706 height2HashKey := item .KeyCopy (nil )
702707 delKeys = append (delKeys , height2HashKey )
703708 blockKey := make ([]byte , 9 )
704- blockKey [0 ] = 'b' // blockPrefix
709+ blockKey [0 ] = blockPrefix
705710 binary .BigEndian .PutUint64 (blockKey [1 :], height )
706711 delKeys = append (delKeys , blockKey )
707712 hash , err := item .ValueCopy (nil )
@@ -710,7 +715,7 @@ func (s *Store) ResetTo(base uint64) error {
710715 return err
711716 }
712717 hash2HeightKey := make ([]byte , len (hash )+ 1 )
713- hash2HeightKey [0 ] = 'c' // hash2HeightPrefix
718+ hash2HeightKey [0 ] = hash2HeightPrefix
714719 copy (hash2HeightKey [1 :], hash )
715720 delKeys = append (delKeys , hash2HeightKey )
716721 } else {
@@ -778,15 +783,15 @@ func (s *Store) SetGenesis(val *model.BlockMeta) error {
778783 }
779784 hval := make ([]byte , 8 )
780785 binary .BigEndian .PutUint64 (hval , val .Height )
781- blockKey := append (blockPrefix , hval ... )
786+ blockKey := append ([] byte { blockPrefix } , hval ... )
782787 blockValue , err := proto .Marshal (& model.IndexedBlock {
783788 Timestamp : val .Timestamp ,
784789 })
785790 if err != nil {
786791 log .Fatalf ("Failed to encode model.IndexedBlock: %s" , err )
787792 }
788- hash2heightKey := append (hash2HeightPrefix , val .Hash ... )
789- height2hashKey := append (height2HashPrefix , hval ... )
793+ hash2heightKey := append ([] byte { hash2HeightPrefix } , val .Hash ... )
794+ height2hashKey := append ([] byte { height2HashPrefix } , hval ... )
790795 err = s .db .Update (func (txn * badger.Txn ) error {
791796 if err := txn .Set ([]byte ("genesis" ), genesis ); err != nil {
792797 return err
@@ -815,7 +820,7 @@ func (s *Store) SetGenesis(val *model.BlockMeta) error {
815820func (s * Store ) balanceByHeight (acct []byte , height uint64 ) (uint64 , error ) {
816821 balance := uint64 (0 )
817822 key := make ([]byte , 1 + 8 + 8 )
818- key [0 ] = 'a'
823+ key [0 ] = accountPrefix
819824 copy (key [1 :9 ], acct )
820825 binary .BigEndian .PutUint64 (key [9 :], height )
821826 err := s .db .View (func (txn * badger.Txn ) error {
@@ -844,7 +849,7 @@ func (s *Store) balanceByHeight(acct []byte, height uint64) (uint64, error) {
844849func (s * Store ) blockByHeight (height uint64 ) (* model.IndexedBlock , error ) {
845850 block := & model.IndexedBlock {}
846851 key := make ([]byte , 9 )
847- key [0 ] = 'b'
852+ key [0 ] = blockPrefix
848853 binary .BigEndian .PutUint64 (key [1 :], height )
849854 err := s .db .View (func (txn * badger.Txn ) error {
850855 item , err := txn .Get (key )
0 commit comments