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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * SPDX-License-Identifier: Apache-2.0
 */
use super::{DataType, StateBasedEndorsement};
use crate::{contract::LedgerError, runtimeapi::LedgerService};
use fabric_ledger_protos::ledger_messages;

///
/// A State is the combination of key and value that are contained within a collection.
///
/// State-based endorsement should be set on this object as well.
///
///  Represents the things that are contained within the Collections.
///
#[derive(Default)]
pub struct State {
    key: String,
    data: Vec<u8>,
    collection_name: Option<String>,
}

impl State {
    /// Make a composite key.
    ///
    /// # Remarks
    ///
    /// Takes a set of strings and arranges them in a ':' separate single string
    ///
    /// # Arguments
    ///
    /// - `keytype`     The first part of the key. Referred to as 'type' but is just the 1st string
    ///                 part of the key.
    /// - `attributes`  Vector of Strings to make the rest of the ky
    pub fn make_composite_key(keytype: String, attributes: Vec<String>) -> String {
        todo!("make key");
    }

    /// Splits a composite key
    ///
    /// Splits the composite key into the parts, the first is the
    /// string that is passed in as the keytype above
    pub fn split_composite_key(key: String) -> Vec<String> {
        todo!("make key");
    }

    /// Creates a new state
    pub fn new(key: String, data: Vec<u8>, collection_name: String) -> State {
        State {
            key,
            data,
            collection_name: Some(collection_name),
        }
    }

    /// Get the buffer that  this state
    pub fn value(&self) -> Vec<u8> {
        self.data.clone()
    }

    /// Get the key that used for this state
    pub fn key(&self) -> String {
        self.key.clone()
    }

    /// Returns an iterator of the state history for this state
    pub fn get_history(&self) /* -> TODO  Iterator of StateHistory */ {}

    /// gets the private hash for this stae
    pub fn get_hash(&self) ->Result<Vec<u8>,LedgerError> {
        let name = self
        .collection_name
        .clone()
        .ok_or(LedgerError::from("Put to a colletion first".to_string()))?;

        LedgerService::get_hash(&self.key, &name)

    }

    /// Sets the State Based Endorsment for this state
    pub fn set_endorsment(&self, sbe: StateBasedEndorsement) -> Result<(), LedgerError> {
        let name = self
            .collection_name
            .clone()
            .ok_or(LedgerError::from("Put to a colletion first".to_string()))?;
        LedgerService::set_endorsement_policy(&self.key, &name, sbe)
    }

    pub fn get_endorsement(&self) -> Result<Option<StateBasedEndorsement>, LedgerError> {
        let name = self
            .collection_name
            .clone()
            .ok_or(LedgerError::from("Put to a colletion first".to_string()))?;

        match LedgerService::get_endorsement_policy(&self.key, &name) {
            Ok(e) => Ok(Some(e)),
            Err(e) => Err(e),
        }
    }
}

impl From<()> for State {
    fn from(_: ()) -> Self {
        Self::default()
    }
}

/// Implementation of converting to a state from a datatype
///
/// # Example
///
/// ```ignore
/// let myAsset = MyAsset::new();
/// state.from(myAsset);
///
/// impl From<Box<dyn DataType>> for State {
///     fn from(_:Box<dyn DataType>) -> Self {
///        Self::default()
///     }
/// }
/// ```

impl From<(String, Vec<u8>)> for State {
    fn from((a, b): (String, Vec<u8>)) -> Self {
        Self {
            key: a,
            data: b,
            collection_name: Option::None,
        }
    }
}

impl From<&ledger_messages::State> for State {
    fn from(lms: &ledger_messages::State) -> Self {
        Self {
            key: lms.get_key().to_string(),
            data: lms.get_value().to_vec(),
            collection_name: Option::None,
        }
    }
}

impl From<(&ledger_messages::State, String)> for State {
    fn from((lms, c): (&ledger_messages::State, String)) -> Self {
        Self {
            key: lms.get_key().to_string(),
            data: lms.get_value().to_vec(),
            collection_name: Some(c),
        }
    }
}

impl From<ledger_messages::State> for State {
    fn from(lms: ledger_messages::State) -> Self {
        Self {
            key: lms.get_key().to_string(),
            data: lms.get_value().to_vec(),
            collection_name: Option::None,
        }
    }
}

impl std::convert::From<State> for fabric_ledger_protos::ledger_messages::State {
    fn from(_: State) -> Self {
        todo!()
    }
}

pub struct StateHash {
    pub hash: Vec<u8>,
}

pub trait VerifyHashConsistency<T> {
    fn verify_consistent(&self, o: T) -> Result<bool, LedgerError>;
}

impl VerifyHashConsistency<StateHash> for StateHash {
    fn verify_consistent(&self, o: StateHash) -> Result<bool, LedgerError> {
        todo!()
    }
}

// impl VerifyHashConsistency<State> for State {
//     fn verify_consistent(&self, o: State) -> Result<bool, LedgerError> {
//         todo!()
//     }
// }

impl<T: DataType> VerifyHashConsistency<T> for StateHash {
    fn verify_consistent(&self, o: T) -> Result<bool, LedgerError> {
        todo!()
    }
}