Skip to content

Commit

Permalink
added search contract
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jan 11, 2025
1 parent 29dadbf commit bfbce84
Show file tree
Hide file tree
Showing 18 changed files with 495 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/rs-drive-abci/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ impl Default for ExecutionConfig {
Self {
use_document_triggers: ExecutionConfig::default_use_document_triggers(),
verify_sum_trees: ExecutionConfig::default_verify_sum_trees(),
verify_token_sum_trees: ExecutionConfig::default_verify_token_sum_trees(),
epoch_time_length_s: ExecutionConfig::default_epoch_time_length_s(),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use dpp::block::epoch::Epoch;
use drive::drive::Drive;
use drive::grovedb::Transaction;

use crate::error::execution::ExecutionError;
use crate::error::Error;
use crate::execution::types::block_execution_context::BlockExecutionContext;
use crate::platform_types::platform::Platform;
use platform_version::version::PlatformVersion;
use dpp::version::PlatformVersion;

impl<CoreRPCLike> Platform<CoreRPCLike> {
/// Adds operations to GroveDB op batch related to processing
Expand All @@ -22,25 +20,24 @@ impl<CoreRPCLike> Platform<CoreRPCLike> {
) -> Result<(), Error> {
if self.config.execution.verify_token_sum_trees {
// Verify sum trees
let credits_verified = self
let token_balance = self
.drive
.calculate_total_token_balance(Some(transaction), &platform_version.drive)
.calculate_total_tokens_balance(Some(transaction), &platform_version.drive)
.map_err(Error::Drive)?;

if !credits_verified.ok()? {
if !token_balance.ok()? {
return Err(Error::Execution(
ExecutionError::CorruptedCreditsNotBalanced(format!(
"credits are not balanced after block execution {:?} off by {}",
credits_verified,
credits_verified
.total_in_trees()
.unwrap()
.abs_diff(credits_verified.total_credits_in_platform)
token_balance,
token_balance
.total_identity_token_balances
.abs_diff(token_balance.total_tokens_in_platform)
)),
));
}
}

Ok(outcome)
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl Drive {
&mut drive_operations,
&platform_version.drive,
)?;
println!("{:?}", drive_operations);
let fees = Drive::calculate_fee(
None,
Some(drive_operations),
Expand Down
4 changes: 0 additions & 4 deletions packages/rs-drive/src/drive/initialization/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ mod tests {
let platform_version = PlatformVersion::first();
let drive = setup_drive_with_initial_state_structure(Some(platform_version));

let _db_transaction = drive.grove.start_transaction();

let platform_version = PlatformVersion::first();
let drive_version = &platform_version.drive;

Expand Down Expand Up @@ -574,8 +572,6 @@ mod tests {
fn test_initial_state_structure_proper_heights_in_latest_protocol_version() {
let drive = setup_drive_with_initial_state_structure(None);

let _db_transaction = drive.grove.start_transaction();

let platform_version = PlatformVersion::latest();
let drive_version = &platform_version.drive;

Expand Down
18 changes: 18 additions & 0 deletions packages/search-contract/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "airbnb-base",
"rules": {
"no-plusplus": 0,
"eol-last": [
"error",
"always"
],
"class-methods-use-this": "off",
"curly": [
"error",
"all"
]
},
"globals": {
"BigInt": true
}
}
2 changes: 2 additions & 0 deletions packages/search-contract/.mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require: test/bootstrap.js
recursive: true
13 changes: 13 additions & 0 deletions packages/search-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "token-history-contract"
description = "Token history data contract schema and tools"
version = "1.6.2"
edition = "2021"
rust-version.workspace = true
license = "MIT"

[dependencies]
thiserror = "1.0.64"
platform-version = { path = "../rs-platform-version" }
serde_json = { version = "1.0" }
platform-value = { path = "../rs-platform-value" }
20 changes: 20 additions & 0 deletions packages/search-contract/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2019 Dash Core Group, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 changes: 26 additions & 0 deletions packages/search-contract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Search Contract

[![Build Status](https://github.com/dashpay/platform/actions/workflows/release.yml/badge.svg)](https://github.com/dashpay/platform/actions/workflows/release.yml)
[![NPM version](https://img.shields.io/npm/v/@dashevo/wallet-contract.svg?style=flat-square)](https://npmjs.org/package/@dashevo/wallet-contract)

JSON Contracts for Searching Data Contracts

## Table of Contents

- [Install](#install)
- [Contributing](#contributing)
- [License](#license)

## Install

```sh
npm install @dashevo/search-contract
```

## Contributing

Feel free to dive in! [Open an issue](https://github.com/dashpay/platform/issues/new/choose) or submit PRs.

## License

[MIT](LICENSE) &copy; Dash Core Group, Inc.
4 changes: 4 additions & 0 deletions packages/search-contract/lib/systemIds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
ownerId: '11111111111111111111111111111111',
contractId: '8v8CoKCDdBcQu1Y7GDNJjR7a5vkMmgpXycJURkaUhfU9',
};
29 changes: 29 additions & 0 deletions packages/search-contract/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@dashevo/search-contract",
"version": "1.6.2",
"description": "A contract that allows searching for contracts",
"scripts": {
"lint": "eslint .",
"test": "yarn run test:unit",
"test:unit": "mocha 'test/unit/**/*.spec.js'"
},
"contributors": [
{
"name": "Samuel Westrich",
"email": "[email protected]",
"url": "https://github.com/quantumexplorer"
}
],
"license": "MIT",
"devDependencies": {
"@dashevo/wasm-dpp": "workspace:*",
"chai": "^4.3.10",
"dirty-chai": "^2.0.1",
"eslint": "^8.53.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.29.0",
"mocha": "^10.2.0",
"sinon": "^17.0.1",
"sinon-chai": "^3.7.0"
}
}
62 changes: 62 additions & 0 deletions packages/search-contract/schema/v1/search-contract-documents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"contract": {
"type": "object",
"documentsMutable": false,
"canBeDeleted": false,
"referenceType": "contract",
"creationRestrictionMode": 2,
"indices": [
{
"name": "byKeyword",
"properties": [
{
"keyword": "asc"
}
]
}
],
"properties": {
"keyword": {
"type": "string",
"minLength": 3,
"maxLength": 50,
"position": 0
}
},
"required": [
"$contractId",
"keyword"
],
"additionalProperties": false
},
"token": {
"type": "object",
"documentsMutable": false,
"canBeDeleted": false,
"referenceType": "token",
"creationRestrictionMode": 2,
"indices": [
{
"name": "byKeyword",
"properties": [
{
"keyword": "asc"
}
]
}
],
"properties": {
"keyword": {
"type": "string",
"minLength": 3,
"maxLength": 50,
"position": 0
}
},
"required": [
"$tokenId",
"keyword"
],
"additionalProperties": false
}
}
17 changes: 17 additions & 0 deletions packages/search-contract/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use platform_version::version::FeatureVersion;

#[derive(thiserror::Error, Debug)]
pub enum Error {
/// Platform expected some specific versions
#[error("platform unknown version on {method}, received: {received}")]
UnknownVersionMismatch {
/// method
method: String,
/// the allowed versions for this method
known_versions: Vec<FeatureVersion>,
/// requested core height
received: FeatureVersion,
},
#[error("schema deserialize error: {0}")]
InvalidSchemaJson(#[from] serde_json::Error),
}
37 changes: 37 additions & 0 deletions packages/search-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
mod error;
pub mod v1;

pub use crate::error::Error;
use platform_value::{Identifier, IdentifierBytes32};
use platform_version::version::PlatformVersion;
use serde_json::Value;

pub const ID_BYTES: [u8; 32] = [
92, 20, 14, 101, 92, 2, 101, 187, 194, 168, 8, 113, 109, 225, 132, 121, 133, 19, 89, 24, 173,
81, 205, 253, 11, 118, 102, 75, 169, 91, 163, 124,
];

pub const OWNER_ID_BYTES: [u8; 32] = [0; 32];

pub const ID: Identifier = Identifier(IdentifierBytes32(ID_BYTES));
pub const OWNER_ID: Identifier = Identifier(IdentifierBytes32(OWNER_ID_BYTES));
pub fn load_definitions(platform_version: &PlatformVersion) -> Result<Option<Value>, Error> {
match platform_version.system_data_contracts.withdrawals {
1 => Ok(None),
version => Err(Error::UnknownVersionMismatch {
method: "search_contract::load_definitions".to_string(),
known_versions: vec![1],
received: version,
}),
}
}
pub fn load_documents_schemas(platform_version: &PlatformVersion) -> Result<Value, Error> {
match platform_version.system_data_contracts.withdrawals {
1 => v1::load_documents_schemas(),
version => Err(Error::UnknownVersionMismatch {
method: "search_contract::load_documents_schemas".to_string(),
known_versions: vec![1],
received: version,
}),
}
}
27 changes: 27 additions & 0 deletions packages/search-contract/src/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::Error;
use serde_json::Value;

pub mod document_types {
pub mod contract {
pub const NAME: &str = "contract";

pub mod properties {
pub const KEY_INDEX: &str = "byKeyword";
}
}

pub mod token {
pub const NAME: &str = "token";

pub mod properties {
pub const KEY_INDEX: &str = "byKeyword";
}
}
}

pub fn load_documents_schemas() -> Result<Value, Error> {
serde_json::from_str(include_str!(
"../../schema/v1/search-contract-documents.json"
))
.map_err(Error::InvalidSchemaJson)
}
12 changes: 12 additions & 0 deletions packages/search-contract/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"env": {
"node": true,
"mocha": true
},
"rules": {
"import/no-extraneous-dependencies": "off"
},
"globals": {
"expect": true
}
}
30 changes: 30 additions & 0 deletions packages/search-contract/test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const sinon = require('sinon');
const sinonChai = require('sinon-chai');

const { expect, use } = require('chai');
const dirtyChai = require('dirty-chai');

const {
default: loadWasmDpp,
} = require('@dashevo/wasm-dpp');

use(dirtyChai);
use(sinonChai);

exports.mochaHooks = {
beforeAll: loadWasmDpp,

beforeEach() {
if (!this.sinon) {
this.sinon = sinon.createSandbox();
} else {
this.sinon.restore();
}
},

afterEach() {
this.sinon.restore();
},
};

global.expect = expect;
Loading

0 comments on commit bfbce84

Please sign in to comment.