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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// RGB standard library
// Written in 2020 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

#[cfg(feature = "serde")]
use serde_json;
use std::collections::BTreeMap;
#[cfg(any(
    feature = "serde_yaml",
    feature = "serde_json",
    feature = "toml"
))]
use std::io::{Read, Write};
use std::path::PathBuf;
use std::{fs, io};

use lnpbp::bitcoin;
use lnpbp::rgb::prelude::*;
use lnpbp::strict_encoding::{strict_encode, StrictDecode, StrictEncode};

use super::Cache;
use crate::fungible::cache::CacheError;
use crate::fungible::Asset;
use crate::util::file::*;
use crate::DataFormat;

#[derive(Debug, Display, Error, From)]
#[display(Debug)]
#[non_exhaustive]
pub enum FileCacheError {
    #[from]
    Io(io::Error),

    #[from(lnpbp::hashes::Error)]
    HashName,

    #[from(lnpbp::hex::Error)]
    BrokenHexFilenames,

    #[from]
    Encoding(lnpbp::strict_encoding::Error),

    #[cfg(feature = "serde")]
    #[from]
    SerdeJson(serde_json::Error),

    #[cfg(feature = "serde")]
    #[from]
    SerdeYaml(serde_yaml::Error),

    #[cfg(feature = "serde")]
    #[from(toml::de::Error)]
    #[from(toml::ser::Error)]
    SerdeToml,

    NotFound,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, Display)]
#[display(Debug)]
pub struct FileCacheConfig {
    pub data_dir: PathBuf,
    pub data_format: DataFormat,
}

impl FileCacheConfig {
    #[inline]
    pub fn assets_dir(&self) -> PathBuf {
        self.data_dir.join("assets")
    }

    #[inline]
    pub fn assets_filename(&self) -> PathBuf {
        self.assets_dir()
            .join("assets")
            .with_extension(self.data_format.extension())
    }
}

/// Keeps all source/binary RGB contract data, stash etc
#[derive(Debug)]
pub struct FileCache {
    config: FileCacheConfig,
    assets: BTreeMap<ContractId, Asset>,
}

impl FileCache {
    pub fn new(config: FileCacheConfig) -> Result<Self, FileCacheError> {
        debug!("Instantiating RGB fungible assets storage (disk storage) ...");

        let data_dir = config.data_dir.clone();
        if !data_dir.exists() {
            debug!(
                "RGB fungible assets data directory '{:?}' is not found; creating one",
                data_dir
            );
            fs::create_dir_all(data_dir)?;
        }
        let assets_dir = config.assets_dir();
        if !assets_dir.exists() {
            debug!(
                "RGB fungible assets information directory '{:?}' is not found; creating one",
                assets_dir
            );
            fs::create_dir_all(assets_dir)?;
        }

        let mut me = Self {
            config,
            assets: bmap![],
        };
        let filename = me.config.assets_filename();
        if filename.exists() {
            me.load()?;
        } else {
            debug!("Initializing assets file {:?} ...", filename.to_str());
            me.save()?;
        }

        Ok(me)
    }

    fn load(&mut self) -> Result<(), FileCacheError> {
        debug!("Reading assets information ...");
        let filename = self.config.assets_filename();
        let mut f = file(filename, FileMode::Read)?;
        self.assets = match self.config.data_format {
            #[cfg(feature = "serde_yaml")]
            DataFormat::Yaml => serde_yaml::from_reader(&f)?,
            #[cfg(feature = "serde_json")]
            DataFormat::Json => serde_json::from_reader(&f)?,
            #[cfg(feature = "toml")]
            DataFormat::Toml => {
                let mut data = String::new();
                f.read_to_string(&mut data)?;
                toml::from_str(&data)?
            }
            DataFormat::StrictEncode => StrictDecode::strict_decode(&mut f)?,
        };
        Ok(())
    }

    pub fn save(&self) -> Result<(), FileCacheError> {
        trace!("Saving assets information ...");
        let filename = self.config.assets_filename();
        let _ = fs::remove_file(&filename);
        let mut f = file(filename, FileMode::Create)?;
        match self.config.data_format {
            #[cfg(feature = "serde_yaml")]
            DataFormat::Yaml => serde_yaml::to_writer(&f, &self.assets)?,
            #[cfg(feature = "serde_json")]
            DataFormat::Json => serde_json::to_writer(&f, &self.assets)?,
            #[cfg(feature = "toml")]
            DataFormat::Toml => f.write_all(&toml::to_vec(&self.assets)?)?,
            DataFormat::StrictEncode => {
                self.assets.strict_encode(&mut f)?;
            }
        }
        Ok(())
    }

    pub fn export(
        &self,
        data_format: Option<DataFormat>,
    ) -> Result<Vec<u8>, FileCacheError> {
        trace!("Exporting assets information ...");
        let assets = self.assets.values().cloned().collect::<Vec<Asset>>();
        Ok(match data_format.unwrap_or(self.config.data_format) {
            #[cfg(feature = "serde_yaml")]
            DataFormat::Yaml => serde_yaml::to_vec(&assets)?,
            #[cfg(feature = "serde_json")]
            DataFormat::Json => serde_json::to_vec(&assets)?,
            #[cfg(feature = "toml")]
            DataFormat::Toml => toml::to_vec(&assets)?,
            DataFormat::StrictEncode => strict_encode(&assets)?,
        })
    }
}

impl Cache for FileCache {
    type Error = CacheError;

    fn assets(&self) -> Result<Vec<&Asset>, CacheError> {
        Ok(self.assets.values().collect())
    }

    #[inline]
    fn asset(&self, id: ContractId) -> Result<&Asset, CacheError> {
        Ok(self.assets.get(&id).ok_or(CacheError::DataIntegrityError(
            "Asset is not known".to_string(),
        ))?)
    }

    #[inline]
    fn has_asset(&self, id: ContractId) -> Result<bool, CacheError> {
        Ok(self.assets.contains_key(&id))
    }

    fn add_asset(&mut self, asset: Asset) -> Result<bool, CacheError> {
        let exists = self.assets.insert(*asset.id(), asset).is_some();
        self.save()?;
        Ok(exists)
    }

    #[inline]
    fn remove_asset(&mut self, id: ContractId) -> Result<bool, CacheError> {
        let existed = self.assets.remove(&id).is_some();
        self.save()?;
        Ok(existed)
    }

    fn asset_allocations(
        &self,
        contract_id: ContractId,
    ) -> Result<BTreeMap<bitcoin::OutPoint, Vec<AtomicValue>>, CacheError> {
        // Process known_allocation map to produce the intended map
        let result: BTreeMap<bitcoin::OutPoint, Vec<AtomicValue>> = self
            .asset(contract_id)?
            .known_allocations()
            .into_iter()
            .map(|(outpoint, allocations)| {
                (
                    *outpoint,
                    allocations.into_iter().map(|a| a.value().value).collect(),
                )
            })
            .collect();

        Ok(result)
    }

    fn outpoint_assets(
        &self,
        outpoint: bitcoin::OutPoint,
    ) -> Result<BTreeMap<ContractId, Vec<AtomicValue>>, CacheError> {
        let mut result = BTreeMap::new();

        for asset in self.assets()?.into_iter().cloned().collect::<Vec<Asset>>()
        {
            let allocations: Vec<AtomicValue> =
                match asset.known_allocations().get(&outpoint) {
                    Some(allocations) => allocations
                        .into_iter()
                        .map(|alloc| alloc.value().value)
                        .collect(),
                    None => continue,
                };

            result.insert(*asset.id(), allocations);
        }

        Ok(result)
    }
}

#[cfg(test)]
mod test {
    use super::super::sql::{SqlCache, SqlCacheConfig};
    use super::*;
    use lnpbp::bp::TaggedHash;
    use lnpbp::hex::FromHex;
    use std::env;

    #[test]
    #[ignore]
    fn test_filecache_mappings() {
        // -------------------------------------------------
        // Setup sqlite database connection
        // This is done to easily copy test assets data
        // from sqlite database to required file format
        // As a consequence this should be run after running tests
        // in sql.rs module.

        let database_url = env::var("DATABASE_URL").expect(
            "Environment Variable 'DATABASE_URL' must be set to run this test",
        );
        let filepath = PathBuf::from(&database_url[..]);
        let config = SqlCacheConfig {
            data_dir: filepath.clone(),
        };
        let sql_cache = SqlCache::new(&config).unwrap();

        // Get the test assets
        let assets = sql_cache.assets().unwrap();

        // Setup a new Filecache with same Pathbuf and Json extension from above
        let filecache_config = FileCacheConfig {
            data_dir: filepath.clone(),
            #[cfg(feature = "serde_json")]
            data_format: DataFormat::Json,
            #[cfg(not(feature = "serde_json"))]
            data_format: DataFormat::StrictEncode,
        };

        // Init new FileCache
        let mut filecache = FileCache::new(filecache_config).unwrap();

        // Save the test assets inside FileCache
        // You should see an assets.json file in the DataDir
        for asset in assets {
            filecache.add_asset(asset.clone()).unwrap();
        }

        // -------------------------------------------------
        // TEST UTXO-ALLOCATION MAP

        // Test Contract_id to fetch data against
        let contract_id = ContractId::from_hex(
            "5bb162c7c84fa69bd263a12b277b82155787a03537691619fed731432f6855dc",
        )
        .unwrap();

        // Construct expected allocation-utxo mapping for the given asset
        // associated with the above contract_id
        let mut expected_map = BTreeMap::new();

        expected_map.insert(
            bitcoin::OutPoint {
                txid: bitcoin::Txid::from_hex(
                    "db2f3035e05795d72e2744dc0e88b2f72acbed97ee9a54c2c7f52d426ae05627",
                )
                .unwrap(),
                vout: 4,
            },
            vec![7 as AtomicValue, 9],
        );

        expected_map.insert(
            bitcoin::OutPoint {
                txid: bitcoin::Txid::from_hex(
                    "d47df6cf7a0eff79d3afeab7614404e43a0fa4498ff081918a2e75d7366cd730",
                )
                .unwrap(),
                vout: 5,
            },
            vec![11 as AtomicValue, 13],
        );

        expected_map.insert(
            bitcoin::OutPoint {
                txid: bitcoin::Txid::from_hex(
                    "fc63f797af718cc5a11988f69507701d5fe84e58cdd900e1b02856c0ea5a058a",
                )
                .unwrap(),
                vout: 3,
            },
            vec![1 as AtomicValue, 3, 5],
        );

        // Fetch the allocation-utxo map using cache api
        let calculated_map = filecache.asset_allocations(contract_id).unwrap();

        // Assert calculation meets expectation
        assert_eq!(expected_map, calculated_map);

        //-----------------------------------------------------
        // TEST ASSET-ALLOCATION MAP

        // Test utxo against which Asset allocations to be found
        let utxo = bitcoin::OutPoint {
            txid: bitcoin::Txid::from_hex(
                "fc63f797af718cc5a11988f69507701d5fe84e58cdd900e1b02856c0ea5a058a",
            )
            .unwrap(),
            vout: 3,
        };

        // Construct the expected mapping. The above utxo holds allocation
        // for 2 assets, Bitcoin and Ethereum. The target map is Map[Asset_name,
        // Allocated_amount]
        let mut expected_map = BTreeMap::new();
        expected_map.insert(ContractId::from_hex("5bb162c7c84fa69bd263a12b277b82155787a03537691619fed731432f6855dc").unwrap(), vec![1 as AtomicValue, 3, 5]);
        expected_map.insert(ContractId::from_hex("7ce3b67036e32628fe5351f23d57186181dba3103b7e0a5d55ed511446f5a6a9").unwrap(), vec![15 as AtomicValue, 17]);

        // Fetch the asset-amount map for the above utxo using cache api
        let allocation_map_calculated =
            filecache.outpoint_assets(utxo).unwrap();

        // Assert caclulation meets expectation
        assert_eq!(expected_map, allocation_map_calculated);
    }
}