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
/*
 * Copyright 2019 Bitwise IO, Inc.
 * Copyright 2019 Cargill Incorporated
 *
 * 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.
 * -----------------------------------------------------------------------------
 */

//! This module provides a thread-safe ContextManager.
//!
//! For many uses of the context manager, it will need to be shared between multiple threads,
//! with some threads reading and writing to a context while others create contexts.
use std::sync::{Arc, Mutex};

use crate::context::error::ContextManagerError;
use crate::context::{manager, ContextId, ContextLifecycle};
use crate::protocol::receipt::{Event, TransactionReceipt};
use crate::state::Read;

/// A thread-safe ContextManager.
#[derive(Clone)]
pub struct ContextManager {
    internal_manager: Arc<Mutex<manager::ContextManager>>,
}

impl ContextManager {
    /// Constructs a new Context Manager around a given state Read.
    ///
    /// The Read defines the state on which the context built.
    pub fn new(database: Box<dyn Read<StateId = String, Key = String, Value = Vec<u8>>>) -> Self {
        ContextManager {
            internal_manager: Arc::new(Mutex::new(manager::ContextManager::new(database))),
        }
    }

    /// Return a set of values from a context.
    ///
    /// The values are returned as key-value tuples
    ///
    ///
    /// # Errors
    ///
    /// Returns an error if the context id does not exist, or an error occurs while reading
    /// from the underlying state.
    pub fn get(
        &self,
        context_id: &ContextId,
        keys: &[String],
    ) -> Result<Vec<(String, Vec<u8>)>, ContextManagerError> {
        self.internal_manager
            .lock()
            .expect("Lock in the get method was poisoned")
            .get(context_id, keys)
    }

    /// # Errors
    ///
    /// Returns an error if the context id does not exist, or an error occurs while reading
    /// from the underlying state.
    pub fn set_state(
        &self,
        context_id: &ContextId,
        key: String,
        value: Vec<u8>,
    ) -> Result<(), ContextManagerError> {
        self.internal_manager
            .lock()
            .expect("Lock in set_state was poisoned")
            .set_state(context_id, key, value)
    }

    pub fn delete_state(
        &self,
        context_id: &ContextId,
        key: &str,
    ) -> Result<Option<Vec<u8>>, ContextManagerError> {
        self.internal_manager
            .lock()
            .expect("Lock in delete_state was poisoned")
            .delete_state(context_id, key)
    }

    pub fn add_event(
        &self,
        context_id: &ContextId,
        event: Event,
    ) -> Result<(), ContextManagerError> {
        self.internal_manager
            .lock()
            .expect("Lock in add_event was poisoned")
            .add_event(context_id, event)
    }

    pub fn add_data(
        &self,
        context_id: &ContextId,
        data: Vec<u8>,
    ) -> Result<(), ContextManagerError> {
        self.internal_manager
            .lock()
            .expect("Lock in add_data was poisoned")
            .add_data(context_id, data)
    }
}

impl ContextLifecycle for ContextManager {
    /// Creates a Context, and returns the resulting ContextId.
    fn create_context(&mut self, dependent_contexts: &[ContextId], state_id: &str) -> ContextId {
        self.internal_manager
            .lock()
            .expect("Lock in create_context was poisoned")
            .create_context(dependent_contexts, state_id)
    }

    fn drop_context(&mut self, context_id: ContextId) {
        self.internal_manager
            .lock()
            .expect("Lock in drop_context was poisoned")
            .drop_context(context_id)
    }

    fn get_transaction_receipt(
        &self,
        context_id: &ContextId,
        transaction_id: &str,
    ) -> Result<TransactionReceipt, ContextManagerError> {
        self.internal_manager
            .lock()
            .expect("Lock in get_transaction_receipt was poisoned")
            .get_transaction_receipt(context_id, transaction_id)
    }
}