Skip to content

Commit 2c8c628

Browse files
Merge pull request #309 from permaweb/dev/milestone-3-beta-1
HyperBEAM Milestone 3 BETA-1.
2 parents b519dcd + 6957443 commit 2c8c628

118 files changed

Lines changed: 21945 additions & 7264 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.githooks/_/install.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

.githooks/commit-msg

Lines changed: 0 additions & 25 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
config.*
12
.rebar3
23
_build
34
_checkouts

.vscode/launch.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"internalConsoleOptions": "openOnSessionStart",
1515
"module": "hb_debugger",
1616
"function": "start_and_break",
17-
"args": "[${input:moduleName}, ${input:functionName}, [${input:funcArgs}]]"
17+
"args": "[${input:moduleName}, ${input:functionName}, [${input:funcArgs}], <<\"${input:debuggerScope}\">>]"
1818
},
1919
{
2020
"name": "Attach to a 'rebar3 debugger' node.",
@@ -65,6 +65,11 @@
6565
"id": "funcArgs",
6666
"type": "promptString",
6767
"description": "(Optional) Pass arguments to the function:"
68+
},
69+
{
70+
"id": "debuggerScope",
71+
"type": "promptString",
72+
"description": "(Optional) Additional modules/prefixes for debugger scope:"
6873
}
6974
]
7075
}

config.flat

Lines changed: 0 additions & 1 deletion
This file was deleted.

rebar.config

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{plugins, [pc, rebar3_rustler, rebar_edown_plugin]}.
33

44
{profiles, [
5+
{no_events, [{erl_opts, [{d, 'NO_EVENTS', true}]}]},
6+
{store_events, [{erl_opts, [{d, 'STORE_EVENTS', true}]}]},
57
{genesis_wasm, [
68
{erl_opts, [{d, 'ENABLE_GENESIS_WASM', true}]},
79
{pre_hooks, [
@@ -58,7 +60,8 @@
5860
{ compile, "rm -f native/hb_beamr/*.o native/hb_beamr/*.d"},
5961
{ compile, "rm -f native/hb_keccak/*.o native/hb_keccak/*.d"},
6062
{ compile, "mkdir -p priv/html"},
61-
{ compile, "cp -R src/html/* priv/html"}
63+
{ compile, "cp -R src/html/* priv/html"},
64+
{ compile, "cp _build/default/lib/elmdb/priv/* priv/ 2>/dev/null || true" }
6265
]}.
6366

6467
{provider_hooks, [
@@ -87,6 +90,8 @@
8790
]}.
8891

8992
{deps, [
93+
{elmdb, {git, "https://github.com/samcamwilliams/elmdb", {ref, "5feb2b039e40adb3b2b70241d1c212ad162f5659"}}},
94+
{lmdb, {git, "https://github.com/twilson63/erlang-lmdb", {ref, "8a4fedc38c7a44eef6af816b5e840bd362d67f1a"}}},
9095
{b64fast, {git, "https://github.com/ArweaveTeam/b64fast.git", {ref, "58f0502e49bf73b29d95c6d02460d1fb8d2a5273"}}},
9196
{cowboy, {git, "https://github.com/ninenines/cowboy", {ref, "022013b6c4e967957c7e0e7e7cdefa107fc48741"}}},
9297
{gun, {git, "https://github.com/ninenines/gun", {ref, "8efcedd3a089e6ab5317e4310fed424a4ee130f8"}}},
@@ -107,7 +112,7 @@
107112
{eunit_opts, [verbose]}.
108113

109114
{relx, [
110-
{release, {'hb', "0.0.1"}, [hb, b64fast, cowboy, gun, luerl, prometheus, prometheus_cowboy]},
115+
{release, {'hb', "0.0.1"}, [hb, b64fast, cowboy, gun, luerl, prometheus, prometheus_cowboy, elmdb]},
111116
{include_erts, true},
112117
{extended_start_script, true},
113118
{overlay, [
@@ -133,6 +138,11 @@
133138
[
134139
{eunit, "--module dev_lua_test"}
135140
]
141+
},
142+
{'deploy-scripts',
143+
[
144+
{shell, "--eval hb:deploy_scripts()."}
145+
]
136146
}
137147
]}.
138148

rebar.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212
{git,"https://github.com/ninenines/cowlib",
1313
{ref,"1c3d5defba28e92a88ce45c440d57e178ab1c514"}},
1414
1},
15+
{<<"elmdb">>,
16+
{git,"https://github.com/samcamwilliams/elmdb",
17+
{ref,"5feb2b039e40adb3b2b70241d1c212ad162f5659"}},
18+
0},
1519
{<<"gun">>,
1620
{git,"https://github.com/ninenines/gun",
1721
{ref,"8efcedd3a089e6ab5317e4310fed424a4ee130f8"}},
1822
0},
23+
{<<"lmdb">>,
24+
{git,"https://github.com/twilson63/erlang-lmdb",
25+
{ref,"8a4fedc38c7a44eef6af816b5e840bd362d67f1a"}},
26+
0},
1927
{<<"luerl">>,{pkg,<<"luerl">>,<<"1.3.0">>},0},
2028
{<<"prometheus">>,{pkg,<<"prometheus">>,<<"4.11.0">>},0},
2129
{<<"prometheus_cowboy">>,{pkg,<<"prometheus_cowboy">>,<<"0.1.8">>},0},

scripts/dynamic-router.lua

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,25 @@ end
203203
-- Register a new host to a route.
204204
function register(state, assignment, opts)
205205
state = ensure_defaults(state)
206+
ao.event({"register", { state = state, assignment = assignment, opts = opts }})
206207
local req = assignment.body
208+
209+
-- If the message is signed by an explicitly trusted peer, we can skip the
210+
-- is-admissible check.
211+
if state["trusted-peer"] then
212+
local committers = ao.get("committers", req)
213+
for _, committer in ipairs(committers) do
214+
if committer == state["trusted-peer"] then
215+
state = add_node(state, req)
216+
return recalculate(state, assignment, opts)
217+
end
218+
end
219+
end
220+
207221
req.path = state["is-admissible"].path or "is-admissible"
208222
local status, is_admissible = ao.resolve(state["is-admissible"], req)
209223

210-
ao.event("is-admissible result:", { status, is_admissible })
224+
ao.event({"is-admissible result:", { status, is_admissible }})
211225
if status == "ok" and is_admissible == "true" then
212226
state = add_node(state, req)
213227
return recalculate(state, assignment, opts)
@@ -270,11 +284,11 @@ function duration(state, assignment, opts)
270284
end
271285

272286
function compute(state, assignment, opts)
273-
if assignment.body.path == "register" then
287+
if assignment.body.action == "register" then
274288
return register(state, assignment, opts)
275-
elseif assignment.body.path == "recalculate" then
289+
elseif assignment.body.action == "recalculate" then
276290
return recalculate(state, assignment, opts)
277-
elseif assignment.body.path == "performance" then
291+
elseif assignment.body.action == "performance" then
278292
return duration(state, assignment, opts)
279293
else
280294
-- If we have been called without a relevant path, simply ensure that
@@ -377,7 +391,7 @@ function performance_test()
377391
-- Record the starting scores for the nodes
378392
local t0_node1_score = state.routes[1].nodes[1].weight
379393
local t0_node2_score = state.routes[1].nodes[1].weight
380-
394+
381395
if t0_node1_score ~= t0_node2_score then
382396
error("Initial node scores should be equal. Received: "
383397
.. tostring(t0_node1_score) .. " and " .. tostring(t0_node2_score))
@@ -403,7 +417,7 @@ function performance_test()
403417
state = state
404418
}}
405419
)
406-
420+
407421
-- now trigger a recalc
408422
_, state = recalculate(state, { body = { path = "recalculate" } }, {})
409423

@@ -429,6 +443,6 @@ function performance_test()
429443
if t1_node2_score >= t0_node2_score then
430444
error("Node 2 score should have decreased!")
431445
end
432-
446+
433447
return "ok"
434448
end

scripts/hyper-token-p4-client.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--- A simple script that can be used as a `~p4@1.0` ledger device, marshalling
2+
--- requests to a local process.
3+
4+
-- Find the user's balance in the current ledger state.
5+
function balance(base, request)
6+
local status, res = ao.resolve({
7+
path =
8+
base["ledger-path"]
9+
.. "/now/balance/"
10+
.. request["target"]
11+
})
12+
ao.event({ "client received balance response",
13+
{ status = status, res = res, target = request["target"] } }
14+
)
15+
-- If the balance request fails (most likely because the user has no balance),
16+
-- return a balance of 0.
17+
if status ~= "ok" then
18+
return "ok", 0
19+
end
20+
21+
-- We have successfully retrieved the balance, so return it.
22+
return "ok", res
23+
end
24+
25+
-- Charge the user's balance in the current ledger state.
26+
function charge(base, request)
27+
ao.event("debug_charge", {
28+
"client starting charge",
29+
{ request = request, base = base }
30+
})
31+
local status, res = ao.resolve({
32+
path = "(" .. base["ledger-path"] .. ")/push",
33+
method = "POST",
34+
body = request
35+
})
36+
ao.event("debug_charge", {
37+
"client received charge response",
38+
{ status = status, res = res }
39+
})
40+
return "ok", res
41+
end

scripts/hyper-token-p4.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--- An extension to the `hyper-token.lua` script, for execution with the
2+
--- `lua@5.3a` device. This script adds the ability for an `admin' account to
3+
--- charge a user's account. This is useful for allowing a node operator to
4+
--- collect fees from users, if they are running in a trusted execution
5+
--- environment.
6+
---
7+
--- This script must be added as after the `hyper-token.lua` script in the
8+
--- `process-definition`s `script` field.
9+
10+
-- Process an `admin' charge request:
11+
-- 1. Verify the sender's identity.
12+
-- 2. Ensure that the quantity and account are present in the request.
13+
-- 3. Debit the source account.
14+
-- 4. Increment the balance of the recipient account.
15+
function charge(base, assignment)
16+
ao.event("debug_charge", { "Charge received: ", { assignment = assignment } })
17+
local admin = base.admin
18+
local status, res, request = validate_request(base, assignment)
19+
if status ~= "ok" then
20+
return status, res
21+
end
22+
23+
-- Verify that the request is signed by the admin.
24+
local committers = ao.get("committers", {"as", "message@1.0", assignment.body})
25+
ao.event("debug_charge", { "Validating request: ", {
26+
committers = committers,
27+
admin = admin
28+
} })
29+
if count_common(committers, admin) ~= 1 then
30+
return "error", base
31+
end
32+
33+
-- Ensure that the quantity and account are present in the request.
34+
if not request.quantity or not request.account then
35+
ao.event({ "Failure: Quantity or account not found in request.",
36+
{ request = request } })
37+
base.result = {
38+
status = "error",
39+
error = "Quantity or account not found in request."
40+
}
41+
return "ok", base
42+
end
43+
44+
-- Debit the source. Note: We do not check the source balance here, because
45+
-- the node is capable of debiting the source at-will -- even it puts the
46+
-- source into debt. This is important because the node may estimate the
47+
-- cost of an execution at lower than its actual cost. Subsequently, the
48+
-- ledger should at least debit the source, even if the source may not
49+
-- deposit to restore this balance.
50+
ao.event({ "Debit request validated: ", { assignment = assignment } })
51+
base.balance = base.balance or {}
52+
base.balance[request.account] =
53+
(base.balance[request.account] or 0) - request.quantity
54+
55+
-- Increment the balance of the recipient account.
56+
base.balance[request.recipient] =
57+
(base.balance[request.recipient] or 0) + request.quantity
58+
59+
ao.event("debug_charge", { "Charge processed: ", { balances = base.balance } })
60+
return "ok", base
61+
end

0 commit comments

Comments
 (0)