referrerpolicy=no-referrer-when-downgrade
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
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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 contains routines for accessing and altering a contract transient storage.

use crate::{
	exec::{AccountIdOf, Key},
	storage::WriteOutcome,
	Config, Error,
};
use alloc::{collections::BTreeMap, vec::Vec};
use codec::Encode;
use core::{marker::PhantomData, mem};
use frame_support::DefaultNoBound;
use sp_runtime::{DispatchError, DispatchResult, Saturating};

/// Meter entry tracks transaction allocations.
#[derive(Default, Debug)]
pub struct MeterEntry {
	/// Allocations made in the current transaction.
	pub amount: u32,
	/// Allocations limit in the current transaction.
	pub limit: u32,
}

impl MeterEntry {
	/// Create a new entry.
	fn new(limit: u32) -> Self {
		Self { limit, amount: Default::default() }
	}

	/// Check if the allocated amount exceeds the limit.
	fn exceeds_limit(&self, amount: u32) -> bool {
		self.amount.saturating_add(amount) > self.limit
	}

	/// Absorb the allocation amount of the nested entry into the current entry.
	fn absorb(&mut self, rhs: Self) {
		self.amount.saturating_accrue(rhs.amount)
	}
}

// The storage meter enforces a limit for each transaction,
// which is calculated as free_storage * (1 - 1/16) for each subsequent frame.
#[derive(DefaultNoBound)]
pub struct StorageMeter<T: Config> {
	nested_meters: Vec<MeterEntry>,
	root_meter: MeterEntry,
	_phantom: PhantomData<T>,
}

impl<T: Config> StorageMeter<T> {
	const STORAGE_FRACTION_DENOMINATOR: u32 = 16;
	/// Create a new storage allocation meter.
	fn new(memory_limit: u32) -> Self {
		Self { root_meter: MeterEntry::new(memory_limit), ..Default::default() }
	}

	/// Charge the allocated amount of transaction storage from the meter.
	fn charge(&mut self, amount: u32) -> DispatchResult {
		let meter = self.current_mut();
		if meter.exceeds_limit(amount) {
			return Err(Error::<T>::OutOfTransientStorage.into());
		}
		meter.amount.saturating_accrue(amount);
		Ok(())
	}

	/// Revert a transaction meter.
	fn revert(&mut self) {
		self.nested_meters.pop().expect(
			"A call to revert a meter must be preceded by a corresponding call to start a meter;
			the code within this crate makes sure that this is always the case; qed",
		);
	}

	/// Start a transaction meter.
	fn start(&mut self) {
		let meter = self.current();
		let mut transaction_limit = meter.limit.saturating_sub(meter.amount);
		if !self.nested_meters.is_empty() {
			// Allow use of (1 - 1/STORAGE_FRACTION_DENOMINATOR) of free storage for subsequent
			// calls.
			transaction_limit.saturating_reduce(
				transaction_limit.saturating_div(Self::STORAGE_FRACTION_DENOMINATOR),
			);
		}

		self.nested_meters.push(MeterEntry::new(transaction_limit));
	}

	/// Commit a transaction meter.
	fn commit(&mut self) {
		let transaction_meter = self.nested_meters.pop().expect(
			"A call to commit a meter must be preceded by a corresponding call to start a meter;
			the code within this crate makes sure that this is always the case; qed",
		);
		self.current_mut().absorb(transaction_meter)
	}

	/// The total allocated amount of memory.
	#[cfg(test)]
	fn total_amount(&self) -> u32 {
		self.nested_meters
			.iter()
			.fold(self.root_meter.amount, |acc, e| acc.saturating_add(e.amount))
	}

	/// A mutable reference to the current meter entry.
	pub fn current_mut(&mut self) -> &mut MeterEntry {
		self.nested_meters.last_mut().unwrap_or(&mut self.root_meter)
	}

	/// A reference to the current meter entry.
	pub fn current(&self) -> &MeterEntry {
		self.nested_meters.last().unwrap_or(&self.root_meter)
	}
}

/// An entry representing a journal change.
struct JournalEntry {
	key: Vec<u8>,
	prev_value: Option<Vec<u8>>,
}

impl JournalEntry {
	/// Create a new change.
	fn new(key: Vec<u8>, prev_value: Option<Vec<u8>>) -> Self {
		Self { key, prev_value }
	}

	/// Revert the change.
	fn revert(self, storage: &mut Storage) {
		storage.write(&self.key, self.prev_value);
	}
}

/// A journal containing transient storage modifications.
struct Journal(Vec<JournalEntry>);

impl Journal {
	/// Create a new journal.
	fn new() -> Self {
		Self(Default::default())
	}

	/// Add a change to the journal.
	fn push(&mut self, entry: JournalEntry) {
		self.0.push(entry);
	}

	/// Length of the journal.
	fn len(&self) -> usize {
		self.0.len()
	}

	/// Roll back all journal changes until the chackpoint
	fn rollback(&mut self, storage: &mut Storage, checkpoint: usize) {
		self.0.drain(checkpoint..).rev().for_each(|entry| entry.revert(storage));
	}
}

/// Storage for maintaining the current transaction state.
#[derive(Default)]
struct Storage(BTreeMap<Vec<u8>, Vec<u8>>);

impl Storage {
	/// Read the storage entry.
	fn read(&self, key: &Vec<u8>) -> Option<Vec<u8>> {
		self.0.get(key).cloned()
	}

	/// Write the storage entry.
	fn write(&mut self, key: &Vec<u8>, value: Option<Vec<u8>>) -> Option<Vec<u8>> {
		if let Some(value) = value {
			// Insert storage entry.
			self.0.insert(key.clone(), value)
		} else {
			// Remove storage entry.
			self.0.remove(key)
		}
	}
}

/// Transient storage behaves almost identically to regular storage but is discarded after each
/// transaction. It consists of a `BTreeMap` for the current state, a journal of all changes, and a
/// list of checkpoints. On entry to the `start_transaction` function, a marker (checkpoint) is
/// added to the list. New values are written to the current state, and the previous value is
/// recorded in the journal (`write`). When the `commit_transaction` function is called, the marker
/// to the journal index (checkpoint) of when that call was entered is discarded.
/// On `rollback_transaction`, all entries are reverted up to the last checkpoint.
pub struct TransientStorage<T: Config> {
	// The storage and journal size is limited by the storage meter.
	storage: Storage,
	journal: Journal,
	// The size of the StorageMeter is limited by the stack depth.
	meter: StorageMeter<T>,
	// The size of the checkpoints is limited by the stack depth.
	checkpoints: Vec<usize>,
}

impl<T: Config> TransientStorage<T> {
	/// Create new transient storage with the supplied memory limit.
	pub fn new(memory_limit: u32) -> Self {
		TransientStorage {
			storage: Default::default(),
			journal: Journal::new(),
			checkpoints: Default::default(),
			meter: StorageMeter::new(memory_limit),
		}
	}

	/// Read the storage value. If the entry does not exist, `None` is returned.
	pub fn read(&self, account: &AccountIdOf<T>, key: &Key<T>) -> Option<Vec<u8>> {
		self.storage.read(&Self::storage_key(&account.encode(), &key.hash()))
	}

	/// Write a value to storage.
	///
	/// If the `value` is `None`, then the entry is removed. If `take` is true,
	/// a [`WriteOutcome::Taken`] is returned instead of a [`WriteOutcome::Overwritten`].
	/// If the entry did not exist, [`WriteOutcome::New`] is returned.
	pub fn write(
		&mut self,
		account: &AccountIdOf<T>,
		key: &Key<T>,
		value: Option<Vec<u8>>,
		take: bool,
	) -> Result<WriteOutcome, DispatchError> {
		let key = Self::storage_key(&account.encode(), &key.hash());
		let prev_value = self.storage.read(&key);
		// Skip if the same value is being set.
		if prev_value != value {
			// Calculate the allocation size.
			if let Some(value) = &value {
				// Charge the key, value and journal entry.
				// If a new value is written, a new journal entry is created. The previous value is
				// moved to the journal along with its key, and the new value is written to
				// storage.
				let key_len = key.capacity();
				let mut amount = value
					.capacity()
					.saturating_add(key_len)
					.saturating_add(mem::size_of::<JournalEntry>());
				if prev_value.is_none() {
					// Charge a new storage entry.
					// If there was no previous value, a new entry is added to storage (BTreeMap)
					// containing a Vec for the key and a Vec for the value. The value was already
					// included in the amount.
					amount.saturating_accrue(key_len.saturating_add(mem::size_of::<Vec<u8>>()));
				}
				self.meter.charge(amount as _)?;
			}
			self.storage.write(&key, value);
			// Update the journal.
			self.journal.push(JournalEntry::new(key, prev_value.clone()));
		}

		Ok(match (take, prev_value) {
			(_, None) => WriteOutcome::New,
			(false, Some(prev_value)) => WriteOutcome::Overwritten(prev_value.len() as _),
			(true, Some(prev_value)) => WriteOutcome::Taken(prev_value),
		})
	}

	/// Start a new nested transaction.
	///
	/// This allows to either commit or roll back all changes that are made after this call.
	/// For every transaction there must be a matching call to either `rollback_transaction`
	/// or `commit_transaction`.
	pub fn start_transaction(&mut self) {
		self.meter.start();
		self.checkpoints.push(self.journal.len());
	}

	/// Rollback the last transaction started by `start_transaction`.
	///
	/// Any changes made during that transaction are discarded.
	///
	/// # Panics
	///
	/// Will panic if there is no open transaction.
	pub fn rollback_transaction(&mut self) {
		let checkpoint = self
			.checkpoints
			.pop()
			.expect(
				"A call to rollback_transaction must be preceded by a corresponding call to start_transaction;
				the code within this crate makes sure that this is always the case; qed"
			);
		self.meter.revert();
		self.journal.rollback(&mut self.storage, checkpoint);
	}

	/// Commit the last transaction started by `start_transaction`.
	///
	/// Any changes made during that transaction are committed.
	///
	/// # Panics
	///
	/// Will panic if there is no open transaction.
	pub fn commit_transaction(&mut self) {
		self.checkpoints
			.pop()
			.expect(
				"A call to commit_transaction must be preceded by a corresponding call to start_transaction;
				the code within this crate makes sure that this is always the case; qed"
			);
		self.meter.commit();
	}

	/// The storage allocation meter used for transaction metering.
	#[cfg(any(test, feature = "runtime-benchmarks"))]
	pub fn meter(&mut self) -> &mut StorageMeter<T> {
		return &mut self.meter
	}

	fn storage_key(account: &[u8], key: &[u8]) -> Vec<u8> {
		let mut storage_key = Vec::with_capacity(account.len() + key.len());
		storage_key.extend_from_slice(&account);
		storage_key.extend_from_slice(&key);
		storage_key
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{
		tests::{Test, ALICE, BOB, CHARLIE},
		Error,
	};
	use core::u32::MAX;

	// Calculate the allocation size for the given entry.
	fn allocation_size(
		account: &AccountIdOf<Test>,
		key: &Key<Test>,
		value: Option<Vec<u8>>,
	) -> u32 {
		let mut storage: TransientStorage<Test> = TransientStorage::<Test>::new(MAX);
		storage
			.write(account, key, value, false)
			.expect("Could not write to transient storage.");
		storage.meter().current().amount
	}

	#[test]
	fn read_write_works() {
		let mut storage: TransientStorage<Test> = TransientStorage::<Test>::new(2048);
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false),
			Ok(WriteOutcome::New)
		);
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![2]), true),
			Ok(WriteOutcome::New)
		);
		assert_eq!(
			storage.write(&BOB, &Key::Fix([3; 32]), Some(vec![3]), false),
			Ok(WriteOutcome::New)
		);
		assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1]));
		assert_eq!(storage.read(&ALICE, &Key::Fix([2; 32])), Some(vec![2]));
		assert_eq!(storage.read(&BOB, &Key::Fix([3; 32])), Some(vec![3]));
		// Overwrite values.
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![4, 5]), false),
			Ok(WriteOutcome::Overwritten(1))
		);
		assert_eq!(
			storage.write(&BOB, &Key::Fix([3; 32]), Some(vec![6, 7]), true),
			Ok(WriteOutcome::Taken(vec![3]))
		);
		assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1]));
		assert_eq!(storage.read(&ALICE, &Key::Fix([2; 32])), Some(vec![4, 5]));
		assert_eq!(storage.read(&BOB, &Key::Fix([3; 32])), Some(vec![6, 7]));

		// Check for an empty value.
		assert_eq!(
			storage.write(&BOB, &Key::Fix([3; 32]), Some(vec![]), true),
			Ok(WriteOutcome::Taken(vec![6, 7]))
		);
		assert_eq!(storage.read(&BOB, &Key::Fix([3; 32])), Some(vec![]));

		assert_eq!(
			storage.write(&BOB, &Key::Fix([3; 32]), None, true),
			Ok(WriteOutcome::Taken(vec![]))
		);
		assert_eq!(storage.read(&BOB, &Key::Fix([3; 32])), None);
	}

	#[test]
	fn read_write_with_var_sized_keys_works() {
		let mut storage = TransientStorage::<Test>::new(2048);
		assert_eq!(
			storage.write(
				&ALICE,
				&Key::<Test>::try_from_var([1; 64].to_vec()).unwrap(),
				Some(vec![1]),
				false
			),
			Ok(WriteOutcome::New)
		);
		assert_eq!(
			storage.write(
				&BOB,
				&Key::<Test>::try_from_var([2; 64].to_vec()).unwrap(),
				Some(vec![2, 3]),
				false
			),
			Ok(WriteOutcome::New)
		);
		assert_eq!(
			storage.read(&ALICE, &Key::<Test>::try_from_var([1; 64].to_vec()).unwrap()),
			Some(vec![1])
		);
		assert_eq!(
			storage.read(&BOB, &Key::<Test>::try_from_var([2; 64].to_vec()).unwrap()),
			Some(vec![2, 3])
		);
		// Overwrite values.
		assert_eq!(
			storage.write(
				&ALICE,
				&Key::<Test>::try_from_var([1; 64].to_vec()).unwrap(),
				Some(vec![4, 5]),
				false
			),
			Ok(WriteOutcome::Overwritten(1))
		);
		assert_eq!(
			storage.read(&ALICE, &Key::<Test>::try_from_var([1; 64].to_vec()).unwrap()),
			Some(vec![4, 5])
		);
	}

	#[test]
	fn rollback_transaction_works() {
		let mut storage = TransientStorage::<Test>::new(1024);

		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false),
			Ok(WriteOutcome::New)
		);
		storage.rollback_transaction();
		assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), None)
	}

	#[test]
	fn commit_transaction_works() {
		let mut storage = TransientStorage::<Test>::new(1024);

		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false),
			Ok(WriteOutcome::New)
		);
		storage.commit_transaction();
		assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1]))
	}

	#[test]
	fn overwrite_and_commmit_transaction_works() {
		let mut storage = TransientStorage::<Test>::new(1024);
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false),
			Ok(WriteOutcome::New)
		);
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1, 2]), false),
			Ok(WriteOutcome::Overwritten(1))
		);
		storage.commit_transaction();
		assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1, 2]))
	}

	#[test]
	fn rollback_in_nested_transaction_works() {
		let mut storage = TransientStorage::<Test>::new(1024);
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false),
			Ok(WriteOutcome::New)
		);
		storage.start_transaction();
		assert_eq!(
			storage.write(&BOB, &Key::Fix([1; 32]), Some(vec![1]), false),
			Ok(WriteOutcome::New)
		);
		storage.rollback_transaction();
		storage.commit_transaction();
		assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1]));
		assert_eq!(storage.read(&BOB, &Key::Fix([1; 32])), None)
	}

	#[test]
	fn commit_in_nested_transaction_works() {
		let mut storage = TransientStorage::<Test>::new(1024);
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false),
			Ok(WriteOutcome::New)
		);
		storage.start_transaction();
		assert_eq!(
			storage.write(&BOB, &Key::Fix([1; 32]), Some(vec![2]), false),
			Ok(WriteOutcome::New)
		);
		storage.start_transaction();
		assert_eq!(
			storage.write(&CHARLIE, &Key::Fix([1; 32]), Some(vec![3]), false),
			Ok(WriteOutcome::New)
		);
		storage.commit_transaction();
		storage.commit_transaction();
		storage.commit_transaction();
		assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1]));
		assert_eq!(storage.read(&BOB, &Key::Fix([1; 32])), Some(vec![2]));
		assert_eq!(storage.read(&CHARLIE, &Key::Fix([1; 32])), Some(vec![3]));
	}

	#[test]
	fn rollback_all_transactions_works() {
		let mut storage = TransientStorage::<Test>::new(1024);
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false),
			Ok(WriteOutcome::New)
		);
		storage.start_transaction();
		assert_eq!(
			storage.write(&BOB, &Key::Fix([1; 32]), Some(vec![2]), false),
			Ok(WriteOutcome::New)
		);
		storage.start_transaction();
		assert_eq!(
			storage.write(&CHARLIE, &Key::Fix([1; 32]), Some(vec![3]), false),
			Ok(WriteOutcome::New)
		);
		storage.commit_transaction();
		storage.commit_transaction();
		storage.rollback_transaction();
		assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), None);
		assert_eq!(storage.read(&BOB, &Key::Fix([1; 32])), None);
		assert_eq!(storage.read(&CHARLIE, &Key::Fix([1; 32])), None);
	}

	#[test]
	fn metering_transactions_works() {
		let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]));
		let mut storage = TransientStorage::<Test>::new(size * 2);
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		let limit = storage.meter().current().limit;
		storage.commit_transaction();

		storage.start_transaction();
		assert_eq!(storage.meter().current().limit, limit - size);
		assert_eq!(storage.meter().current().limit - storage.meter().current().amount, size);
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		assert_eq!(storage.meter().current().amount, size);
		storage.commit_transaction();
		assert_eq!(storage.meter().total_amount(), size * 2);
	}

	#[test]
	fn metering_nested_transactions_works() {
		let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]));
		let mut storage = TransientStorage::<Test>::new(size * 3);

		storage.start_transaction();
		let limit = storage.meter().current().limit;
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		storage.start_transaction();
		assert_eq!(storage.meter().total_amount(), size);
		assert!(storage.meter().current().limit < limit - size);
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		storage.commit_transaction();
		assert_eq!(storage.meter().current().limit, limit);
		assert_eq!(storage.meter().total_amount(), storage.meter().current().amount);
		storage.commit_transaction();
	}

	#[test]
	fn metering_transaction_fails() {
		let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]));
		let mut storage = TransientStorage::<Test>::new(size - 1);
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false),
			Err(Error::<Test>::OutOfTransientStorage.into())
		);
		assert_eq!(storage.meter.current().amount, 0);
		storage.commit_transaction();
		assert_eq!(storage.meter.total_amount(), 0);
	}

	#[test]
	fn metering_nested_transactions_fails() {
		let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]));
		let mut storage = TransientStorage::<Test>::new(size * 2);

		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false),
			Err(Error::<Test>::OutOfTransientStorage.into())
		);
		storage.commit_transaction();
		storage.commit_transaction();
		assert_eq!(storage.meter.total_amount(), size);
	}

	#[test]
	fn metering_nested_transaction_with_rollback_works() {
		let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]));
		let mut storage = TransientStorage::<Test>::new(size * 2);

		storage.start_transaction();
		let limit = storage.meter.current().limit;
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		storage.rollback_transaction();

		assert_eq!(storage.meter.total_amount(), 0);
		assert_eq!(storage.meter.current().limit, limit);
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		let amount = storage.meter().current().amount;
		assert_eq!(storage.meter().total_amount(), amount);
		storage.commit_transaction();
	}

	#[test]
	fn metering_with_rollback_works() {
		let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]));
		let mut storage = TransientStorage::<Test>::new(size * 5);

		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		let amount = storage.meter.total_amount();
		storage.start_transaction();
		assert_eq!(
			storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		storage.start_transaction();
		assert_eq!(
			storage.write(&BOB, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false),
			Ok(WriteOutcome::New)
		);
		storage.commit_transaction();
		storage.rollback_transaction();
		assert_eq!(storage.meter.total_amount(), amount);
		storage.commit_transaction();
	}
}