From 2486a05d60bace7a35140f27f32cc85c64bc15d5 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Mon, 3 Aug 2020 04:20:05 -0700 Subject: [PATCH] Rename subscription to peer (#212) * Rename subscription to peer * Rename subscribed to downloading * Rename publishing to uploading * Rename subcription to peer in frontend --- .../CreatePeerDialog.js} | 52 +++---- .../components/CreatePeerDialog/package.json | 6 + .../styles.js | 0 .../CreateSubscriptionDialog/package.json | 6 - .../src/components/Layout/Layout.js | 8 +- .../src/components/Sidebar/Sidebar.js | 4 +- .../src/pages/peer/Peer.js | 131 ++++++++++++++++++ .../src/pages/peer/package.json | 6 + .../pages/{subscription => peer}/styles.js | 0 .../Subscriptions.js => peers/Peers.js} | 76 +++++----- .../src/pages/peers/package.json | 6 + .../src/pages/subscription/Subscription.js | 131 ------------------ .../src/pages/subscription/package.json | 6 - .../src/pages/subscriptions/package.json | 6 - init.sql | 10 +- itests/tests/conftest.py | 12 +- itests/tests/test_squeak_node.py | 96 ++++++------- proto/lnd.proto | 20 +-- proto/squeak_admin.proto | 94 ++++++------- .../admin/squeak_admin_server_handler.py | 44 +++--- .../admin/squeak_admin_server_servicer.py | 84 +++++------ squeakserver/node/squeak_node.py | 38 ++--- ...ownloader.py => squeak_peer_downloader.py} | 20 +-- squeakserver/server/postgres_db.py | 80 +++++------ squeakserver/server/squeak_peer.py | 5 + squeakserver/server/squeak_subscription.py | 5 - 26 files changed, 473 insertions(+), 473 deletions(-) rename frontend/squeak-node-frontend/src/components/{CreateSubscriptionDialog/CreateSubscriptionDialog.js => CreatePeerDialog/CreatePeerDialog.js} (71%) create mode 100644 frontend/squeak-node-frontend/src/components/CreatePeerDialog/package.json rename frontend/squeak-node-frontend/src/components/{CreateSubscriptionDialog => CreatePeerDialog}/styles.js (100%) delete mode 100644 frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/package.json create mode 100644 frontend/squeak-node-frontend/src/pages/peer/Peer.js create mode 100644 frontend/squeak-node-frontend/src/pages/peer/package.json rename frontend/squeak-node-frontend/src/pages/{subscription => peer}/styles.js (100%) rename frontend/squeak-node-frontend/src/pages/{subscriptions/Subscriptions.js => peers/Peers.js} (57%) create mode 100644 frontend/squeak-node-frontend/src/pages/peers/package.json delete mode 100644 frontend/squeak-node-frontend/src/pages/subscription/Subscription.js delete mode 100644 frontend/squeak-node-frontend/src/pages/subscription/package.json delete mode 100644 frontend/squeak-node-frontend/src/pages/subscriptions/package.json rename squeakserver/node/{squeak_subscription_downloader.py => squeak_peer_downloader.py} (64%) create mode 100644 squeakserver/server/squeak_peer.py delete mode 100644 squeakserver/server/squeak_subscription.py diff --git a/frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/CreateSubscriptionDialog.js b/frontend/squeak-node-frontend/src/components/CreatePeerDialog/CreatePeerDialog.js similarity index 71% rename from frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/CreateSubscriptionDialog.js rename to frontend/squeak-node-frontend/src/components/CreatePeerDialog/CreatePeerDialog.js index 5c550489..f05a267a 100644 --- a/frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/CreateSubscriptionDialog.js +++ b/frontend/squeak-node-frontend/src/components/CreatePeerDialog/CreatePeerDialog.js @@ -30,13 +30,13 @@ import Widget from "../../components/Widget"; import SqueakThreadItem from "../../components/SqueakThreadItem"; import { - CreateSubscriptionRequest, + CreatePeerRequest, } from "../../proto/squeak_admin_pb" import {SqueakAdminClient} from "../../proto/squeak_admin_grpc_web_pb" var client = new SqueakAdminClient('http://' + window.location.hostname + ':8080') -export default function CreateSubscriptionDialog({ +export default function CreatePeerDialog({ open, handleClose, ...props @@ -44,12 +44,12 @@ export default function CreateSubscriptionDialog({ var classes = useStyles(); const history = useHistory(); - var [subscriptionName, setsubscriptionName] = useState(''); + var [peerName, setpeerName] = useState(''); var [host, setHost] = useState(''); var [port, setPort] = useState(''); - const handleChangeSubscriptionName = (event) => { - setsubscriptionName(event.target.value); + const handleChangePeerName = (event) => { + setpeerName(event.target.value); }; const handleChangeHost = (event) => { @@ -60,23 +60,23 @@ export default function CreateSubscriptionDialog({ setPort(event.target.value); }; - const createSubscription = (subscriptionName, host, port) => { - console.log("called createSubscription"); + const createPeer = (peerName, host, port) => { + console.log("called createPeer"); - var createSubscriptionRequest = new CreateSubscriptionRequest() - createSubscriptionRequest.setSubscriptionName(subscriptionName); - createSubscriptionRequest.setHost(host); - createSubscriptionRequest.setPort(port); - console.log(createSubscriptionRequest); + var createPeerRequest = new CreatePeerRequest() + createPeerRequest.setPeerName(peerName); + createPeerRequest.setHost(host); + createPeerRequest.setPort(port); + console.log(createPeerRequest); - client.createSubscription(createSubscriptionRequest, {}, (err, response) => { + client.createPeer(createPeerRequest, {}, (err, response) => { if (err) { console.log(err.message); - alert('Error creating subscription: ' + err.message); + alert('Error creating peer: ' + err.message); return; } console.log(response); - console.log(response.getSubscriptionId()); + console.log(response.getPeerId()); // goToProfilePage(response.getProfileId()); }); }; @@ -87,7 +87,7 @@ export default function CreateSubscriptionDialog({ function handleSubmit(event) { event.preventDefault(); - console.log( 'subscriptionName:', subscriptionName); + console.log( 'peerName:', peerName); console.log( 'host:', host); console.log( 'port:', port); if (!host) { @@ -98,18 +98,18 @@ export default function CreateSubscriptionDialog({ alert('Port cannot be empty.'); return; } - createSubscription(subscriptionName, host, port); + createPeer(peerName, host, port); handleClose(); } - function CreateSubscriptionNameInput() { + function CreatePeerNameInput() { return ( @@ -154,7 +154,7 @@ export default function CreateSubscriptionDialog({ ) } - function CreateSubscriptionButton() { + function CreatePeerButton() { return ( ) } return ( - Create Subscription + Create Peer
- {CreateSubscriptionNameInput()} + {CreatePeerNameInput()}
{CreateHostInput()} @@ -184,7 +184,7 @@ export default function CreateSubscriptionDialog({ {CancelButton()} - {CreateSubscriptionButton()} + {CreatePeerButton()}
diff --git a/frontend/squeak-node-frontend/src/components/CreatePeerDialog/package.json b/frontend/squeak-node-frontend/src/components/CreatePeerDialog/package.json new file mode 100644 index 00000000..55711ae1 --- /dev/null +++ b/frontend/squeak-node-frontend/src/components/CreatePeerDialog/package.json @@ -0,0 +1,6 @@ +{ + "name": "CreatePeerDialog", + "version": "0.0.0", + "private": true, + "main": "CreatePeerDialog.js" +} diff --git a/frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/styles.js b/frontend/squeak-node-frontend/src/components/CreatePeerDialog/styles.js similarity index 100% rename from frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/styles.js rename to frontend/squeak-node-frontend/src/components/CreatePeerDialog/styles.js diff --git a/frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/package.json b/frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/package.json deleted file mode 100644 index 5c90a582..00000000 --- a/frontend/squeak-node-frontend/src/components/CreateSubscriptionDialog/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "CreateSubscriptionDialog", - "version": "0.0.0", - "private": true, - "main": "CreateSubscriptionDialog.js" -} diff --git a/frontend/squeak-node-frontend/src/components/Layout/Layout.js b/frontend/squeak-node-frontend/src/components/Layout/Layout.js index 81f553a0..033cdad6 100644 --- a/frontend/squeak-node-frontend/src/components/Layout/Layout.js +++ b/frontend/squeak-node-frontend/src/components/Layout/Layout.js @@ -26,8 +26,8 @@ import Maps from "../../pages/maps"; import Profiles from "../../pages/profiles"; import Icons from "../../pages/icons"; import Charts from "../../pages/charts"; -import Subscriptions from "../../pages/subscriptions"; -import Subscription from "../../pages/subscription"; +import Peers from "../../pages/peers"; +import Peer from "../../pages/peer"; // context import { useLayoutState } from "../../context/LayoutContext"; @@ -57,8 +57,8 @@ function Layout(props) { - - + + }, { id: 1, label: "Profiles", link: "/app/profiles", icon: }, { id: 2, label: "Lightning", link: "/app/lightning", icon: }, - { id: 3, label: "Subscriptions", link: "/app/subscriptions", icon: }, + { id: 3, label: "Peers", link: "/app/peers", icon: }, ]; function Sidebar({ location }) { diff --git a/frontend/squeak-node-frontend/src/pages/peer/Peer.js b/frontend/squeak-node-frontend/src/pages/peer/Peer.js new file mode 100644 index 00000000..fa4ca36f --- /dev/null +++ b/frontend/squeak-node-frontend/src/pages/peer/Peer.js @@ -0,0 +1,131 @@ +import React, { useState, useEffect } from 'react'; +import { useParams } from 'react-router-dom'; +import { + Grid, + FormLabel, + FormControl, + FormGroup, + FormControlLabel, + FormHelperText, + Switch, +} from "@material-ui/core"; + +// styles +import useStyles from "./styles"; + +// components +import PageTitle from "../../components/PageTitle"; +import Widget from "../../components/Widget"; + +import { + GetPeerRequest, + SetPeerDownloadingRequest, + SetPeerUploadingRequest, +} from "../../proto/squeak_admin_pb" +import { SqueakAdminClient } from "../../proto/squeak_admin_grpc_web_pb" + +var client = new SqueakAdminClient('http://' + window.location.hostname + ':8080') + +export default function PeerPage() { + var classes = useStyles(); + const { id } = useParams(); + const [peer, setPeer] = useState(null); + + const getPeer = (id) => { + console.log("called getPeer with peerId: " + id); + var getPeerRequest = new GetPeerRequest() + getPeerRequest.setPeerId(id); + console.log(getPeerRequest); + client.getPeer(getPeerRequest, {}, (err, response) => { + console.log(response); + setPeer(response.getSqueakPeer()) + }); + }; + const setDownloading = (id, downloading) => { + console.log("called setDownloading with peerId: " + id + ", downloading: " + downloading); + var setPeerDownloadingRequest = new SetPeerDownloadingRequest() + setPeerDownloadingRequest.setPeerId(id); + setPeerDownloadingRequest.setDownloading(downloading); + console.log(setPeerDownloadingRequest); + client.setPeerDownloading(setPeerDownloadingRequest, {}, (err, response) => { + console.log(response); + getPeer(id); + }); + }; + const setUploading = (id, uploading) => { + console.log("called setUploading with peerId: " + id + ", uploading: " + uploading); + var setPeerUploadingRequest = new SetPeerUploadingRequest() + setPeerUploadingRequest.setPeerId(id); + setPeerUploadingRequest.setUploading(uploading); + console.log(setPeerUploadingRequest); + client.setPeerUploading(setPeerUploadingRequest, {}, (err, response) => { + console.log(response); + getPeer(id); + }); + }; + + useEffect(()=>{ + getPeer(id) + },[id]); + + const handleSettingsDownloadingChange = (event) => { + console.log("Downloading changed for peer id: " + id); + console.log("Downloading changed to: " + event.target.checked); + setDownloading(id, event.target.checked); + }; + + const handleSettingsUploadingChange = (event) => { + console.log("Uploading changed for peer id: " + id); + console.log("Uploading changed to: " + event.target.checked); + setUploading(id, event.target.checked); + }; + + function NoPeerContent() { + return ( +

+ No peer loaded +

+ ) + } + + function PeerContent() { + return ( + <> +

+ Peer name: {peer.getPeerName()} +

+ {PeerSettingsForm()} + + ) + } + + function PeerSettingsForm() { + return ( + + Peer settings + + } + label="Downloading" + /> + } + label="Uploading" + /> + + + ) + } + + return ( + <> + +
+ {peer + ? PeerContent() + : NoPeerContent() + } +
+ + ); +} diff --git a/frontend/squeak-node-frontend/src/pages/peer/package.json b/frontend/squeak-node-frontend/src/pages/peer/package.json new file mode 100644 index 00000000..4f9e9fe4 --- /dev/null +++ b/frontend/squeak-node-frontend/src/pages/peer/package.json @@ -0,0 +1,6 @@ +{ + "name": "Peer", + "version": "0.0.0", + "main": "Peer.js", + "private": true +} diff --git a/frontend/squeak-node-frontend/src/pages/subscription/styles.js b/frontend/squeak-node-frontend/src/pages/peer/styles.js similarity index 100% rename from frontend/squeak-node-frontend/src/pages/subscription/styles.js rename to frontend/squeak-node-frontend/src/pages/peer/styles.js diff --git a/frontend/squeak-node-frontend/src/pages/subscriptions/Subscriptions.js b/frontend/squeak-node-frontend/src/pages/peers/Peers.js similarity index 57% rename from frontend/squeak-node-frontend/src/pages/subscriptions/Subscriptions.js rename to frontend/squeak-node-frontend/src/pages/peers/Peers.js index e49ff278..731c14f2 100644 --- a/frontend/squeak-node-frontend/src/pages/subscriptions/Subscriptions.js +++ b/frontend/squeak-node-frontend/src/pages/peers/Peers.js @@ -19,13 +19,13 @@ import {makeStyles} from '@material-ui/core/styles'; import PageTitle from "../../components/PageTitle"; import Widget from "../../components/Widget"; import Table from "../dashboard/components/Table/Table"; -import CreateSubscriptionDialog from "../../components/CreateSubscriptionDialog"; +import CreatePeerDialog from "../../components/CreatePeerDialog"; // data import mock from "../dashboard/mock"; import { - GetSubscriptionsRequest, + GetPeersRequest, } from "../../proto/squeak_admin_pb" import {SqueakAdminClient} from "../../proto/squeak_admin_grpc_web_pb" @@ -39,10 +39,10 @@ const useStyles = makeStyles((theme) => ({ } })); -export default function Subscriptions() { +export default function Peers() { const classes = useStyles(); - const [subscriptions, setSubscriptions] = useState([]); - const [createSubscriptionDialogOpen, setCreateSubscriptionDialogOpen] = useState(false); + const [peers, setPeers] = useState([]); + const [createPeerDialogOpen, setCreatePeerDialogOpen] = useState(false); const history = useHistory(); function a11yProps(index) { @@ -52,38 +52,38 @@ export default function Subscriptions() { }; } - const getSqueakSubscriptions = () => { + const getSqueakPeers = () => { console.log("called getSigningProfiles"); - var getSubscriptionsRequest = new GetSubscriptionsRequest(); + var getPeersRequest = new GetPeersRequest(); - client.getSubscriptions(getSubscriptionsRequest, {}, (err, response) => { + client.getPeers(getPeersRequest, {}, (err, response) => { if (err) { console.log(err.message); return; } console.log(response); - setSubscriptions(response.getSqueakSubscriptionsList()); + setPeers(response.getSqueakPeersList()); }); }; - const goToSubscriptionPage = (id) => { - history.push("/app/Subscription/" + id); + const goToPeerPage = (id) => { + history.push("/app/Peer/" + id); }; - const handleClickOpenCreateSubscriptionDialog = () => { - setCreateSubscriptionDialogOpen(true); + const handleClickOpenCreatePeerDialog = () => { + setCreatePeerDialogOpen(true); }; - const handleCloseCreateSubscriptionDialog = () => { - setCreateSubscriptionDialogOpen(false); + const handleCloseCreatePeerDialog = () => { + setCreatePeerDialogOpen(false); }; useEffect(() => { - getSqueakSubscriptions() + getSqueakPeers() }, []); - function CreateSubscriptionButton() { + function CreatePeerButton() { return ( <> @@ -91,8 +91,8 @@ export default function Subscriptions() { @@ -100,22 +100,22 @@ export default function Subscriptions() { ) } - function SubscriptionsInfo() { + function PeersInfo() { return ( <> - {CreateSubscriptionButton()} + {CreatePeerButton()} + title="Peers" + data={peers.map(s => [ - s.getSubscriptionId(), - s.getSubscriptionName(), + s.getPeerId(), + s.getPeerName(), s.getHost(), s.getPort(), - s.getSubscribed().toString(), - s.getPublishing().toString(), + s.getDownloading().toString(), + s.getUploading().toString(), ] )} columns={[ @@ -128,8 +128,8 @@ export default function Subscriptions() { "Name", "Host", "Port", - "Subscribed", - "Publishing", + "Downloading", + "Uploading", ]} options={{ filter: false, @@ -139,7 +139,7 @@ export default function Subscriptions() { onRowClick: rowData => { var id = rowData[0]; console.log("clicked on id" + id); - goToSubscriptionPage(id); + goToPeerPage(id); }, }}/> @@ -148,21 +148,21 @@ export default function Subscriptions() { ) } - function CreateSubscriptionDialogContent() { + function CreatePeerDialogContent() { return ( <> - + ) } return ( <> - < PageTitle title = "Subscriptions" /> - {SubscriptionsInfo()} - {CreateSubscriptionDialogContent()} + < PageTitle title = "Peers" /> + {PeersInfo()} + {CreatePeerDialogContent()} < />); } diff --git a/frontend/squeak-node-frontend/src/pages/peers/package.json b/frontend/squeak-node-frontend/src/pages/peers/package.json new file mode 100644 index 00000000..44fefbfa --- /dev/null +++ b/frontend/squeak-node-frontend/src/pages/peers/package.json @@ -0,0 +1,6 @@ +{ + "name": "Peers", + "version": "0.0.0", + "main": "Peers.js", + "private": true +} diff --git a/frontend/squeak-node-frontend/src/pages/subscription/Subscription.js b/frontend/squeak-node-frontend/src/pages/subscription/Subscription.js deleted file mode 100644 index d62ab879..00000000 --- a/frontend/squeak-node-frontend/src/pages/subscription/Subscription.js +++ /dev/null @@ -1,131 +0,0 @@ -import React, { useState, useEffect } from 'react'; -import { useParams } from 'react-router-dom'; -import { - Grid, - FormLabel, - FormControl, - FormGroup, - FormControlLabel, - FormHelperText, - Switch, -} from "@material-ui/core"; - -// styles -import useStyles from "./styles"; - -// components -import PageTitle from "../../components/PageTitle"; -import Widget from "../../components/Widget"; - -import { - GetSubscriptionRequest, - SetSubscriptionSubscribedRequest, - SetSubscriptionPublishingRequest, -} from "../../proto/squeak_admin_pb" -import { SqueakAdminClient } from "../../proto/squeak_admin_grpc_web_pb" - -var client = new SqueakAdminClient('http://' + window.location.hostname + ':8080') - -export default function SubscriptionPage() { - var classes = useStyles(); - const { id } = useParams(); - const [subscription, setSubscription] = useState(null); - - const getSubscription = (id) => { - console.log("called getSubscription with subscriptionId: " + id); - var getSubscriptionRequest = new GetSubscriptionRequest() - getSubscriptionRequest.setSubscriptionId(id); - console.log(getSubscriptionRequest); - client.getSubscription(getSubscriptionRequest, {}, (err, response) => { - console.log(response); - setSubscription(response.getSqueakSubscription()) - }); - }; - const setSubscribed = (id, subscribed) => { - console.log("called setSubscribed with subscriptionId: " + id + ", subscribed: " + subscribed); - var setSubscriptionSubscribedRequest = new SetSubscriptionSubscribedRequest() - setSubscriptionSubscribedRequest.setSubscriptionId(id); - setSubscriptionSubscribedRequest.setSubscribed(subscribed); - console.log(setSubscriptionSubscribedRequest); - client.setSubscriptionSubscribed(setSubscriptionSubscribedRequest, {}, (err, response) => { - console.log(response); - getSubscription(id); - }); - }; - const setPublishing = (id, publishing) => { - console.log("called setPublishing with subscriptionId: " + id + ", publishing: " + publishing); - var setSubscriptionPublishingRequest = new SetSubscriptionPublishingRequest() - setSubscriptionPublishingRequest.setSubscriptionId(id); - setSubscriptionPublishingRequest.setPublishing(publishing); - console.log(setSubscriptionPublishingRequest); - client.setSubscriptionPublishing(setSubscriptionPublishingRequest, {}, (err, response) => { - console.log(response); - getSubscription(id); - }); - }; - - useEffect(()=>{ - getSubscription(id) - },[id]); - - const handleSettingsSubscribedChange = (event) => { - console.log("Subscribed changed for subscription id: " + id); - console.log("Subscribed changed to: " + event.target.checked); - setSubscribed(id, event.target.checked); - }; - - const handleSettingsPublishingChange = (event) => { - console.log("Publishing changed for subscription id: " + id); - console.log("Publishing changed to: " + event.target.checked); - setPublishing(id, event.target.checked); - }; - - function NoSubscriptionContent() { - return ( -

- No subscription loaded -

- ) - } - - function SubscriptionContent() { - return ( - <> -

- Subscription name: {subscription.getSubscriptionName()} -

- {SubscriptionSettingsForm()} - - ) - } - - function SubscriptionSettingsForm() { - return ( - - Subscription settings - - } - label="Subscribed" - /> - } - label="Publishing" - /> - - - ) - } - - return ( - <> - -
- {subscription - ? SubscriptionContent() - : NoSubscriptionContent() - } -
- - ); -} diff --git a/frontend/squeak-node-frontend/src/pages/subscription/package.json b/frontend/squeak-node-frontend/src/pages/subscription/package.json deleted file mode 100644 index 1e01872e..00000000 --- a/frontend/squeak-node-frontend/src/pages/subscription/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "Subscription", - "version": "0.0.0", - "main": "Subscription.js", - "private": true -} diff --git a/frontend/squeak-node-frontend/src/pages/subscriptions/package.json b/frontend/squeak-node-frontend/src/pages/subscriptions/package.json deleted file mode 100644 index a4bbc198..00000000 --- a/frontend/squeak-node-frontend/src/pages/subscriptions/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "Subscriptions", - "version": "0.0.0", - "main": "Subscriptions.js", - "private": true -} diff --git a/init.sql b/init.sql index ebab0e80..1a688cf2 100644 --- a/init.sql +++ b/init.sql @@ -33,12 +33,12 @@ CREATE TABLE IF NOT EXISTS profile ( whitelisted BOOLEAN NOT NULL ); -CREATE TABLE IF NOT EXISTS subscription ( - subscription_id SERIAL PRIMARY KEY, +CREATE TABLE IF NOT EXISTS peer ( + peer_id SERIAL PRIMARY KEY, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - subscription_name VARCHAR(64), + peer_name VARCHAR(64), server_host VARCHAR(256) NOT NULL, server_port INTEGER NOT NULL, - publishing BOOLEAN NOT NULL, - subscribed BOOLEAN NOT NULL + uploading BOOLEAN NOT NULL, + downloading BOOLEAN NOT NULL ); diff --git a/itests/tests/conftest.py b/itests/tests/conftest.py index 45147a78..35fc04cc 100644 --- a/itests/tests/conftest.py +++ b/itests/tests/conftest.py @@ -108,10 +108,10 @@ def saved_squeak_hash(server_stub, admin_stub, signing_profile_id): yield bytes.fromhex(squeak_hash_str) @pytest.fixture -def subscription_id(server_stub, admin_stub): - # Create a new subscription - create_subscription_response = admin_stub.CreateSubscription( - squeak_admin_pb2.CreateSubscriptionRequest(host="fake_host", port=1234,) +def peer_id(server_stub, admin_stub): + # Create a new peer + create_peer_response = admin_stub.CreatePeer( + squeak_admin_pb2.CreatePeerRequest(host="fake_host", port=1234,) ) - subscription_id = create_subscription_response.subscription_id - yield subscription_id + peer_id = create_peer_response.peer_id + yield peer_id diff --git a/itests/tests/test_squeak_node.py b/itests/tests/test_squeak_node.py index 95234b9b..dccd220d 100644 --- a/itests/tests/test_squeak_node.py +++ b/itests/tests/test_squeak_node.py @@ -443,76 +443,76 @@ def test_delete_squeak(server_stub, admin_stub, saved_squeak_hash): ) -def test_create_subscription(server_stub, admin_stub): - # Add a new subscription - create_subscription_response = admin_stub.CreateSubscription( - squeak_admin_pb2.CreateSubscriptionRequest(host="fake_host", port=1234,) +def test_create_peer(server_stub, admin_stub): + # Add a new peer + create_peer_response = admin_stub.CreatePeer( + squeak_admin_pb2.CreatePeerRequest(host="fake_host", port=1234,) ) - subscription_id = create_subscription_response.subscription_id + peer_id = create_peer_response.peer_id - # Get the new subscription - get_subscription_response = admin_stub.GetSubscription( - squeak_admin_pb2.GetSubscriptionRequest( - subscription_id=subscription_id, + # Get the new peer + get_peer_response = admin_stub.GetPeer( + squeak_admin_pb2.GetPeerRequest( + peer_id=peer_id, ) ) - assert get_subscription_response.squeak_subscription.host == "fake_host" - assert get_subscription_response.squeak_subscription.port == 1234 + assert get_peer_response.squeak_peer.host == "fake_host" + assert get_peer_response.squeak_peer.port == 1234 - # Get all subscriptions - get_subscriptions_response = admin_stub.GetSubscriptions( - squeak_admin_pb2.GetSubscriptionsRequest() + # Get all peers + get_peers_response = admin_stub.GetPeers( + squeak_admin_pb2.GetPeersRequest() ) - subscription_hosts = [ - squeak_subscription.host - for squeak_subscription in get_subscriptions_response.squeak_subscriptions + peer_hosts = [ + squeak_peer.host + for squeak_peer in get_peers_response.squeak_peers ] - assert "fake_host" in subscription_hosts + assert "fake_host" in peer_hosts -def test_set_subscription_subscribed(server_stub, admin_stub, subscription_id): - # Get the subscription - get_subscription_response = admin_stub.GetSubscription( - squeak_admin_pb2.GetSubscriptionRequest( - subscription_id=subscription_id, +def test_set_peer_downloading(server_stub, admin_stub, peer_id): + # Get the peer + get_peer_response = admin_stub.GetPeer( + squeak_admin_pb2.GetPeerRequest( + peer_id=peer_id, ) ) - assert get_subscription_response.squeak_subscription.subscribed == False + assert get_peer_response.squeak_peer.downloading == False - # Set the subscription to be subscribed - admin_stub.SetSubscriptionSubscribed( - squeak_admin_pb2.SetSubscriptionSubscribedRequest( - subscription_id=subscription_id, subscribed=True, + # Set the peer to be downloading + admin_stub.SetPeerDownloading( + squeak_admin_pb2.SetPeerDownloadingRequest( + peer_id=peer_id, downloading=True, ) ) - # Get the subscription again - get_subscription_response = admin_stub.GetSubscription( - squeak_admin_pb2.GetSubscriptionRequest( - subscription_id=subscription_id, + # Get the peer again + get_peer_response = admin_stub.GetPeer( + squeak_admin_pb2.GetPeerRequest( + peer_id=peer_id, ) ) - assert get_subscription_response.squeak_subscription.subscribed == True + assert get_peer_response.squeak_peer.downloading == True -def test_set_subscription_publishing(server_stub, admin_stub, subscription_id): - # Get the subscription - get_subscription_response = admin_stub.GetSubscription( - squeak_admin_pb2.GetSubscriptionRequest( - subscription_id=subscription_id, +def test_set_peer_uploading(server_stub, admin_stub, peer_id): + # Get the peer + get_peer_response = admin_stub.GetPeer( + squeak_admin_pb2.GetPeerRequest( + peer_id=peer_id, ) ) - assert get_subscription_response.squeak_subscription.publishing == False + assert get_peer_response.squeak_peer.uploading == False - # Set the subscription to be publishing - admin_stub.SetSubscriptionPublishing( - squeak_admin_pb2.SetSubscriptionPublishingRequest( - subscription_id=subscription_id, publishing=True, + # Set the peer to be uploading + admin_stub.SetPeerUploading( + squeak_admin_pb2.SetPeerUploadingRequest( + peer_id=peer_id, uploading=True, ) ) - # Get the subscription again - get_subscription_response = admin_stub.GetSubscription( - squeak_admin_pb2.GetSubscriptionRequest( - subscription_id=subscription_id, + # Get the peer again + get_peer_response = admin_stub.GetPeer( + squeak_admin_pb2.GetPeerRequest( + peer_id=peer_id, ) ) - assert get_subscription_response.squeak_subscription.publishing == True + assert get_peer_response.squeak_peer.uploading == True diff --git a/proto/lnd.proto b/proto/lnd.proto index ee5504d7..5d574b70 100644 --- a/proto/lnd.proto +++ b/proto/lnd.proto @@ -131,7 +131,7 @@ service Lightning { the client in which any events relevant to the state of peers are sent over. Events include peers going online and offline. */ - rpc SubscribePeerEvents (PeerEventSubscription) returns (stream PeerEvent); + rpc SubscribePeerEvents (PeerEventPeer) returns (stream PeerEvent); /* lncli: `getinfo` GetInfo returns general information concerning the lightning node including @@ -170,7 +170,7 @@ service Lightning { sent over. Events include new active channels, inactive channels, and closed channels. */ - rpc SubscribeChannelEvents (ChannelEventSubscription) + rpc SubscribeChannelEvents (ChannelEventPeer) returns (stream ChannelEventUpdate); /* lncli: `closedchannels` @@ -312,7 +312,7 @@ service Lightning { of these fields can be set. If no fields are set, then we'll only send out the latest add/settle events. */ - rpc SubscribeInvoices (InvoiceSubscription) returns (stream Invoice); + rpc SubscribeInvoices (InvoicePeer) returns (stream Invoice); /* lncli: `decodepayreq` DecodePayReq takes an encoded payment request string and attempts to decode @@ -396,7 +396,7 @@ service Lightning { channels being advertised, updates in the routing policy for a directional channel edge, and when channels are closed on-chain. */ - rpc SubscribeChannelGraph (GraphTopologySubscription) + rpc SubscribeChannelGraph (GraphTopologyPeer) returns (stream GraphTopologyUpdate); /* lncli: `debuglevel` @@ -482,7 +482,7 @@ service Lightning { ups, but the updated set of encrypted multi-chan backups with the closed channel(s) removed. */ - rpc SubscribeChannelBackups (ChannelBackupSubscription) + rpc SubscribeChannelBackups (ChannelBackupPeer) returns (stream ChanBackupSnapshot); /* lncli: `bakemacaroon` @@ -1416,7 +1416,7 @@ message ListPeersResponse { repeated Peer peers = 1; } -message PeerEventSubscription { +message PeerEventPeer { } message PeerEvent { @@ -2042,7 +2042,7 @@ message PendingChannelsResponse { repeated WaitingCloseChannel waiting_close_channels = 5; } -message ChannelEventSubscription { +message ChannelEventPeer { } message ChannelEventUpdate { @@ -2515,7 +2515,7 @@ message StopRequest { message StopResponse { } -message GraphTopologySubscription { +message GraphTopologyPeer { } message GraphTopologyUpdate { repeated NodeUpdate node_updates = 1; @@ -2845,7 +2845,7 @@ message ListInvoiceResponse { uint64 first_index_offset = 3; } -message InvoiceSubscription { +message InvoicePeer { /* If specified (non-zero), then we'll first start by sending out notifications for all added indexes with an add_index greater than this @@ -3311,7 +3311,7 @@ message RestoreChanBackupRequest { message RestoreBackupResponse { } -message ChannelBackupSubscription { +message ChannelBackupPeer { } message VerifyChanBackupResponse { diff --git a/proto/squeak_admin.proto b/proto/squeak_admin.proto index 871dc358..fce63613 100644 --- a/proto/squeak_admin.proto +++ b/proto/squeak_admin.proto @@ -82,25 +82,25 @@ service SqueakAdmin { */ rpc DeleteSqueak (DeleteSqueakRequest) returns (DeleteSqueakReply) {} - /** sqkadmin: `addsubscription` + /** sqkadmin: `addpeer` */ - rpc CreateSubscription (CreateSubscriptionRequest) returns (CreateSubscriptionReply) {} + rpc CreatePeer (CreatePeerRequest) returns (CreatePeerReply) {} - /** sqkadmin: `getsubscription` + /** sqkadmin: `getpeer` */ - rpc GetSubscription (GetSubscriptionRequest) returns (GetSubscriptionReply) {} + rpc GetPeer (GetPeerRequest) returns (GetPeerReply) {} - /** sqkadmin: `getsubscriptions` + /** sqkadmin: `getpeers` */ - rpc GetSubscriptions (GetSubscriptionsRequest) returns (GetSubscriptionsReply) {} + rpc GetPeers (GetPeersRequest) returns (GetPeersReply) {} - /** sqkadmin: `setsubscriptionsubscribed` + /** sqkadmin: `setpeerdownloading` */ - rpc SetSubscriptionSubscribed (SetSubscriptionSubscribedRequest) returns (SetSubscriptionSubscribedReply) {} + rpc SetPeerDownloading (SetPeerDownloadingRequest) returns (SetPeerDownloadingReply) {} - /** sqkadmin: `setsubscriptionsubscribed` + /** sqkadmin: `setpeerdownloading` */ - rpc SetSubscriptionPublishing (SetSubscriptionPublishingRequest) returns (SetSubscriptionPublishingReply) {} + rpc SetPeerUploading (SetPeerUploadingRequest) returns (SetPeerUploadingReply) {} } @@ -321,9 +321,9 @@ message DeleteSqueakRequest { message DeleteSqueakReply { } -message CreateSubscriptionRequest { - /// Name of the subscription - string subscription_name = 1; +message CreatePeerRequest { + /// Name of the peer + string peer_name = 1; /// Host string host = 2; @@ -332,35 +332,35 @@ message CreateSubscriptionRequest { int32 port = 3; } -message CreateSubscriptionReply { - /// The subscription id - int32 subscription_id = 1; +message CreatePeerReply { + /// The peer id + int32 peer_id = 1; } -message GetSubscriptionRequest { - /// The subscription id - int32 subscription_id = 1; +message GetPeerRequest { + /// The peer id + int32 peer_id = 1; } -message GetSubscriptionReply { - /// The subscription - SqueakSubscription squeak_subscription = 1; +message GetPeerReply { + /// The peer + SqueakPeer squeak_peer = 1; } -message GetSubscriptionsRequest { +message GetPeersRequest { } -message GetSubscriptionsReply { - /// The subscriptions - repeated SqueakSubscription squeak_subscriptions = 1; +message GetPeersReply { + /// The peers + repeated SqueakPeer squeak_peers = 1; } -message SqueakSubscription { - /// The subscription id - int32 subscription_id = 1; +message SqueakPeer { + /// The peer id + int32 peer_id = 1; - /// The subscription name - string subscription_name = 2; + /// The peer name + string peer_name = 2; /// Host string host = 3; @@ -368,31 +368,31 @@ message SqueakSubscription { /// Port int32 port = 4; - /// Publishing - bool publishing = 5; + /// Uploading + bool uploading = 5; - /// Subscribed - bool subscribed = 6; + /// Downloading + bool downloading = 6; } -message SetSubscriptionSubscribedRequest { - /// The subscription id - int32 subscription_id = 1; +message SetPeerDownloadingRequest { + /// The peer id + int32 peer_id = 1; - /// Subscribed - bool subscribed = 2; + /// Downloading + bool downloading = 2; } -message SetSubscriptionSubscribedReply { +message SetPeerDownloadingReply { } -message SetSubscriptionPublishingRequest { - /// The subscription id - int32 subscription_id = 1; +message SetPeerUploadingRequest { + /// The peer id + int32 peer_id = 1; - /// Publishing - bool publishing = 2; + /// Uploading + bool uploading = 2; } -message SetSubscriptionPublishingReply { +message SetPeerUploadingReply { } diff --git a/squeakserver/admin/squeak_admin_server_handler.py b/squeakserver/admin/squeak_admin_server_handler.py index b9adeb2c..54a3cc65 100644 --- a/squeakserver/admin/squeak_admin_server_handler.py +++ b/squeakserver/admin/squeak_admin_server_handler.py @@ -153,41 +153,41 @@ class SqueakAdminServerHandler(object): self.squeak_node.delete_squeak(squeak_hash) logger.info("Deleted squeak entry with hash: {}".format(squeak_hash)) - def handle_create_subscription(self, subscription_name, host, port): + def handle_create_peer(self, peer_name, host, port): logger.info( - "Handle create subscription with name: {}, host: {}, port: {}".format( - subscription_name, host, port, + "Handle create peer with name: {}, host: {}, port: {}".format( + peer_name, host, port, ) ) - subscription_id = self.squeak_node.create_subscription( - subscription_name, + peer_id = self.squeak_node.create_peer( + peer_name, host, port, ) - return subscription_id + return peer_id - def handle_get_squeak_subscription(self, subscription_id): - logger.info("Handle get squeak subscription with id: {}".format(subscription_id)) - squeak_subscription = self.squeak_node.get_subscription(subscription_id) - return squeak_subscription + def handle_get_squeak_peer(self, peer_id): + logger.info("Handle get squeak peer with id: {}".format(peer_id)) + squeak_peer = self.squeak_node.get_peer(peer_id) + return squeak_peer - def handle_get_squeak_subscriptions(self): - logger.info("Handle get squeak subscriptions") - squeak_subscriptions = self.squeak_node.get_subscriptions() - return squeak_subscriptions + def handle_get_squeak_peers(self): + logger.info("Handle get squeak peers") + squeak_peers = self.squeak_node.get_peers() + return squeak_peers - def handle_set_squeak_subscription_subscribed(self, subscription_id, subscribed): + def handle_set_squeak_peer_downloading(self, peer_id, downloading): logger.info( - "Handle set subscription subscribed with subscription id: {}, subscribed: {}".format( - subscription_id, subscribed, + "Handle set peer downloading with peer id: {}, downloading: {}".format( + peer_id, downloading, ) ) - self.squeak_node.set_subscription_subscribed(subscription_id, subscribed) + self.squeak_node.set_peer_downloading(peer_id, downloading) - def handle_set_squeak_subscription_publishing(self, subscription_id, publishing): + def handle_set_squeak_peer_uploading(self, peer_id, uploading): logger.info( - "Handle set subscription publishing with subscription id: {}, publishing: {}".format( - subscription_id, publishing, + "Handle set peer uploading with peer id: {}, uploading: {}".format( + peer_id, uploading, ) ) - self.squeak_node.set_subscription_publishing(subscription_id, publishing) + self.squeak_node.set_peer_uploading(peer_id, uploading) diff --git a/squeakserver/admin/squeak_admin_server_servicer.py b/squeakserver/admin/squeak_admin_server_servicer.py index 0ec2769a..85afb70a 100644 --- a/squeakserver/admin/squeak_admin_server_servicer.py +++ b/squeakserver/admin/squeak_admin_server_servicer.py @@ -152,54 +152,54 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer): self.handler.handle_delete_squeak(squeak_hash) return squeak_admin_pb2.DeleteSqueakReply() - def CreateSubscription(self, request, context): - subscription_name = request.subscription_name if request.subscription_name else None + def CreatePeer(self, request, context): + peer_name = request.peer_name if request.peer_name else None host = request.host port = request.port - subscription_id = self.handler.handle_create_subscription( - subscription_name, + peer_id = self.handler.handle_create_peer( + peer_name, host, port, ) - return squeak_admin_pb2.CreateSubscriptionReply( - subscription_id=subscription_id, + return squeak_admin_pb2.CreatePeerReply( + peer_id=peer_id, ) - def GetSubscription(self, request, context): - subscription_id = request.subscription_id - squeak_subscription = self.handler.handle_get_squeak_subscription(subscription_id) - squeak_subscription_msg = self._squeak_subscription_to_message(squeak_subscription) - return squeak_admin_pb2.GetSubscriptionReply( - squeak_subscription=squeak_subscription_msg, + def GetPeer(self, request, context): + peer_id = request.peer_id + squeak_peer = self.handler.handle_get_squeak_peer(peer_id) + squeak_peer_msg = self._squeak_peer_to_message(squeak_peer) + return squeak_admin_pb2.GetPeerReply( + squeak_peer=squeak_peer_msg, ) - def GetSubscriptions(self, request, context): - squeak_subscriptions = self.handler.handle_get_squeak_subscriptions() - squeak_subscription_msgs = [ - self._squeak_subscription_to_message(squeak_subscription) - for squeak_subscription in squeak_subscriptions + def GetPeers(self, request, context): + squeak_peers = self.handler.handle_get_squeak_peers() + squeak_peer_msgs = [ + self._squeak_peer_to_message(squeak_peer) + for squeak_peer in squeak_peers ] - return squeak_admin_pb2.GetSubscriptionsReply( - squeak_subscriptions=squeak_subscription_msgs, + return squeak_admin_pb2.GetPeersReply( + squeak_peers=squeak_peer_msgs, ) - def SetSubscriptionSubscribed(self, request, context): - subscription_id = request.subscription_id - subscribed = request.subscribed - self.handler.handle_set_squeak_subscription_subscribed( - subscription_id, - subscribed, + def SetPeerDownloading(self, request, context): + peer_id = request.peer_id + downloading = request.downloading + self.handler.handle_set_squeak_peer_downloading( + peer_id, + downloading, ) - return squeak_admin_pb2.SetSubscriptionSubscribedReply() + return squeak_admin_pb2.SetPeerDownloadingReply() - def SetSubscriptionPublishing(self, request, context): - subscription_id = request.subscription_id - publishing = request.publishing - self.handler.handle_set_squeak_subscription_publishing( - subscription_id, - publishing, + def SetPeerUploading(self, request, context): + peer_id = request.peer_id + uploading = request.uploading + self.handler.handle_set_squeak_peer_uploading( + peer_id, + uploading, ) - return squeak_admin_pb2.SetSubscriptionPublishingReply() + return squeak_admin_pb2.SetPeerUploadingReply() def _squeak_entry_to_message(self, squeak_entry_with_profile): if squeak_entry_with_profile is None: @@ -242,16 +242,16 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer): whitelisted=squeak_profile.whitelisted, ) - def _squeak_subscription_to_message(self, squeak_subscription): - if squeak_subscription is None: + def _squeak_peer_to_message(self, squeak_peer): + if squeak_peer is None: return None - return squeak_admin_pb2.SqueakSubscription( - subscription_id=squeak_subscription.subscription_id, - subscription_name=squeak_subscription.subscription_name, - host=squeak_subscription.host, - port=squeak_subscription.port, - publishing=squeak_subscription.publishing, - subscribed=squeak_subscription.subscribed, + return squeak_admin_pb2.SqueakPeer( + peer_id=squeak_peer.peer_id, + peer_name=squeak_peer.peer_name, + host=squeak_peer.host, + port=squeak_peer.port, + uploading=squeak_peer.uploading, + downloading=squeak_peer.downloading, ) def serve(self): diff --git a/squeakserver/node/squeak_node.py b/squeakserver/node/squeak_node.py index 32151533..07a3abbd 100644 --- a/squeakserver/node/squeak_node.py +++ b/squeakserver/node/squeak_node.py @@ -12,10 +12,10 @@ from squeakserver.node.squeak_maker import SqueakMaker from squeakserver.node.squeak_rate_limiter import SqueakRateLimiter from squeakserver.node.squeak_whitelist import SqueakWhitelist from squeakserver.node.squeak_store import SqueakStore -from squeakserver.node.squeak_subscription_downloader import SqueakSubscriptionDownloader +from squeakserver.node.squeak_peer_downloader import SqueakPeerDownloader from squeakserver.server.buy_offer import BuyOffer from squeakserver.server.squeak_profile import SqueakProfile -from squeakserver.server.squeak_subscription import SqueakSubscription +from squeakserver.server.squeak_peer import SqueakPeer from squeakserver.server.util import generate_offer_preimage @@ -54,7 +54,7 @@ class SqueakNode: self.squeak_rate_limiter, self.squeak_whitelist, ) - self.squeak_subscription_downloader = SqueakSubscriptionDownloader( + self.squeak_peer_downloader = SqueakPeerDownloader( postgres_db, self.squeak_store, self.blockchain_client, @@ -63,7 +63,7 @@ class SqueakNode: def start_running(self): # self.squeak_block_periodic_worker.start_running() self.squeak_block_queue_worker.start_running() - self.squeak_subscription_downloader.start_running() + self.squeak_peer_downloader.start_running() def save_uploaded_squeak(self, squeak): return self.squeak_store.save_uploaded_squeak(squeak) @@ -201,25 +201,25 @@ class SqueakNode: def delete_squeak(self, squeak_hash): return self.squeak_store.delete_squeak(squeak_hash) - def create_subscription(self, subscription_name, host, port): - squeak_subscription = SqueakSubscription( - subscription_id=None, - subscription_name=subscription_name, + def create_peer(self, peer_name, host, port): + squeak_peer = SqueakPeer( + peer_id=None, + peer_name=peer_name, host=host, port=port, - publishing=False, - subscribed=False, + uploading=False, + downloading=False, ) - return self.postgres_db.insert_subscription(squeak_subscription) + return self.postgres_db.insert_peer(squeak_peer) - def get_subscription(self, subscription_id): - return self.postgres_db.get_subscription(subscription_id) + def get_peer(self, peer_id): + return self.postgres_db.get_peer(peer_id) - def get_subscriptions(self): - return self.postgres_db.get_subscriptions() + def get_peers(self): + return self.postgres_db.get_peers() - def set_subscription_subscribed(self, subscription_id, subscribed): - self.postgres_db.set_subscription_subscribed(subscription_id, subscribed) + def set_peer_downloading(self, peer_id, downloading): + self.postgres_db.set_peer_downloading(peer_id, downloading) - def set_subscription_publishing(self, subscription_id, publishing): - self.postgres_db.set_subscription_publishing(subscription_id, publishing) + def set_peer_uploading(self, peer_id, uploading): + self.postgres_db.set_peer_uploading(peer_id, uploading) diff --git a/squeakserver/node/squeak_subscription_downloader.py b/squeakserver/node/squeak_peer_downloader.py similarity index 64% rename from squeakserver/node/squeak_subscription_downloader.py rename to squeakserver/node/squeak_peer_downloader.py index 34491e07..ba9bb960 100644 --- a/squeakserver/node/squeak_subscription_downloader.py +++ b/squeakserver/node/squeak_peer_downloader.py @@ -8,7 +8,7 @@ logger = logging.getLogger(__name__) SUBSCRIBE_UPDATE_INTERVAL_S = 60.0 -class SqueakSubscriptionDownloader: +class SqueakPeerDownloader: def __init__(self, postgres_db, squeak_store, @@ -20,9 +20,9 @@ class SqueakSubscriptionDownloader: self.blockchain_client = blockchain_client self.update_interval_s = update_interval_s - def sync_subscriptions(self): - logger.info("Syncing subscriptions...") - subscriptions = self._get_subscriptions() + def sync_peers(self): + logger.info("Syncing peers...") + peers = self._get_peers() try: block_info = self.blockchain_client.get_best_block_info() @@ -31,13 +31,13 @@ class SqueakSubscriptionDownloader: logger.error("Failed to sync because unable to get blockchain info.", exc_info=True) return - for subscription in subscriptions: - if subscription.subscribed: - logger.info("Syncing subscription: {} with current block: {}".format(subscription, block_height)) + for peer in peers: + if peer.downloading: + logger.info("Syncing peer: {} with current block: {}".format(peer, block_height)) def start_running(self): threading.Timer(self.update_interval_s, self.start_running).start() - self.sync_subscriptions() + self.sync_peers() - def _get_subscriptions(self): - return self.postgres_db.get_subscriptions() + def _get_peers(self): + return self.postgres_db.get_peers() diff --git a/squeakserver/server/postgres_db.py b/squeakserver/server/postgres_db.py index 463559ce..d6b71473 100644 --- a/squeakserver/server/postgres_db.py +++ b/squeakserver/server/postgres_db.py @@ -10,7 +10,7 @@ from squeakserver.blockchain.util import parse_block_header from squeakserver.core.squeak_entry import SqueakEntry from squeakserver.core.squeak_entry_with_profile import SqueakEntryWithProfile from squeakserver.server.squeak_profile import SqueakProfile -from squeakserver.server.squeak_subscription import SqueakSubscription +from squeakserver.server.squeak_peer import SqueakPeer from squeakserver.server.util import get_hash logger = logging.getLogger(__name__) @@ -370,69 +370,69 @@ class PostgresDb: with self.get_cursor() as curs: curs.execute(sql, (squeak_hash_str,)) - def insert_subscription(self, squeak_subscription): - """ Insert a new squeak subscription. """ + def insert_peer(self, squeak_peer): + """ Insert a new squeak peer. """ sql = """ - INSERT INTO subscription(subscription_name, server_host, server_port, publishing, subscribed) + INSERT INTO peer(peer_name, server_host, server_port, uploading, downloading) VALUES(%s, %s, %s, %s, %s) - RETURNING subscription_id; + RETURNING peer_id; """ with self.get_cursor() as curs: # execute the INSERT statement curs.execute( sql, ( - squeak_subscription.subscription_name, - squeak_subscription.host, - squeak_subscription.port, - squeak_subscription.publishing, - squeak_subscription.subscribed, + squeak_peer.peer_name, + squeak_peer.host, + squeak_peer.port, + squeak_peer.uploading, + squeak_peer.downloading, ), ) - # get the new subscription id back + # get the new peer id back row = curs.fetchone() - return row["subscription_id"] + return row["peer_id"] - def get_subscription(self, subscription_id): - """ Get a subscription. """ + def get_peer(self, peer_id): + """ Get a peer. """ sql = """ - SELECT * FROM subscription WHERE subscription_id=%s""" + SELECT * FROM peer WHERE peer_id=%s""" with self.get_cursor() as curs: - curs.execute(sql, (subscription_id,)) + curs.execute(sql, (peer_id,)) row = curs.fetchone() - return self._parse_squeak_subscription(row) + return self._parse_squeak_peer(row) - def get_subscriptions(self): - """ Get all subscriptions. """ + def get_peers(self): + """ Get all peers. """ sql = """ - SELECT * FROM subscription; + SELECT * FROM peer; """ with self.get_cursor() as curs: curs.execute(sql) rows = curs.fetchall() - subscriptions = [self._parse_squeak_subscription(row) for row in rows] - return subscriptions + peers = [self._parse_squeak_peer(row) for row in rows] + return peers - def set_subscription_subscribed(self, subscription_id, subscribed): - """ Set a subscription is subscribed. """ + def set_peer_downloading(self, peer_id, downloading): + """ Set a peer is downloading. """ sql = """ - UPDATE subscription - SET subscribed=%s - WHERE subscription_id=%s; + UPDATE peer + SET downloading=%s + WHERE peer_id=%s; """ with self.get_cursor() as curs: - curs.execute(sql, (subscribed, subscription_id,)) + curs.execute(sql, (downloading, peer_id,)) - def set_subscription_publishing(self, subscription_id, publishing): - """ Set a subscription is publishing. """ + def set_peer_uploading(self, peer_id, uploading): + """ Set a peer is uploading. """ sql = """ - UPDATE subscription - SET publishing=%s - WHERE subscription_id=%s; + UPDATE peer + SET uploading=%s + WHERE peer_id=%s; """ with self.get_cursor() as curs: - curs.execute(sql, (publishing, subscription_id,)) + curs.execute(sql, (uploading, peer_id,)) def _parse_squeak_entry(self, row): vch_decryption_key_column = row["vch_decryption_key"] @@ -486,14 +486,14 @@ class PostgresDb: squeak_entry=squeak_entry, squeak_profile=squeak_profile, ) - def _parse_squeak_subscription(self, row): + def _parse_squeak_peer(self, row): if row is None: return None - return SqueakSubscription( - subscription_id=row["subscription_id"], - subscription_name=row["subscription_name"], + return SqueakPeer( + peer_id=row["peer_id"], + peer_name=row["peer_name"], host=row["server_host"], port=row["server_port"], - publishing=row["publishing"], - subscribed=row["subscribed"], + uploading=row["uploading"], + downloading=row["downloading"], ) diff --git a/squeakserver/server/squeak_peer.py b/squeakserver/server/squeak_peer.py new file mode 100644 index 00000000..9369bc89 --- /dev/null +++ b/squeakserver/server/squeak_peer.py @@ -0,0 +1,5 @@ +from collections import namedtuple + +SqueakPeer = namedtuple( + "SqueakPeer", "peer_id, peer_name, host, port, uploading, downloading", +) diff --git a/squeakserver/server/squeak_subscription.py b/squeakserver/server/squeak_subscription.py deleted file mode 100644 index b9dcc806..00000000 --- a/squeakserver/server/squeak_subscription.py +++ /dev/null @@ -1,5 +0,0 @@ -from collections import namedtuple - -SqueakSubscription = namedtuple( - "SqueakSubscription", "subscription_id, subscription_name, host, port, publishing, subscribed", -)