steem-block-db/main.go

142 lines
2.9 KiB
Go

package main
//import "github.com/spf13/viper"
//import "encoding/json"
//import "fmt"
import "sync"
import "time"
import log "github.com/sirupsen/logrus"
const steemAPIURL = "https://api.steemit.com"
func main() {
processinit()
app := NewApp(&appconfig{})
app.main()
}
func processinit() {
//FIXME(sneak) use viper to set loglevel
log.SetLevel(log.DebugLevel)
}
type App struct {
datastore *SteemDataStore
api *SteemAPI
currentNetworkBlockHeight int
currentLocalBlockHeight int
currentFetchingThreads int
desiredFetchingThreads int
lock *sync.Mutex
}
type appconfig map[string]string
func NewApp(config *appconfig) *App {
self := new(App)
self.init(config)
return self
}
func (self *App) init(config *appconfig) {
self.api = NewSteemAPI(steemAPIURL)
self.datastore = NewSteemDataStore("./d")
self.desiredFetchingThreads = 10
self.currentFetchingThreads = 0
self.lock = &sync.Mutex{}
}
func (self *App) updateCurrentBlockHeight() {
h := self.fetchCurrentBlockHeight()
if h > self.currentNetworkBlockHeight {
self.lock.Lock()
defer self.lock.Unlock()
self.currentNetworkBlockHeight = h
log.Infof("current block height is now %d", self.currentNetworkBlockHeight)
}
}
func (self *App) main() {
log.Infof("steem block data fetcher starting up...")
self.mainloop()
}
func (self *App) numFetchers() int {
self.lock.Lock()
defer self.lock.Unlock()
return self.currentFetchingThreads
}
func (self *App) incrFetchers() {
self.lock.Lock()
defer self.lock.Unlock()
self.currentFetchingThreads += 1
}
func (self *App) decrFetchers() {
self.lock.Lock()
defer self.lock.Unlock()
self.currentFetchingThreads -= 1
}
func (self *App) spawnNewFetcher(blockNum int) {
self.incrFetchers()
}
func (self *App) mainloop() {
for {
self.updateCurrentBlockHeight()
time.Sleep(1 * time.Second)
}
}
func (self *App) fetchCurrentBlockHeight() int {
r, err := self.api.GetDynamicGlobalProperties()
if err != nil {
panic("can't fetch global properties, bailing")
}
if r.LastIrreversibleBlockNum < 100 {
panic("can't fetch global properties, bailing")
}
return r.LastIrreversibleBlockNum
}
func fetchBlockRange(s *SteemAPI, state *SteemDataStore, startBlock int, endBlock int) *error {
log.Debugf("fetching block range %d to %d inclusive", startBlock, endBlock)
for i := startBlock; i <= endBlock; i++ {
//fetchSingleBlock(s, fs, i)
}
return nil
}
/*
func (self *App) fetchSingleBlockOps(blockNum int) (*byte[]) {
tmpName := fmt.Sprintf("/blockOps/%d.json.tmp", blockNum)
realName := fmt.Sprintf("/blockOps/%d.json", blockNum)
done, err := afero.Exists(fs, realName)
if err != nil {
panic(err)
}
if done {
return
}
r, err := s.GetOpsInBlock(blockNum)
if err != nil {
panic(err)
}
bytes, err := json.Marshal(r)
if err != nil {
panic(err)
}
err = afero.WriteFile(fs, tmpName, bytes, 0)
if err != nil {
panic(err)
}
fs.Rename(tmpName, realName)
}
*/