Show peer info for buy offer (#998)

* Show peer host and port in offer detail component

* Remove peer field from offer display entry
This commit is contained in:
Jonathan Zernik 2021-08-20 19:07:59 -07:00 committed by GitHub
parent 0de52cc20f
commit b1489e45ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 120 deletions

View file

@ -25,6 +25,7 @@ import moment from 'moment';
import {
goToPeerPage,
goToPeerAddressPage,
goToLightningNodePage,
} from "../../navigation/navigation"
@ -40,12 +41,11 @@ export default function BuyOfferDetailItem({
const onPeerClick = (event) => {
event.preventDefault();
const peerId = getPeerId();
if (peerId == null) {
return;
}
console.log("Handling peer click for peerId: " + peerId);
goToPeerPage(history, peerId);
goToPeerAddressPage(
history,
offer.getPeerHost(),
offer.getPeerPort(),
);
}
const onLightningNodeClick = (event) => {
@ -58,16 +58,6 @@ export default function BuyOfferDetailItem({
)
}
const getPeerId = () => {
if (!offer.getHasPeer()) {
return null;
}
const peer = offer.getPeer();
return peer.getPeerId();
}
const handleClickPayOffer = () => {
console.log("Handle click pay offer.");
setPayOfferDialogOpen(true);
@ -86,43 +76,17 @@ export default function BuyOfferDetailItem({
)
}
function PeerDisplay() {
if (offer.getHasPeer()) {
return HasPeerDisplay(offer.getPeer());
} else {
return HasNoPeerDisplay();
}
}
function HasPeerDisplay(peer) {
const peerId = peer.getPeerId();
const peerName = peer.getPeerName();
const peerDisplayName = peerName ? peerName : peerId;
return (
<Link href="#"
onClick={onPeerClick}
>{peerDisplayName}
</Link>
)
}
function HasNoPeerDisplay() {
return (
<>
Unknown Peer
</>
)
}
function ProfileInfoContent() {
function PeerInfoContent() {
console.log(offer);
const peerAddress = offer.getPeerHost() + ":" + offer.getPeerPort();
return (
<Box>
<Typography
size="md"
>Peer: {PeerDisplay()}
</Typography>
>Peer: <Link href="#" onClick={onPeerClick}>
{peerAddress}
</Link>
</Typography>
</Box>
)
}
@ -142,7 +106,7 @@ export default function BuyOfferDetailItem({
)
}
function PeerNodeInfoContent(offer) {
function LightningPeerInfoContent(offer) {
const lightningAddress = offer.getNodeHost() + ":" + offer.getNodePort();
const lightningPubkey = offer.getNodePubkey();
return (
@ -181,7 +145,7 @@ export default function BuyOfferDetailItem({
alignItems="flex-start"
>
<Grid item>
{ProfileInfoContent()}
{PeerInfoContent()}
</Grid>
</Grid>
<Grid
@ -191,7 +155,7 @@ export default function BuyOfferDetailItem({
alignItems="flex-start"
>
<Grid item>
{PeerNodeInfoContent(offer)}
{LightningPeerInfoContent(offer)}
</Grid>
</Grid>
<Grid

View file

@ -683,6 +683,12 @@ message OfferDisplayEntry {
/// The invoice expiry
int32 invoice_expiry = 10;
/// The host of the squeak peer
string peer_host = 11;
/// The port of the squeak peer
int32 peer_port = 12;
}
message SyncSqueaksRequest {

View file

@ -3,7 +3,7 @@ import logging
from proto import squeak_admin_pb2
from squeaknode.admin.profile_image_util import bytes_to_base64_string
from squeaknode.admin.profile_image_util import load_default_profile_image
from squeaknode.core.received_offer_with_peer import ReceivedOfferWithPeer
from squeaknode.core.received_offer_with_peer import ReceivedOffer
from squeaknode.core.received_payment import ReceivedPayment
from squeaknode.core.received_payment_summary import ReceivedPaymentSummary
from squeaknode.core.sent_offer import SentOffer
@ -15,7 +15,6 @@ from squeaknode.core.squeak_profile import SqueakProfile
from squeaknode.core.util import get_hash
from squeaknode.network.peer import Peer
logger = logging.getLogger(__name__)
@ -84,16 +83,9 @@ def squeak_peer_to_message(squeak_peer: SqueakPeer) -> squeak_admin_pb2.SqueakPe
)
def offer_entry_to_message(received_offer_entry: ReceivedOfferWithPeer) -> squeak_admin_pb2.OfferDisplayEntry:
received_offer = received_offer_entry.received_offer
def offer_entry_to_message(received_offer: ReceivedOffer) -> squeak_admin_pb2.OfferDisplayEntry:
if received_offer.received_offer_id is None:
raise Exception("Received offer id cannot be None.")
peer = received_offer_entry.peer
has_peer = False
peer_msg = None
if peer is not None:
has_peer = True
peer_msg = squeak_peer_to_message(peer)
return squeak_admin_pb2.OfferDisplayEntry(
offer_id=received_offer.received_offer_id,
squeak_hash=received_offer.squeak_hash.hex(),
@ -101,10 +93,10 @@ def offer_entry_to_message(received_offer_entry: ReceivedOfferWithPeer) -> squea
node_pubkey=received_offer.destination,
node_host=received_offer.lightning_address.host,
node_port=received_offer.lightning_address.port,
has_peer=has_peer,
peer=peer_msg,
invoice_timestamp=received_offer.invoice_timestamp,
invoice_expiry=received_offer.invoice_expiry,
peer_host=received_offer.peer_address.host,
peer_port=received_offer.peer_address.port,
)

View file

@ -462,7 +462,7 @@ class SqueakAdminServerHandler(object):
squeak_hash = bytes.fromhex(squeak_hash_str)
logger.info(
"Handle get received offers for hash: {}".format(squeak_hash_str))
offers = self.squeak_controller.get_received_offers_with_peer(
offers = self.squeak_controller.get_received_offers(
squeak_hash)
offer_msgs = [offer_entry_to_message(offer) for offer in offers]
return squeak_admin_pb2.GetBuyOffersReply(
@ -472,7 +472,7 @@ class SqueakAdminServerHandler(object):
def handle_get_buy_offer(self, request):
offer_id = request.offer_id
logger.info("Handle get buy offer for hash: {}".format(offer_id))
offer = self.squeak_controller.get_buy_offer_with_peer(offer_id)
offer = self.squeak_controller.get_received_offer(offer_id)
if offer is None:
return squeak_admin_pb2.GetBuyOfferReply(
offer=None,

View file

@ -18,7 +18,6 @@ from squeaknode.bitcoin.util import parse_block_header
from squeaknode.core.lightning_address import LightningAddressHostPort
from squeaknode.core.peer_address import PeerAddress
from squeaknode.core.received_offer import ReceivedOffer
from squeaknode.core.received_offer_with_peer import ReceivedOfferWithPeer
from squeaknode.core.received_payment import ReceivedPayment
from squeaknode.core.received_payment_summary import ReceivedPaymentSummary
from squeaknode.core.sent_offer import SentOffer
@ -743,17 +742,10 @@ class SqueakDb:
received_offer_id = res.inserted_primary_key[0]
return received_offer_id
def get_received_offers_with_peer(self, squeak_hash: bytes) -> List[ReceivedOfferWithPeer]:
def get_received_offers(self, squeak_hash: bytes) -> List[ReceivedOffer]:
""" Get offers with peer for a squeak hash. """
s = (
select([self.received_offers, self.peers])
.select_from(
self.received_offers.outerjoin(
self.peers,
self.peers.c.host == self.received_offers.c.peer_host,
self.peers.c.port == self.received_offers.c.peer_port,
)
)
select([self.received_offers])
.where(self.received_offers.c.squeak_hash == squeak_hash.hex())
.where(self.received_offer_is_not_paid)
.where(self.received_offer_is_not_expired)
@ -761,21 +753,16 @@ class SqueakDb:
with self.get_connection() as connection:
result = connection.execute(s)
rows = result.fetchall()
offers_with_peer = [
self._parse_received_offer_with_peer(row) for row in rows]
return offers_with_peer
offers = [
self._parse_received_offer(row)
for row in rows
]
return offers
def get_offer_with_peer(self, received_offer_id: int) -> Optional[ReceivedOfferWithPeer]:
def get_received_offer(self, received_offer_id: int) -> Optional[ReceivedOffer]:
""" Get offer with peer for an offer id. """
s = (
select([self.received_offers, self.peers])
.select_from(
self.received_offers.outerjoin(
self.peers,
self.peers.c.host == self.received_offers.c.peer_host,
self.peers.c.port == self.received_offers.c.peer_port,
)
)
select([self.received_offers])
.where(self.received_offers.c.received_offer_id == received_offer_id)
)
with self.get_connection() as connection:
@ -783,24 +770,17 @@ class SqueakDb:
row = result.fetchone()
if row is None:
return None
offer_with_peer = self._parse_received_offer_with_peer(row)
return offer_with_peer
offer = self._parse_received_offer(row)
return offer
def get_received_offer_for_squeak_and_peer(
self,
squeak_hash: bytes,
peer_address: PeerAddress,
) -> Optional[ReceivedOfferWithPeer]:
) -> Optional[ReceivedOffer]:
""" Get offer with peer for a given peer address and squeak hash . """
s = (
select([self.received_offers, self.peers])
.select_from(
self.received_offers.outerjoin(
self.peers,
self.peers.c.host == self.received_offers.c.peer_host,
self.peers.c.port == self.received_offers.c.peer_port,
)
)
select([self.received_offers])
.where(self.received_offers.c.squeak_hash == squeak_hash.hex())
.where(self.received_offers.c.peer_host == peer_address.host)
.where(self.received_offers.c.peer_port == peer_address.port)
@ -812,8 +792,8 @@ class SqueakDb:
row = result.fetchone()
if row is None:
return None
offer_with_peer = self._parse_received_offer_with_peer(row)
return offer_with_peer
offer = self._parse_received_offer(row)
return offer
def delete_expired_received_offers(self):
""" Delete all expired offers. """
@ -1179,16 +1159,16 @@ class SqueakDb:
),
)
def _parse_received_offer_with_peer(self, row) -> ReceivedOfferWithPeer:
offer = self._parse_received_offer(row)
if row[self.peers.c.peer_id] is None:
peer = None
else:
peer = self._parse_squeak_peer(row)
return ReceivedOfferWithPeer(
received_offer=offer,
peer=peer,
)
# def _parse_received_offer_with_peer(self, row) -> ReceivedOfferWithPeer:
# offer = self._parse_received_offer(row)
# if row[self.peers.c.peer_id] is None:
# peer = None
# else:
# peer = self._parse_squeak_peer(row)
# return ReceivedOfferWithPeer(
# received_offer=offer,
# peer=peer,
# )
def _parse_sent_payment(self, row) -> SentPayment:
return SentPayment(

View file

@ -20,7 +20,6 @@ from squeaknode.core.block_range import BlockRange
from squeaknode.core.offer import Offer
from squeaknode.core.peer_address import PeerAddress
from squeaknode.core.received_offer import ReceivedOffer
from squeaknode.core.received_offer_with_peer import ReceivedOfferWithPeer
from squeaknode.core.received_payment_summary import ReceivedPaymentSummary
from squeaknode.core.sent_offer import SentOffer
from squeaknode.core.sent_payment_summary import SentPaymentSummary
@ -269,32 +268,31 @@ class SqueakController:
def delete_peer(self, peer_id: int):
self.squeak_db.delete_peer(peer_id)
def get_received_offers_with_peer(self, squeak_hash: bytes) -> List[ReceivedOfferWithPeer]:
return self.squeak_db.get_received_offers_with_peer(squeak_hash)
def get_received_offers(self, squeak_hash: bytes) -> List[ReceivedOffer]:
return self.squeak_db.get_received_offers(squeak_hash)
def get_received_offer_for_squeak_and_peer(
self,
squeak_hash: bytes,
peer_addresss: PeerAddress,
) -> Optional[ReceivedOfferWithPeer]:
) -> Optional[ReceivedOffer]:
return self.squeak_db.get_received_offer_for_squeak_and_peer(
squeak_hash,
peer_addresss,
)
def get_buy_offer_with_peer(self, received_offer_id: int) -> Optional[ReceivedOfferWithPeer]:
return self.squeak_db.get_offer_with_peer(
def get_received_offer(self, received_offer_id: int) -> Optional[ReceivedOffer]:
return self.squeak_db.get_received_offer(
received_offer_id)
def pay_offer(self, received_offer_id: int) -> int:
# Get the offer from the database
received_offer_with_peer = self.squeak_db.get_offer_with_peer(
received_offer = self.squeak_db.get_received_offer(
received_offer_id)
if received_offer_with_peer is None:
if received_offer is None:
raise Exception("Received offer with id {} not found.".format(
received_offer_id,
))
received_offer = received_offer_with_peer.received_offer
logger.info("Paying received offer: {}".format(received_offer))
sent_payment = self.squeak_core.pay_offer(received_offer)
sent_payment_id = self.squeak_db.insert_sent_payment(sent_payment)