Skip to content

Commit a4b3780

Browse files
committed
fix IPFS bootstrap comparison
1 parent 89e1fb5 commit a4b3780

File tree

3 files changed

+66
-25
lines changed

3 files changed

+66
-25
lines changed

packages/ethereum-storage/src/config.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { BigNumber } from 'ethers';
33

44
// This contains default values used to use Ethereum Network and IPFS
55
// if information are not provided by the user
6-
const config: any = {
6+
const config = {
77
ethereum: {
8-
default: 'private',
8+
default: 'private' as const,
99
gasPriceDefault: '100000000000',
1010
maxRetries: 5,
1111
nodeUrlDefault: {
@@ -22,22 +22,19 @@ const config: any = {
2222
defaultNode: {
2323
host: 'localhost',
2424
port: 5001,
25-
protocol: 'http',
25+
protocol: 'http' as StorageTypes.IpfsGatewayProtocol,
2626
timeout: 30000,
2727
},
2828
errorHandling: {
2929
delayBetweenRetries: 500,
3030
maxRetries: 3,
3131
},
32-
expectedBootstrapNodes: [
33-
'/dns4/ipfs-bootstrap.request.network/tcp/4001/ipfs/QmaSrBXFBaupfeGMTuigswtKtsthbVaSonurjTV967Fdxx',
34-
35-
'/dns4/ipfs-bootstrap-2.request.network/tcp/4001/ipfs/QmYdcSoVNU1axgSnkRAyHtwsKiSvFHXeVvRonGCAV9LVEj',
36-
37-
'/dns4/ipfs-2.request.network/tcp/4001/ipfs/QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd',
38-
39-
'/dns4/ipfs-survival.request.network/tcp/4001/ipfs/Qmb6a5DH45k8JwLdLVZUhRhv1rnANpsbXjtsH41esGhNCh',
40-
],
32+
expectedBootstrapNodes: {
33+
'ipfs-bootstrap.request.network': 'QmaSrBXFBaupfeGMTuigswtKtsthbVaSonurjTV967Fdxx',
34+
'ipfs-bootstrap-2.request.network': 'QmYdcSoVNU1axgSnkRAyHtwsKiSvFHXeVvRonGCAV9LVEj',
35+
'ipfs-2.request.network': 'QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd',
36+
'ipfs-survival.request.network': 'Qmb6a5DH45k8JwLdLVZUhRhv1rnANpsbXjtsH41esGhNCh',
37+
},
4138
maxIpfsReadRetry: 1,
4239
pinRequest: {
4340
delayBetweenCalls: 1000,
@@ -147,10 +144,12 @@ export function getIpfsErrorHandlingConfig(): StorageTypes.IIpfsErrorHandlingCon
147144

148145
/**
149146
* Retrieve from config the ipfs bootstrap nodes of the ipfs node
150-
* @returns array of the swarm addresses
147+
* @returns array of the swarm addresses regexes
151148
*/
152-
export function getIpfsExpectedBootstrapNodes(): string[] {
153-
return config.ipfs.expectedBootstrapNodes;
149+
export function getIpfsExpectedBootstrapNodes(): RegExp[] {
150+
return Object.entries(config.ipfs.expectedBootstrapNodes).map(
151+
([host, id]) => new RegExp(`/dns4/${host}/tcp/4001/(ipfs|p2p)/${id}`),
152+
);
154153
}
155154

156155
/**

packages/ethereum-storage/src/ipfs-storage.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,7 @@ export class IpfsStorage implements StorageTypes.IIpfsStorage {
125125
this.logger.info('Checking ipfs network', ['ipfs', 'sanity']);
126126
try {
127127
const bootstrapList = await this.ipfsManager.getBootstrapList();
128-
129-
const bootstrapNodeFoundCount: number = getIpfsExpectedBootstrapNodes().filter(
130-
(nodeExpected) => bootstrapList.includes(nodeExpected),
131-
).length;
132-
133-
if (bootstrapNodeFoundCount !== getIpfsExpectedBootstrapNodes().length) {
128+
if (!IpfsStorage.hasRequiredBootstrapNodes(bootstrapList)) {
134129
throw Error(
135130
`The list of bootstrap node in the ipfs config don't match the expected bootstrap nodes`,
136131
);
@@ -139,4 +134,13 @@ export class IpfsStorage implements StorageTypes.IIpfsStorage {
139134
throw Error(`IPFS node bootstrap node check failed: ${error}`);
140135
}
141136
}
137+
138+
static hasRequiredBootstrapNodes(actualList: string[]): boolean {
139+
const expectedList = getIpfsExpectedBootstrapNodes();
140+
const bootstrapNodeFoundCount: number = expectedList.filter((nodeExpected) =>
141+
actualList.find((actual) => nodeExpected.test(actual)),
142+
).length;
143+
144+
return bootstrapNodeFoundCount === expectedList.length;
145+
}
142146
}

packages/ethereum-storage/test/ipfs-storage.test.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,47 @@ describe('IPFS Storage', () => {
9393
});
9494

9595
await ipfsStorage.pinDataToIPFS([hash1]);
96-
expect(warnLogMock).toHaveBeenCalledWith(
97-
'Failed pinning some hashes the IPFS node: Error: expected error',
98-
['ipfs'],
99-
);
96+
expect(
97+
warnLogMock,
98+
).toHaveBeenCalledWith('Failed pinning some hashes the IPFS node: Error: expected error', [
99+
'ipfs',
100+
]);
101+
});
102+
103+
describe('compareBootstrapNodes', () => {
104+
describe.each(['ipfs', 'p2p'])('It supports the %s path', (path) => {
105+
it('Returns true for same list', () => {
106+
expect(
107+
IpfsStorage.hasRequiredBootstrapNodes([
108+
`/dns4/ipfs-survival.request.network/tcp/4001/${path}/Qmb6a5DH45k8JwLdLVZUhRhv1rnANpsbXjtsH41esGhNCh`,
109+
`/dns4/ipfs-2.request.network/tcp/4001/${path}/QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd`,
110+
`/dns4/ipfs-bootstrap-2.request.network/tcp/4001/${path}/QmYdcSoVNU1axgSnkRAyHtwsKiSvFHXeVvRonGCAV9LVEj`,
111+
`/dns4/ipfs-bootstrap.request.network/tcp/4001/${path}/QmaSrBXFBaupfeGMTuigswtKtsthbVaSonurjTV967Fdxx`,
112+
]),
113+
).toBeTruthy();
114+
});
115+
116+
it('Returns false for additional items', () => {
117+
expect(
118+
IpfsStorage.hasRequiredBootstrapNodes([
119+
`/dns4/ipfs-survival.request.network/tcp/4001/${path}/Qmb6a5DH45k8JwLdLVZUhRhv1rnANpsbXjtsH41esGhNCh`,
120+
`/dns4/ipfs-2.request.network/tcp/4001/${path}/QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd`,
121+
`/dns4/ipfs-bootstrap-2.request.network/tcp/4001/${path}/QmYdcSoVNU1axgSnkRAyHtwsKiSvFHXeVvRonGCAV9LVEj`,
122+
`/dns4/ipfs-bootstrap.request.network/tcp/4001/${path}/QmaSrBXFBaupfeGMTuigswtKtsthbVaSonurjTV967Fdxx`,
123+
`/dns4/ipfs-bootstrap-NONEXISTANT.request.network/tcp/4001/${path}/QmaSrBXFBaupfeGMTuigswtKtsthbVaSonurjTV967FaKe`,
124+
]),
125+
).toBeTruthy();
126+
});
127+
128+
it('Returns false for missing items', () => {
129+
expect(
130+
IpfsStorage.hasRequiredBootstrapNodes([
131+
`/dns4/ipfs-survival.request.network/tcp/4001/${path}/Qmb6a5DH45k8JwLdLVZUhRhv1rnANpsbXjtsH41esGhNCh`,
132+
`/dns4/ipfs-2.request.network/tcp/4001/${path}/QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd`,
133+
`/dns4/ipfs-bootstrap-2.request.network/tcp/4001/${path}/QmYdcSoVNU1axgSnkRAyHtwsKiSvFHXeVvRonGCAV9LVEj`,
134+
]),
135+
).toBeFalsy();
136+
});
137+
});
100138
});
101139
});

0 commit comments

Comments
 (0)