@@ -0,0 +1 @@ | |||
0.0.0: Qmc8avR3R61qdrX8inmkKqnWtUQHQaN2XR6idG4wpYgYLf |
@@ -1,63 +1,62 @@ | |||
package main | |||
import "log" | |||
import "io/ioutil" | |||
import "github.com/dgraph-io/badger" | |||
type KeyValueStore struct { | |||
db *badger.DB | |||
db *badger.DB | |||
} | |||
func NewKeyValueStore() *KeyValueStore { | |||
kv := new(KeyValueStore) | |||
kv.init() | |||
return kv | |||
kv := new(KeyValueStore) | |||
kv.init() | |||
return kv | |||
} | |||
func (kv *KeyValueStore) init() { | |||
dir, err := ioutil.TempDir("", "badger") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
opts := badger.DefaultOptions | |||
opts.Dir = dir | |||
opts.ValueDir = dir | |||
kv.db, err = badger.Open(opts) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
dir, err := ioutil.TempDir("", "badger") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
opts := badger.DefaultOptions | |||
opts.Dir = dir | |||
opts.ValueDir = dir | |||
kv.db, err = badger.Open(opts) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
} | |||
func (kv *KeyValueStore) Close() { | |||
kv.db.Close() | |||
kv.db.Close() | |||
} | |||
func (kv *KeyValueStore) Put(key *string, value *string) error { | |||
txn := kv.db.NewTransaction(true) // Read-write txn | |||
err := txn.Set([]byte(*key), []byte(*value)) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
err = txn.Commit(nil) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
return nil | |||
txn := kv.db.NewTransaction(true) // Read-write txn | |||
err := txn.Set([]byte(*key), []byte(*value)) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
err = txn.Commit(nil) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
return nil | |||
} | |||
func (kv *KeyValueStore) Get(key *string) (*string, error) { | |||
txn := kv.db.NewTransaction(true) // Read-write txn | |||
item, err := txn.Get([]byte(*key)) | |||
if err != nil { | |||
return nil, err | |||
} | |||
val, err := item.ValueCopy(nil) | |||
if err != nil { | |||
return nil, err | |||
} | |||
s := string(val) | |||
return &s, nil | |||
txn := kv.db.NewTransaction(true) // Read-write txn | |||
item, err := txn.Get([]byte(*key)) | |||
if err != nil { | |||
return nil, err | |||
} | |||
val, err := item.ValueCopy(nil) | |||
if err != nil { | |||
return nil, err | |||
} | |||
s := string(val) | |||
return &s, nil | |||
} |
@@ -7,59 +7,53 @@ import ( | |||
"bytes" | |||
"encoding/json" | |||
"fmt" | |||
log "github.com/sirupsen/logrus" | |||
"io" | |||
"io/ioutil" | |||
"log" | |||
"net/http" | |||
"os" | |||
) | |||
type logger interface { | |||
Println(v ...interface{}) | |||
} | |||
type httpClient interface { | |||
type httpRPCClient interface { | |||
Post(url string, contentType string, body io.Reader) (*http.Response, error) | |||
} | |||
type JsonRpcError struct { | |||
type JSONRPCError struct { | |||
Code int `json:"code"` | |||
Message string `json:"message"` | |||
} | |||
func (err JsonRpcError) Error() string { | |||
func (err JSONRPCError) Error() string { | |||
return fmt.Sprintf("Error %d (%s)", err.Code, err.Message) | |||
} | |||
type JsonRpcResponse struct { | |||
ID string `json:"id"` | |||
JSONRPC string `json:"jsonrpc"` | |||
Result json.RawMessage `json:"result"` | |||
Error *JsonRpcError `json:"error"` | |||
} | |||
type JsonRpcRequest struct { | |||
type JSONRPCResponse struct { | |||
ID string `json:"id"` | |||
JSONRPC string `json:"jsonrpc"` | |||
Method string `json:"method"` | |||
Params []interface{} `json:"params"` | |||
Result json.RawMessage `json:"result"` | |||
Error *JSONRPCError `json:"error"` | |||
} | |||
type JSONRPCRequest struct { | |||
ID string `json:"id"` | |||
JSONRPC string `json:"jsonrpc"` | |||
Method string `json:"method"` | |||
Params []json.RawMessage `json:"params"` | |||
} | |||
// JsonRpc Client Object | |||
type JsonRpc struct { | |||
type JSONRPC struct { | |||
url string | |||
client httpClient | |||
log logger | |||
client httpRPCClient | |||
Debug bool | |||
log *log.Logger | |||
} | |||
// New create new rpc client with given url | |||
func JsonRpcClient(url string, options ...func(rpc *JsonRpc)) *JsonRpc { | |||
rpc := &JsonRpc{ | |||
func NewJSONRPC(url string, options ...func(rpc *JSONRPC)) *JSONRPC { | |||
rpc := &JSONRPC{ | |||
url: url, | |||
client: http.DefaultClient, | |||
log: log.New(os.Stderr, "", log.LstdFlags), | |||
} | |||
rpc.log = log.New() | |||
for _, option := range options { | |||
option(rpc) | |||
} | |||
@@ -68,8 +62,8 @@ func JsonRpcClient(url string, options ...func(rpc *JsonRpc)) *JsonRpc { | |||
} | |||
// Call returns raw response of method call | |||
func (rpc *JsonRpc) Call(method string, params ...interface{}) (json.RawMessage, error) { | |||
request := JsonRpcRequest{ | |||
func (rpc *JSONRPC) Call(method string, params ...json.RawMessage) (json.RawMessage, error) { | |||
request := JSONRPCRequest{ | |||
ID: "1", | |||
JSONRPC: "2.0", | |||
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)) | |||
} | |||
resp := new(JsonRpcResponse) | |||
resp := new(JSONRPCResponse) | |||
if err := json.Unmarshal(data, resp); err != nil { | |||
return nil, err | |||
} |
@@ -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 | |||
} |
@@ -7,7 +7,7 @@ import ( | |||
) | |||
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.GetOpsInBlock(1) |
@@ -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" | |||
} | |||
@@ -4,9 +4,6 @@ import "encoding/json" | |||
import "github.com/joeshaw/iso8601" | |||
type UnparsedSteemVirtualOp struct { | |||
} | |||
type SteemVirtualTransaction struct { | |||
TxID string `json:"trx_id"` | |||
BlockNum uint64 `json:"block"` | |||
@@ -14,21 +11,25 @@ type SteemVirtualTransaction struct { | |||
OpInTx int `json:"op_in_trx"` | |||
IsVirtual int `json:"virtual_op"` | |||
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) | |||
if err1 != nil { | |||
return nil, err1 | |||
} | |||
var result []*SteemVirtualTransaction | |||
tmp := make([]SteemVirtualTransaction) | |||
//var result []interface{} | |||
err2 := json.Unmarshal(raw, &result) | |||
return result, err2 | |||
err2 := json.Unmarshal(raw, &tmp) | |||
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) | |||
return r, err | |||
} |