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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// RGB standard library for working with smart contracts on Bitcoin & Lightning
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2023 by
//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::{BTreeMap, BTreeSet};
use std::convert::Infallible;
use std::ops::{Deref, DerefMut};

use amplify::confinement::{MediumOrdMap, MediumOrdSet, TinyOrdMap};
use amplify::RawArray;
use bp::dbc::Anchor;
use bp::Txid;
use commit_verify::mpc::MerkleBlock;
use rgb::validation::{Status, Validity, Warning};
use rgb::{
    validation, AnchorId, AnchoredBundle, Assign, AssignmentType, BundleId, ContractHistory,
    ContractId, ContractState, ExposedState, Extension, Genesis, GenesisSeal, GraphSeal, OpId,
    Operation, Opout, SecretSeal, SubSchema, Transition, TransitionBundle, TxoSeal, TypedAssigns,
    WitnessAnchor,
};
use strict_encoding::{StrictDeserialize, StrictSerialize};

use crate::containers::{Bindle, Cert, Consignment, ContentId, Contract, TerminalSeal, Transfer};
use crate::interface::{
    ContractIface, Iface, IfaceId, IfaceImpl, IfacePair, SchemaIfaces, TypedState,
};
use crate::persistence::inventory::{DataError, IfaceImplError, InventoryInconsistency};
use crate::persistence::{
    Hoard, Inventory, InventoryDataError, InventoryError, Stash, StashInconsistency,
};
use crate::resolvers::ResolveHeight;
use crate::{Outpoint, LIB_NAME_RGB_STD};

#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STD)]
pub struct IndexedBundle(ContractId, BundleId);

#[derive(Clone, Debug, Default)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STD)]
pub struct ContractIndex {
    public_opouts: MediumOrdSet<Opout>,
    outpoint_opouts: MediumOrdMap<Outpoint, MediumOrdSet<Opout>>,
}

/// Stock is an in-memory inventory (stash, index, contract state) useful for
/// WASM implementations.
///
/// Can hold data about up to 256 contracts.
#[derive(Clone, Debug, Getters)]
#[getter(prefix = "debug_")]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STD)]
pub struct Stock {
    // stash
    hoard: Hoard,
    // state
    history: TinyOrdMap<ContractId, ContractHistory>,
    // index
    bundle_op_index: MediumOrdMap<OpId, IndexedBundle>,
    anchor_bundle_index: MediumOrdMap<BundleId, AnchorId>,
    contract_index: TinyOrdMap<ContractId, ContractIndex>,
    terminal_index: MediumOrdMap<SecretSeal, Opout>,
    // secrets
    seal_secrets: MediumOrdSet<GraphSeal>,
}

impl Default for Stock {
    fn default() -> Self {
        Stock {
            hoard: Hoard::preset(),
            history: empty!(),
            bundle_op_index: empty!(),
            anchor_bundle_index: empty!(),
            contract_index: empty!(),
            terminal_index: empty!(),
            seal_secrets: empty!(),
        }
    }
}

impl StrictSerialize for Stock {}
impl StrictDeserialize for Stock {}

impl Deref for Stock {
    type Target = Hoard;

    fn deref(&self) -> &Self::Target { &self.hoard }
}

impl DerefMut for Stock {
    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.hoard }
}

#[allow(clippy::result_large_err)]
impl Stock {
    fn consume_consignment<R: ResolveHeight, const TYPE: bool>(
        &mut self,
        mut consignment: Consignment<TYPE>,
        resolver: &mut R,
        force: bool,
    ) -> Result<validation::Status, InventoryError<Infallible>>
    where
        R::Error: 'static,
    {
        let mut status = validation::Status::new();
        match consignment.validation_status() {
            None => return Err(DataError::NotValidated.into()),
            Some(status) if status.validity() == Validity::Invalid => {
                return Err(DataError::Invalid(status.clone()).into());
            }
            Some(status) if status.validity() == Validity::UnresolvedTransactions && !force => {
                return Err(DataError::UnresolvedTransactions.into());
            }
            Some(status) if status.validity() == Validity::UnminedTerminals && !force => {
                return Err(DataError::TerminalsUnmined.into());
            }
            Some(s) if s.validity() == Validity::UnresolvedTransactions && !force => {
                status.add_warning(Warning::Custom(s!(
                    "contract contains unknown transactions and was forcefully imported"
                )));
            }
            Some(s) if s.validity() == Validity::UnminedTerminals && !force => {
                status.add_warning(Warning::Custom(s!("contract contains not yet mined final \
                                                       transactions and was forcefully imported")));
            }
            _ => {}
        }

        let id = consignment.contract_id();

        self.import_schema(consignment.schema.clone())?;
        for IfacePair { iface, iimpl } in consignment.ifaces.values() {
            self.import_iface(iface.clone())?;
            self.import_iface_impl(iimpl.clone())?;
        }

        // clone needed due to borrow checker
        for (bundle_id, terminal) in consignment.terminals.clone() {
            for secret in terminal.seals.iter().filter_map(TerminalSeal::secret_seal) {
                if let Some(seal) = self
                    .seal_secrets
                    .iter()
                    .find(|s| s.to_concealed_seal() == secret)
                {
                    consignment.reveal_bundle_seal(bundle_id, *seal);
                }
            }
        }

        // Update existing contract state
        let history = consignment
            .update_history(self.history.get(&id), resolver)
            .map_err(|err| DataError::HeightResolver(Box::new(err)))?;
        self.history.insert(id, history)?;

        let contract_id = consignment.contract_id();
        if !self.contract_index.contains_key(&contract_id) {
            self.contract_index.insert(contract_id, empty!())?;
        }
        self.index_genesis(contract_id, &consignment.genesis)?;
        for extension in &consignment.extensions {
            self.index_extension(contract_id, extension)?;
        }
        for AnchoredBundle { anchor, bundle } in &mut consignment.bundles {
            let bundle_id = bundle.bundle_id();
            let anchor_id = anchor.anchor_id(contract_id, bundle_id.into())?;
            self.anchor_bundle_index.insert(bundle_id, anchor_id)?;
            self.index_bundle(contract_id, bundle, anchor.txid)?;
        }

        self.hoard.consume_consignment(consignment)?;

        Ok(status)
    }

    fn index_genesis(
        &mut self,
        id: ContractId,
        genesis: &Genesis,
    ) -> Result<(), InventoryError<<Self as Inventory>::Error>> {
        let opid = genesis.id();
        for (type_id, assign) in genesis.assignments.iter() {
            match assign {
                TypedAssigns::Declarative(vec) => {
                    self.index_genesis_assignments(id, vec, opid, *type_id)?;
                }
                TypedAssigns::Fungible(vec) => {
                    self.index_genesis_assignments(id, vec, opid, *type_id)?;
                }
                TypedAssigns::Structured(vec) => {
                    self.index_genesis_assignments(id, vec, opid, *type_id)?;
                }
                TypedAssigns::Attachment(vec) => {
                    self.index_genesis_assignments(id, vec, opid, *type_id)?;
                }
            }
        }
        Ok(())
    }

    fn index_extension(
        &mut self,
        id: ContractId,
        extension: &Extension,
    ) -> Result<(), InventoryError<<Self as Inventory>::Error>> {
        let opid = extension.id();
        for (type_id, assign) in extension.assignments.iter() {
            match assign {
                TypedAssigns::Declarative(vec) => {
                    self.index_genesis_assignments(id, vec, opid, *type_id)?;
                }
                TypedAssigns::Fungible(vec) => {
                    self.index_genesis_assignments(id, vec, opid, *type_id)?;
                }
                TypedAssigns::Structured(vec) => {
                    self.index_genesis_assignments(id, vec, opid, *type_id)?;
                }
                TypedAssigns::Attachment(vec) => {
                    self.index_genesis_assignments(id, vec, opid, *type_id)?;
                }
            }
        }
        Ok(())
    }

    fn index_bundle(
        &mut self,
        id: ContractId,
        bundle: &TransitionBundle,
        witness_txid: Txid,
    ) -> Result<(), InventoryError<<Self as Inventory>::Error>> {
        let bundle_id = bundle.bundle_id();
        for (opid, item) in bundle.iter() {
            if let Some(transition) = &item.transition {
                self.bundle_op_index
                    .insert(*opid, IndexedBundle(id, bundle_id))?;
                for (type_id, assign) in transition.assignments.iter() {
                    match assign {
                        TypedAssigns::Declarative(vec) => {
                            self.index_transition_assignments(
                                id,
                                vec,
                                *opid,
                                *type_id,
                                witness_txid,
                            )?;
                        }
                        TypedAssigns::Fungible(vec) => {
                            self.index_transition_assignments(
                                id,
                                vec,
                                *opid,
                                *type_id,
                                witness_txid,
                            )?;
                        }
                        TypedAssigns::Structured(vec) => {
                            self.index_transition_assignments(
                                id,
                                vec,
                                *opid,
                                *type_id,
                                witness_txid,
                            )?;
                        }
                        TypedAssigns::Attachment(vec) => {
                            self.index_transition_assignments(
                                id,
                                vec,
                                *opid,
                                *type_id,
                                witness_txid,
                            )?;
                        }
                    }
                }
            }
        }

        Ok(())
    }

    fn index_genesis_assignments<State: ExposedState>(
        &mut self,
        contract_id: ContractId,
        vec: &[Assign<State, GenesisSeal>],
        opid: OpId,
        type_id: AssignmentType,
    ) -> Result<(), InventoryError<<Self as Inventory>::Error>> {
        let index = self
            .contract_index
            .get_mut(&contract_id)
            .ok_or(StashInconsistency::ContractAbsent(contract_id))?;

        for (no, a) in vec.iter().enumerate() {
            let opout = Opout::new(opid, type_id, no as u16);
            if let Assign::ConfidentialState { seal, .. } | Assign::Revealed { seal, .. } = a {
                let outpoint = seal.outpoint_or(seal.txid);
                match index.outpoint_opouts.get_mut(&outpoint) {
                    Some(opouts) => {
                        opouts.push(opout)?;
                    }
                    None => {
                        index
                            .outpoint_opouts
                            .insert(outpoint, confined_bset!(opout))?;
                    }
                }
            }
            if let Assign::Confidential { seal, .. } | Assign::ConfidentialSeal { seal, .. } = a {
                self.terminal_index.insert(*seal, opout)?;
            }
        }
        Ok(())
    }

    fn index_transition_assignments<State: ExposedState>(
        &mut self,
        contract_id: ContractId,
        vec: &[Assign<State, GraphSeal>],
        opid: OpId,
        type_id: AssignmentType,
        witness_txid: Txid,
    ) -> Result<(), InventoryError<<Self as Inventory>::Error>> {
        let index = self
            .contract_index
            .get_mut(&contract_id)
            .ok_or(StashInconsistency::ContractAbsent(contract_id))?;

        for (no, a) in vec.iter().enumerate() {
            let opout = Opout::new(opid, type_id, no as u16);
            if let Assign::ConfidentialState { seal, .. } | Assign::Revealed { seal, .. } = a {
                let outpoint = seal.outpoint_or(witness_txid);
                match index.outpoint_opouts.get_mut(&outpoint) {
                    Some(opouts) => {
                        opouts.push(opout)?;
                    }
                    None => {
                        index
                            .outpoint_opouts
                            .insert(outpoint, confined_bset!(opout))?;
                    }
                }
            }
            if let Assign::Confidential { seal, .. } | Assign::ConfidentialSeal { seal, .. } = a {
                self.terminal_index.insert(*seal, opout)?;
            }
        }
        Ok(())
    }
}

impl Inventory for Stock {
    type Stash = Hoard;
    // In-memory representation doesn't have connectivity errors
    type Error = Infallible;

    fn stash(&self) -> &Self::Stash { self }

    fn import_sigs<I>(
        &mut self,
        content_id: ContentId,
        sigs: I,
    ) -> Result<(), InventoryDataError<Self::Error>>
    where
        I: IntoIterator<Item = Cert>,
        I::IntoIter: ExactSizeIterator<Item = Cert>,
    {
        self.import_sigs_internal(content_id, sigs)?;
        Ok(())
    }

    fn import_schema(
        &mut self,
        schema: impl Into<Bindle<SubSchema>>,
    ) -> Result<validation::Status, InventoryDataError<Self::Error>> {
        let bindle = schema.into();
        let (schema, sigs) = bindle.into_split();
        let id = schema.schema_id();

        let mut status = schema.verify();
        if !status.failures.is_empty() {
            return Err(status.into());
        }
        if self.schemata.contains_key(&id) {
            status.add_warning(Warning::Custom(format!("schema {id::<0} is already known")));
        } else {
            let schema_ifaces = SchemaIfaces::new(schema);
            self.schemata.insert(id, schema_ifaces)?;
        }

        let content_id = ContentId::Schema(id);
        // Do not bother if we can't import all the sigs
        self.import_sigs_internal(content_id, sigs).ok();

        Ok(status)
    }

    fn import_iface(
        &mut self,
        iface: impl Into<Bindle<Iface>>,
    ) -> Result<validation::Status, InventoryDataError<Self::Error>> {
        let bindle = iface.into();
        let (iface, sigs) = bindle.into_split();
        let id = iface.iface_id();

        let mut status = validation::Status::new();

        // TODO: Do interface check on internal consistency
        if self.ifaces.insert(id, iface)?.is_some() {
            status.add_warning(Warning::Custom(format!("interface {id::<0} is already known")));
        }

        let content_id = ContentId::Iface(id);
        // Do not bother if we can't import all the sigs
        self.import_sigs_internal(content_id, sigs).ok();

        Ok(status)
    }

    fn import_iface_impl(
        &mut self,
        iimpl: impl Into<Bindle<IfaceImpl>>,
    ) -> Result<validation::Status, InventoryDataError<Self::Error>> {
        let bindle = iimpl.into();
        let (iimpl, sigs) = bindle.into_split();
        let iface_id = iimpl.iface_id;
        let impl_id = iimpl.impl_id();

        let mut status = validation::Status::new();

        if !self.ifaces.contains_key(&iface_id) {
            return Err(IfaceImplError::UnknownIface(iface_id).into());
        }
        let Some(schema_ifaces) = self.schemata.get_mut(&iimpl.schema_id) else {
            return Err(IfaceImplError::UnknownSchema(iimpl.schema_id).into());
        };
        // TODO: Do interface check on internal consistency
        if schema_ifaces.iimpls.insert(iface_id, iimpl)?.is_some() {
            status.add_warning(Warning::Custom(format!(
                "interface implementation {impl_id::<0} is already known",
            )));
        }

        let content_id = ContentId::IfaceImpl(impl_id);
        // Do not bother if we can't import all the sigs
        self.import_sigs_internal(content_id, sigs).ok();

        Ok(status)
    }

    fn import_contract<R: ResolveHeight>(
        &mut self,
        contract: Contract,
        resolver: &mut R,
    ) -> Result<validation::Status, InventoryError<Self::Error>>
    where
        R::Error: 'static,
    {
        self.consume_consignment(contract, resolver, false)
    }

    fn accept_transfer<R: ResolveHeight>(
        &mut self,
        transfer: Transfer,
        resolver: &mut R,
        force: bool,
    ) -> Result<Status, InventoryError<Self::Error>>
    where
        R::Error: 'static,
    {
        self.consume_consignment(transfer, resolver, force)
    }

    fn consume_anchor(
        &mut self,
        anchor: Anchor<MerkleBlock>,
    ) -> Result<(), InventoryError<Self::Error>> {
        let anchor_id = anchor.anchor_id();
        for (_, bundle_id) in anchor.mpc_proof.to_known_message_map() {
            self.anchor_bundle_index
                .insert(bundle_id.to_raw_array().into(), anchor_id)?;
        }
        self.hoard.consume_anchor(anchor)?;
        Ok(())
    }

    fn consume_bundle(
        &mut self,
        contract_id: ContractId,
        bundle: TransitionBundle,
        witness_txid: Txid,
    ) -> Result<(), InventoryError<<Self as Inventory>::Error>> {
        self.index_bundle(contract_id, &bundle, witness_txid)?;
        let history = self
            .history
            .get_mut(&contract_id)
            .ok_or(InventoryInconsistency::StateAbsent(contract_id))?;
        for item in bundle.values() {
            if let Some(transition) = &item.transition {
                let ord_txid = WitnessAnchor::from_mempool(witness_txid);
                history.add_transition(transition, ord_txid);
            }
        }
        self.hoard.consume_bundle(bundle)?;
        Ok(())
    }

    unsafe fn import_contract_force<R: ResolveHeight>(
        &mut self,
        contract: Contract,
        resolver: &mut R,
    ) -> Result<validation::Status, InventoryError<Self::Error>>
    where
        R::Error: 'static,
    {
        self.consume_consignment(contract, resolver, true)
    }

    fn contract_iface(
        &mut self,
        contract_id: ContractId,
        iface_id: IfaceId,
    ) -> Result<ContractIface, InventoryError<Self::Error>> {
        let history = self
            .history
            .get(&contract_id)
            .ok_or(InventoryInconsistency::StateAbsent(contract_id))?
            .clone();
        let schema_id = history.schema_id();
        let schema_ifaces = self
            .schemata
            .get(&schema_id)
            .ok_or(StashInconsistency::SchemaAbsent(schema_id))?;
        let state = ContractState {
            schema: schema_ifaces.schema.clone(),
            history,
        };
        let iimpl = schema_ifaces
            .iimpls
            .get(&iface_id)
            .ok_or(StashInconsistency::IfaceImplAbsent(iface_id, schema_id))?
            .clone();
        Ok(ContractIface {
            state,
            iface: iimpl,
        })
    }

    fn transition(&self, opid: OpId) -> Result<&Transition, InventoryError<Self::Error>> {
        let IndexedBundle(_, bundle_id) = self
            .bundle_op_index
            .get(&opid)
            .ok_or(InventoryInconsistency::BundleAbsent(opid))?;
        let bundle = self.bundle(*bundle_id)?;
        let item = bundle.get(&opid).ok_or(DataError::Concealed)?;
        let transition = item.transition.as_ref().ok_or(DataError::Concealed)?;
        Ok(transition)
    }

    fn anchored_bundle(&self, opid: OpId) -> Result<AnchoredBundle, InventoryError<Self::Error>> {
        let IndexedBundle(contract_id, bundle_id) = self
            .bundle_op_index
            .get(&opid)
            .ok_or(InventoryInconsistency::BundleAbsent(opid))?;

        let anchor_id = self
            .anchor_bundle_index
            .get(bundle_id)
            .ok_or(InventoryInconsistency::NoBundleAnchor(*bundle_id))?;

        let bundle = self.bundle(*bundle_id)?.clone();
        let anchor = self.anchor(*anchor_id)?;
        let anchor = anchor.to_merkle_proof(*contract_id)?;
        // TODO: Conceal all transitions except the one we need

        Ok(AnchoredBundle { anchor, bundle })
    }

    fn contracts_by_outpoints(
        &mut self,
        outpoints: impl IntoIterator<Item = impl Into<Outpoint>>,
    ) -> Result<BTreeSet<ContractId>, InventoryError<Self::Error>> {
        let outpoints = outpoints
            .into_iter()
            .map(|o| o.into())
            .collect::<BTreeSet<_>>();
        let mut selected = BTreeSet::new();
        for (contract_id, index) in &self.contract_index {
            for outpoint in &outpoints {
                if index.outpoint_opouts.contains_key(outpoint) {
                    selected.insert(*contract_id);
                }
            }
        }
        Ok(selected)
    }

    fn public_opouts(
        &mut self,
        contract_id: ContractId,
    ) -> Result<BTreeSet<Opout>, InventoryError<Self::Error>> {
        let index = self
            .contract_index
            .get(&contract_id)
            .ok_or(StashInconsistency::ContractAbsent(contract_id))?;
        Ok(index.public_opouts.to_inner())
    }

    fn opouts_by_outpoints(
        &mut self,
        contract_id: ContractId,
        outpoints: impl IntoIterator<Item = impl Into<Outpoint>>,
    ) -> Result<BTreeSet<Opout>, InventoryError<Self::Error>> {
        let index = self
            .contract_index
            .get(&contract_id)
            .ok_or(StashInconsistency::ContractAbsent(contract_id))?;
        let mut opouts = BTreeSet::new();
        for outpoint in outpoints.into_iter().map(|o| o.into()) {
            let set = index
                .outpoint_opouts
                .get(&outpoint)
                .ok_or(DataError::OutpointUnknown(outpoint, contract_id))?;
            opouts.extend(set)
        }
        Ok(opouts)
    }

    fn opouts_by_terminals(
        &mut self,
        terminals: impl IntoIterator<Item = SecretSeal>,
    ) -> Result<BTreeSet<Opout>, InventoryError<Self::Error>> {
        let terminals = terminals.into_iter().collect::<BTreeSet<_>>();
        Ok(self
            .terminal_index
            .iter()
            .filter(|(seal, _)| terminals.contains(*seal))
            .map(|(_, opout)| *opout)
            .collect())
    }

    fn state_for_outpoints(
        &mut self,
        contract_id: ContractId,
        outpoints: impl IntoIterator<Item = impl Into<Outpoint>>,
    ) -> Result<BTreeMap<Opout, TypedState>, InventoryError<Self::Error>> {
        let outpoints = outpoints
            .into_iter()
            .map(|o| o.into())
            .collect::<BTreeSet<_>>();

        let history = self
            .history
            .get(&contract_id)
            .ok_or(StashInconsistency::ContractAbsent(contract_id))?;

        let mut res = BTreeMap::new();

        for output in history.fungibles() {
            if outpoints.contains(&output.seal) {
                res.insert(output.opout, TypedState::Amount(output.state.value.as_u64()));
            }
        }

        for output in history.data() {
            if outpoints.contains(&output.seal) {
                res.insert(output.opout, TypedState::Data(output.state.clone()));
            }
        }

        for output in history.rights() {
            if outpoints.contains(&output.seal) {
                res.insert(output.opout, TypedState::Void);
            }
        }

        for output in history.attach() {
            if outpoints.contains(&output.seal) {
                res.insert(output.opout, TypedState::Attachment(output.state.clone().into()));
            }
        }

        Ok(res)
    }

    fn store_seal_secret(&mut self, seal: GraphSeal) -> Result<(), InventoryError<Self::Error>> {
        self.seal_secrets.push(seal)?;
        Ok(())
    }

    fn seal_secrets(&mut self) -> Result<BTreeSet<GraphSeal>, InventoryError<Self::Error>> {
        Ok(self.seal_secrets.to_inner())
    }
}