runtime: sled blockchain access

This commit is contained in:
parazyd
2022-11-04 14:41:20 +01:00
parent dbdbc42705
commit 9848a1e5f5
5 changed files with 72 additions and 38 deletions

View File

@@ -2601,6 +2601,7 @@ dependencies = [
"darkfi-serial",
"getrandom",
"simplelog",
"sled",
]
[[package]]

View File

@@ -27,4 +27,4 @@ getrandom = { version = "0.2", features = ["custom"] }
[dev-dependencies]
darkfi = { path = "../../", features = ["wasm-runtime"] }
simplelog = "0.12.0"
sled = "0.34.7"

View File

@@ -16,11 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use darkfi::{crypto::contract_id::ContractId, runtime::vm_runtime::Runtime, Result};
use darkfi_sdk::{
pasta::pallas,
tx::FuncCall
use darkfi::{
blockchain::Blockchain,
consensus::{TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
crypto::contract_id::ContractId,
runtime::vm_runtime::Runtime,
Result,
};
use darkfi_sdk::{pasta::pallas, tx::FuncCall};
use darkfi_serial::{serialize, Encodable, WriteExt};
use smart_contract::{FooCallData, Function};
@@ -36,20 +39,20 @@ fn run_contract() -> Result<()> {
simplelog::TerminalMode::Mixed,
simplelog::ColorChoice::Auto,
)?;
// =============================================================
// Build a ledger state so the runtime has something to work on
// =============================================================
//let state_machine = State::dummy()?;
// Add a nullifier to the nullifier set. (This is checked by the contract)
//state_machine.nullifiers.insert(&[Nullifier::from(pallas::Base::from(0x10))])?;
// =============================
// 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("contract.wasm")?;
let contract_id = ContractId::new(pallas::Base::from(1));
let mut runtime = Runtime::new(&wasm_bytes, contract_id)?;
let mut runtime = Runtime::new(&wasm_bytes, blockchain, 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.
@@ -59,9 +62,9 @@ fn run_contract() -> Result<()> {
// 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 }),
contract_id: pallas::Base::from(110),
func_id: pallas::Base::from(4),
call_data: serialize(&FooCallData { a: 777, b: 666 }),
}];
let func_call_index: u32 = 0;