Commit 0adc8fc7 authored by Aaron's avatar Aaron

Merge branch '#164433106-setup_unit_test_framework_and_write_some_simple_tests' into 'master'

Merge of #164433106-setup_unit_test_framework_and_write_some_simple_tests to master

See merge request !13
parents e3178b77 b092d6c4
Pipeline #54305396 canceled with stages
in 4 minutes and 8 seconds
test_output.xml
.postgres
# Created by https://www.gitignore.io/api/go,vim,emacs,intellij
......@@ -170,4 +169,4 @@ tags
# Persistent undo
[._]*.un~
# End of https://www.gitignore.io/api/go,vim,emacs,intellij
\ No newline at end of file
# End of https://www.gitignore.io/api/go,vim,emacs,intellij
......@@ -24,6 +24,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/lib/pq v1.0.0
github.com/magiconair/properties v1.8.0 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
......
......@@ -107,6 +107,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
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=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
......
package esp
import sdk "github.com/cosmos/cosmos-sdk/types"
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"gitlab.com/regen-network/regen-ledger/x/geo"
)
//type SchemaType int
//
......@@ -27,7 +30,7 @@ type ESPResult struct {
Name string `json:"name"`
Version string `json:"version"`
Verifier sdk.AccAddress `json:"verifier"`
GeoID []byte `json:"geo_id,omitempty"`
GeoID geo.GeoAddress `json:"geo_id,omitempty"`
Data []byte `json:"data"`
// TODO link this to data module for either on or off-chain result storage
}
......@@ -24,5 +24,11 @@ func NewHandler(keeper Keeper) sdk.Handler {
}
func handleMsgStoreGeometry(ctx sdk.Context, keeper Keeper, msg MsgStoreGeometry) sdk.Result {
return keeper.StoreGeometry(ctx, msg.Data)
addr, err := keeper.StoreGeometry(ctx, msg.Data)
if err != nil {
return err.Result()
}
tags := sdk.EmptyTags()
tags = tags.AppendTag("geo.id", GeoURL(addr))
return sdk.Result{Tags: tags}
}
......@@ -19,54 +19,50 @@ type Keeper struct {
const (
Bech32Prefix = "xrn:geo/"
WGS84_SRID = 4326
)
func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, pgIndexer postgresql.Indexer) Keeper {
return Keeper{storeKey, cdc, pgIndexer}
}
func (keeper Keeper) GetGeometry(ctx sdk.Context, hash []byte) []byte {
func (keeper Keeper) GetGeometry(ctx sdk.Context, addr GeoAddress) []byte {
store := ctx.KVStore(keeper.storeKey)
bz := store.Get(hash)
bz := store.Get(addr)
if bz == nil {
return nil
}
var geom []byte
keeper.cdc.MustUnmarshalBinaryBare(bz, &geom)
return geom
return bz
}
func GeoURL(addr GeoAddress) string {
return util.MustEncodeBech32(Bech32Prefix, addr)
}
func (keeper Keeper) StoreGeometry(ctx sdk.Context, geometry Geometry) sdk.Result {
func (keeper Keeper) StoreGeometry(ctx sdk.Context, geometry Geometry) (addr GeoAddress, err sdk.Error) {
// TODO consume gas
store := ctx.KVStore(keeper.storeKey)
bz := keeper.cdc.MustMarshalBinaryBare(geometry)
hash, err := blake2b.New256(nil)
if err != nil {
panic(err)
hash, e := blake2b.New256(nil)
if e != nil {
return nil, sdk.ErrUnknownRequest(e.Error())
}
ewkb := geometry.EWKB
hash.Write(ewkb)
hashBz := hash.Sum(nil)
existing := store.Get(hashBz)
if existing != nil {
return sdk.Result{
Code: sdk.CodeUnknownRequest,
Log: "already exists",
}
return nil, sdk.ErrUnknownRequest("already exists")
}
store.Set(hashBz, bz)
tags := sdk.EmptyTags()
url := util.MustEncodeBech32(Bech32Prefix, hashBz)
tags = tags.AppendTag("geo.id", url)
store.Set(hashBz, ewkb)
// Do Indexing
if keeper.pgIndexer != nil {
keeper.pgIndexer.Exec(
"INSERT INTO geo (url, geog, geom) VALUES ($1, st_geogfromwkb($2), st_geomfromewkb($3))",
url, ewkb, ewkb)
GeoURL(hashBz), ewkb, ewkb)
}
return sdk.Result{Tags: tags}
return hashBz, nil
}
func MustDecodeBech32GeoID(bech string) []byte {
......
This diff is collapsed.
......@@ -46,6 +46,10 @@ func (msg MsgStoreGeometry) ValidateBasic() sdk.Error {
return sdk.ErrUnknownRequest(fmt.Sprintf("Geometry is not in EWKB format: %+v", err))
}
if g.SRID() != WGS84_SRID {
return sdk.ErrUnknownRequest(fmt.Sprintf("Geometry does not use WGS84 SRID, got %d", g.SRID()))
}
featureType := msg.Data.Type
actual, err := GetFeatureType(g)
......
......@@ -12,7 +12,7 @@ type MsgStoreGeo struct {
type FeatureType int
const (
Point FeatureType = 0
Point FeatureType = iota
LineString
Polygon
MultiPoint
......@@ -27,6 +27,8 @@ type Geometry struct {
EWKB []byte `json:"ewkb,omitempty"`
}
type GeoAddress []byte
const PostgresSchema = `
CREATE TABLE geo (
url text NOT NULL PRIMARY KEY,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment