openethereum/ethcore/private-tx/src/private_state_db.rs
Anton Gavrilov 66e4410be7
Private contract migration and offchain state sync (#10748)
* Temp storage for the private state added

* Temp storage for the private state added

* Request message added

* Store and retrieve offchain state logic

* State sync cache

* Private state column added to key value db

* Private state column added to key value db

* Indexing stored states via its hash

* Works with errors changed

* Private state stored into the local db

* Access to private state db added to sync io

* Private state db file added

* Rlp packets for retrieiving private state data added

* Handling of private sync completed message

* Test code fixed

* External flag for offchain storing added

* Test for private state sync added

* Saving private state logic corrected

* Migration code corrected

* Fixes after merge with master

* Merge with head

* Additional checks for slices

* Log for private state retrieval added

* Limit time of retrieving private states

* Store required hashes for every request, mark them stale if needed

* Store requested private state hashes and check received data

* Log stale requests

* State insertion fix

* Refactoring of how logging passed to state store

* Heapsize removed, syncing hashes structure reworked

* Check state length returned by contract

* Get rid of OverlayDB

* hash-db version updated

* Test fixed

* One more test fixed
2019-08-16 14:45:52 +02:00

62 lines
2.2 KiB
Rust

// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.
// Parity Ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc;
use ethereum_types::H256;
use bytes::Bytes;
use kvdb::{KeyValueDB, DBTransaction};
use keccak_hasher::KeccakHasher;
use hash_db::Hasher;
use ethcore_db::COL_PRIVATE_TRANSACTIONS_STATE;
use error::Error;
/// Wrapper around local db with private state for sync purposes
pub struct PrivateStateDB {
db: Arc<KeyValueDB>,
}
impl PrivateStateDB {
/// Constructs the object
pub fn new(db: Arc<KeyValueDB>) -> Self {
PrivateStateDB {
db,
}
}
/// Returns saved state for the hash
pub fn state(&self, state_hash: &H256) -> Result<Bytes, Error> {
trace!(target: "privatetx", "Retrieve private state from db with hash: {:?}", state_hash);
self.db.get(COL_PRIVATE_TRANSACTIONS_STATE, state_hash.as_bytes())
.expect("Low-level database error. Some issue with your hard disk?")
.map(|s| s.to_vec())
.ok_or(Error::PrivateStateNotFound)
}
/// Stores state for the hash
pub fn save_state(&self, storage: &Bytes) -> Result<H256, Error> {
let state_hash = self.state_hash(storage)?;
let mut transaction = DBTransaction::new();
transaction.put(COL_PRIVATE_TRANSACTIONS_STATE, state_hash.as_bytes(), storage);
self.db.write(transaction).map_err(|_| Error::DatabaseWriteError)?;
trace!(target: "privatetx", "Private state saved to db, its hash: {:?}", state_hash);
Ok(state_hash)
}
/// Returns state's hash without committing it to DB
pub fn state_hash(&self, state: &Bytes) -> Result<H256, Error> {
Ok(KeccakHasher::hash(state))
}
}