Skip to content

Commit

Permalink
feat!: add testnet faucet component, remove create free test coins
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Jul 17, 2024
1 parent 6f5df88 commit ebb7b2c
Show file tree
Hide file tree
Showing 67 changed files with 877 additions and 558 deletions.
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.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ use tari_dan_engine::{
use tari_dan_storage::global::{DbTemplate, DbTemplateType, DbTemplateUpdate, GlobalDb, TemplateStatus};
use tari_dan_storage_sqlite::global::SqliteGlobalDbAdapter;
use tari_engine_types::calculate_template_binary_hash;
use tari_template_builtin::{get_template_builtin, ACCOUNT_NFT_TEMPLATE_ADDRESS, ACCOUNT_TEMPLATE_ADDRESS};
use tari_template_builtin::{
get_template_builtin,
ACCOUNT_NFT_TEMPLATE_ADDRESS,
ACCOUNT_TEMPLATE_ADDRESS,
FAUCET_TEMPLATE_ADDRESS,
};
use tari_template_lib::models::TemplateAddress;

use super::TemplateConfig;
Expand Down Expand Up @@ -90,36 +95,36 @@ impl<TAddr: NodeAddressable> TemplateManager<TAddr> {

fn load_builtin_templates() -> HashMap<TemplateAddress, Template> {
// for now, we only load the "account" template
let mut builtin_templates = HashMap::new();
let mut builtin_templates = HashMap::with_capacity(3);

// get the builtin WASM code of the account template
let compiled_code = get_template_builtin(&ACCOUNT_TEMPLATE_ADDRESS);
let template = Self::load_builtin_template("account", ACCOUNT_TEMPLATE_ADDRESS, compiled_code.to_vec());
let template = Self::convert_code_to_template("Account", ACCOUNT_TEMPLATE_ADDRESS, compiled_code.to_vec());
builtin_templates.insert(ACCOUNT_TEMPLATE_ADDRESS, template);

// get the builtin WASM code of the account nft template
let compiled_code = get_template_builtin(&ACCOUNT_NFT_TEMPLATE_ADDRESS);
let template = Self::load_builtin_template("account_nft", ACCOUNT_NFT_TEMPLATE_ADDRESS, compiled_code.to_vec());
let template =
Self::convert_code_to_template("AccountNft", ACCOUNT_NFT_TEMPLATE_ADDRESS, compiled_code.to_vec());
builtin_templates.insert(ACCOUNT_NFT_TEMPLATE_ADDRESS, template);

// get the builtin WASM code of the account nft template
let compiled_code = get_template_builtin(&FAUCET_TEMPLATE_ADDRESS);
let template = Self::convert_code_to_template("XtrFaucet", FAUCET_TEMPLATE_ADDRESS, compiled_code.to_vec());
builtin_templates.insert(FAUCET_TEMPLATE_ADDRESS, template);

builtin_templates
}

fn load_builtin_template(name: &str, address: TemplateAddress, compiled_code: Vec<u8>) -> Template {
let compiled_code_len = compiled_code.len();
info!(
target: LOG_TARGET,
"Loading builtin {} template: {} bytes", name, compiled_code_len
);

fn convert_code_to_template(name: &str, address: TemplateAddress, compiled_code: Vec<u8>) -> Template {
// build the template object of the account template
let binary_sha = calculate_template_binary_hash(&compiled_code);
Template {
metadata: TemplateMetadata {
name: name.to_string(),
address,
url: "".to_string(),
binary_sha: binary_sha.to_vec(),
binary_sha,
height: 0,
},
executable: TemplateExecutable::CompiledWasm(compiled_code),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// 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 tari_dan_storage::global::{DbTemplate, DbTemplateType};
use tari_template_lib::models::TemplateAddress;
use tari_validator_node_client::types::TemplateAbi;
Expand All @@ -34,7 +35,7 @@ pub struct TemplateMetadata {
// this must be in the form of "https://example.com/my_template.wasm"
pub url: String,
/// SHA hash of binary
pub binary_sha: Vec<u8>,
pub binary_sha: FixedHash,
/// Block height in which the template was published
pub height: u64,
}
Expand All @@ -45,7 +46,9 @@ impl From<TemplateRegistration> for TemplateMetadata {
name: reg.template_name,
address: reg.template_address,
url: reg.registration.binary_url.into_string(),
binary_sha: reg.registration.binary_sha.into_vec(),
binary_sha: FixedHash::try_from(reg.registration.binary_sha.into_vec())
// TODO: impl Fallible conversion
.expect("binary_sha must be 32 bytes long"),
height: reg.mined_height,
}
}
Expand All @@ -58,7 +61,7 @@ impl From<DbTemplate> for TemplateMetadata {
name: record.template_name,
address: (*record.template_address).into(),
url: record.url,
binary_sha: vec![],
binary_sha: FixedHash::zero(),
height: record.height,
}
}
Expand Down Expand Up @@ -87,7 +90,7 @@ impl From<DbTemplate> for Template {
address: (*record.template_address).into(),
url: record.url,
// TODO: add field to db
binary_sha: vec![],
binary_sha: FixedHash::zero(),
height: record.height,
},
executable: match record.template_type {
Expand Down
58 changes: 23 additions & 35 deletions applications/tari_dan_wallet_daemon/src/handlers/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tari_dan_common_types::optional::Optional;
use tari_dan_wallet_crypto::ConfidentialProofStatement;
use tari_dan_wallet_sdk::{
apis::{confidential_transfer::TransferParams, jwt::JrpcPermission, key_manager, substate::ValidatorScanResult},
models::{NewAccountInfo, VersionedSubstateId},
models::NewAccountInfo,
storage::WalletStore,
DanWalletSdk,
};
Expand All @@ -32,6 +32,7 @@ use tari_key_manager::key_manager::DerivedKey;
use tari_template_builtin::ACCOUNT_TEMPLATE_ADDRESS;
use tari_template_lib::{
args,
constants::{XTR, XTR_FAUCET_COMPONENT_ADDRESS, XTR_FAUCET_VAULT_ADDRESS},
models::{Amount, UnclaimedConfidentialOutputAddress},
prelude::CONFIDENTIAL_TARI_RESOURCE_ADDRESS,
};
Expand Down Expand Up @@ -101,10 +102,11 @@ pub async fn handle_create(
}

let default_account = sdk.accounts_api().get_default()?;
let inputs = sdk
let mut inputs = sdk
.substate_api()
.locate_dependent_substates(&[default_account.address.clone()])
.await?;
inputs.push(SubstateRequirement::unversioned(XTR));

let signing_key_index = req.key_id.unwrap_or(default_account.key_index);
let signing_key = key_manager_api.derive_key(key_manager::TRANSACTION_BRANCH, signing_key_index)?;
Expand All @@ -124,11 +126,7 @@ pub async fn handle_create(
let transaction = Transaction::builder()
.fee_transaction_pay_from_component(default_account.address.as_component_address().unwrap(), max_fee)
.create_account(owner_pk.clone())
.with_inputs(
inputs
.iter()
.map(|addr| SubstateRequirement::new(addr.substate_id.clone(), Some(addr.version))),
)
.with_inputs(inputs)
.sign(&signing_key.key)
.build();

Expand Down Expand Up @@ -541,12 +539,8 @@ pub async fn handle_claim_burn(

// Add all versioned account child addresses as inputs
// add the commitment substate id as input to the claim burn transaction
let commitment_substate_address = VersionedSubstateId {
substate_id: SubstateId::UnclaimedConfidentialOutput(UnclaimedConfidentialOutputAddress::try_from(
commitment.as_slice(),
)?),
version: 0,
};
let commitment_substate_address =
SubstateRequirement::unversioned(UnclaimedConfidentialOutputAddress::try_from(commitment.as_slice())?);
inputs.push(commitment_substate_address.clone());

info!(
Expand All @@ -561,7 +555,7 @@ pub async fn handle_claim_burn(
.substate_api()
.scan_for_substate(
&commitment_substate_address.substate_id,
Some(commitment_substate_address.version),
commitment_substate_address.version,
)
.await?;
let output = output.into_unclaimed_confidential_output().unwrap();
Expand Down Expand Up @@ -648,7 +642,7 @@ async fn finish_claiming<T: WalletStore>(
account_address: SubstateId,
new_account_name: Option<String>,
sdk: &DanWalletSdk<SqliteWalletStore, IndexerJsonRpcNetworkInterface>,
mut inputs: Vec<VersionedSubstateId>,
mut inputs: Vec<SubstateRequirement>,
account_public_key: &RistrettoPublicKey,
max_fee: Amount,
account_secret_key: DerivedKey<RistrettoPublicKey>,
Expand All @@ -670,7 +664,7 @@ async fn finish_claiming<T: WalletStore>(
if new_account_name.is_none() {
// Add all versioned account child addresses as inputs unless the account is new
let child_addresses = sdk.substate_api().load_dependent_substates(&[&account_address])?;
inputs.extend(child_addresses);
inputs.extend(child_addresses.into_iter().map(Into::into));
instructions.push(Instruction::CallMethod {
component_address: account_component_address,
method: "deposit".to_string(),
Expand All @@ -687,9 +681,6 @@ async fn finish_claiming<T: WalletStore>(
method: "pay_fee".to_string(),
args: args![max_fee],
});
let inputs = inputs
.into_iter()
.map(|s| SubstateRequirement::new(s.substate_id.clone(), Some(s.version)));
let transaction = Transaction::builder()
.with_fee_instructions(instructions)
.with_inputs(inputs)
Expand Down Expand Up @@ -727,7 +718,6 @@ async fn finish_claiming<T: WalletStore>(
}

/// Mints free test coins into an account. If an account name is provided which does not exist, that account is created
#[allow(clippy::too_many_lines)]
pub async fn handle_create_free_test_coins(
context: &HandlerContext,
token: Option<String>,
Expand All @@ -748,24 +738,22 @@ pub async fn handle_create_free_test_coins(
return Err(invalid_params("fee", Some("cannot be negative")));
}

let mut inputs = vec![];
let mut inputs = vec![
SubstateRequirement::unversioned(XTR),
SubstateRequirement::unversioned(XTR_FAUCET_COMPONENT_ADDRESS),
SubstateRequirement::unversioned(XTR_FAUCET_VAULT_ADDRESS),
];
let accounts_api = sdk.accounts_api();
let (account_address, account_secret_key, new_account_name) =
get_or_create_account(&account, &accounts_api, key_id, sdk, &mut inputs)?;

let account_public_key = PublicKey::from_secret_key(&account_secret_key.key);
let output = sdk
.confidential_crypto_api()
.generate_output_for_dest(&account_public_key, amount)?;

let instructions = vec![
// TODO: We create double what is expected, amount confidential and amount revealed. Should let the caller
// specify these values separately.
Instruction::CreateFreeTestCoins {
revealed_amount: amount,
output: Some(output),
},
];

let instructions = vec![Instruction::CallMethod {
component_address: XTR_FAUCET_COMPONENT_ADDRESS,
method: "take".to_string(),
args: args![amount],
}];

// ------------------------------
let (tx_id, finalized) = finish_claiming(
Expand Down Expand Up @@ -799,7 +787,7 @@ fn get_or_create_account<T: WalletStore>(
accounts_api: &tari_dan_wallet_sdk::apis::accounts::AccountsApi<'_, T>,
key_id: Option<u64>,
sdk: &DanWalletSdk<SqliteWalletStore, IndexerJsonRpcNetworkInterface>,
inputs: &mut Vec<VersionedSubstateId>,
inputs: &mut Vec<SubstateRequirement>,
) -> Result<(SubstateId, DerivedKey<RistrettoPublicKey>, Option<String>), anyhow::Error> {
let maybe_account = match account {
Some(ref addr_or_name) => get_account(addr_or_name, accounts_api).optional()?,
Expand All @@ -819,7 +807,7 @@ fn get_or_create_account<T: WalletStore>(
.key_manager_api()
.derive_key(key_manager::TRANSACTION_BRANCH, key_index)?;
let account_substate = sdk.substate_api().get_substate(&account.address)?;
inputs.push(account_substate.address);
inputs.push(account_substate.address.into());

(account.address, account_secret_key, None)
},
Expand Down
9 changes: 1 addition & 8 deletions applications/tari_dan_wallet_daemon/src/handlers/nfts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,11 @@ async fn mint_account_nft(
let sdk = context.wallet_sdk();
sdk.jwt_api().check_auth(token, &[JrpcPermission::Admin])?;

let inputs = sdk
let mut inputs = sdk
.substate_api()
.locate_dependent_substates(&[account.address.clone()])
.await?;

let mut inputs = inputs
.iter()
.map(|v| SubstateRequirement::new(v.substate_id.clone(), Some(v.version)))
.collect::<Vec<_>>();
inputs.extend([SubstateRequirement::new(SubstateId::Component(component_address), None)]);

let instructions = vec![
Expand Down Expand Up @@ -252,9 +248,6 @@ async fn create_account_nft(
.substate_api()
.locate_dependent_substates(&[account.address.clone()])
.await?;
let inputs = inputs
.iter()
.map(|addr| SubstateRequirement::new(addr.substate_id.clone(), Some(addr.version)));

let transaction = Transaction::builder()
.fee_transaction_pay_from_component(account.address.as_component_address().unwrap(), fee)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,7 @@ pub async fn handle_submit(
.unwrap_or(&req.fee_instructions),
)?);
let substates = substates.into_iter().collect::<Vec<_>>();
let loaded_dependent_substates = sdk
.substate_api()
.locate_dependent_substates(&substates)
.await?
.into_iter()
.map(Into::into)
.collect();
let loaded_dependent_substates = sdk.substate_api().locate_dependent_substates(&substates).await?;
[req.inputs, loaded_dependent_substates].concat()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function ActionMenu() {
const onClaimFreeCoins = () => {
mutate({
accountName: accountName,
amount: 100000,
amount: 200000,
fee: 1000,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Onboarding() {
mutate(
{
accountName: accountFormState.accountName,
amount: 100000,
amount: 200000,
fee: 1000,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function Accounts() {
const onClaimFreeCoins = async () => {
await mutateCreateFeeTestCoins({
accountName: "TestAccount",
amount: 100000,
amount: 200000,
fee: 1000,
});
};
Expand Down
12 changes: 2 additions & 10 deletions applications/tari_indexer/src/dry_run/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ use tari_dan_app_utilities::{
transaction_executor::{TariDanTransactionProcessor, TransactionExecutor as _},
};
use tari_dan_common_types::{Epoch, PeerAddress, SubstateAddress};
use tari_dan_engine::{
bootstrap_state,
fees::FeeTable,
state_store::{memory::MemoryStateStore, AtomicDb, StateWriter},
};
use tari_dan_engine::{fees::FeeTable, state_store::memory::MemoryStateStore};
use tari_engine_types::{
commit_result::ExecuteResult,
instruction::Instruction,
Expand Down Expand Up @@ -273,9 +269,5 @@ where TSubstateCache: SubstateCache + 'static
}

fn new_state_store() -> MemoryStateStore {
let state_store = MemoryStateStore::new();
let mut tx = state_store.write_access().unwrap();
bootstrap_state(&mut tx).unwrap();
tx.commit().unwrap();
state_store
MemoryStateStore::new()
}
2 changes: 1 addition & 1 deletion applications/tari_indexer/src/json_rpc/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ impl JsonRpcHandlers {
name: t.name,
address: t.address,
url: t.url,
binary_sha: to_hex(&t.binary_sha),
binary_sha: to_hex(t.binary_sha.as_slice()),
height: t.height,
})
.collect(),
Expand Down
4 changes: 2 additions & 2 deletions applications/tari_swarm_daemon/webui/src/routes/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ function ExtraInfoVN({ name, url, setRow, addTxToPool, autoRefresh, state, horiz
<td>Decision</td>
<td>Stage</td>
</tr>
{pool.map(({ atom }, i) => (
{pool.map((atom, i) => (
<tr key={i}>
<td
onClick={() => copyToClipboard(atom.id)}>{copied && "Copied" || shorten(atom.id)}</td>
onClick={() => copyToClipboard(atom.transaction_id)}>{copied && "Copied" || shorten(atom.transaction_id)}</td>
<td>{atom.is_ready && "Yes" || "No"}</td>
<td>{atom.decision || "_"}</td>
<td>{atom.stage}</td>
Expand Down
Loading

0 comments on commit ebb7b2c

Please sign in to comment.