2018-10-28 16:31:26 +00:00
|
|
|
package main
|
|
|
|
|
2018-10-31 08:48:53 +00:00
|
|
|
import log "github.com/sirupsen/logrus"
|
2018-10-31 10:25:37 +00:00
|
|
|
|
|
|
|
//import "io/ioutil"
|
2018-10-31 09:44:19 +00:00
|
|
|
import "fmt"
|
|
|
|
import "strconv"
|
|
|
|
|
2018-10-31 11:05:25 +00:00
|
|
|
//steem block fetcher
|
|
|
|
const appPrefix = "sbf"
|
2018-10-31 09:44:19 +00:00
|
|
|
|
|
|
|
type SteemDataStorer interface {
|
2018-11-03 14:41:53 +00:00
|
|
|
SetCurrentNetworkBlockHeight(BlockNumber) error
|
|
|
|
SetCurrentLocalBlockHeight(BlockNumber) error
|
|
|
|
UpdateCurrentLocalBlockHeight()
|
|
|
|
CurrentLocalBlockHeight() BlockNumber
|
|
|
|
CurrentNetworkBlockHeight() BlockNumber
|
2018-10-31 09:44:19 +00:00
|
|
|
HaveOpsForBlock(BlockNumber) bool
|
|
|
|
StoreBlockOps(BlockNumber, *[]byte) error
|
|
|
|
}
|
2018-10-28 16:31:26 +00:00
|
|
|
|
|
|
|
// SteemDataStore is the object with which the rest of this tool interacts
|
|
|
|
type SteemDataStore struct {
|
2018-10-31 08:48:53 +00:00
|
|
|
kv KVStorer
|
2018-10-28 16:31:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 03:35:30 +00:00
|
|
|
func NewSteemDataStore(hostname string) SteemDataStorer {
|
2018-10-28 16:31:26 +00:00
|
|
|
self := new(SteemDataStore)
|
2018-11-01 03:35:30 +00:00
|
|
|
self.kv = NewRedisKVStore(hostname)
|
2018-11-03 14:41:53 +00:00
|
|
|
self.init()
|
2018-10-28 16:31:26 +00:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2018-11-03 14:41:53 +00:00
|
|
|
func (self *SteemDataStore) init() {
|
|
|
|
self.UpdateCurrentLocalBlockHeight()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SteemDataStore) UpdateCurrentLocalBlockHeight() {
|
|
|
|
cur := self.CurrentLocalBlockHeight()
|
|
|
|
next := self.FindHighestContiguousBlockInDb(cur)
|
|
|
|
if next != cur {
|
|
|
|
err := self.SetCurrentLocalBlockHeight(next)
|
|
|
|
log.Infof("current highest contig block in db is now %d", next)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SteemDataStore) SetCurrentLocalBlockHeight(blockNum BlockNumber) error {
|
|
|
|
keyname := fmt.Sprintf("%s.meta.CurrentLocalBlockHeight", appPrefix)
|
|
|
|
value := fmt.Sprintf("%d", blockNum)
|
|
|
|
return self.kv.Put(&keyname, &value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SteemDataStore) SetCurrentNetworkBlockHeight(blockNum BlockNumber) error {
|
|
|
|
keyname := fmt.Sprintf("%s.meta.CurrentNetworkBlockHeight", appPrefix)
|
2018-10-31 10:25:37 +00:00
|
|
|
value := fmt.Sprintf("%d", blockNum)
|
|
|
|
return self.kv.Put(&keyname, &value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SteemDataStore) FindHighestContiguousBlockInDb(from BlockNumber) BlockNumber {
|
|
|
|
last := from
|
|
|
|
|
|
|
|
var keyname string
|
|
|
|
var try BlockNumber
|
|
|
|
|
|
|
|
for {
|
|
|
|
try = BlockNumber(uint64(last) + 1)
|
2018-10-31 11:05:25 +00:00
|
|
|
keyname = fmt.Sprintf("%s.ops_in_block.%d", appPrefix, try)
|
2018-11-03 14:41:53 +00:00
|
|
|
exists, err := self.kv.Exists(&keyname)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2018-10-31 10:25:37 +00:00
|
|
|
if exists == false {
|
|
|
|
log.Debugf("cannot find block %d in db, highest found is %d", try, last)
|
|
|
|
return last
|
|
|
|
} else {
|
|
|
|
last = try
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 09:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SteemDataStore) StoreBlockOps(blockNum BlockNumber, blockOps *[]byte) error {
|
2018-10-31 11:05:25 +00:00
|
|
|
keyname := fmt.Sprintf("%s.ops_in_block.%d", appPrefix, blockNum)
|
2018-10-31 10:25:37 +00:00
|
|
|
value := string(*blockOps)
|
|
|
|
return self.kv.Put(&keyname, &value)
|
2018-10-31 09:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SteemDataStore) HaveOpsForBlock(blockNum BlockNumber) bool {
|
2018-10-31 11:05:25 +00:00
|
|
|
keyname := fmt.Sprintf("%s.ops_in_block.%d", appPrefix, blockNum)
|
|
|
|
exists, _ := self.kv.Exists(&keyname)
|
|
|
|
return exists
|
2018-10-31 09:44:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 14:41:53 +00:00
|
|
|
func (self *SteemDataStore) CurrentNetworkBlockHeight() BlockNumber {
|
|
|
|
keyname := fmt.Sprintf("%s.meta.CurrentNetworkBlockHeight", appPrefix)
|
|
|
|
val, err := self.kv.Get(&keyname)
|
|
|
|
if err != nil {
|
|
|
|
// assume this is key not found, initialize key to default
|
|
|
|
self.SetCurrentNetworkBlockHeight(0)
|
|
|
|
// retry
|
|
|
|
return self.CurrentNetworkBlockHeight()
|
|
|
|
}
|
|
|
|
intval, err := strconv.ParseUint(*val, 10, 64)
|
|
|
|
return BlockNumber(intval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SteemDataStore) CurrentLocalBlockHeight() BlockNumber {
|
|
|
|
keyname := fmt.Sprintf("%s.meta.CurrentLocalBlockHeight", appPrefix)
|
2018-10-31 09:44:19 +00:00
|
|
|
val, err := self.kv.Get(&keyname)
|
|
|
|
if err != nil {
|
2018-10-31 10:25:37 +00:00
|
|
|
// assume this is key not found, initialize key to default
|
2018-11-03 14:41:53 +00:00
|
|
|
self.SetCurrentLocalBlockHeight(0)
|
2018-10-31 10:25:37 +00:00
|
|
|
// retry
|
2018-11-03 14:41:53 +00:00
|
|
|
return self.CurrentLocalBlockHeight()
|
2018-10-31 09:44:19 +00:00
|
|
|
}
|
|
|
|
intval, err := strconv.ParseUint(*val, 10, 64)
|
|
|
|
return BlockNumber(intval)
|
2018-10-28 16:31:26 +00:00
|
|
|
}
|