A classic encoding of a merkle tree is to store the layers consecutively
in memory breadth-first. This encoding has several advantages:
* Good performance for accessing successive nodes, such as when
constructing the tree or serializing it
* Significantly lower memory usage - avoids the per-node allocation
overhead which otherwise more than doubles the memory usage for
"regular" 32-byte hashes
* Less memory management - a single memory allocation can reserve memory
for the whole tree meaning that there are fewer allocations to keep
track of
* Simplified buffer lifetimes - with all memory allocated up-front,
there's no need for cross-thread memory management or transfers
While we're here, we can clean up a few other things in the
implementation:
* Move async implementation to `merkletree` so that it doesn't have to
be repeated
* Factor tree construction into preparation and computation - the latter
is the part offloaded onto a different thread
* Simplify task posting - `threadpools` already creates a "task" from
the worker function call
* Deprecate several high-overhead accessors that presumably are only
needed in tests
For minimal correctness, we must ensure that buffers that cross the
thread boundary are allocated and deallocated within the same thread and
that there is no reference counting going on during the computation.
To get there with minimal changes:
* Preallocate a buffer for the outcome of the merkle tree computation
* Pass pointers instead of `ref` types between threads
* Avoid relying on isolation - this is an ORC-only feature
* Add `SharedBuf` as a simple "view" type that allows working with a set
of values while at the same time avoiding allocations and refcounts -
the view checks for out-of-bounds acccess much like a seq, but the user
is responsible for managing lifetime (which in this case is simple since
all that needs to happen is for the task to complete)
* In order not to upset the code too much, use a simple linear packer
for the hashes that simply copies the values back and forth
* Block cancellation and panic if the thread signalling mechanism fails
- cancelling the task itself would require inserting cancellation points
in the computation
The worker task relies on a nuance, namely that calling a closure
procedure does not count as a reference-counting event - while this
works, it can be brittle in "general" code since it's easy to make copy
of the closure itself by accident - the refactoring necessary for
addressing this point is beyond the scope of this change however.
* Use totalRemainingCollateral instead of totalCollateral to compare the availability changes
* Update test to use totalRemainingCollateral instead of totalCollateral when testing OnAvailabilitySaved
* Reduce poll interval
* Fix flaky test
* Fix format
* chore: bump Nim to 2.2.4
* fix: resolve symbol ambiguity and drop auto type
* fix: use reference to task instead of pointer or the compiler will deallocate `task` before the encoding/decoding is done
* fix: convention that maxCollateralPerByte equals totalRemainingCollateral when freeSize is 0 to avoid DivByZeroDefect
* fix: bump compiler version in CI pipeline as well
* Change token allowance method because increaseAllowance does not exist anymore
* Returns collateral when a reservation is deleted and not only a slot is filled
* Remove the returnedCollateral when the slot is not filled by the host
* Add returnedCollateral when the sale is ignored
* Add returnsCollateral variable for ignored state
* Rebase the contracts submodule on the master
* Add integration test
* Fix duration
* Remove unnecessary teardown function
* Remove misleading comment
* Get returned collateral from the request
* Enable logs to debug on CI
* Fix test
* Increase test timeout
* Fix typo
* Fix rebase
* fix(sales): handle cancellation of slot cleanup
Ensures that processing slots from the slot queue
continues even when cleanup of a slot is cancelled.
Co-Authored-By: Eric <5089238+emizzle@users.noreply.github.com>
* chore(reservations): add more `raises` annotations
* Fix cleanup cancellation
* Add remove-agent to trackedfutures instead of the cleanup function
* Increase the timeout to match the request expiry
* Enable logs to debug on CI
* Remove useless except and do not return when add item back to slot queue fails
* Reduce poll interval to detect sale cancelled state
* Avoid cancelling cleanup routine
* Do not cancel creating reservation in order to avoid inconsistent state
* Remove useless try except
---------
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
Co-authored-by: Eric <5089238+emizzle@users.noreply.github.com>
* checked exceptions in stores
* makes asynciter as much exception safe as it gets
* introduce "SafeAsyncIter" that uses Results and limits exceptions to cancellations
* adds {.push raises: [].} to errors
* uses SafeAsyncIter in "listBlocks" and in "getBlockExpirations"
* simplifies safeasynciter (magic of auto)
* gets rid of ugly casts
* tiny fix in hte way we create raising futures in tests of safeasynciter
* Removes two more casts caused by using checked exceptions
* adds an extended explanation of one more complex SafeAsyncIter test
* adds missing "finishOnErr" param in slice constructor of SafeAsyncIter
* better fix for "Error: Exception can raise an unlisted exception: Exception" error.
---------
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
* fix(slotqueue): simplify slot queue workers
- worker is now just an async running loop
- instead of passing a "done" Future, use an
AsyncEvent to signal completion
* chore(slotqueue): address review comments
Co-Authored-By: Eric <5089238+emizzle@users.noreply.github.com>
Co-Authored-By: Dmitriy Ryajov <dryajov@gmail.com>
---------
Co-authored-by: Eric <5089238+emizzle@users.noreply.github.com>
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
* docs(openapi): add local data delete endpoint
* chore: feedback
Co-authored-by: Eric <5089238+emizzle@users.noreply.github.com>
Signed-off-by: Adam Uhlíř <adam@uhlir.dev>
---------
Signed-off-by: Adam Uhlíř <adam@uhlir.dev>
Co-authored-by: Eric <5089238+emizzle@users.noreply.github.com>
* update nim-ethers to version 2.0.0
To allow for gas estimation of contract calls
* contracts: add 10% extra gas to contract calls
These calls could otherwise run out of gas because
the on-chain state may have changed between the time
of the estimate and the time of processing the
transaction.
* makes sure we do not call "get" on unverified result
* make handling of failed blocks in fetchBatched even more explicit
* simplifies allFinishedValues and makes it independent from allFinishedFailed
* only sleep if not iter.finished in fetchBatched