mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-09 14:48:08 -05:00
runtime: Cleanup and add payload support to init()
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
use darkfi_sdk::{
|
||||
crypto::Nullifier,
|
||||
db::{db_init, db_lookup, db_get, db_begin_tx, db_set, db_end_tx},
|
||||
db::{db_begin_tx, db_end_tx, db_get, db_init, db_lookup, db_set},
|
||||
entrypoint,
|
||||
error::{ContractError, ContractResult},
|
||||
error::ContractResult,
|
||||
initialize, msg,
|
||||
pasta::pallas,
|
||||
state::{nullifier_exists, set_update},
|
||||
state::set_update,
|
||||
tx::Transaction,
|
||||
update_state,
|
||||
};
|
||||
use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable, ReadExt, Decodable};
|
||||
use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable};
|
||||
|
||||
/// Available functions for this contract.
|
||||
/// We identify them with the first byte passed in through the payload.
|
||||
@@ -48,7 +46,7 @@ pub struct FooUpdate {
|
||||
}
|
||||
|
||||
initialize!(init_contract);
|
||||
fn init_contract() -> ContractResult {
|
||||
fn init_contract(_ix: &[u8]) -> ContractResult {
|
||||
msg!("wakeup wagies!");
|
||||
db_init("wagies")?;
|
||||
|
||||
@@ -73,7 +71,8 @@ fn process_instruction(ix: &[u8]) -> ContractResult {
|
||||
let tx_data = &ix[1..];
|
||||
// ...
|
||||
let (func_call_index, tx): (u32, Transaction) = deserialize(tx_data)?;
|
||||
let call_data: FooCallData = deserialize(&tx.func_calls[func_call_index as usize].call_data)?;
|
||||
let call_data: FooCallData =
|
||||
deserialize(&tx.func_calls[func_call_index as usize].call_data)?;
|
||||
msg!("call_data {{ a: {}, b: {} }}", call_data.a, call_data.b);
|
||||
// ...
|
||||
let update = FooUpdate { name: "john_doe".to_string(), age: 110 };
|
||||
|
||||
@@ -16,15 +16,14 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use darkfi::{
|
||||
crypto::contract_id::ContractId,
|
||||
runtime::{util::serialize_payload, vm_runtime::Runtime},
|
||||
Result,
|
||||
use darkfi::{crypto::contract_id::ContractId, runtime::vm_runtime::Runtime, Result};
|
||||
use darkfi_sdk::{
|
||||
pasta::pallas,
|
||||
tx::{FuncCall, Transaction},
|
||||
};
|
||||
use darkfi_sdk::{crypto::nullifier::Nullifier, pasta::pallas, tx::{Transaction, FuncCall}};
|
||||
use darkfi_serial::{serialize, Encodable, WriteExt};
|
||||
|
||||
use smart_contract::FooCallData;
|
||||
use smart_contract::{FooCallData, Function};
|
||||
|
||||
#[test]
|
||||
fn run_contract() -> Result<()> {
|
||||
@@ -52,43 +51,40 @@ fn run_contract() -> Result<()> {
|
||||
let contract_id = ContractId::new(pallas::Base::from(1));
|
||||
let mut runtime = Runtime::new(&wasm_bytes, contract_id)?;
|
||||
|
||||
runtime.deploy()?;
|
||||
// Deploy function to initialize the smart contract state.
|
||||
// Here we pass an empty payload, but it's possible to feed in arbitrary data.
|
||||
runtime.deploy(&[])?;
|
||||
|
||||
// =============================================
|
||||
// Build some kind of payload to show an example
|
||||
// =============================================
|
||||
let tx = Transaction {
|
||||
func_calls: vec![
|
||||
FuncCall {
|
||||
contract_id: pallas::Base::from(110),
|
||||
func_id: pallas::Base::from(4),
|
||||
call_data: serialize(&FooCallData { a: 777, b: 666 }),
|
||||
proofs: Vec::new()
|
||||
}
|
||||
],
|
||||
signatures: Vec::new()
|
||||
func_calls: vec![FuncCall {
|
||||
contract_id: pallas::Base::from(110),
|
||||
func_id: pallas::Base::from(4),
|
||||
call_data: serialize(&FooCallData { a: 777, b: 666 }),
|
||||
proofs: Vec::new(),
|
||||
}],
|
||||
signatures: Vec::new(),
|
||||
};
|
||||
let func_call_index: u32 = 0;
|
||||
|
||||
let mut payload = Vec::new();
|
||||
// Prepend the func id = 0x00
|
||||
// Selects which path executes in the contract.
|
||||
payload.write_u8(0x00);
|
||||
payload.write_u8(Function::Foo as u8)?;
|
||||
// Write the actual payload data
|
||||
payload.write_u32(func_call_index);
|
||||
payload.write_u32(func_call_index)?;
|
||||
tx.encode(&mut payload)?;
|
||||
|
||||
// ============================================================
|
||||
// Serialize the payload into the runtime format and execute it
|
||||
// ============================================================
|
||||
runtime.exec(&serialize_payload(&payload))?;
|
||||
runtime.exec(&payload)?;
|
||||
|
||||
// =====================================================
|
||||
// If exec was successful, try to apply the state change
|
||||
// =====================================================
|
||||
runtime.apply()?;
|
||||
//Ok(())
|
||||
|
||||
//runtime.exec(&serialize_payload(&payload))?;
|
||||
|
||||
//runtime.apply()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user