dao2: get imports and running contract skeleton working

This commit is contained in:
x
2022-11-06 11:20:28 +00:00
parent 83670232e0
commit c9d55c1bcb
5 changed files with 3073 additions and 2 deletions

2930
example/dao2/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -14,3 +14,8 @@ members = [
[dependencies]
dao-contract = {path = "contract/dao"}
money-contract = {path = "contract/money"}
darkfi-sdk = { path = "../../src/sdk" }
darkfi-serial = { path = "../../src/serial" }
darkfi = { path = "../../", features = ["wasm-runtime"] }
simplelog = "0.12.0"
sled = "0.34.7"

View File

@@ -15,6 +15,7 @@ WASM_BIN = \
BIN = dao
all: $(WASM_BIN) $(BIN)
@./dao
dao: $(WASM_SRC) $(SRC)
$(CARGO) build --release --bin $@

View File

@@ -1,3 +1,15 @@
use darkfi_sdk::{
crypto::ContractId,
db::{db_get, db_init, db_lookup, db_set},
define_contract,
error::ContractResult,
msg,
pasta::pallas,
tx::FuncCall,
util::{set_return_data, put_object_bytes, get_object_bytes, get_object_size},
};
use darkfi_serial::{deserialize, serialize, Encodable, SerialDecodable, SerialEncodable, WriteExt, ReadExt};
#[repr(u8)]
pub enum DaoFunction {
Foo = 0x00,
@@ -6,3 +18,49 @@ pub enum DaoFunction {
fn foo() {
println!("foo");
}
define_contract!(
init: init_contract,
exec: process_instruction,
apply: process_update,
metadata: get_metadata
);
fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
Ok(())
}
fn get_metadata(_cid: ContractId, ix: &[u8]) -> ContractResult {
let zk_public_values = vec![
(
"DaoProposeInput".to_string(),
vec![pallas::Base::from(110), pallas::Base::from(4)],
),
("DaoProposeInput".to_string(), vec![pallas::Base::from(7), pallas::Base::from(4)]),
(
"DaoProposeMain".to_string(),
vec![
pallas::Base::from(1),
pallas::Base::from(3),
pallas::Base::from(5),
pallas::Base::from(7),
],
),
];
let signature_public_keys: Vec<pallas::Point> = vec![
//pallas::Point::identity()
];
let mut metadata = Vec::new();
zk_public_values.encode(&mut metadata)?;
signature_public_keys.encode(&mut metadata)?;
set_return_data(&metadata)?;
Ok(())
}
fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
Ok(())
}
fn process_update(_cid: ContractId, update_data: &[u8]) -> ContractResult {
Ok(())
}

View File

@@ -1,5 +1,84 @@
use darkfi::{
blockchain::Blockchain,
consensus::{TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
runtime::vm_runtime::Runtime,
Result,
};
use darkfi_sdk::{crypto::ContractId, pasta::pallas, tx::FuncCall};
use darkfi_serial::{serialize, Decodable, Encodable, WriteExt};
use std::io::Cursor;
use dao_contract::DaoFunction;
fn main() {
fn main() -> Result<()> {
println!("wakie wakie young wagie");
// Debug log configuration
let mut cfg = simplelog::ConfigBuilder::new();
cfg.add_filter_ignore("sled".to_string());
simplelog::TermLogger::init(
simplelog::LevelFilter::Debug,
cfg.build(),
simplelog::TerminalMode::Mixed,
simplelog::ColorChoice::Auto,
)?;
// =============================
// Initialize a dummy blockchain
// =============================
// TODO: This blockchain interface should perhaps be ValidatorState and Mutex/RwLock.
let db = sled::Config::new().temporary(true).open()?;
let blockchain = Blockchain::new(&db, *TESTNET_GENESIS_TIMESTAMP, *TESTNET_GENESIS_HASH_BYTES)?;
// ================================================================
// Load the wasm binary into memory and create an execution runtime
// ================================================================
let wasm_bytes = std::fs::read("dao_contract.wasm")?;
let contract_id = ContractId::from(pallas::Base::from(1));
let mut runtime = Runtime::new(&wasm_bytes, blockchain.clone(), contract_id)?;
// 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(&[])?;
// This is another call so we instantiate a new runtime.
let mut runtime = Runtime::new(&wasm_bytes, blockchain, contract_id)?;
// =============================================
// Build some kind of payload to show an example
// =============================================
let func_calls = vec![FuncCall {
contract_id: pallas::Base::from(110),
func_id: pallas::Base::from(4),
//call_data: serialize(&FooCallData { a: 777, b: 666 }),
call_data: Vec::new()
}];
let func_call_index: u32 = 0;
let mut payload = Vec::new();
// Selects which path executes in the contract.
//payload.write_u8(Function::Foo as u8)?;
//// Write the actual payload data
//payload.write_u32(func_call_index)?;
//func_calls.encode(&mut payload)?;
// ============================================================
// Serialize the payload into the runtime format and execute it
// ============================================================
let update = runtime.exec(&payload)?;
// =====================================================
// If exec was successful, try to apply the state change
// =====================================================
runtime.apply(&update)?;
// =====================================================
// Verify ZK proofs and signatures
// =====================================================
let metadata = runtime.metadata(&payload)?;
let mut decoder = Cursor::new(&metadata);
let zk_public_values: Vec<(String, Vec<pallas::Base>)> = Decodable::decode(&mut decoder)?;
let signature_public_keys: Vec<pallas::Point> = Decodable::decode(decoder)?;
Ok(())
}