Fixes for allocator + factory + misc improvements (#3534)

* Clear up import/export misunderstandings

* Fetch minimum period from runtime

* Remove unnecessary comment

This variable is already fetched from the runtime
in the line below.

* Fix bug in factory

The `best_block_id` stayed the same, it was always the
genesis hash. This resulted in the factory failing after
4096 blocks, since `client/db` discards hashes (in this
case the genesis hash) after 4096 blocks from the database.

* Fix tense in error message

* Improve allocator documentation

* Fix bug in allocator

Under certain circumstances an invalid pointer was
returned: when the `ptr` was calculated as equal
to the `max_heap_size`. This is an invalid pointer
since there is no access allowed after the heap limit.

The way to provoke this was to repeatedly allocate
with sizes which were previously not allocated and
immediately deallocate right afterwards. What this
did was to increment the `bumper` with each allocation,
whilst keeping the `total_size` of the heap `0`.
If this repeated allocation/deallocation scheme resulted
in `max_heap_size == ptr` the `ptr` was still returned.

The allocator only checked if the `total_size` was
still within the `max_heap_size` limits, and not
if the resulting `ptr` was still within the valid
heap region.

This commit introduces a check to validate if the
calculated `ptr` is within the heap.

* Add test for zero byte allocation and document behavior

* Improve code readability by introducing a const

* Fix error message in test

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix code review suggestions

* Replace early return with assertion

* Remove test for zero size allocations

* Shorten test code

* Shorten comment

* Make bump() return Result

* Add comment for bump()

* Remove ambiguous comment

* Replace value with const

* Use proof for panic message

* Fix merge

* Add comment regarding minimum allocation size
This commit is contained in:
Michael Müller
2019-09-13 15:47:15 +02:00
committed by Gavin Wood
parent b7c6bc1ed5
commit 5cb8c0dc1c
5 changed files with 147 additions and 62 deletions
+5 -6
View File
@@ -23,7 +23,10 @@ use rand::rngs::StdRng;
use codec::{Encode, Decode};
use keyring::sr25519::Keyring;
use node_runtime::{Call, CheckedExtrinsic, UncheckedExtrinsic, SignedExtra, BalancesCall, ExistentialDeposit};
use node_runtime::{
Call, CheckedExtrinsic, UncheckedExtrinsic, SignedExtra, BalancesCall, ExistentialDeposit,
MinimumPeriod,
};
use primitives::{sr25519, crypto::Pair};
use sr_primitives::{generic::Era, traits::{Block as BlockT, Header as HeaderT, SignedExtension}};
use transaction_factory::RuntimeAdapter;
@@ -32,9 +35,6 @@ use inherents::InherentData;
use timestamp;
use finality_tracker;
// TODO get via api: <T as timestamp::Trait>::MinimumPeriod::get(). See #2587.
const MINIMUM_PERIOD: u64 = 99;
pub struct FactoryState<N> {
block_no: N,
@@ -152,7 +152,7 @@ impl RuntimeAdapter for FactoryState<Number> {
}
fn inherent_extrinsics(&self) -> InherentData {
let timestamp = self.block_no as u64 * MINIMUM_PERIOD;
let timestamp = (self.block_no as u64 + 1) * MinimumPeriod::get();
let mut inherent = InherentData::new();
inherent.put_data(timestamp::INHERENT_IDENTIFIER, &timestamp)
@@ -163,7 +163,6 @@ impl RuntimeAdapter for FactoryState<Number> {
}
fn minimum_balance() -> Self::Balance {
// TODO get correct amount via api. See #2587.
ExistentialDeposit::get()
}