2022-05-09 13:15:23 +00:00
|
|
|
import std/json
|
|
|
|
import std/strutils
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
import pkg/questionable/results
|
|
|
|
import ../sales
|
2022-05-11 08:51:59 +00:00
|
|
|
import ../purchasing
|
2023-06-21 05:46:18 +00:00
|
|
|
import ../utils/stintutils
|
|
|
|
|
|
|
|
export json
|
2022-05-09 13:15:23 +00:00
|
|
|
|
2022-05-10 12:13:39 +00:00
|
|
|
type
|
|
|
|
StorageRequestParams* = object
|
|
|
|
duration*: UInt256
|
2023-03-27 13:47:25 +00:00
|
|
|
proofProbability*: UInt256
|
2022-07-20 12:11:00 +00:00
|
|
|
reward*: UInt256
|
2023-04-14 09:04:17 +00:00
|
|
|
collateral*: UInt256
|
2022-05-18 11:28:34 +00:00
|
|
|
expiry*: ?UInt256
|
2022-08-02 15:50:57 +00:00
|
|
|
nodes*: ?uint
|
|
|
|
tolerance*: ?uint
|
2022-05-10 12:13:39 +00:00
|
|
|
|
2022-05-09 13:15:23 +00:00
|
|
|
proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability =
|
|
|
|
let json = ?catch parseJson(string.fromBytes(bytes))
|
2023-06-21 05:46:18 +00:00
|
|
|
let size = ?catch UInt256.fromDecimal(json["size"].getStr)
|
|
|
|
let duration = ?catch UInt256.fromDecimal(json["duration"].getStr)
|
|
|
|
let minPrice = ?catch UInt256.fromDecimal(json["minPrice"].getStr)
|
|
|
|
let maxCollateral = ?catch UInt256.fromDecimal(json["maxCollateral"].getStr)
|
2023-04-14 09:04:17 +00:00
|
|
|
success Availability.init(size, duration, minPrice, maxCollateral)
|
2022-05-10 12:13:39 +00:00
|
|
|
|
|
|
|
proc fromJson*(_: type StorageRequestParams,
|
|
|
|
bytes: seq[byte]): ?! StorageRequestParams =
|
|
|
|
let json = ?catch parseJson(string.fromBytes(bytes))
|
2023-06-21 05:46:18 +00:00
|
|
|
let duration = ?catch UInt256.fromDecimal(json["duration"].getStr)
|
|
|
|
let proofProbability = ?catch UInt256.fromDecimal(json["proofProbability"].getStr)
|
|
|
|
let reward = ?catch UInt256.fromDecimal(json["reward"].getStr)
|
|
|
|
let collateral = ?catch UInt256.fromDecimal(json["collateral"].getStr)
|
|
|
|
let expiry = UInt256.fromDecimal(json["expiry"].getStr).catch.option
|
|
|
|
let nodes = parseUInt(json["nodes"].getStr).catch.option
|
|
|
|
let tolerance = parseUInt(json["tolerance"].getStr).catch.option
|
2022-05-18 11:28:34 +00:00
|
|
|
success StorageRequestParams(
|
|
|
|
duration: duration,
|
2023-03-27 13:47:25 +00:00
|
|
|
proofProbability: proofProbability,
|
2022-07-20 12:11:00 +00:00
|
|
|
reward: reward,
|
2023-04-14 09:04:17 +00:00
|
|
|
collateral: collateral,
|
2022-08-02 15:50:57 +00:00
|
|
|
expiry: expiry,
|
|
|
|
nodes: nodes,
|
|
|
|
tolerance: tolerance
|
2022-05-18 11:28:34 +00:00
|
|
|
)
|
2022-05-11 08:51:59 +00:00
|
|
|
|
|
|
|
func `%`*(address: Address): JsonNode =
|
|
|
|
% $address
|
|
|
|
|
2023-06-21 05:46:18 +00:00
|
|
|
func `%`*(stint: StInt|StUint): JsonNode=
|
|
|
|
%(stint.toString)
|
2022-05-11 08:51:59 +00:00
|
|
|
|
|
|
|
func `%`*(arr: openArray[byte]): JsonNode =
|
|
|
|
%("0x" & arr.toHex)
|
|
|
|
|
[marketplace] Add Reservations Module (#340)
* [marketplace] reservations module
- add de/serialization for Availability
- add markUsed/markUnused in persisted availability
- add query for unused
- add reserve/release
- reservation module tests
- split ContractInteractions into client contracts and host contracts
- remove reservations start/stop as the repo start/stop is being managed by the node
- remove dedicated reservations metadata store and use the metadata store from the repo instead
- Split ContractInteractions into:
- ClientInteractions (with purchasing)
- HostInteractions (with sales and proving)
- compilation fix for nim 1.2
[repostore] fix started flag, add tests
[marketplace] persist slot index
For loading the sales state from chain, the slot index was not previously persisted in the contract. Will retrieve the slot index from the contract when the sales state is loaded.
* Revert repostore changes
In favour of separate PR https://github.com/status-im/nim-codex/pull/374.
* remove warnings
* clean up
* tests: stop repostore during teardown
* change constructor type identifier
Change Contracts constructor to accept Contracts type instead of ContractInteractions.
* change constructor return type to Result instead of Option
* fix and split interactions tests
* clean up, fix tests
* find availability by slot id
* remove duplication in host/client interactions
* add test for finding availability by slotId
* log instead of raiseAssert when failed to mark availability as unused
* move to SaleErrored state instead of raiseAssert
* remove unneeded reverse
It appears that order is not preserved in the repostore, so reversing does not have the intended effect here.
* update open api spec for potential rest endpoint errors
* move functions about available bytes to repostore
* WIP: reserve and release availabilities as needed
WIP: not tested yet
Availabilities are marked as used when matched (just before downloading starts) so that future matching logic does not match an availability currently in use.
As the download progresses, batches of blocks are written to disk, and the equivalent bytes are released from the reservation module. The size of the availability is reduced as well.
During a reserve or release operation, availability updates occur after the repo is manipulated. If the availability update operation fails, the reserve or release is rolled back to maintain correct accounting of bytes.
Finally, once download completes, or if an error occurs, the availability is marked as unused so future matching can occur.
* delete availability when all bytes released
* fix tests + cleanup
* remove availability from SalesContext callbacks
Availability is no longer used past the SaleDownloading state in the state machine. Cleanup of Availability (marking unused) is handled directly in the SaleDownloading state, and no longer in SaleErrored or SaleFinished. Likewise, Availabilities shouldn’t need to be handled on node restart.
Additionally, Availability was being passed in SalesContext callbacks, and now that Availability is only used temporarily in the SaleDownloading state, Availability is contextually irrelevant to the callbacks, except in OnStore possibly, though it was not being consumed.
* test clean up
* - remove availability from callbacks and constructors from previous commit that needed to be removed (oopsie)
- fix integration test that checks availabilities
- there was a bug fixed that crashed the node due to a missing `return success` in onStore
- the test was fixed by ensuring that availabilities are remaining on the node, and the size has been reduced
- change Availability back to non-ref object and constructor back to init
- add trace logging of all state transitions in state machine
- add generally useful trace logging
* fixes after rebase
1. Fix onProve callbacks
2. Use Slot type instead of tuple for retrieving active slot.
3. Bump codex-contracts-eth that exposes getActivceSlot call.
* swap contracts branch to not support slot collateral
Slot collateral changes in the contracts require further changes in the client code, so we’ll skip those changes for now and add in a separate commit.
* modify Interactions and Deployment constructors
- `HostInteractions` and `ClientInteractions` constructors were simplified to take a contract address and no overloads
- `Interactions` prepared simplified so there are no overloads
- `Deployment` constructor updated so that it takes an optional string parameter, instead `Option[string]`
* Move `batchProc` declaration
`batchProc` needs to be consumed by both `node` and `salescontext`, and they can’t reference each other as it creates a circular dependency.
* [reservations] rename `available` to `hasAvailable`
* [reservations] default error message to inner error msg
* add SaleIngored state
When a storage request is handled but the request does match availabilities, the sales agent machine is sent to the SaleIgnored state. In addition, the agent is constructed in a way that if the request is ignored, the sales agent is removed from the list of active agents being tracked in the sales module.
2023-04-04 07:05:16 +00:00
|
|
|
func `%`*(id: RequestId | SlotId | Nonce | AvailabilityId): JsonNode =
|
2022-08-18 05:55:53 +00:00
|
|
|
% id.toArray
|
|
|
|
|
2023-06-20 12:52:15 +00:00
|
|
|
func `%`*(obj: StorageRequest | Slot): JsonNode =
|
|
|
|
let jsonObj = newJObject()
|
|
|
|
for k, v in obj.fieldPairs: jsonObj[k] = %v
|
|
|
|
jsonObj["id"] = %(obj.id)
|
|
|
|
|
|
|
|
return jsonObj
|
|
|
|
|
2022-05-11 08:51:59 +00:00
|
|
|
func `%`*(purchase: Purchase): JsonNode =
|
|
|
|
%*{
|
2023-06-05 08:48:06 +00:00
|
|
|
"state": purchase.state |? "none",
|
2022-05-12 11:43:42 +00:00
|
|
|
"error": purchase.error.?msg,
|
|
|
|
"request": purchase.request,
|
2022-05-11 08:51:59 +00:00
|
|
|
}
|