1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::sync::Arc;

use bytecheck::CheckBytes;
use rkyv::{Archive, Deserialize, Serialize};
use wasmer::Module;

use crate::error::Error;
use crate::instance::Store;
use piecrust_uplink::ContractId;

pub struct ContractData<'a, A, const N: usize> {
    pub(crate) contract_id: Option<ContractId>,
    pub(crate) constructor_arg: Option<&'a A>,
    pub(crate) owner: [u8; N],
}

// `()` is done on purpose, since by default it should be that the constructor
// takes no argument.
impl<'a, const N: usize> ContractData<'a, (), N> {
    /// Build a deploy data structure.
    ///
    /// This function returns a builder that can be used to set optional fields
    /// in contract deployment.
    pub fn builder(owner: [u8; N]) -> ContractDataBuilder<'a, (), N> {
        ContractDataBuilder {
            contract_id: None,
            constructor_arg: None,
            owner,
        }
    }
}

impl<'a, A, const N: usize> From<ContractDataBuilder<'a, A, N>>
    for ContractData<'a, A, N>
{
    fn from(builder: ContractDataBuilder<'a, A, N>) -> Self {
        builder.build()
    }
}

pub struct ContractDataBuilder<'a, A, const N: usize> {
    contract_id: Option<ContractId>,
    owner: [u8; N],
    constructor_arg: Option<&'a A>,
}

impl<'a, A, const N: usize> ContractDataBuilder<'a, A, N> {
    /// Set the deployment contract ID.
    pub fn contract_id(mut self, id: ContractId) -> Self {
        self.contract_id = Some(id);
        self
    }

    /// Set the constructor argument for deployment.
    pub fn constructor_arg<B>(self, arg: &B) -> ContractDataBuilder<B, N> {
        ContractDataBuilder {
            contract_id: self.contract_id,
            owner: self.owner,
            constructor_arg: Some(arg),
        }
    }

    pub fn build(self) -> ContractData<'a, A, N> {
        ContractData {
            contract_id: self.contract_id,
            constructor_arg: self.constructor_arg,
            owner: self.owner,
        }
    }
}

#[derive(Archive, Serialize, Deserialize, Debug, Clone)]
#[archive_attr(derive(CheckBytes))]
pub struct ContractMetadata {
    pub contract_id: ContractId,
    pub owner: Vec<u8>,
}

#[derive(Clone)]
pub struct WrappedContract {
    serialized: Arc<Vec<u8>>,
}

impl WrappedContract {
    pub fn new<B: AsRef<[u8]>, C: AsRef<[u8]>>(
        bytecode: B,
        objectcode: Option<C>,
    ) -> Result<Self, Error> {
        let store = Store::new_store();
        let serialized = match objectcode {
            Some(obj) => obj.as_ref().to_vec(),
            _ => {
                let contract = Module::new(&store, bytecode.as_ref())?;
                contract.serialize()?.to_vec()
            }
        };

        Ok(WrappedContract {
            serialized: Arc::new(serialized),
        })
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.serialized
    }
}