Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
feat!: strict data contract schema validation (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Shuplenkov authored Jun 18, 2021
1 parent 44d91a8 commit 33905f0
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 48 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#DRIVE_BRANCH=
DASHMATE_BRANCH=v0.20-dev
#TEST_SUITE_BRANCH=
#SDK_BRANCH=
DRIVE_BRANCH=update-dpp
DASHMATE_BRANCH=update-dpp
TEST_SUITE_BRANCH=update-dpp
SDK_BRANCH=update-dpp
12 changes: 9 additions & 3 deletions lib/utils/Validator.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const Ajv = require('ajv');
const { default: Ajv } = require('ajv/dist/2020');
const ArgumentsValidationError = require('../errors/ArgumentsValidationError');

class Validator {
constructor(schema) {
this.validateArguments = new Ajv().compile(schema);
this.validateArguments = new Ajv({
strictTypes: true,
strictTuples: true,
strictRequired: true,
addUsedSchema: false,
strict: true,
}).compile(schema);
}

validate(args) {
if (!this.validateArguments(args)) {
throw new ArgumentsValidationError(`params${this.validateArguments.errors[0].dataPath} ${this.validateArguments.errors[0].message}`);
throw new ArgumentsValidationError(`params${this.validateArguments.errors[0].instancePath} ${this.validateArguments.errors[0].message}`);
}
}
}
Expand Down
173 changes: 148 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
},
"dependencies": {
"@dashevo/dapi-grpc": "~0.19.0",
"@dashevo/dashcore-lib": "~0.19.23",
"@dashevo/dashcore-lib": "~0.19.25",
"@dashevo/dashd-rpc": "^2.0.2",
"@dashevo/dpp": "~0.19.1",
"@dashevo/dpp": "~0.20.0-dev.2",
"@dashevo/grpc-common": "~0.3.3",
"ajv": "^6.4.0",
"ajv": "^8.6.0",
"bs58": "^4.0.1",
"cbor": "^4.1.5",
"dotenv": "^6.0.0",
Expand Down
2 changes: 2 additions & 0 deletions scripts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async function main() {
});

const dppForParsingContracts = new DashPlatformProtocol();
await dppForParsingContracts.initialize();
const driveStateRepository = new DriveStateRepository(driveClient, dppForParsingContracts);

log.info(`Connecting to Tenderdash on ${config.tendermintCore.host}:${config.tendermintCore.port}`);
Expand Down Expand Up @@ -96,6 +97,7 @@ async function main() {
const dpp = new DashPlatformProtocol({
stateRepository: driveStateRepository,
});
await dpp.initialize();

// Start GRPC server
log.info('Starting GRPC server');
Expand Down
3 changes: 2 additions & 1 deletion test/integration/dpp/DriveStateRepository.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ describe('DriveStateRepository', () => {
let stateRepository;
let dataContractFixture;

beforeEach(function before() {
beforeEach(async function before() {
dataContractFixture = getDataContractFixture();

dpp = new DashPlatformProtocol();
await dpp.initialize();
sinon.spy(dpp.dataContract, 'createFromBuffer');

driveClientMock = sinon.stub();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('broadcastStateTransitionHandlerFactory', () => {

beforeEach(async function beforeEach() {
const dpp = new DashPlatformProtocol();
await dpp.initialize();

const dataContractFixture = getDataContractFixture();
stateTransitionFixture = dpp.dataContract.createStateTransition(dataContractFixture);
Expand Down
Loading

0 comments on commit 33905f0

Please sign in to comment.