Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine)!: publish template as substate #1214

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4d181b7
impl in progress
ksrichard Nov 29, 2024
a0c833d
impl in progress
ksrichard Nov 29, 2024
bbf020d
impl done
ksrichard Dec 2, 2024
1bea85d
fix test
ksrichard Dec 2, 2024
62e6f99
format
ksrichard Dec 2, 2024
e20f4bf
added new publish template substate
ksrichard Dec 3, 2024
5778757
impl in progress
ksrichard Dec 4, 2024
7f20941
impl in progress
ksrichard Dec 5, 2024
00c3ef0
Merge remote-tracking branch 'origin/development' into feature/publis…
ksrichard Dec 5, 2024
e0de43a
impl in progress
ksrichard Dec 5, 2024
fe9807b
impl in progress
ksrichard Dec 5, 2024
0450d49
updating templates db almost done
ksrichard Dec 5, 2024
7b3a6b4
Merge remote-tracking branch 'origin/development' into feature/publis…
ksrichard Dec 6, 2024
a7b90b5
impl in progress
ksrichard Dec 6, 2024
8e29347
renamed PublishedTemplate substate to Template and almost done adding…
ksrichard Dec 9, 2024
9cb0766
Template publishing implemented, new templates are represented as sub…
ksrichard Dec 10, 2024
94598d0
Merge remote-tracking branch 'origin/development' into feature/publis…
ksrichard Dec 10, 2024
fb1bf11
small update
ksrichard Dec 10, 2024
b752270
CI fixes
ksrichard Dec 11, 2024
29436dd
Merge remote-tracking branch 'origin/development' into feature/publis…
ksrichard Dec 11, 2024
cbaef1b
small fixes
ksrichard Dec 11, 2024
29f1fde
small fix
ksrichard Dec 11, 2024
63a531b
small fix
ksrichard Dec 11, 2024
40d9936
Merge remote-tracking branch 'origin/development' into feature/publis…
ksrichard Dec 11, 2024
1e949ed
small fixes
ksrichard Dec 11, 2024
716d6aa
removed unused import
ksrichard Dec 11, 2024
221ca6c
clippy + bug fix + minor code cleanup + reduce mem usage
sdbondi Dec 12, 2024
890188f
fix unncessary heap allocations in engine
sdbondi Dec 12, 2024
83f117c
support for base layer registration
ksrichard Dec 12, 2024
d8c90de
clippy fixes
ksrichard Dec 12, 2024
6ac8a73
Merge branch 'development' into feature/publish-template-as-substate
ksrichard Dec 12, 2024
1ece847
small fix
ksrichard Dec 12, 2024
6dab228
Merge remote-tracking branch 'origin/feature/publish-template-as-subs…
ksrichard Dec 12, 2024
07f8895
fixed auto registration of local templates
ksrichard Dec 12, 2024
1076659
fix clippy
sdbondi Dec 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions applications/tari_dan_app_utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ tokio = { workspace = true, features = [
] }
tokio-stream = { workspace = true, features = ["sync"] }
config = { workspace = true }
url = { workspace = true }
93 changes: 51 additions & 42 deletions applications/tari_dan_app_utilities/src/base_layer_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use std::time::Duration;

use log::*;
use reqwest::Url;
use tari_base_node_client::{
grpc::GrpcBaseNodeClient,
types::{BaseLayerMetadata, BlockInfo},
Expand Down Expand Up @@ -61,45 +62,47 @@ use tari_dan_storage::{
StorageError,
};
use tari_dan_storage_sqlite::{error::SqliteStorageError, global::SqliteGlobalDbAdapter};
use tari_engine_types::{confidential::UnclaimedConfidentialOutput, substate::SubstateId};
use tari_engine_types::{confidential::UnclaimedConfidentialOutput, substate::SubstateId, TemplateAddress};
use tari_epoch_manager::{base_layer::EpochManagerHandle, EpochManagerError, EpochManagerReader};
use tari_shutdown::ShutdownSignal;
use tari_state_store_sqlite::SqliteStateStore;
use tari_template_lib::models::{EncryptedData, TemplateAddress, UnclaimedConfidentialOutputAddress};
use tari_template_lib::models::{EncryptedData, UnclaimedConfidentialOutputAddress};
use tokio::{task, task::JoinHandle, time};
use url::ParseError;

use crate::template_manager::interface::{TemplateManagerError, TemplateManagerHandle, TemplateRegistration};
use crate::template_manager::interface::{TemplateExecutable, TemplateManagerError, TemplateManagerHandle};

const LOG_TARGET: &str = "tari::dan::base_layer_scanner";

pub fn spawn<TAddr: NodeAddressable + 'static>(
global_db: GlobalDb<SqliteGlobalDbAdapter<TAddr>>,
base_node_client: GrpcBaseNodeClient,
epoch_manager: EpochManagerHandle<TAddr>,
template_manager: TemplateManagerHandle,
shutdown: ShutdownSignal,
consensus_constants: ConsensusConstants,
shard_store: SqliteStateStore<TAddr>,
scan_base_layer: bool,
base_layer_scanning_interval: Duration,
validator_node_sidechain_id: Option<RistrettoPublicKey>,
template_sidechain_id: Option<RistrettoPublicKey>,
burnt_utxo_sidechain_id: Option<RistrettoPublicKey>,
// TODO: remove when base layer template registration is removed too
template_manager: TemplateManagerHandle,
template_sidechain_id: Option<PublicKey>,
) -> JoinHandle<anyhow::Result<()>> {
task::spawn(async move {
let base_layer_scanner = BaseLayerScanner::new(
global_db,
base_node_client,
epoch_manager,
template_manager,
shutdown,
consensus_constants,
shard_store,
scan_base_layer,
base_layer_scanning_interval,
validator_node_sidechain_id,
template_sidechain_id,
burnt_utxo_sidechain_id,
template_manager,
template_sidechain_id,
);

base_layer_scanner.start().await?;
Expand All @@ -116,32 +119,33 @@ pub struct BaseLayerScanner<TAddr> {
next_block_hash: Option<FixedHash>,
base_node_client: GrpcBaseNodeClient,
epoch_manager: EpochManagerHandle<TAddr>,
template_manager: TemplateManagerHandle,
shutdown: ShutdownSignal,
consensus_constants: ConsensusConstants,
state_store: SqliteStateStore<TAddr>,
scan_base_layer: bool,
base_layer_scanning_interval: Duration,
has_attempted_scan: bool,
validator_node_sidechain_id: Option<PublicKey>,
template_sidechain_id: Option<PublicKey>,
burnt_utxo_sidechain_id: Option<PublicKey>,
// TODO: remove template related data, when removed base layer template registration support
template_manager: TemplateManagerHandle,
template_sidechain_id: Option<PublicKey>,
}

impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
pub fn new(
global_db: GlobalDb<SqliteGlobalDbAdapter<TAddr>>,
base_node_client: GrpcBaseNodeClient,
epoch_manager: EpochManagerHandle<TAddr>,
template_manager: TemplateManagerHandle,
shutdown: ShutdownSignal,
consensus_constants: ConsensusConstants,
state_store: SqliteStateStore<TAddr>,
scan_base_layer: bool,
base_layer_scanning_interval: Duration,
validator_node_sidechain_id: Option<PublicKey>,
template_sidechain_id: Option<PublicKey>,
burnt_utxo_sidechain_id: Option<PublicKey>,
template_manager: TemplateManagerHandle,
template_sidechain_id: Option<PublicKey>,
) -> Self {
Self {
global_db,
Expand All @@ -152,16 +156,16 @@ impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
next_block_hash: None,
base_node_client,
epoch_manager,
template_manager,
shutdown,
consensus_constants,
state_store,
scan_base_layer,
base_layer_scanning_interval,
has_attempted_scan: false,
validator_node_sidechain_id,
template_sidechain_id,
burnt_utxo_sidechain_id,
template_manager,
template_sidechain_id,
}
}

Expand Down Expand Up @@ -344,14 +348,14 @@ impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
if sidechain_feature.sidechain_public_key() != self.validator_node_sidechain_id.as_ref() {
debug!(
target: LOG_TARGET,
"Ignoring code template registration for sidechain ID {:?}. Local node's sidechain ID: {:?}",
"Ignoring code template registration for sidechain ID {:?}.",
sidechain_feature.sidechain_public_key(),
self.template_sidechain_id,
);
continue;
}
trace!(target: LOG_TARGET, "New validator node registration scanned: {reg:?}");
},
// TODO: remove completely SideChainFeature::CodeTemplateRegistration at some point
SideChainFeatureData::CodeTemplateRegistration(reg) => {
if sidechain_feature.sidechain_public_key() != self.template_sidechain_id.as_ref() {
debug!(
Expand Down Expand Up @@ -460,6 +464,32 @@ impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
Ok(())
}

async fn register_code_template_registration(
&mut self,
template_name: String,
template_address: TemplateAddress,
registration: CodeTemplateRegistration,
block_info: &BlockInfo,
) -> Result<(), BaseLayerScannerError> {
info!(
target: LOG_TARGET,
"🌠 new template found with address {} at height {}", template_address, block_info.height
);
self.template_manager
.add_template(
registration.author_public_key,
template_address,
TemplateExecutable::DownloadableWasm(
Url::parse(registration.binary_url.as_str())?,
registration.binary_sha,
),
Some(template_name),
)
.await?;

Ok(())
}

async fn update_validators(&mut self, epoch: Epoch) -> Result<(), BaseLayerScannerError> {
info!(
target: LOG_TARGET,
Expand Down Expand Up @@ -508,8 +538,8 @@ impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
) -> Result<(), BaseLayerScannerError> {
let commitment_address = UnclaimedConfidentialOutputAddress::try_from_commitment(output.commitment.as_bytes())
.map_err(|e|
// Technically impossible, but anyway
BaseLayerScannerError::InvalidSideChainUtxoResponse(format!("Invalid commitment: {}", e)))?;
// Technically impossible, but anyway
BaseLayerScannerError::InvalidSideChainUtxoResponse(format!("Invalid commitment: {}", e)))?;
let substate_id = SubstateId::UnclaimedConfidentialOutput(commitment_address);
let consensus_constants = self.epoch_manager.get_base_layer_consensus_constants().await?;
let epoch = consensus_constants.height_to_epoch(block_info.height);
Expand Down Expand Up @@ -605,29 +635,6 @@ impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
Ok(())
}

async fn register_code_template_registration(
&mut self,
template_name: String,
template_address: TemplateAddress,
registration: CodeTemplateRegistration,
block_info: &BlockInfo,
) -> Result<(), BaseLayerScannerError> {
info!(
target: LOG_TARGET,
"🌠 new template found with address {} at height {}", template_address, block_info.height
);
let template = TemplateRegistration {
template_name,
template_address,
registration,
mined_height: block_info.height,
mined_hash: block_info.hash,
};
self.template_manager.add_template(template).await?;

Ok(())
}

fn set_last_scanned_block(&mut self, tip: FixedHash, block_info: &BlockInfo) -> Result<(), BaseLayerScannerError> {
let mut tx = self.global_db.create_transaction()?;
let mut metadata = self.global_db.metadata(&mut tx);
Expand All @@ -652,8 +659,6 @@ pub enum BaseLayerScannerError {
SqliteStorageError(#[from] SqliteStorageError),
#[error("Epoch manager error: {0}")]
EpochManagerError(#[from] EpochManagerError),
#[error("Template manager error: {0}")]
TemplateManagerError(#[from] TemplateManagerError),
#[error("Base node client error: {0}")]
BaseNodeError(#[from] BaseNodeClientError),
#[error("Invalid side chain utxo response: {0}")]
Expand All @@ -667,6 +672,10 @@ pub enum BaseLayerScannerError {
PublicKeyConversion(ByteArrayError),
#[error("GRPC conversion error: {0}")]
GrpcConversion(String),
#[error("Template manager error: {0}")]
TemplateManagerError(#[from] TemplateManagerError),
#[error("URL parse error: {0}")]
UrlParse(#[from] ParseError),
}

enum BlockchainProgression {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
// 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.

// TODO: rewrite downloader to get template from other peer(s) OR completely drop this concept and implement somewhere
// else

use bytes::Bytes;
use futures::{future::BoxFuture, stream::FuturesUnordered};
use prost::bytes;
Expand Down
Loading
Loading