Skip to content

Commit

Permalink
Merge pull request #23 from regen-network/16-store_on-chain_data_graph
Browse files Browse the repository at this point in the history
#16 store data graph on-chain
  • Loading branch information
aaronc committed Mar 29, 2019
2 parents dabf50f + b2a4f76 commit c1da652
Show file tree
Hide file tree
Showing 29 changed files with 830 additions and 880 deletions.
12 changes: 10 additions & 2 deletions app.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/regen-network/regen-ledger/x/geo"
"github.com/regen-network/regen-ledger/x/group"
"github.com/regen-network/regen-ledger/x/proposal"
"github.com/regen-network/regen-ledger/x/schema"
"github.com/regen-network/regen-ledger/x/upgrade"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
Expand Down Expand Up @@ -45,6 +46,7 @@ type xrnApp struct {
keyFeeCollection *sdk.KVStoreKey
//schemaStoreKey *sdk.KVStoreKey
dataStoreKey *sdk.KVStoreKey
schemaStoreKey *sdk.KVStoreKey
espStoreKey *sdk.KVStoreKey
geoStoreKey *sdk.KVStoreKey
agentStoreKey *sdk.KVStoreKey
Expand All @@ -58,6 +60,7 @@ type xrnApp struct {
bankKeeper bank.Keeper
feeCollectionKeeper auth.FeeCollectionKeeper
dataKeeper data.Keeper
schemaKeeper schema.Keeper
espKeeper esp.Keeper
geoKeeper geo.Keeper
agentKeeper group.Keeper
Expand Down Expand Up @@ -93,6 +96,7 @@ func NewXrnApp(logger log.Logger, db dbm.DB, postgresUrl string) *xrnApp {
keyFeeCollection: sdk.NewKVStoreKey("fee_collection"),
//schemaStoreKey: sdk.NewKVStoreKey("schema"),
dataStoreKey: sdk.NewKVStoreKey("data"),
schemaStoreKey: sdk.NewKVStoreKey("schema"),
espStoreKey: sdk.NewKVStoreKey("esp"),
geoStoreKey: sdk.NewKVStoreKey("geo"),
agentStoreKey: sdk.NewKVStoreKey("group"),
Expand Down Expand Up @@ -133,7 +137,9 @@ func NewXrnApp(logger log.Logger, db dbm.DB, postgresUrl string) *xrnApp {
// The FeeCollectionKeeper collects transaction fees and renders them to the fee distribution module
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(cdc, app.keyFeeCollection)

app.dataKeeper = data.NewKeeper(app.dataStoreKey, cdc)
app.schemaKeeper = schema.NewKeeper(app.schemaStoreKey, cdc)

app.dataKeeper = data.NewKeeper(app.dataStoreKey, app.schemaKeeper, cdc)

app.agentKeeper = group.NewKeeper(app.agentStoreKey, cdc)

Expand Down Expand Up @@ -162,7 +168,8 @@ func NewXrnApp(logger log.Logger, db dbm.DB, postgresUrl string) *xrnApp {
AddRoute("data", data.NewHandler(app.dataKeeper)).
AddRoute("geo", geo.NewHandler(app.geoKeeper)).
AddRoute("group", group.NewHandler(app.agentKeeper)).
AddRoute("proposal", proposal.NewHandler(app.proposalKeeper))
AddRoute("proposal", proposal.NewHandler(app.proposalKeeper)).
AddRoute("schema", schema.NewHandler(app.schemaKeeper))

// The app.QueryRouter is the main query router where each module registers its routes
app.QueryRouter().
Expand All @@ -179,6 +186,7 @@ func NewXrnApp(logger log.Logger, db dbm.DB, postgresUrl string) *xrnApp {
app.keyMain,
app.keyAccount,
app.keyFeeCollection,
app.schemaStoreKey,
app.dataStoreKey,
app.espStoreKey,
app.geoStoreKey,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Expand Up @@ -10,6 +10,7 @@ require (
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/btcsuite/btcd v0.0.0-20181130015935-7d2daa5bfef2 // indirect
github.com/btcsuite/btcutil v0.0.0-20180706230648-ab6388e0c60a
github.com/campoy/unique v0.0.0-20180121183637-88950e537e7e
github.com/cosmos/cosmos-sdk v0.32.0
//github.com/cosmos/cosmos-sdk v0.32.0
Expand All @@ -28,7 +29,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmhodges/levigo v0.0.0-20161115193449-c42d9e0ca023 // indirect
github.com/leanovate/gopter v0.2.4
github.com/leanovate/gopter v0.0.0-20190000000000-6e7780f59df75750618bf30eeafcb1a88e457fcb
github.com/lib/pq v1.0.0
github.com/magiconair/properties v1.8.0 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Expand Up @@ -82,6 +82,12 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/leanovate/gopter v0.0.0-20161115193449-6e7780f59df75750618bf30eeafcb1a88e457fcb h1:PXHyfWWcfjaKOv+kkphu63mi/ZUQ00O3moYhRx+3A1k=
github.com/leanovate/gopter v0.0.0-20161115193449-6e7780f59df75750618bf30eeafcb1a88e457fcb/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/leanovate/gopter v0.0.0-20190000000000-6e7780f59df75750618bf30eeafcb1a88e457fcb h1:hs0vi//Il4Cf5W3zUuAb/XaAkZZ7LiIGB+gmlniKBQM=
github.com/leanovate/gopter v0.0.0-20190000000000-6e7780f59df75750618bf30eeafcb1a88e457fcb/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/leanovate/gopter v0.0.0-20190005193449-6e7780f59df75750618bf30eeafcb1a88e457fcb/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/leanovate/gopter v0.0.0-20191115193449-6e7780f59df75750618bf30eeafcb1a88e457fcb/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/leanovate/gopter v0.2.4 h1:U4YLBggDFhJdqQsG4Na2zX7joVTky9vHaj/AGEwSuXU=
github.com/leanovate/gopter v0.2.4/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
Expand Down
53 changes: 0 additions & 53 deletions graph/binary/common.go

This file was deleted.

15 changes: 15 additions & 0 deletions graph/binary/consts/consts.go
@@ -0,0 +1,15 @@
package consts

const (
PrefixGeoAddress byte = iota
PrefixAccAddress
PrefixHashID
)

const (
PrefixIDMax = PrefixHashID
)

const (
PrefixPropertyID byte = 0
)
16 changes: 8 additions & 8 deletions graph/binary/deserialize.go
Expand Up @@ -4,16 +4,16 @@ import (
"encoding/binary"
"fmt"
"github.com/regen-network/regen-ledger/graph"
"github.com/regen-network/regen-ledger/graph/binary/consts"
"github.com/regen-network/regen-ledger/graph/impl"
"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/x/schema"
"io"
"math"
)
import sdk "github.com/cosmos/cosmos-sdk/types"

// DeserializeGraph deserializes a Graph from a binary reader
func DeserializeGraph(resolver SchemaResolver, r io.ByteScanner) (g graph.Graph, err error) {
func DeserializeGraph(resolver graph.SchemaResolver, r io.ByteScanner) (g graph.Graph, err error) {
ctx := &dszContext{resolver, r, 0}
g, err = ctx.readGraph()
if err != nil {
Expand All @@ -23,7 +23,7 @@ func DeserializeGraph(resolver SchemaResolver, r io.ByteScanner) (g graph.Graph,
}

type dszContext struct {
resolver SchemaResolver
resolver graph.SchemaResolver
r io.ByteScanner
version uint64
}
Expand Down Expand Up @@ -66,11 +66,11 @@ func (ctx *dszContext) readNode() (n graph.Node, err error) {
func (ctx *dszContext) readID() (id types.HasURI, err error) {
prefix := ctx.mustReadByte()
switch prefix {
case prefixGeoAddress:
case consts.PrefixGeoAddress:
return types.GeoAddress(ctx.readByteSlice()), nil
case prefixAccAddress:
case consts.PrefixAccAddress:
return graph.AccAddressID{sdk.AccAddress(ctx.readByteSlice())}, nil
case prefixHashID:
case consts.PrefixHashID:
return graph.HashID{Fragment: ctx.readString()}, nil
default:
return nil, fmt.Errorf("unexpected ID prefix %d", prefix)
Expand All @@ -92,11 +92,11 @@ func (ctx *dszContext) readNodeProperties() (n graph.Node, err error) {

func (ctx *dszContext) readProperty() (prop graph.Property, value interface{}, err error) {
prefix := ctx.mustReadByte()
if prefix != prefixPropertyID {
if prefix != consts.PrefixPropertyID {
return nil, nil, fmt.Errorf("unexpected property ID prefix %d", prefix)
}
id := ctx.mustReadVarint64()
prop = ctx.resolver.GetPropertyByID(schema.PropertyID(id))
prop = ctx.resolver.GetPropertyByID(graph.PropertyID(id))
if prop == nil {
return nil, nil, fmt.Errorf("can't resolve property with ID %d", id)
}
Expand Down
13 changes: 7 additions & 6 deletions graph/binary/serialize.go
Expand Up @@ -5,13 +5,14 @@ import (
"encoding/binary"
"fmt"
"github.com/regen-network/regen-ledger/graph"
"github.com/regen-network/regen-ledger/graph/binary/consts"
"github.com/regen-network/regen-ledger/types"
"io"
"math"
)

// SerializeGraph serializes a Graph in binary format
func SerializeGraph(schema SchemaResolver, g graph.Graph, w io.Writer) error {
func SerializeGraph(schema graph.SchemaResolver, g graph.Graph, w io.Writer) error {
ctx := &szContext{schema, bufio.NewWriter(w)}
err := ctx.serializeGraph(g)
if err != nil {
Expand All @@ -25,7 +26,7 @@ func SerializeGraph(schema SchemaResolver, g graph.Graph, w io.Writer) error {
}

type szContext struct {
resolver SchemaResolver
resolver graph.SchemaResolver
w *bufio.Writer
}

Expand Down Expand Up @@ -78,13 +79,13 @@ func (s *szContext) serializeNode(root bool, n graph.Node) error {
func (s *szContext) writeID(id types.HasURI) error {
switch id := id.(type) {
case types.GeoAddress:
s.writeByte(prefixGeoAddress)
s.writeByte(consts.PrefixGeoAddress)
s.writeByteSlice(id)
case graph.AccAddressID:
s.writeByte(prefixAccAddress)
s.writeByte(consts.PrefixAccAddress)
s.writeByteSlice(id.AccAddress)
case graph.HashID:
s.writeByte(prefixHashID)
s.writeByte(consts.PrefixHashID)
s.writeString(id.Fragment)
default:
return fmt.Errorf("unexpected ID %s", id.String())
Expand All @@ -93,7 +94,7 @@ func (s *szContext) writeID(id types.HasURI) error {
}

func (s *szContext) writeProperty(w *bufio.Writer, p graph.Property, value interface{}) error {
s.writeByte(prefixPropertyID)
s.writeByte(consts.PrefixPropertyID)
id := s.resolver.GetPropertyID(p)
if id == 0 {
return fmt.Errorf("can't resolve property %s in schema", p.URI())
Expand Down

0 comments on commit c1da652

Please sign in to comment.