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,6 +1,5 @@
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"

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"` ID string `json:"id"`
JSONRPC string `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
Result json.RawMessage `json:"result"` Result json.RawMessage `json:"result"`
Error *JsonRpcError `json:"error"` Error *JSONRPCError `json:"error"`
} }
type JsonRpcRequest struct { type JSONRPCRequest struct {
ID string `json:"id"` ID string `json:"id"`
JSONRPC string `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
Method string `json:"method"` Method string `json:"method"`
Params []interface{} `json:"params"` Params []json.RawMessage `json:"params"`
} }
// JsonRpc Client Object type JSONRPC struct {
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
} }