use blastr to write only

This commit is contained in:
=
2023-03-09 22:46:59 +00:00
parent 9996e5bbd5
commit 8264199f86
2 changed files with 83 additions and 27 deletions

View File

@@ -215,7 +215,7 @@ describe('', () => {
}); });
await fetchMyProfileEvents(SampleEvents.kind0.pubkey, mockEventProcessor); await fetchMyProfileEvents(SampleEvents.kind0.pubkey, mockEventProcessor);
}; };
test('1 write relays, function called with custom relay and 2 default relays + blaster', async () => { test('1 write relays, function called with custom relay and 2 default relays', async () => {
storeMyProfileEvent({ storeMyProfileEvent({
...SampleEvents.kind10002, ...SampleEvents.kind10002,
tags: [ tags: [
@@ -230,12 +230,11 @@ describe('', () => {
'wss://alicerelay.example.com', 'wss://alicerelay.example.com',
'wss://relay.damus.io', 'wss://relay.damus.io',
'wss://nostr-pub.wellorder.net', 'wss://nostr-pub.wellorder.net',
'wss://nostr.mutinywallet.com',
], ],
expect.anything(), expect.anything(),
); );
}); });
test('2 write relays function called with custom relays and 1 default relays + blaster', async () => { test('2 write relays function called with custom relays and 1 default relays', async () => {
storeMyProfileEvent({ storeMyProfileEvent({
...SampleEvents.kind10002, ...SampleEvents.kind10002,
tags: [ tags: [
@@ -251,12 +250,11 @@ describe('', () => {
'wss://alicerelay.example.com', 'wss://alicerelay.example.com',
'wss://expensive-relay.example2.com', 'wss://expensive-relay.example2.com',
'wss://relay.damus.io', 'wss://relay.damus.io',
'wss://nostr.mutinywallet.com',
], ],
expect.anything(), expect.anything(),
); );
}); });
test('2 write relays including first defauly relay. function called with custom relays and 1 different default relays + blaster', async () => { test('2 write relays including first defauly relay. function called with custom relays and 1 different default relays', async () => {
storeMyProfileEvent({ storeMyProfileEvent({
...SampleEvents.kind10002, ...SampleEvents.kind10002,
tags: [ tags: [
@@ -272,12 +270,11 @@ describe('', () => {
'wss://relay.damus.io', 'wss://relay.damus.io',
'wss://expensive-relay.example2.com', 'wss://expensive-relay.example2.com',
'wss://nostr-pub.wellorder.net', 'wss://nostr-pub.wellorder.net',
'wss://nostr.mutinywallet.com',
], ],
expect.anything(), expect.anything(),
); );
}); });
test('with 4 write relays function called with all custom relays + blaster', async () => { test('with 4 write relays function called with all custom relays', async () => {
storeMyProfileEvent({ storeMyProfileEvent({
...SampleEvents.kind10002, ...SampleEvents.kind10002,
tags: [ tags: [
@@ -296,7 +293,6 @@ describe('', () => {
'wss://brando-relay.com', 'wss://brando-relay.com',
'wss://expensive-relay.example2.com', 'wss://expensive-relay.example2.com',
'wss://alicerelay.example3.com', 'wss://alicerelay.example3.com',
'wss://nostr.mutinywallet.com',
], ],
expect.anything(), expect.anything(),
); );
@@ -319,13 +315,12 @@ describe('', () => {
'wss://alicerelay.example.com', 'wss://alicerelay.example.com',
'wss://brando-relay.com', 'wss://brando-relay.com',
'wss://expensive-relay.example2.com', 'wss://expensive-relay.example2.com',
'wss://nostr.mutinywallet.com',
], ],
expect.anything(), expect.anything(),
); );
}); });
}); });
describe('and when no cached 10002 events are present', () => { describe('and when no cached 10002 events are present and none are return', () => {
const mockstoreMyProfileEvent = jest.spyOn(FetchEvents, 'storeMyProfileEvent'); const mockstoreMyProfileEvent = jest.spyOn(FetchEvents, 'storeMyProfileEvent');
beforeEach(async () => { beforeEach(async () => {
mockisUptodate.mockReturnValue(false); mockisUptodate.mockReturnValue(false);
@@ -339,10 +334,7 @@ describe('', () => {
test('updateLastFetchDate called once', () => { test('updateLastFetchDate called once', () => {
expect(mockupdateLastFetchDate).toBeCalledTimes(1); expect(mockupdateLastFetchDate).toBeCalledTimes(1);
}); });
test('fetchCachedProfileEvent only to be called once to getRelays', () => { test('requestEventsFromRelays called once', () => {
expect(fetchCachedProfileEventSpy).toBeCalledTimes(1);
});
test('requestEventsFromRelays called', () => {
expect(mockrequestEventsFromRelays).toBeCalledTimes(1); expect(mockrequestEventsFromRelays).toBeCalledTimes(1);
}); });
test('mockrequestEventsFromRelays called with correct pubkey', () => { test('mockrequestEventsFromRelays called with correct pubkey', () => {
@@ -361,7 +353,6 @@ describe('', () => {
'wss://relay.damus.io', 'wss://relay.damus.io',
'wss://nostr-pub.wellorder.net', 'wss://nostr-pub.wellorder.net',
'wss://nostr-relay.wlvs.space', 'wss://nostr-relay.wlvs.space',
'wss://nostr.mutinywallet.com',
], ],
expect.anything(), expect.anything(),
); );
@@ -386,6 +377,59 @@ describe('', () => {
expect(mockEventProcessor).toBeCalledTimes(2); expect(mockEventProcessor).toBeCalledTimes(2);
}); });
}); });
describe('and when new 10002 event is recieved with non-default write relays', () => {
beforeEach(async () => {
mockisUptodate.mockReturnValue(false);
mockrequestEventsFromRelays.mockReset()
.mockImplementation(async (_pubkey, eventProcessor) => {
eventProcessor({ ...SampleEvents.kind0 });
eventProcessor({
...SampleEvents.kind10002,
tags: [
['r', 'wss://alicerelay.example.com'],
],
});
});
await fetchMyProfileEvents(SampleEvents.kind0.pubkey, mockEventProcessor);
});
test('requestEventsFromRelays called twice', () => {
expect(mockrequestEventsFromRelays).toBeCalledTimes(2);
});
test('mockrequestEventsFromRelays called with correct non-default relays the second time', () => {
expect(mockrequestEventsFromRelays).toHaveBeenNthCalledWith(
2,
expect.anything(),
expect.anything(),
[
'wss://alicerelay.example.com',
'wss://relay.damus.io',
'wss://nostr-pub.wellorder.net',
],
expect.anything(),
);
});
});
});
});
describe('publishEvent', () => {
const mockstoreMyProfileEvent = jest.spyOn(FetchEvents, 'storeMyProfileEvent');
const mockPublishEventToRelay = jest.spyOn(RelayManagement, 'publishEventToRelay');
beforeEach(async () => {
mockstoreMyProfileEvent.mockReset();
mockPublishEventToRelay.mockReset()
.mockImplementation(() => new Promise((r) => { r(true); }));
});
test('publishEventToRelay is called with getRelays output + blastr', () => {
FetchEvents.publishEvent(SampleEvents.kind0);
expect(mockPublishEventToRelay).toBeCalledWith(
SampleEvents.kind0,
[
'wss://relay.damus.io',
'wss://nostr-pub.wellorder.net',
'wss://nostr-relay.wlvs.space',
'wss://nostr.mutinywallet.com', // blastr
],
);
}); });
}); });
}); });

View File

@@ -95,16 +95,13 @@ export const fetchCachedProfileEvent = (kind: 0 | 2 | 10002 | 3): null | Event =
const getRelays = () => { const getRelays = () => {
const e = fetchCachedProfileEvent(10002); const e = fetchCachedProfileEvent(10002);
const mywriterelays = !e ? [] : e.tags.filter((r) => !r[2] || r[2] === 'write').map((r) => r[1]); const mywriterelays = !e ? [] : e.tags.filter((r) => !r[2] || r[2] === 'write').map((r) => r[1]);
// return minimum of 3 relays + blastr, filling in with default relays (removing duplicates) // return minimum of 3 relays, filling in with default relays (removing duplicates)
return [ return mywriterelays.length > 3 ? mywriterelays : [...new Set([
...(mywriterelays.length > 3 ? mywriterelays : [...new Set([ ...mywriterelays,
...mywriterelays, 'wss://relay.damus.io',
'wss://relay.damus.io', 'wss://nostr-pub.wellorder.net',
'wss://nostr-pub.wellorder.net', 'wss://nostr-relay.wlvs.space',
'wss://nostr-relay.wlvs.space', ])].slice(0, 3);
])].slice(0, 3)),
'wss://nostr.mutinywallet.com', // blastr
];
}; };
/** get my latest profile events either from cache (if isUptodate) or from relays */ /** get my latest profile events either from cache (if isUptodate) or from relays */
@@ -114,10 +111,19 @@ export const fetchMyProfileEvents = async (
): Promise<void> => { ): Promise<void> => {
// get events from relays, store them and run profileEventProcesser // get events from relays, store them and run profileEventProcesser
if (!isUptodate()) { if (!isUptodate()) {
const starterrelays = getRelays();
await requestEventsFromRelays([pubkey], (event: Event) => { await requestEventsFromRelays([pubkey], (event: Event) => {
storeMyProfileEvent(event); storeMyProfileEvent(event);
profileEventProcesser(event); profileEventProcesser(event);
}, getRelays(), [0, 2, 10002, 3]); }, starterrelays, [0, 2, 10002, 3]);
// if new 10002 event found with more write relays
if (
fetchCachedProfileEvent(10002)?.tags
.some((t) => starterrelays.indexOf(t[1]) === -1 && (!t[2] || t[2] === 'write'))
) {
// fetch events again to ensure we got all my profile events
await fetchMyProfileEvents(pubkey, profileEventProcesser);
}
// update last-fetch-from-relays date // update last-fetch-from-relays date
updateLastFetchDate(); updateLastFetchDate();
} else { } else {
@@ -130,7 +136,13 @@ export const fetchMyProfileEvents = async (
}; };
export const publishEvent = async (event:Event):Promise<boolean> => { export const publishEvent = async (event:Event):Promise<boolean> => {
const r = await publishEventToRelay(event, getRelays()); const r = await publishEventToRelay(
event,
[
...getRelays(),
'wss://nostr.mutinywallet.com', // blastr
],
);
if (r) storeMyProfileEvent(event); if (r) storeMyProfileEvent(event);
return r; return r;
}; };