9 Commits

Author SHA1 Message Date
Peter Harpending 46345da283 we now spy keyblocks for spends, need to test but probably close to working 2026-03-02 18:54:33 -08:00
Peter Harpending bc870e5f2d wip 2026-03-02 18:27:05 -08:00
Peter Harpending 4ee1825b43 grids random payload defined
ok so next step is go on the server side
- extract recipient, amount, payload,
- register search pattern
- wait for matching transaction to appear
- notify correct party
2026-02-27 11:43:33 -08:00
Peter Harpending be6aceb41a start spy process 2026-02-27 11:34:33 -08:00
pharpend 8a9f060b2d made a little progress on grids demo
i mostly have my head wrapped around the problem a lot more, and need to just
let my brain sit
2026-02-25 18:22:26 -08:00
pharpend 9adbf67ebd shed biketh 2025-12-30 11:45:25 -08:00
pharpend ee35e6cf1f HTML BIKESHED 2025-12-29 15:41:29 -08:00
pharpend 3343dcf137 basic grids demo seems to work... 2025-12-29 14:04:03 -08:00
pharpend f0d1097f1f qr demo 2025-12-29 08:49:42 -08:00
18 changed files with 816 additions and 53 deletions
+6
View File
@@ -1,3 +1,9 @@
2026-02-25(PRH):
grids: instead of spying the chain for a tx, let's use dead drop
- so for "generate", need to form tx for the user to sign
- make dead drop -> have process that knows each tx and the dead drop location
OPEN LOOPS - 2025-11-12 OPEN LOOPS - 2025-11-12
- websockets - websockets
- separate websocket handling from websocket parsing/sending - separate websocket handling from websocket parsing/sending
+37 -3
View File
@@ -1,9 +1,43 @@
# fewd = front end web dev fewd = front end web dev
=====================================================================
this is me (PRH) trying to learn some front end web dev because pixels are this is me (PRH) trying to learn some front end web dev because pixels are
important despite my wishes. important despite my wishes.
# notes Building/Running
---------------------------------------------------------------------
## goal queue ### Prereqs
1. [Install Erlang and ZX](https://git.qpq.swiss/QPQ-AG/research-megadoc/wiki/Installing-Erlang-and-zx)
2. **DEV ONLY**: `apt install node-typescript` (Devuan Excalibur)
This is needed if you want to **edit** the `.ts` files found in
`/priv/static/js/ts/*.ts`. The built JS files are under version control and can
be found in `/priv/static/js/dist/`
### Building/Running HTTP Server
If you are only changing the Erlang or simply just want to run the program
without developing it, then just run
```
zxh runlocal
```
### Building TS->JS
**This is only necessary if you edited the `.ts` files and want to transpile
them over to JS.**
This requires you installed `tsc` as above.
```
make tsc
```
If you're doing development you may want
```
make watch
```
View File
+20
View File
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>FIXME</title>
<link rel="stylesheet" href="/css/default.css">
</head>
<body>
<div id="titlebar">
<div class="content">
<a href="/" class="tb-home">Home</a>
</div>
</div>
<div class="content">
<h1 class="content-title">FEWD: FIXME</h1>
</div>
</body>
</html>
+9
View File
@@ -0,0 +1,9 @@
/**
* Title: Title
* Description: Description
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: YYYY-MM-DD
* Last-Updated: YYYY-MM-DD
*
* @module
*/
+60
View File
@@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Basic GRIDS Demo</title>
<link rel="stylesheet" href="/css/default.css">
</head>
<body>
<div id="titlebar">
<div class="content">
<a href="/" class="tb-home">Home</a>
</div>
</div>
<div class="content">
<h1 class="content-title">FEWD: GRIDS DEMO</h1>
<h2>Making a Spend</h2>
<label for="grids-n">Network ID:</label>
<input type = "text"
id = "grids-n"
value = "groot.testnet"
disabled>
<br>
<label for="grids-r">Recipient:</label>
<input type = "text"
id = "grids-r"
value = "ak_n6aVQ6PkBdVdv7kRRcfnzDVmBsH6hqEwVWSB6UAEb3kkjrPMe"
disabled>
<br>
<label for="grids-a">Amount (P):</label>
<input type = "number"
id = "grids-a"
value = "69"
disabled>
<br>
<label for="grids-p">Payload:</label>
<input type = "text"
id = "grids-p"
disabled>
<br>
<input type = "button"
id = "grids-submit"
value = "Generate">
<br>
<textarea disabled id="grids-url" hidden></textarea>
<br>
<img id="grids-png" hidden>
</div>
<script src="/js/dist/grids-basic.js"></script>
</body>
</html>
+1
View File
@@ -17,6 +17,7 @@
<ul> <ul>
<li><a href="/echo.html">Echo</a></li> <li><a href="/echo.html">Echo</a></li>
<li><a href="/grids-basic.html">GRIDS: Basic Demo</a></li>
<li><a href="/wfc.html">WFC</a></li> <li><a href="/wfc.html">WFC</a></li>
</ul> </ul>
</div> </div>
+29
View File
@@ -0,0 +1,29 @@
/**
* Title: GRIDS Basic Page Script
* Description: Page Script for /grids-basic.html
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2025-12-29
* Last-Updated: 2025-12-29
*
* @module
*/
/**
* Runs on page load
*/
declare function main(): Promise<void>;
declare function on_submit(n_input: HTMLInputElement, r_input: HTMLInputElement, a_input: HTMLInputElement, p_input: HTMLInputElement, grids_url_elt: HTMLTextAreaElement, grids_png_elt: HTMLImageElement): Promise<void>;
type Safe<t> = {
ok: true;
result: t;
} | {
ok: false;
error: string;
};
type GridsResult = {
url: string;
png_base64: string;
};
/**
* gets the grids url
*/
declare function grids_request(net_id: string, recipient: string, amount: number, payload: string): Promise<Safe<GridsResult>>;
+87
View File
@@ -0,0 +1,87 @@
"use strict";
/**
* Title: GRIDS Basic Page Script
* Description: Page Script for /grids-basic.html
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2025-12-29
* Last-Updated: 2025-12-29
*
* @module
*/
main();
/**
* Runs on page load
*/
async function main() {
let n_input = document.getElementById('grids-n');
let r_input = document.getElementById('grids-r');
let a_input = document.getElementById('grids-a');
let p_input = document.getElementById('grids-p');
let submit_btn = document.getElementById('grids-submit');
let grids_url_elt = document.getElementById('grids-url');
let grids_png_elt = document.getElementById('grids-png');
let rand_data = new Uint8Array(32);
window.crypto.getRandomValues(rand_data);
let hex = rand_data.toHex().toUpperCase();
p_input.value = 'text payload ' + hex;
// Page initialization
submit_btn.addEventListener('click', async function (e) {
await on_submit(n_input, r_input, a_input, p_input, grids_url_elt, grids_png_elt);
});
// enable buttons
submit_btn.disabled = false;
}
async function on_submit(n_input, r_input, a_input, p_input, grids_url_elt, grids_png_elt) {
// pull out values
let network_id = n_input.value;
let recipient = r_input.value;
let amount = parseInt(a_input.value);
let payload = p_input.value;
let result = await grids_request(network_id, recipient, amount, payload);
// show url field and png
if (result.ok) {
let url = result.result.url;
let png_base64 = result.result.png_base64;
let src_prefix = 'data:image/png;base64,';
let src = src_prefix + png_base64;
grids_url_elt.innerText = url;
grids_png_elt.src = src;
grids_url_elt.hidden = false;
grids_png_elt.hidden = false;
}
else {
alert('ERROR: ' + result.error);
}
}
/**
* gets the grids url
*/
async function grids_request(net_id, recipient, amount, payload) {
// format for network transmission
let obj = { 'network_id': net_id,
'recipient': recipient,
'amount': amount,
'payload': payload };
let obj_text = JSON.stringify(obj, undefined, 4);
let url = '/grids-mkdd';
let req_options = { method: 'POST',
headers: { 'content-type': 'application/json' },
body: obj_text };
let result = { ok: false,
error: 'IT DO BE LIKE THAT MISTA STANCIL' };
try {
let response = await fetch(url, req_options);
if (response.ok)
result = await response.json();
else {
console.log('bad http response:', response);
result = { ok: false, error: 'BAD HTTP RESPONSE' };
}
}
catch (x) {
console.log('network error:', x);
result = { ok: false, error: 'NETWORK ERROR' };
}
return result;
}
//# sourceMappingURL=grids-basic.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"grids-basic.js","sourceRoot":"","sources":["../ts/grids-basic.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;AAEH,IAAI,EAAE,CAAC;AAGP;;GAEG;AACH,KAAK,UACL,IAAI;IAGA,IAAI,OAAO,GAAM,QAAQ,CAAC,cAAc,CAAC,SAAS,CAA0B,CAAC;IAC7E,IAAI,OAAO,GAAM,QAAQ,CAAC,cAAc,CAAC,SAAS,CAA0B,CAAC;IAC7E,IAAI,OAAO,GAAM,QAAQ,CAAC,cAAc,CAAC,SAAS,CAA0B,CAAC;IAC7E,IAAI,OAAO,GAAM,QAAQ,CAAC,cAAc,CAAC,SAAS,CAA0B,CAAC;IAC7E,IAAI,UAAU,GAAG,QAAQ,CAAC,cAAc,CAAC,cAAc,CAAqB,CAAC;IAE7E,IAAI,aAAa,GAAG,QAAQ,CAAC,cAAc,CAAC,WAA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,KAAK,EAAE,mBAAmB,EAAC,CAAC;QACrD,CAAC;IACL,CAAC;IACD,OAAO,CAAM,EAAE,CAAC;QACZ,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC;QACjC,MAAM,GAAG,EAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAC,CAAC;IACjD,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
+136
View File
@@ -0,0 +1,136 @@
/**
* Title: GRIDS Basic Page Script
* Description: Page Script for /grids-basic.html
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2025-12-29
* Last-Updated: 2025-12-29
*
* @module
*/
main();
/**
* Runs on page load
*/
async function
main
()
{
let n_input = document.getElementById('grids-n') as HTMLInputElement;
let r_input = document.getElementById('grids-r') as HTMLInputElement;
let a_input = document.getElementById('grids-a') as HTMLInputElement;
let p_input = document.getElementById('grids-p') as HTMLInputElement;
let submit_btn = document.getElementById('grids-submit') as HTMLInputElement;
let grids_url_elt = document.getElementById('grids-url') as HTMLTextAreaElement;
let grids_png_elt = document.getElementById('grids-png') as HTMLImageElement;
let rand_data : Uint8Array = new Uint8Array(32);
window.crypto.getRandomValues(rand_data);
let hex : string = rand_data.toHex().toUpperCase();
p_input.value = 'text payload ' + hex;
// Page initialization
submit_btn.addEventListener(
'click',
async function(e) {
await on_submit(n_input, r_input, a_input, p_input, grids_url_elt, grids_png_elt)
}
);
// enable buttons
submit_btn.disabled = false;
}
async function
on_submit
(n_input : HTMLInputElement,
r_input : HTMLInputElement,
a_input : HTMLInputElement,
p_input : HTMLInputElement,
grids_url_elt : HTMLTextAreaElement,
grids_png_elt : HTMLImageElement)
: Promise<void>
{
// pull out values
let network_id : string = n_input.value;
let recipient : string = r_input.value;
let amount : number = parseInt(a_input.value);
let payload : string = p_input.value;
let result: Safe<GridsResult> = await grids_request(network_id, recipient, amount, payload);
// show url field and png
if (result.ok) {
let url : string = result.result.url;
let png_base64 : string = result.result.png_base64;
let src_prefix : string = 'data:image/png;base64,'
let src = src_prefix + png_base64;
grids_url_elt.innerText = url;
grids_png_elt.src = src;
grids_url_elt.hidden = false;
grids_png_elt.hidden = false;
}
else {
alert('ERROR: ' + result.error);
}
}
type Safe<t> = {ok: true, result: t}
| {ok: false, error: string};
type GridsResult = {url : string,
png_base64: string};
/**
* gets the grids url
*/
async function
grids_request
(net_id : string,
recipient : string,
amount : number,
payload : string)
: Promise<Safe<GridsResult>>
{
// format for network transmission
let obj : object = {'network_id' : net_id,
'recipient' : recipient,
'amount' : amount,
'payload' : payload};
let obj_text : string = JSON.stringify(obj, undefined, 4);
let url = '/grids-mkdd';
let req_options = {method: 'POST',
headers: {'content-type': 'application/json'},
body: obj_text};
let result: Safe<GridsResult> =
{ok : false,
error : 'IT DO BE LIKE THAT MISTA STANCIL'};
try {
let response : Response = await fetch(url, req_options);
if (response.ok)
result = await response.json() as Safe<GridsResult>;
else {
console.log('bad http response:', response);
result = {ok: false, error: 'BAD HTTP RESPONSE'};
}
}
catch (x: any) {
console.log('network error:', x);
result = {ok: false, error: 'NETWORK ERROR'};
}
return result;
}
+70 -42
View File
@@ -9,7 +9,7 @@
-export([ -export([
%% caller context %% caller context
get_url/2, mkdd/4,
%% api %% api
start_link/0, start_link/0,
@@ -21,22 +21,19 @@
-include("$zx_include/zx_logger.hrl"). -include("$zx_include/zx_logger.hrl").
-type mh_str() :: string(). -type hex() :: binary().
-define(SEC, 1).
-define(MIN, 60*SEC).
-define(DEAD_DROP_TTL, 30*MIN).
%% for craig's autism -record(dd,
-record(sp, {created_at :: integer(),
{recipient :: string(), payload :: string()}).
amount :: non_neg_integer(), -type dd() :: #dd{}.
payload :: binary()}).
-type search_pattern() :: #sp{}.
-record(s, -record(s,
{current_gen_height :: pos_integer(), {drops = #{} :: #{hex() := dd()}}).
current_gen_hash :: string(),
current_gen_seen_mb_hashes :: [mh_str()],
past_gen_seen_mb_hashes :: [mh_str()],
looking_for :: [search_pattern()]}).
-type state() :: #s{}. -type state() :: #s{}.
@@ -44,14 +41,20 @@
%% caller context %% caller context
%%----------------------------------------------------------------------------- %%-----------------------------------------------------------------------------
-spec get_url(Amount, Payload) -> {ok, URL, QR_PNG} | {error, term()} -spec mkdd(NetworkId, Recipient, Amount, Payload) -> Result
when Amount :: none | pos_integer(), when NetworkId :: string(),
Payload :: none | binary(), Recipient :: string(),
URL :: string(), Amount :: non_neg_integer(),
QR_PNG :: binary(). Payload :: binary(),
Result :: {ok, URL, QR_PNG}
| {error, string()},
URL :: string(),
QR_PNG :: binary().
% @doc
% make a dead drop
get_url(Amount, Payload) -> mkdd(NetworkId, Recipient, Amount, Payload) ->
gen_server:call(?MODULE, {get_url, Amount, Payload}). gen_server:call(?MODULE, {mkdd, NetworkId, Recipient, Amount, Payload}).
%% gen_server callbacks %% gen_server callbacks
@@ -71,12 +74,10 @@ init(none) ->
{ok, InitState}. {ok, InitState}.
handle_call({get_url, Amount, Payload}, From, State) -> handle_call({mkdd, NetworkId, Recipient, Amount, Payload}, From, State) ->
case i_get_url(Amount, Payload, From, State) of case do_mkdd(NetworkId, Recipient, Amount, Payload, State) of
{ok, URL, PNG, NewState} -> {ok, URL, PNG, NewState} -> {reply, {ok, URL, PNG}, NewState};
{reply, {ok, URL, PNG}, NewState}; Error -> {reply, Error, State}
Error ->
{reply, Error, State}
end; end;
handle_call(Unexpected, From, State) -> handle_call(Unexpected, From, State) ->
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]), tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
@@ -104,19 +105,46 @@ terminate(_, _) ->
%% internals %% internals
%%----------------------------------------------------------------------------- %%-----------------------------------------------------------------------------
-spec i_get_url(Amount, Payload, From, State) -> Result -spec do_mkdd(NetworkId, Recipient, Amount, Payload, State) -> Result
when Amount :: none | pos_integer(), when NetworkId :: string(),
Payload :: none | binary(), Recipient :: string(),
From :: {pid(), reference()}, Amount :: pos_integer(),
State :: state(), Payload :: binary(),
URL :: string(), State :: state(),
QR_PNG :: binary(). Result :: {ok, URL, QR_PNG, NewState}
Result :: {ok, URL, QR_PNG, NewState} | {error, Reason},
| {error, term()}. URL :: string(),
QR_PNG :: binary(),
NewState :: state(),
Reason :: any().
i_get_url(Amount, Payload, From, State) -> do_mkdd(NetId, Recipient, Amount, Payload, State = #s{drops = Drops}) ->
NetworkId = fewd:network_id(), HexStr = rand_hex(),
Pubkey = fewd:pubkey(), CreatedAt = now_sec(),
URL = gmgrids:encode({spend, NetworkId, Pubkey}, TxStr = form_txstr(NetId, Recipient, Amount, Payload),
[{amount, Amount}, {payload, Payload}]), DD = #dd{created_at = CreatedAt, payload = TxStr},
URL = dd_url(HexStr),
PNG = qr:encode_png(unicode:characters_to_binary(URL)),
NewDrops = maps:put(HexStr, DD, Drops),
NewState = State#s{drops = NewDrops},
{ok, URL, PNG, NewState}.
now_sec() ->
calendar:datetime_to_gregorian_seconds(calendar:universal_time()).
-spec rand_hex() -> hex().
rand_hex() ->
unicode:characters_to_binary(hexify(rand:bytes(10))).
hexify(<<B:8, Rest/binary>>) when B < 16#10 -> ["0", integer_to_list(B, 16), hexify(Rest)];
hexify(<<B:8, Rest/binary>>) -> [integer_to_list(B, 16), hexify(Rest)];
hexify(<<>>) -> [].
dd_url(HexStr) ->
unicode:characters_to_list(["grids://", fewd:host(), "/1/d/", HexStr]).
% ref
form_txstr(_NetId, _Recipient, _Amount, _Payload) ->
"foobar".
+33 -2
View File
@@ -238,8 +238,9 @@ route(Sock, get, Route, Request, Received) ->
end; end;
route(Sock, post, Route, Request, Received) -> route(Sock, post, Route, Request, Received) ->
case Route of case Route of
<<"/wfcin">> -> wfcin(Sock, Request) , Received; <<"/grids-mkdd">> -> grids_mkdd(Sock, Request) , Received;
_ -> fd_httpd_utils:http_err(Sock, 404) , Received <<"/wfcin">> -> wfcin(Sock, Request) , Received;
_ -> fd_httpd_utils:http_err(Sock, 404) , Received
end; end;
route(Sock, _, _, _, Received) -> route(Sock, _, _, _, Received) ->
fd_httpd_utils:http_err(Sock, 404), fd_httpd_utils:http_err(Sock, 404),
@@ -319,6 +320,36 @@ ws_echo_loop(Sock, Frames, Received) ->
error(Error) error(Error)
end. end.
%% ------------------------------
%% grids
%% ------------------------------
grids_mkdd(Sock, #request{enctype = json,
body = B = #{"network_id" := NetId,
"recipient" := Recipient,
"amount" := Amount,
"payload" := Payload}}) ->
tell("grids_mkdd good request: ~tp", [B]),
RespObj =
case fd_gridsd:mkdd(NetId, Recipient, Amount, unicode:characters_to_binary(Payload)) of
{ok, URL, PNG} ->
#{"ok" => true,
"result" => #{"url" => URL,
"png_base64" => unicode:characters_to_list(base64:encode(PNG))}};
{error, String} ->
#{"ok" => false,
"error" => String}
end,
Body = zj:encode(RespObj),
% update cache with new context
Response = #response{headers = [{"content-type", "application/json"}],
body = Body},
fd_httpd_utils:respond(Sock, Response);
grids_mkdd(Sock, Request) ->
tell("grids_mkdd: bad request: ~tp", [Request]),
fd_httpd_utils:http_err(Sock, 400).
%% ------------------------------ %% ------------------------------
%% wfc %% wfc
+84
View File
@@ -0,0 +1,84 @@
% @doc hz helper functions
-module(fd_hz).
-export_type([
]).
-export([
txs_current/0,
txs_since_height/1,
txs_minus/1,
txs_from_to/2,
current_height/0,
txs_of_height/1,
mhs_of_height/1,
txs_of_mh/1,
filter_spends/1,
filter_spend/1,
spend_info/1,
test/0
]).
-spec test() -> no_return().
test() ->
io:format("~tp~n", [hz:gen_current()]),
io:format("~tp~n", [hz:kb_current()]),
ok.
txs_current() ->
H = current_height(),
txs_of_height(H).
txs_since_height(H) ->
txs_from_to(H, current_height()).
txs_minus(N) ->
H = current_height(),
L = H - N,
txs_from_to(L, H).
txs_from_to(Min, Max) when Min =< Max ->
lists:append([txs_of_height(H) || H <- lists:seq(Min, Max)]);
txs_from_to(Min, Max) when Min > Max ->
[].
current_height() ->
{ok, H} = hz:kb_current_height(),
H.
txs_of_height(Height) ->
lists:append([txs_of_mh(MH) || MH <- mhs_of_height(Height)]).
-spec mhs_of_height(Height :: pos_integer()) -> term().
mhs_of_height(Height) ->
{ok, #{"micro_blocks" := MHs}} = hz:gen_by_height(Height),
MHs.
txs_of_mh(MH) ->
{ok, TXs} = hz:mb_txs(MH),
TXs.
filter_spends(TXs) ->
lists:filtermap(fun filter_spend/1, TXs).
filter_spend(#{"tx" := TX = #{"type" := "SpendTx"}}) -> {true, spend_info(TX)};
filter_spend(_) -> false.
spend_info(#{"type" := "SpendTx",
"recipient_id" := Recipient,
"amount" := Amount,
"payload" := Payload}) ->
{sp, Recipient, Amount, Payload}.
+213
View File
@@ -0,0 +1,213 @@
% @doc spy: knows constraints {recipient, amount, payload}
%
% spies the chain for transactions that satisfy that constraint
%
-module(fd_spy).
-vsn("0.2.0").
% MVP: register search patterns
% simply print to console when one of them is found
-behavior(gen_server).
-export_type([
]).
-export([
%% caller context
reg/3,
%% api
start_link/0,
%% process context
init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2
]).
% recipient is a string
-type pubkey32() :: <<_:256>>.
-record(sp,
{recipient :: pubkey32(),
amount :: pos_integer(),
payload :: binary()}).
-type search_pattern() :: #sp{}.
-record(s,
{last_height_seen = none :: none | integer(),
searching_for = [] :: [search_pattern()]}).
-type state() :: #s{}.
-include("$zx_include/zx_logger.hrl").
%%-----------------------------------------------------------------------------
%% caller context
%%-----------------------------------------------------------------------------
-spec reg(Recipient, Amount, Payload) -> ok | {error, Reason}
when Recipient :: pubkey32(),
Amount :: pos_integer(),
Payload :: binary(),
Reason :: any().
reg(R, A, P) when is_binary(R), byte_size(R) =:= 32,
is_integer(A), A >= 0,
is_binary(P) ->
gen_server:call(?MODULE, {reg, R, A, P}).
%% gen_server callbacks
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
%%-----------------------------------------------------------------------------
%% process context below this line
%%-----------------------------------------------------------------------------
%% gen_server callbacks
-spec init(none) -> {ok, state()}.
init(none) ->
tell("starting fd_spy"),
hz:chain_nodes(fewd:chain_nodes()),
erlang:send_after(1000, self(), check_chain),
InitState = #s{},
{ok, InitState}.
-spec handle_call(Msg, From, State) -> Result
when Msg :: any(),
From :: {pid(), reference()},
State :: state(),
Result :: {reply, Reply, NewState}
| {noreply, NewState},
Reply :: any(),
NewState :: state().
handle_call({reg, Recipient, Amount, Payload}, _From, State) ->
{Reply, NewState} = do_reg(Recipient, Amount, Payload, State),
{reply, Reply, NewState};
handle_call(Unexpected, From, State) ->
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
{noreply, State}.
handle_cast(Unexpected, State) ->
tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]),
{noreply, State}.
handle_info(check_chain, State) ->
NewState = do_check_chain(State),
erlang:send_after(1000, self(), check_chain),
{noreply, NewState};
handle_info(Unexpected, State) ->
tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]),
{noreply, State}.
code_change(_, State, _) ->
{ok, State}.
terminate(_, _) ->
ok.
%%-----------------------------------------------------------------------------
%% internals
%%-----------------------------------------------------------------------------
do_check_chain(State = #s{last_height_seen = none}) ->
case hz:kb_current_height() of
{ok, Max} ->
hh(Max-1, Max, State);
Error ->
tell("~tp hz error: ~tp", [?MODULE, Error]),
State
end;
do_check_chain(State = #s{last_height_seen = Min}) ->
case hz:kb_current_height() of
{ok, Max} ->
hh(Min, Max, State);
Error ->
tell("~tp hz error: ~tp", [?MODULE, Error]),
State
end.
% handle height
hh(PrevHeight, NewHeight, State) when PrevHeight < NewHeight ->
tell("~tp cool: PrevHeight=~tp, NewHeight=~tp", [?MODULE, PrevHeight, NewHeight]),
Spends = fd_hz:filter_spends(fd_hz:txs_from_to(PrevHeight + 1, NewHeight)),
tell("~tp spends: ~tp", [?MODULE, Spends]),
NewState = State#s{last_height_seen = NewHeight},
NewState;
hh(PrevHeight, NewHeight, State) when PrevHeight >= NewHeight ->
log(info, "~tp lame: PrevHeight=~tp, NewHeight=~tp", [?MODULE, PrevHeight, NewHeight]),
State.
-spec do_reg(Recipient, Amount, Payload, State) -> {Reply, NewState}
when Recipient :: pubkey32(),
Amount :: pos_integer(),
Payload :: binary(),
State :: state(),
Reply :: ok
| {error, Reason},
Reason :: any(),
NewState :: state().
do_reg(Recipient, Amount, Payload, State) ->
case already_registered(Recipient, Payload, State) of
true -> {error, already_registered};
false -> really_register(Recipient, Amount, Payload, State)
end.
-spec already_registered(Recipient, Payload, State) -> boolean()
when Recipient :: pubkey32(),
Payload :: binary(),
State :: state().
already_registered(Recipient, Payload, _State = #s{searching_for = SPs}) ->
ar(Recipient, Payload, SPs).
ar(Recipient, Payload, [#sp{recipient=Recipient, payload=Payload} | _]) ->
true;
ar(Recipient, Payload, [_ | Rest]) ->
ar(Recipient, Payload, Rest);
ar(_, _, []) ->
false.
-spec really_register(Recipient, Amount, Payload, State) -> {Reply, NewState}
when Recipient :: pubkey32(),
Amount :: pos_integer(),
Payload :: binary(),
State :: state(),
Reply :: ok
| {error, Reason},
Reason :: any(),
NewState :: state().
really_register(R, A, P, State = #s{searching_for = SPs}) ->
NewSearchPattern = #sp{recipient = R,
amount = A,
payload = P},
tell("~tp: really_register(~tp,~tp,~tp)", [?MODULE, R, A, P]),
NewSearchPatterns = [NewSearchPattern | SPs],
NewState = State#s{searching_for = NewSearchPatterns},
{reply, ok, NewState}.
+7 -1
View File
@@ -36,6 +36,12 @@ start_link() ->
init([]) -> init([]) ->
RestartStrategy = {one_for_one, 1, 60}, RestartStrategy = {one_for_one, 1, 60},
Spy = {fd_spy,
{fd_spy, start_link, []},
permanent,
5000,
worker,
[fd_spy]},
GridsD = {fd_gridsd, GridsD = {fd_gridsd,
{fd_gridsd, start_link, []}, {fd_gridsd, start_link, []},
permanent, permanent,
@@ -54,5 +60,5 @@ init([]) ->
5000, 5000,
supervisor, supervisor,
[fd_httpd]}, [fd_httpd]},
Children = [GridsD, WFCd, Httpd], Children = [Spy, GridsD, WFCd, Httpd],
{ok, {RestartStrategy, Children}}. {ok, {RestartStrategy, Children}}.
+11 -2
View File
@@ -9,15 +9,24 @@
-copyright("Peter Harpending <peterharpending@qpq.swiss>"). -copyright("Peter Harpending <peterharpending@qpq.swiss>").
-license("BSD-2-Clause-FreeBSD"). -license("BSD-2-Clause-FreeBSD").
-export([network_id/0, pubkey/0]). -export([chain_nodes/0, url/0, host/0, network_id/0, pubkey/0, akstr/0]).
-export([listen/1, ignore/0]). -export([listen/1, ignore/0]).
-export([start/2, stop/1]). -export([start/2, stop/1]).
-include("$zx_include/zx_logger.hrl"). -include("$zx_include/zx_logger.hrl").
%% for testing: use mainnet
%chain_nodes() ->
% [{"tsuriai.jp", 3013}].
chain_nodes() ->
[{"tsuriai.jp", 4013}].
url() -> "http://" ++ host().
host() -> "localhost:8000".
network_id() -> "groot.testnet". network_id() -> "groot.testnet".
pubkey() -> pad32("fewd demo"). pubkey() -> pad32(<<"fewd demo">>).
akstr() -> gmgrids:akstr(pubkey()).
pad32(Bytes) -> pad32(Bytes) ->
+12 -3
View File
@@ -5,9 +5,18 @@
{prefix,"fd"}. {prefix,"fd"}.
{desc,"Front End Web Dev in Erlang stuff"}. {desc,"Front End Web Dev in Erlang stuff"}.
{package_id,{"otpr","fewd",{0,2,0}}}. {package_id,{"otpr","fewd",{0,2,0}}}.
{deps,[{"otpr","hakuzaru",{0,7,0}}, {deps,[{"otpr","hakuzaru",{0,8,3}},
{"otpr","qr",{0,1,0}}, {"otpr","sophia",{9,0,0}},
{"otpr","zj",{1,1,2}}]}. {"otpr","gmserialization",{0,1,3}},
{"otpr","eblake2",{1,0,1}},
{"otpr","base58",{0,1,1}},
{"otpr","gmbytecode",{3,4,1}},
{"otpr","base58",{0,1,1}},
{"otpr","eblake2",{1,0,1}},
{"otpr","ec_utils",{1,0,0}},
{"otpr","zj",{1,1,2}},
{"otpr","getopt",{1,0,2}},
{"otpr","qr",{0,1,0}}]}.
{key_name,none}. {key_name,none}.
{a_email,"peterharpending@qpq.swiss"}. {a_email,"peterharpending@qpq.swiss"}.
{c_email,"peterharpending@qpq.swiss"}. {c_email,"peterharpending@qpq.swiss"}.