lib.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use darkfi_sdk::{
  2. crypto::{ContractId, constants::MERKLE_DEPTH, MerkleNode, Nullifier},
  3. db::{db_get, db_init, db_lookup, db_set},
  4. define_contract,
  5. error::ContractResult,
  6. msg,
  7. merkle::merkle_add,
  8. pasta::pallas,
  9. tx::ContractCall,
  10. util::{get_object_bytes, get_object_size, put_object_bytes, set_return_data},
  11. incrementalmerkletree::{bridgetree::BridgeTree, Tree}
  12. };
  13. use darkfi_serial::{
  14. deserialize, serialize, Encodable, ReadExt, SerialDecodable, SerialEncodable, WriteExt,
  15. };
  16. type MerkleTree = BridgeTree<MerkleNode, { MERKLE_DEPTH }>;
  17. #[derive(Clone, SerialEncodable, SerialDecodable)]
  18. pub struct DaoBulla(pub pallas::Base);
  19. #[repr(u8)]
  20. pub enum DaoFunction {
  21. Foo = 0x00,
  22. Mint = 0x01,
  23. }
  24. impl From<u8> for DaoFunction {
  25. fn from(b: u8) -> Self {
  26. match b {
  27. 0x00 => Self::Foo,
  28. 0x01 => Self::Mint,
  29. _ => panic!("Invalid function ID: {:#04x?}", b),
  30. }
  31. }
  32. }
  33. #[derive(SerialEncodable, SerialDecodable)]
  34. pub struct DaoMintParams {
  35. pub dao_bulla: DaoBulla,
  36. }
  37. #[derive(SerialEncodable, SerialDecodable)]
  38. pub struct DaoMintUpdate {
  39. pub dao_bulla: DaoBulla,
  40. }
  41. define_contract!(
  42. init: init_contract,
  43. exec: process_instruction,
  44. apply: process_update,
  45. metadata: get_metadata
  46. );
  47. fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
  48. let db_handle = db_init(cid, "info")?;
  49. let dao_tree = MerkleTree::new(100);
  50. let dao_tree_data = serialize(&dao_tree);
  51. db_set(db_handle, &serialize(&"dao_tree".to_string()), &dao_tree_data)?;
  52. Ok(())
  53. }
  54. fn get_metadata(_cid: ContractId, ix: &[u8]) -> ContractResult {
  55. let zk_public_values: Vec<(String, Vec<pallas::Base>)> = Vec::new();
  56. let signature_public_keys: Vec<pallas::Point> = Vec::new();
  57. let mut metadata = Vec::new();
  58. zk_public_values.encode(&mut metadata)?;
  59. signature_public_keys.encode(&mut metadata)?;
  60. set_return_data(&metadata)?;
  61. Ok(())
  62. }
  63. fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
  64. let (call_idx, call): (u32, Vec<ContractCall>) = deserialize(ix)?;
  65. assert!(call_idx < call.len() as u32);
  66. let self_ = &call[call_idx as usize];
  67. match DaoFunction::from(self_.data[0]) {
  68. DaoFunction::Mint => {
  69. let data = &self_.data[1..];
  70. let params: DaoMintParams = deserialize(data)?;
  71. // No checks in Mint. Just return the update.
  72. let update = DaoMintUpdate { dao_bulla: params.dao_bulla };
  73. let mut update_data = Vec::new();
  74. update_data.write_u8(DaoFunction::Mint as u8);
  75. update.encode(&mut update_data);
  76. set_return_data(&update_data)?;
  77. msg!("update is set!");
  78. }
  79. DaoFunction::Foo => {
  80. unimplemented!();
  81. }
  82. }
  83. Ok(())
  84. }
  85. fn process_update(cid: ContractId, update_data: &[u8]) -> ContractResult {
  86. match DaoFunction::from(update_data[0]) {
  87. DaoFunction::Mint => {
  88. let data = &update_data[1..];
  89. let update: DaoMintUpdate = deserialize(data)?;
  90. let db_handle = db_lookup(cid, "info")?;
  91. let node = MerkleNode::new(update.dao_bulla.0);
  92. merkle_add(db_handle, &serialize(&"dao_tree".to_string()), &node)?;
  93. }
  94. DaoFunction::Foo => {
  95. unimplemented!();
  96. }
  97. }
  98. Ok(())
  99. }