gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/explorer/persist.go (about) 1 package explorer 2 3 import ( 4 "os" 5 //"fmt" 6 "path/filepath" 7 8 "gitlab.com/SiaPrime/SiaPrime/encoding" 9 "gitlab.com/SiaPrime/SiaPrime/modules" 10 "gitlab.com/SiaPrime/SiaPrime/persist" 11 "gitlab.com/SiaPrime/SiaPrime/types" 12 13 bolt "github.com/coreos/bbolt" 14 ) 15 16 var explorerMetadata = persist.Metadata{ 17 Header: "Sia Explorer", 18 Version: "0.5.2", 19 } 20 21 // initPersist initializes the persistent structures of the explorer module. 22 func (e *Explorer) initPersist() error { 23 // Make the persist directory 24 err := os.MkdirAll(e.persistDir, 0700) 25 if err != nil { 26 return err 27 } 28 29 // Open the database 30 db, err := persist.OpenDatabase(explorerMetadata, filepath.Join(e.persistDir, "explorer.db")) 31 if err != nil { 32 return err 33 } 34 e.db = db 35 36 // Initialize the database 37 err = e.db.Update(func(tx *bolt.Tx) error { 38 buckets := [][]byte{ 39 bucketBlockFacts, 40 bucketBlockIDs, 41 bucketBlocksDifficulty, 42 bucketBlockTargets, 43 bucketFileContractHistories, 44 bucketFileContractIDs, 45 bucketInternal, 46 bucketSiacoinOutputIDs, 47 bucketSiacoinOutputs, 48 bucketSiafundOutputIDs, 49 bucketSiafundOutputs, 50 bucketTransactionIDs, 51 bucketUnlockHashes, 52 } 53 for _, b := range buckets { 54 _, err := tx.CreateBucketIfNotExists(b) 55 if err != nil { 56 return err 57 } 58 } 59 60 // set default values for the bucketInternal 61 internalDefaults := []struct { 62 key, val []byte 63 }{ 64 {internalBlockHeight, encoding.Marshal(types.BlockHeight(0))}, 65 {internalRecentChange, encoding.Marshal(modules.ConsensusChangeID{})}, 66 } 67 b := tx.Bucket(bucketInternal) 68 for _, d := range internalDefaults { 69 if b.Get(d.key) != nil { 70 continue 71 } 72 err := b.Put(d.key, d.val) 73 if err != nil { 74 return err 75 } 76 } 77 78 //err = b.Put(internalBlockHeight, encoding.Marshal(types.BlockHeight(98112))) 79 //if err != nil { 80 // fmt.Printf("internal err %v", err) 81 // return err 82 //} 83 84 //err = b.Put(internalRecentChange, encoding.Marshal(modules.ConsensusChangeID{})) 85 //if err != nil { 86 // return err 87 //} 88 89 return nil 90 }) 91 if err != nil { 92 return err 93 } 94 95 return nil 96 }