Skip to content

Commit

Permalink
feat(vn): record contract states (#4241)
Browse files Browse the repository at this point in the history
Description
---
Add a contracts table to the global vn db so we can store the state of contracts the vn is a member of.

Motivation and Context
---
It will make it easier to go back and check known contracts that are awaiting quorum acceptance.

How Has This Been Tested?
---
Manually, and by fixing the previously broken spec.

Commits
---

* Add the constitutions table to the globaldb

* Add contract saving call

* Refactor the naming from constitution to contract

* Save the contract in a pending state

* Store expired contracts

* Update contract states

* Only auto accept if the setting is set

* Make the sleep length configurable

The long sleep length was causing the specs to fail. We also don't want
to wait in minutes for polling to occur. Make the option configurable
and use a much shorter time in testing.

* Rename to state for consistency

* Fix clippy warnings

* No more lockfiles, npm install it
  • Loading branch information
brianp authored Jun 29, 2022
1 parent 34ca795 commit 92ae4ab
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dan_layer_integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
command: build
args: --release --bin tari_validator_node -Z unstable-options
- name: npm ci
run: cd integration_tests && npm ci && cd node_modules/wallet-grpc-client && npm ci
run: cd integration_tests && npm ci && cd node_modules/wallet-grpc-client && npm install
- name: Run integration tests
run: cd integration_tests && mkdir -p cucumber_output && node_modules/.bin/cucumber-js --profile "non-critical" --tags "@dan and not @broken" --format json:cucumber_output/tests.cucumber --exit --retry 2 --retry-tag-filter "@flaky and not @broken"
- name: Generate report
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion applications/tari_validator_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ pub struct ValidatorNodeConfig {
pub p2p: P2pConfig,
pub constitution_auto_accept: bool,
/// Constitution polling interval in block height
pub constitution_management_polling_interval: u64,
pub constitution_management_confirmation_time: u64,
pub constitution_management_polling_interval: u64,
pub constitution_management_polling_interval_in_seconds: u64,
pub grpc_address: Option<Multiaddr>,
}

Expand Down Expand Up @@ -116,6 +117,7 @@ impl Default for ValidatorNodeConfig {
constitution_auto_accept: false,
constitution_management_confirmation_time: 20,
constitution_management_polling_interval: 120,
constitution_management_polling_interval_in_seconds: 60,
p2p,
grpc_address: Some("/ip4/127.0.0.1/tcp/18144".parse().unwrap()),
}
Expand Down
43 changes: 31 additions & 12 deletions applications/tari_validator_node/src/contract_worker_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use tari_dan_core::{
WalletClient,
},
storage::{
global::{GlobalDb, GlobalDbMetadataKey},
global::{ContractState, GlobalDb, GlobalDbMetadataKey},
StorageError,
},
workers::ConsensusWorker,
Expand Down Expand Up @@ -134,6 +134,9 @@ impl ContractWorkerManager {
// TODO: Uncomment line to scan from previous block height once we can
// start up asset workers for existing contracts.
// self.load_initial_state()?;
if self.config.constitution_auto_accept {
info!("constitution_auto_accept is true")
}

if !self.config.scan_for_assets {
info!(
Expand All @@ -155,7 +158,7 @@ impl ContractWorkerManager {
next_scan_height
);
tokio::select! {
_ = time::sleep(Duration::from_secs(60)) => {},
_ = time::sleep(Duration::from_secs(self.config.constitution_management_polling_interval_in_seconds)) => {},
_ = &mut self.shutdown => break,
}
continue;
Expand All @@ -170,20 +173,29 @@ impl ContractWorkerManager {
info!(target: LOG_TARGET, "{} new contract(s) found", active_contracts.len());

for contract in active_contracts {
info!(
target: LOG_TARGET,
"Posting acceptance transaction for contract {}", contract.contract_id
);
self.post_contract_acceptance(&contract).await?;
// TODO: Scan for acceptances and once enough are present, start working on the contract
// for now, we start working immediately.
let kill = self.spawn_asset_worker(contract.contract_id, &contract.constitution);
self.active_workers.insert(contract.contract_id, kill);
self.global_db
.save_contract(contract.contract_id, contract.mined_height, ContractState::Pending)?;

if self.config.constitution_auto_accept {
info!(
target: LOG_TARGET,
"Posting acceptance transaction for contract {}", contract.contract_id
);
self.post_contract_acceptance(&contract).await?;

self.global_db
.update_contract_state(contract.contract_id, ContractState::Accepted)?;

// TODO: Scan for acceptances and once enough are present, start working on the contract
// for now, we start working immediately.
let kill = self.spawn_asset_worker(contract.contract_id, &contract.constitution);
self.active_workers.insert(contract.contract_id, kill);
}
}
self.set_last_scanned_block(tip)?;

tokio::select! {
_ = time::sleep(Duration::from_secs(60)) => {},
_ = time::sleep(Duration::from_secs(self.config.constitution_management_polling_interval_in_seconds)) => {},
_ = &mut self.shutdown => break,
}
}
Expand Down Expand Up @@ -234,6 +246,7 @@ impl ContractWorkerManager {
let mut new_contracts = vec![];
for utxo in outputs {
let output = some_or_continue!(utxo.output.into_unpruned_output());
let mined_height = utxo.mined_height;
let sidechain_features = some_or_continue!(output.features.sidechain_features);
let contract_id = sidechain_features.contract_id;
let constitution = some_or_continue!(sidechain_features.constitution);
Expand All @@ -258,12 +271,17 @@ impl ContractWorkerManager {
constitution.acceptance_requirements.acceptance_period_expiry,
tip.height_of_longest_chain
);

self.global_db
.save_contract(contract_id, mined_height, ContractState::Expired)?;

continue;
}

new_contracts.push(ActiveContract {
contract_id,
constitution,
mined_height,
});
}

Expand Down Expand Up @@ -435,4 +453,5 @@ pub enum WorkerManagerError {
struct ActiveContract {
pub contract_id: FixedHash,
pub constitution: ContractConstitution,
pub mined_height: u64,
}
4 changes: 3 additions & 1 deletion dan_layer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ blake2 = "0.9.2"
clap = "3.1.8"
digest = "0.9.0"
futures = { version = "^0.3.1" }
log = { version = "0.4.8", features = ["std"] }
lmdb-zero = "0.4.4"
log = { version = "0.4.8", features = ["std"] }
num-derive = "0.3.3"
num-traits = "0.2.15"
prost = "0.9"
prost-types = "0.9"
rand = "0.8.4"
Expand Down
21 changes: 20 additions & 1 deletion dan_layer/core/src/storage/global/global_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

use std::sync::Arc;

use tari_common_types::types::FixedHash;

use crate::storage::{
global::{GlobalDbBackendAdapter, GlobalDbMetadataKey},
global::{ContractState, GlobalDbBackendAdapter, GlobalDbMetadataKey},
StorageError,
};

Expand All @@ -48,4 +50,21 @@ impl<TGlobalDbBackendAdapter: GlobalDbBackendAdapter> GlobalDb<TGlobalDbBackendA
pub fn get_data(&self, key: GlobalDbMetadataKey) -> Result<Option<Vec<u8>>, StorageError> {
self.adapter.get_data(key).map_err(TGlobalDbBackendAdapter::Error::into)
}

pub fn save_contract(
&self,
contract_id: FixedHash,
mined_height: u64,
state: ContractState,
) -> Result<(), StorageError> {
self.adapter
.save_contract(contract_id, mined_height, state)
.map_err(TGlobalDbBackendAdapter::Error::into)
}

pub fn update_contract_state(&self, contract_id: FixedHash, state: ContractState) -> Result<(), StorageError> {
self.adapter
.update_contract_state(contract_id, state)
.map_err(TGlobalDbBackendAdapter::Error::into)
}
}
26 changes: 26 additions & 0 deletions dan_layer/core/src/storage/global/global_db_backend_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use tari_common_types::types::FixedHash;

use crate::storage::StorageError;

pub trait GlobalDbBackendAdapter: Send + Sync + Clone {
Expand All @@ -35,6 +39,9 @@ pub trait GlobalDbBackendAdapter: Send + Sync + Clone {
key: &GlobalDbMetadataKey,
connection: &Self::BackendTransaction,
) -> Result<Option<Vec<u8>>, Self::Error>;
fn save_contract(&self, contract_id: FixedHash, mined_height: u64, state: ContractState)
-> Result<(), Self::Error>;
fn update_contract_state(&self, contract_id: FixedHash, state: ContractState) -> Result<(), Self::Error>;
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -51,3 +58,22 @@ impl GlobalDbMetadataKey {
}
}
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, FromPrimitive)]
#[repr(u8)]
pub enum ContractState {
Pending = 0,
Accepted = 1,
Expired = 2,
}

impl ContractState {
pub fn as_byte(self) -> u8 {
self as u8
}

/// Returns the Status that corresponds to the byte. None is returned if the byte does not correspond
pub fn from_byte(value: u8) -> Option<Self> {
FromPrimitive::from_u8(value)
}
}
2 changes: 1 addition & 1 deletion dan_layer/core/src/storage/global/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ mod global_db;
pub use global_db::GlobalDb;

mod global_db_backend_adapter;
pub use global_db_backend_adapter::{GlobalDbBackendAdapter, GlobalDbMetadataKey};
pub use global_db_backend_adapter::{ContractState, GlobalDbBackendAdapter, GlobalDbMetadataKey};
17 changes: 16 additions & 1 deletion dan_layer/core/src/storage/mocks/global_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use tari_common_types::types::FixedHash;

use crate::storage::{
global::{GlobalDbBackendAdapter, GlobalDbMetadataKey},
global::{ContractState, GlobalDbBackendAdapter, GlobalDbMetadataKey},
StorageError,
};

Expand Down Expand Up @@ -55,4 +57,17 @@ impl GlobalDbBackendAdapter for MockGlobalDbBackupAdapter {
) -> Result<Option<Vec<u8>>, Self::Error> {
todo!()
}

fn save_contract(
&self,
_contract_id: FixedHash,
_mined_height: u64,
_status: ContractState,
) -> Result<(), Self::Error> {
todo!()
}

fn update_contract_state(&self, _contract_id: FixedHash, _state: ContractState) -> Result<(), Self::Error> {
todo!()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- // Copyright 2022. The Tari Project
-- //
-- // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
-- // following conditions are met:
-- //
-- // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
-- // disclaimer.
-- //
-- // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
-- // following disclaimer in the documentation and/or other materials provided with the distribution.
-- //
-- // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
-- // products derived from this software without specific prior written permission.
-- //
-- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
-- // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-- // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-- // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-- // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
-- // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- // Copyright 2022. The Tari Project
-- //
-- // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
-- // following conditions are met:
-- //
-- // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
-- // disclaimer.
-- //
-- // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
-- // following disclaimer in the documentation and/or other materials provided with the distribution.
-- //
-- // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
-- // products derived from this software without specific prior written permission.
-- //
-- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
-- // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-- // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-- // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-- // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
-- // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

create table contracts (
id blob primary key not null,
height bigint not null,
state integer not null
);

create index contracts_state_index on contracts (state);
30 changes: 30 additions & 0 deletions dan_layer/storage_sqlite/src/global/models/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::global::schema::*;

#[derive(Queryable, Insertable, Identifiable)]
pub struct Contract {
pub id: Vec<u8>,
pub state: i32,
pub height: i64,
}
1 change: 1 addition & 0 deletions dan_layer/storage_sqlite/src/global/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pub mod contract;
pub mod metadata;
10 changes: 10 additions & 0 deletions dan_layer/storage_sqlite/src/global/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

table! {
contracts (id) {
id -> Binary,
height -> BigInt,
state -> Integer,
}
}

table! {
metadata (key_name) {
key_name -> Binary,
value -> Binary,
}
}

allow_tables_to_appear_in_same_query!(contracts, metadata,);
Loading

0 comments on commit 92ae4ab

Please sign in to comment.