-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(x/data): add basic validation for genesis state (#1395)
* feat(x/data): add state validation * fix lint * add failing scenarios and validate genesis test * update changelog * update changelog * add basic checks for validate genesis * feat(x/ecocredit): query all credit holders * Revert "feat(x/ecocredit): query all credit holders" This reverts commit 1339dbe. Co-authored-by: tyler <{ID}+{username}@users.noreply.github.com>
- Loading branch information
1 parent
8855cba
commit b58cb0f
Showing
21 changed files
with
756 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Feature: DataAnchor | ||
|
||
Scenario: a valid data anchor | ||
Given the data anchor | ||
""" | ||
{ | ||
"id": "cmVnZW4=", | ||
"timestamp": "2020-01-01T00:00:00Z" | ||
} | ||
""" | ||
When the data anchor is validated | ||
Then expect no error | ||
|
||
Scenario: an error is returned if id is empty | ||
Given the data anchor | ||
""" | ||
{} | ||
""" | ||
When the data anchor is validated | ||
Then expect the error "id cannot be empty: parse error" | ||
|
||
Scenario: an error is returned if timestamp is empty | ||
Given the data anchor | ||
""" | ||
{ | ||
"id": "cmVnZW4=" | ||
} | ||
""" | ||
When the data anchor is validated | ||
Then expect the error "timestamp cannot be empty: parse error" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Feature: DataAttestor | ||
|
||
Scenario: a valid data attestor | ||
Given the data attestor | ||
""" | ||
{ | ||
"id": "cmVnZW4=", | ||
"attestor": "BTZfSbi0JKqguZ/tIAPUIhdAa7Y=", | ||
"timestamp": "2020-01-01T00:00:00Z" | ||
} | ||
""" | ||
When the data attestor is validated | ||
Then expect no error | ||
|
||
Scenario: an error is returned if id is empty | ||
Given the data attestor | ||
""" | ||
{} | ||
""" | ||
When the data attestor is validated | ||
Then expect the error "id cannot be empty: parse error" | ||
|
||
Scenario: an error is returned if attestor is empty | ||
Given the data attestor | ||
""" | ||
{ | ||
"id": "cmVnZW4=" | ||
} | ||
""" | ||
When the data attestor is validated | ||
Then expect the error "attestor: empty address string is not allowed: parse error" | ||
|
||
Scenario: an error is returned if timestamp is empty | ||
Given the data attestor | ||
""" | ||
{ | ||
"id": "cmVnZW4=", | ||
"attestor": "BTZfSbi0JKqguZ/tIAPUIhdAa7Y=" | ||
} | ||
""" | ||
When the data attestor is validated | ||
Then expect the error "timestamp cannot be empty: parse error" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
Feature: DataId | ||
|
||
Scenario: a valid data id | ||
Given the data id | ||
""" | ||
{ | ||
"id": "cmVnZW4=", | ||
"iri": "regen:13toVgf5aZqSVSeJQv562xkkeoe3rr3bJWa29PHVKVf77VAkVMcDvVd.rdf" | ||
} | ||
""" | ||
When the data id is validated | ||
Then expect no error | ||
|
||
Scenario: an error is returned if id is empty | ||
Given the data id | ||
""" | ||
{} | ||
""" | ||
When the data id is validated | ||
Then expect the error "id cannot be empty: parse error" | ||
|
||
Scenario: an error is returned if iri is empty | ||
Given the data id | ||
""" | ||
{ | ||
"id": "cmVnZW4=" | ||
} | ||
""" | ||
When the data id is validated | ||
Then expect the error "failed to parse IRI: empty string is not allowed: invalid IRI: parse error" | ||
|
||
Scenario: an error is returned if iri is not formatted | ||
Given the data id | ||
""" | ||
{ | ||
"id": "cmVnZW4=", | ||
"iri": "foo" | ||
} | ||
""" | ||
When the data id is validated | ||
Then expect the error "failed to parse IRI foo: regen: prefix required: invalid IRI: parse error" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Feature: DataResolver | ||
|
||
Scenario: a valid data resolver | ||
Given the data resolver | ||
""" | ||
{ | ||
"id": "cmVnZW4=", | ||
"resolver_id": 1 | ||
} | ||
""" | ||
When the data resolver is validated | ||
Then expect no error | ||
|
||
Scenario: an error is returned if id is empty | ||
Given the data resolver | ||
""" | ||
{} | ||
""" | ||
When the data resolver is validated | ||
Then expect the error "id cannot be empty: parse error" | ||
|
||
Scenario: an error is returned if resolver id is empty | ||
Given the data resolver | ||
""" | ||
{ | ||
"id": "cmVnZW4=" | ||
} | ||
""" | ||
When the data resolver is validated | ||
Then expect the error "resolver id cannot be empty: parse error" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
Feature: Resolver | ||
|
||
Scenario: a valid resolver | ||
Given the resolver | ||
""" | ||
{ | ||
"id": 1, | ||
"url": "https://regen.network", | ||
"manager": "BTZfSbi0JKqguZ/tIAPUIhdAa7Y=" | ||
} | ||
""" | ||
When the resolver is validated | ||
Then expect no error | ||
|
||
Scenario: an error is returned if id is empty | ||
Given the resolver | ||
""" | ||
{} | ||
""" | ||
When the resolver is validated | ||
Then expect the error "id cannot be empty: parse error" | ||
|
||
Scenario: an error is returned if url is empty | ||
Given the resolver | ||
""" | ||
{ | ||
"id": 1 | ||
} | ||
""" | ||
When the resolver is validated | ||
Then expect the error "url cannot be empty: parse error" | ||
|
||
Scenario: an error is returned if url is not formatted | ||
Given the resolver | ||
""" | ||
{ | ||
"id": 1, | ||
"url": "foo" | ||
} | ||
""" | ||
When the resolver is validated | ||
Then expect the error "url: invalid url format: parse error" | ||
|
||
Scenario: an error is returned if manager is empty | ||
Given the resolver | ||
""" | ||
{ | ||
"id": 1, | ||
"url": "https://regen.network" | ||
} | ||
""" | ||
When the resolver is validated | ||
Then expect the error "manager: empty address string is not allowed: parse error" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package genesis | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
dbm "github.com/tendermint/tm-db" | ||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/cosmos/cosmos-sdk/orm/model/ormdb" | ||
"github.com/cosmos/cosmos-sdk/orm/model/ormtable" | ||
"github.com/cosmos/cosmos-sdk/orm/types/ormjson" | ||
|
||
api "github.com/regen-network/regen-ledger/api/regen/data/v1" | ||
"github.com/regen-network/regen-ledger/types/ormutil" | ||
"github.com/regen-network/regen-ledger/x/data" | ||
) | ||
|
||
// ValidateGenesis performs basic validation of genesis state. | ||
func ValidateGenesis(jsonData json.RawMessage) error { | ||
db := dbm.NewMemDB() | ||
backend := ormtable.NewBackend(ormtable.BackendOptions{ | ||
CommitmentStore: db, | ||
IndexStore: db, | ||
}) | ||
|
||
moduleDB, err := ormdb.NewModuleDB(&data.ModuleSchema, ormdb.ModuleDBOptions{ | ||
JSONValidator: validateMsg, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ormCtx := ormtable.WrapContextDefault(backend) | ||
|
||
jsonSource, err := ormjson.NewRawMessageSource(jsonData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = moduleDB.ImportJSON(ormCtx, jsonSource) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := moduleDB.ValidateJSON(jsonSource); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateMsg(m proto.Message) error { | ||
switch m.(type) { | ||
case *api.DataID: | ||
msg := &data.DataID{} | ||
if err := ormutil.PulsarToGogoSlow(m, msg); err != nil { | ||
return err | ||
} | ||
return msg.Validate() | ||
case *api.DataAnchor: | ||
msg := &data.DataAnchor{} | ||
if err := ormutil.PulsarToGogoSlow(m, msg); err != nil { | ||
return err | ||
} | ||
return msg.Validate() | ||
case *api.DataAttestor: | ||
msg := &data.DataAttestor{} | ||
if err := ormutil.PulsarToGogoSlow(m, msg); err != nil { | ||
return err | ||
} | ||
return msg.Validate() | ||
case *api.Resolver: | ||
msg := &data.Resolver{} | ||
if err := ormutil.PulsarToGogoSlow(m, msg); err != nil { | ||
return err | ||
} | ||
return msg.Validate() | ||
case *api.DataResolver: | ||
msg := &data.DataResolver{} | ||
if err := ormutil.PulsarToGogoSlow(m, msg); err != nil { | ||
return err | ||
} | ||
return msg.Validate() | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.