Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit e5841a8

Browse files
teogebbearni95
authored andcommitted
NET-286: Storage node constants (#254)
Added constants StorageNode.STREAMR_GERMANY and StorageNode.STREAMR_DOCKER_DEV. The values are instances of StorageNode class. The parameter of addToStorageNode, removeFromStorageNode and getStreamPartsByStorageNode can now be a StorageNode instance or a string.
1 parent ded10e9 commit e5841a8

20 files changed

+59
-31
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ const stream = await client.getOrCreateStream({
9898
name: 'My awesome stream created via the API',
9999
})
100100
console.log(`Stream ${stream.id} has been created!`)
101+
102+
// Optional: to enable historical data resends, add the stream to a storage node
103+
await stream.addToStorageNode(StorageNode.STREAMR_GERMANY)
104+
101105
// Do something with the stream, for example call stream.publish(message)
102106
```
103107

src/Config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { BytesLike } from '@ethersproject/bytes'
1313
import { isAddress } from '@ethersproject/address'
1414
import has from 'lodash/has'
1515
import get from 'lodash/get'
16+
import { StorageNode } from './stream/StorageNode'
1617

1718
export type EthereumConfig = ExternalProvider|JsonRpcFetchFunc
1819

@@ -144,7 +145,7 @@ export const STREAM_CLIENT_DEFAULTS: StrictStreamrClientOptions = {
144145
templateSidechainAddress: '0xf1E9d6E254BeA3f0129018AcA1A50AEcb7D528be',
145146
},
146147
storageNode: {
147-
address: '0x31546eEA76F2B2b3C5cC06B1c93601dc35c9D916',
148+
address: StorageNode.STREAMR_GERMANY.getAddress(),
148149
url: 'https://corea1.streamr.network:8001'
149150
},
150151
cache: {

src/rest/StreamEndpoints.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { EthereumAddress } from '../types'
1515
import { StreamrClient } from '../StreamrClient'
1616
// TODO change this import when streamr-client-protocol exports StreamMessage type or the enums types directly
1717
import { ContentType, EncryptionType, SignatureType, StreamMessageType } from 'streamr-client-protocol/dist/src/protocol/message_layer/StreamMessage'
18+
import { StorageNode } from '../stream/StorageNode'
1819

1920
const debug = debugFactory('StreamrClient')
2021

@@ -255,7 +256,8 @@ export class StreamEndpoints {
255256
return json
256257
}
257258

258-
async getStreamPartsByStorageNode(address: EthereumAddress) {
259+
async getStreamPartsByStorageNode(node: StorageNode|EthereumAddress) {
260+
const address = (node instanceof StorageNode) ? node.getAddress() : node
259261
type ItemType = { id: string, partitions: number}
260262
const json = await authFetch<ItemType[]>(getEndpointUrl(this.client.options.restUrl, 'storageNodes', address, 'streams'), this.client.session)
261263
let result: StreamPart[] = []

src/stream/StorageNode.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { EthereumAddress } from '../types'
22

33
export class StorageNode {
44

5+
static STREAMR_GERMANY = new StorageNode('0x31546eEA76F2B2b3C5cC06B1c93601dc35c9D916')
6+
static STREAMR_DOCKER_DEV = new StorageNode('0xde1112f631486CfC759A50196853011528bC5FA0')
7+
58
private _address: EthereumAddress
69

710
constructor(address: EthereumAddress) {

src/stream/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import authFetch from '../rest/authFetch'
55

66
import { StorageNode } from './StorageNode'
77
import { StreamrClient } from '../StreamrClient'
8+
import { EthereumAddress } from '../types'
89

910
// TODO explicit types: e.g. we never provide both streamId and id, or both streamPartition and partition
1011
export type StreamPartDefinitionOptions = { streamId?: string, streamPartition?: number, id?: string, partition?: number, stream?: Stream|string }
@@ -223,7 +224,8 @@ export class Stream {
223224
await this.update()
224225
}
225226

226-
async addToStorageNode(address: string, { timeout = 30000 }: { timeout: number } = { timeout: 30000 }) {
227+
async addToStorageNode(node: StorageNode|EthereumAddress, { timeout = 30000 }: { timeout: number } = { timeout: 30000 }) {
228+
const address = (node instanceof StorageNode) ? node.getAddress() : node
227229
// currently we support only one storage node
228230
// -> we can validate that the given address is that address
229231
// -> remove this comparison when we start to support multiple storage nodes
@@ -258,7 +260,8 @@ export class Stream {
258260
throw new Error(`Unexpected response code ${response.status} when fetching stream storage status`)
259261
}
260262

261-
async removeFromStorageNode(address: string) {
263+
async removeFromStorageNode(node: StorageNode|EthereumAddress) {
264+
const address = (node instanceof StorageNode) ? node.getAddress() : node
262265
await authFetch(
263266
getEndpointUrl(this._client.options.restUrl, 'streams', this.id, 'storageNodes', address),
264267
this._client.session,

test/integration/Encryption.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Defer } from '../../src/utils'
66
import { StreamrClient } from '../../src/StreamrClient'
77
import { GroupKey } from '../../src/stream/Encryption'
88
import Connection from '../../src/Connection'
9+
import { StorageNode } from '../../src/stream/StorageNode'
910

1011
import config from './config'
1112

@@ -107,7 +108,7 @@ describe('decryption', () => {
107108
requireEncryptedData: true,
108109
})
109110

110-
await stream.addToStorageNode(config.clientOptions.storageNode.address)
111+
await stream.addToStorageNode(StorageNode.STREAMR_DOCKER_DEV)
111112

112113
publishTestMessages = getPublishTestMessages(client, {
113114
stream

test/integration/GapFill.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Stream } from '../../src/stream'
99
import { Subscriber, Subscription } from '../../src/subscribe'
1010
import { MessageRef } from 'streamr-client-protocol/dist/src/protocol/message_layer'
1111
import { StreamrClientOptions } from '../../src'
12+
import { StorageNode } from '../../src/stream/StorageNode'
1213

1314
const MAX_MESSAGES = 10
1415

@@ -47,7 +48,7 @@ describeRepeats('GapFill with resends', () => {
4748
requireSignedData: true,
4849
name: uid('stream')
4950
})
50-
await stream.addToStorageNode(config.clientOptions.storageNode.address)
51+
await stream.addToStorageNode(StorageNode.STREAMR_DOCKER_DEV)
5152

5253
client.debug('connecting before test <<')
5354
publishTestMessages = getPublishTestMessages(client, stream.id)

test/integration/MultipleClients.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { describeRepeats, uid, fakePrivateKey, getWaitForStorage, getPublishTest
55
import { StreamrClient } from '../../src/StreamrClient'
66
import { counterId, Defer, pLimitFn } from '../../src/utils'
77
import Connection from '../../src/Connection'
8+
import { StorageNode } from '../../src/stream/StorageNode'
89

910
import config from './config'
1011

@@ -51,7 +52,7 @@ describeRepeats('PubSub with multiple clients', () => {
5152
stream = await mainClient.createStream({
5253
name: uid('stream')
5354
})
54-
await stream.addToStorageNode(config.clientOptions.storageNode.address)
55+
await stream.addToStorageNode(StorageNode.STREAMR_DOCKER_DEV)
5556
})
5657

5758
afterEach(async () => {

test/integration/ResendReconnect.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import config from './config'
88
import { Stream } from '../../src/stream'
99
import { Subscription } from '../../src'
1010
import { PublishRequest } from 'streamr-client-protocol/dist/src/protocol/control_layer'
11+
import { StorageNode } from '../../src/stream/StorageNode'
1112

1213
const createClient = (opts = {}) => new StreamrClient({
1314
...config.clientOptions,
@@ -35,7 +36,7 @@ describe('resend/reconnect', () => {
3536
name: uid('resends')
3637
})
3738

38-
await stream.addToStorageNode(config.clientOptions.storageNode.address)
39+
await stream.addToStorageNode(StorageNode.STREAMR_DOCKER_DEV)
3940
}, 10000)
4041

4142
beforeEach(async () => {

test/integration/Resends.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Connection from '../../src/Connection'
77

88
import config from './config'
99
import { Stream } from '../../src/stream'
10+
import { StorageNode } from '../../src/stream/StorageNode'
1011

1112
const MAX_MESSAGES = 10
1213
const WAIT_FOR_STORAGE_TIMEOUT = 6000
@@ -76,7 +77,7 @@ describe('StreamrClient resends', () => {
7677
name: uid('resends')
7778
})
7879

79-
await stream.addToStorageNode(config.clientOptions.storageNode.address)
80+
await stream.addToStorageNode(StorageNode.STREAMR_DOCKER_DEV)
8081
})
8182

8283
beforeEach(async () => {
@@ -337,7 +338,7 @@ describe('StreamrClient resends', () => {
337338
name: uid('resends')
338339
})
339340

340-
await stream.addToStorageNode(config.clientOptions.storageNode.address)
341+
await stream.addToStorageNode(StorageNode.STREAMR_DOCKER_DEV)
341342

342343
publishTestMessages = getPublishTestMessages(client, {
343344
stream

0 commit comments

Comments
 (0)