2022-04-21 14:33:20 +00:00
|
|
|
import std/osproc
|
|
|
|
import std/httpclient
|
2022-05-09 13:15:23 +00:00
|
|
|
import std/json
|
2022-04-21 14:33:20 +00:00
|
|
|
import pkg/chronos
|
2022-05-18 12:31:45 +00:00
|
|
|
import ./ethertest
|
|
|
|
import ./contracts/time
|
2022-05-12 11:42:18 +00:00
|
|
|
import ./integration/nodes
|
2022-05-18 12:31:45 +00:00
|
|
|
import ./integration/tokens
|
2022-04-21 14:33:20 +00:00
|
|
|
|
2022-05-18 12:31:45 +00:00
|
|
|
ethersuite "Integration tests":
|
2022-04-21 14:33:20 +00:00
|
|
|
|
|
|
|
var node1, node2: Process
|
2022-05-09 14:51:08 +00:00
|
|
|
var baseurl1, baseurl2: string
|
2022-04-21 14:33:20 +00:00
|
|
|
var client: HttpClient
|
|
|
|
|
|
|
|
setup:
|
2022-05-18 12:31:45 +00:00
|
|
|
await provider.getSigner(accounts[0]).mint()
|
|
|
|
await provider.getSigner(accounts[1]).mint()
|
|
|
|
await provider.getSigner(accounts[1]).deposit()
|
|
|
|
node1 = startNode [
|
|
|
|
"--api-port=8080",
|
|
|
|
"--udp-port=8090",
|
|
|
|
"--eth-account=" & $accounts[0]
|
|
|
|
]
|
|
|
|
node2 = startNode [
|
|
|
|
"--api-port=8081",
|
|
|
|
"--udp-port=8091",
|
|
|
|
"--eth-account=" & $accounts[1]
|
|
|
|
]
|
2022-05-19 19:56:03 +00:00
|
|
|
baseurl1 = "http://localhost:8080/api/codex/v1"
|
|
|
|
baseurl2 = "http://localhost:8081/api/codex/v1"
|
2022-04-21 14:33:20 +00:00
|
|
|
client = newHttpClient()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
client.close()
|
|
|
|
node1.stop()
|
|
|
|
node2.stop()
|
|
|
|
|
|
|
|
test "nodes can print their peer information":
|
2022-05-09 14:51:08 +00:00
|
|
|
let info1 = client.get(baseurl1 & "/info").body
|
|
|
|
let info2 = client.get(baseurl2 & "/info").body
|
2022-04-21 14:33:20 +00:00
|
|
|
check info1 != info2
|
2022-04-26 06:49:27 +00:00
|
|
|
|
2022-05-10 12:13:39 +00:00
|
|
|
test "node accepts file uploads":
|
|
|
|
let url = baseurl1 & "/upload"
|
|
|
|
let response = client.post(url, "some file contents")
|
|
|
|
check response.status == "200 OK"
|
|
|
|
|
2022-04-26 06:49:27 +00:00
|
|
|
test "node handles new storage availability":
|
2022-05-09 14:51:08 +00:00
|
|
|
let url = baseurl1 & "/sales/availability"
|
|
|
|
let json = %*{"size": "0x1", "duration": "0x2", "minPrice": "0x3"}
|
|
|
|
check client.post(url, $json).status == "200 OK"
|
|
|
|
|
|
|
|
test "node lists storage that is for sale":
|
|
|
|
let url = baseurl1 & "/sales/availability"
|
|
|
|
let json = %*{"size": "0x1", "duration": "0x2", "minPrice": "0x3"}
|
|
|
|
let availability = parseJson(client.post(url, $json).body)
|
|
|
|
let response = client.get(url)
|
2022-05-09 13:15:23 +00:00
|
|
|
check response.status == "200 OK"
|
2022-06-09 15:36:59 +00:00
|
|
|
check %*availability in parseJson(response.body)
|
2022-05-10 12:13:39 +00:00
|
|
|
|
|
|
|
test "node handles storage request":
|
|
|
|
let cid = client.post(baseurl1 & "/upload", "some file contents").body
|
|
|
|
let url = baseurl1 & "/storage/request/" & cid
|
|
|
|
let json = %*{"duration": "0x1", "maxPrice": "0x2"}
|
|
|
|
let response = client.post(url, $json)
|
|
|
|
check response.status == "200 OK"
|
2022-05-11 08:51:59 +00:00
|
|
|
|
|
|
|
test "node retrieves purchase status":
|
|
|
|
let cid = client.post(baseurl1 & "/upload", "some file contents").body
|
|
|
|
let request = %*{"duration": "0x1", "maxPrice": "0x2"}
|
|
|
|
let id = client.post(baseurl1 & "/storage/request/" & cid, $request).body
|
|
|
|
let response = client.get(baseurl1 & "/storage/purchases/" & id)
|
|
|
|
check response.status == "200 OK"
|
|
|
|
let json = parseJson(response.body)
|
|
|
|
check json["request"]["ask"]["duration"].getStr == "0x1"
|
|
|
|
check json["request"]["ask"]["maxPrice"].getStr == "0x2"
|
2022-05-18 12:31:45 +00:00
|
|
|
|
|
|
|
test "nodes negotiate contracts on the marketplace":
|
|
|
|
proc sell =
|
|
|
|
let json = %*{"size": "0x1F00", "duration": "0x200", "minPrice": "0x300"}
|
|
|
|
discard client.post(baseurl2 & "/sales/availability", $json)
|
|
|
|
|
|
|
|
proc available: JsonNode =
|
|
|
|
client.get(baseurl2 & "/sales/availability").body.parseJson
|
|
|
|
|
|
|
|
proc upload: string =
|
|
|
|
client.post(baseurl1 & "/upload", "some file contents").body
|
|
|
|
|
|
|
|
proc buy(cid: string): string =
|
2022-07-04 02:33:55 +00:00
|
|
|
when defined(windows):
|
|
|
|
# Windows builds in GitHub Actions need a longer expiry
|
|
|
|
let expiry = ((waitFor provider.currentTime()) + 30).toHex
|
|
|
|
else:
|
|
|
|
let expiry = ((waitFor provider.currentTime()) + 10).toHex
|
2022-05-18 12:31:45 +00:00
|
|
|
let json = %*{"duration": "0x100", "maxPrice": "0x400", "expiry": expiry}
|
|
|
|
client.post(baseurl1 & "/storage/request/" & cid, $json).body
|
|
|
|
|
2022-07-04 02:33:55 +00:00
|
|
|
proc finish(purchase: string): Future[JsonNode] {.async.} =
|
2022-05-18 12:31:45 +00:00
|
|
|
while true:
|
|
|
|
let response = client.get(baseurl1 & "/storage/purchases/" & purchase)
|
2022-07-04 02:33:55 +00:00
|
|
|
let json = parseJson(response.body)
|
|
|
|
if json["finished"].getBool: return json
|
|
|
|
await sleepAsync(1.seconds)
|
2022-05-18 12:31:45 +00:00
|
|
|
|
|
|
|
sell()
|
2022-07-04 02:33:55 +00:00
|
|
|
let purchase = waitFor upload().buy().finish()
|
2022-05-18 12:31:45 +00:00
|
|
|
|
|
|
|
check purchase["error"].getStr == ""
|
2022-07-06 13:38:02 +00:00
|
|
|
check purchase["selected"].getStr == $accounts[1]
|
2022-05-18 12:31:45 +00:00
|
|
|
check available().len == 0
|