checkpoint, does not work

This commit is contained in:
Jeffrey Paul 2018-10-17 23:31:02 -07:00
parent 053e178942
commit 0289c414fd
Signed by: sneak
GPG Key ID: 052443F4DF2A55C2
7 changed files with 92 additions and 77 deletions

1
.gx/lastpubver Normal file
View File

@ -0,0 +1 @@
0.0.0: Qmc8avR3R61qdrX8inmkKqnWtUQHQaN2XR6idG4wpYgYLf

View File

@ -1,63 +1,62 @@
package main package main
import "log" import "log"
import "io/ioutil" import "io/ioutil"
import "github.com/dgraph-io/badger" import "github.com/dgraph-io/badger"
type KeyValueStore struct { type KeyValueStore struct {
db *badger.DB db *badger.DB
} }
func NewKeyValueStore() *KeyValueStore { func NewKeyValueStore() *KeyValueStore {
kv := new(KeyValueStore) kv := new(KeyValueStore)
kv.init() kv.init()
return kv return kv
} }
func (kv *KeyValueStore) init() { func (kv *KeyValueStore) init() {
dir, err := ioutil.TempDir("", "badger") dir, err := ioutil.TempDir("", "badger")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
opts := badger.DefaultOptions opts := badger.DefaultOptions
opts.Dir = dir opts.Dir = dir
opts.ValueDir = dir opts.ValueDir = dir
kv.db, err = badger.Open(opts) kv.db, err = badger.Open(opts)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
func (kv *KeyValueStore) Close() { func (kv *KeyValueStore) Close() {
kv.db.Close() kv.db.Close()
} }
func (kv *KeyValueStore) Put(key *string, value *string) error { func (kv *KeyValueStore) Put(key *string, value *string) error {
txn := kv.db.NewTransaction(true) // Read-write txn txn := kv.db.NewTransaction(true) // Read-write txn
err := txn.Set([]byte(*key), []byte(*value)) err := txn.Set([]byte(*key), []byte(*value))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = txn.Commit(nil) err = txn.Commit(nil)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
return nil return nil
} }
func (kv *KeyValueStore) Get(key *string) (*string, error) { func (kv *KeyValueStore) Get(key *string) (*string, error) {
txn := kv.db.NewTransaction(true) // Read-write txn txn := kv.db.NewTransaction(true) // Read-write txn
item, err := txn.Get([]byte(*key)) item, err := txn.Get([]byte(*key))
if err != nil { if err != nil {
return nil, err return nil, err
} }
val, err := item.ValueCopy(nil) val, err := item.ValueCopy(nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s := string(val) s := string(val)
return &s, nil return &s, nil
} }

View File

@ -7,59 +7,53 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os"
) )
type logger interface { type httpRPCClient interface {
Println(v ...interface{})
}
type httpClient interface {
Post(url string, contentType string, body io.Reader) (*http.Response, error) Post(url string, contentType string, body io.Reader) (*http.Response, error)
} }
type JsonRpcError struct { type JSONRPCError struct {
Code int `json:"code"` Code int `json:"code"`
Message string `json:"message"` Message string `json:"message"`
} }
func (err JsonRpcError) Error() string { func (err JSONRPCError) Error() string {
return fmt.Sprintf("Error %d (%s)", err.Code, err.Message) return fmt.Sprintf("Error %d (%s)", err.Code, err.Message)
} }
type JsonRpcResponse struct { type JSONRPCResponse struct {
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result json.RawMessage `json:"result"`
Error *JsonRpcError `json:"error"`
}
type JsonRpcRequest struct {
ID string `json:"id"` ID string `json:"id"`
JSONRPC string `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
Method string `json:"method"` Result json.RawMessage `json:"result"`
Params []interface{} `json:"params"` Error *JSONRPCError `json:"error"`
} }
// JsonRpc Client Object type JSONRPCRequest struct {
type JsonRpc struct { ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params []json.RawMessage `json:"params"`
}
type JSONRPC struct {
url string url string
client httpClient client httpRPCClient
log logger
Debug bool Debug bool
log *log.Logger
} }
// New create new rpc client with given url // New create new rpc client with given url
func JsonRpcClient(url string, options ...func(rpc *JsonRpc)) *JsonRpc { func NewJSONRPC(url string, options ...func(rpc *JSONRPC)) *JSONRPC {
rpc := &JsonRpc{ rpc := &JSONRPC{
url: url, url: url,
client: http.DefaultClient, client: http.DefaultClient,
log: log.New(os.Stderr, "", log.LstdFlags),
} }
rpc.log = log.New()
for _, option := range options { for _, option := range options {
option(rpc) option(rpc)
} }
@ -68,8 +62,8 @@ func JsonRpcClient(url string, options ...func(rpc *JsonRpc)) *JsonRpc {
} }
// Call returns raw response of method call // Call returns raw response of method call
func (rpc *JsonRpc) Call(method string, params ...interface{}) (json.RawMessage, error) { func (rpc *JSONRPC) Call(method string, params ...json.RawMessage) (json.RawMessage, error) {
request := JsonRpcRequest{ request := JSONRPCRequest{
ID: "1", ID: "1",
JSONRPC: "2.0", JSONRPC: "2.0",
Method: method, Method: method,
@ -98,7 +92,7 @@ func (rpc *JsonRpc) Call(method string, params ...interface{}) (json.RawMessage,
rpc.log.Println(fmt.Sprintf("%s\nRequest: %s\nResponse: %s\n", method, body, data)) rpc.log.Println(fmt.Sprintf("%s\nRequest: %s\nResponse: %s\n", method, body, data))
} }
resp := new(JsonRpcResponse) resp := new(JSONRPCResponse)
if err := json.Unmarshal(data, resp); err != nil { if err := json.Unmarshal(data, resp); err != nil {
return nil, err return nil, err
} }

8
logging.go Normal file
View File

@ -0,0 +1,8 @@
package main
import log "github.com/sirupsen/logrus"
// wtf go stdlib doesn't have a Logger interface?!
type Logger interface {
log.FieldLogger
}

View File

@ -7,7 +7,7 @@ import (
) )
func main() { func main() {
c := JsonRpcClient("https://api.steemit.com", func(x *JsonRpc) { x.Debug = true }) c := JSONRPCClient("https://api.steemit.com", func(x *JSONRPC) { x.Debug = true })
r, err := c.GetVirtualOpsInBlock(20000000) r, err := c.GetVirtualOpsInBlock(20000000)
//r, err := c.GetOpsInBlock(1) //r, err := c.GetOpsInBlock(1)

12
package.json Normal file
View File

@ -0,0 +1,12 @@
{
"author": "sneak",
"bugs": {},
"gx": {},
"gxVersion": "0.12.1",
"language": "go",
"license": "",
"name": "steem-block-db",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.0.0"
}

View File

@ -4,9 +4,6 @@ import "encoding/json"
import "github.com/joeshaw/iso8601" import "github.com/joeshaw/iso8601"
type UnparsedSteemVirtualOp struct {
}
type SteemVirtualTransaction struct { type SteemVirtualTransaction struct {
TxID string `json:"trx_id"` TxID string `json:"trx_id"`
BlockNum uint64 `json:"block"` BlockNum uint64 `json:"block"`
@ -14,21 +11,25 @@ type SteemVirtualTransaction struct {
OpInTx int `json:"op_in_trx"` OpInTx int `json:"op_in_trx"`
IsVirtual int `json:"virtual_op"` IsVirtual int `json:"virtual_op"`
Timestamp iso8601.Time `json:"timestamp"` Timestamp iso8601.Time `json:"timestamp"`
Op *[]json.RawMessage `json:"op"` Op []json.RawMessage `json:"op"`
} }
func (rpc *JsonRpc) GetVirtualOpsInBlock(blockNum int) ([]*SteemVirtualTransaction, error) { func (rpc *JSONRPC) GetVirtualOpsInBlock(blockNum int) ([]*SteemVirtualTransaction, error) {
raw, err1 := rpc.Call("condenser_api.get_ops_in_block", blockNum, true) raw, err1 := rpc.Call("condenser_api.get_ops_in_block", blockNum, true)
if err1 != nil { if err1 != nil {
return nil, err1 return nil, err1
} }
var result []*SteemVirtualTransaction
tmp := make([]SteemVirtualTransaction)
//var result []interface{} //var result []interface{}
err2 := json.Unmarshal(raw, &result) err2 := json.Unmarshal(raw, &tmp)
return result, err2 if err2 != nil {
return result, nil
}
return nil, err2
} }
func (rpc *JsonRpc) GetOpsInBlock(blockNum int) (json.RawMessage, error) { func (rpc *JSONRPC) GetOpsInBlock(blockNum int) (json.RawMessage, error) {
r, err := rpc.Call("condenser_api.get_ops_in_block", blockNum, false) r, err := rpc.Call("condenser_api.get_ops_in_block", blockNum, false)
return r, err return r, err
} }