-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60a0d1f
commit 80027e4
Showing
17 changed files
with
379 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ deploy.log | |
|
||
# Next.js | ||
.next | ||
.next-* | ||
|
||
# builds | ||
dist | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Explored } from '@siafoundation/explored-js' | ||
import { Hostd } from '@siafoundation/hostd-js' | ||
import { Bus } from '@siafoundation/renterd-js' | ||
import { Walletd } from '@siafoundation/walletd-js' | ||
import { startWebServerCluster, stopWebServer } from './webServerCluster' | ||
import { | ||
clusterd, | ||
setupCluster, | ||
teardownCluster, | ||
} from '@siafoundation/clusterd' | ||
|
||
export type Cluster = Awaited<ReturnType<typeof startCluster>> | ||
|
||
export async function startCluster() { | ||
const exploredCount = 1 | ||
const renterdCount = 1 | ||
const walletdCount = 1 | ||
const hostdCount = 3 | ||
await setupCluster({ | ||
exploredCount, | ||
renterdCount, | ||
walletdCount, | ||
hostdCount, | ||
}) | ||
const renterd = clusterd.nodes.find((n) => n.type === 'renterd') | ||
const explored = clusterd.nodes.find((n) => n.type === 'explored') | ||
const hostds = clusterd.nodes.filter((n) => n.type === 'hostd') | ||
const walletds = clusterd.nodes.filter((n) => n.type === 'walletd') | ||
if (!renterd || !explored || !hostds || !walletds) { | ||
throw new Error('Failed to start cluster') | ||
} | ||
const daemons = { | ||
renterd, | ||
explored, | ||
hostds, | ||
walletds, | ||
} | ||
const apis = { | ||
renterd: Bus({ | ||
api: `${renterd.apiAddress}/api`, | ||
password: renterd.password, | ||
}), | ||
explored: Explored({ | ||
api: `${explored.apiAddress}/api`, | ||
password: explored.password, | ||
}), | ||
hostds: hostds.map((h) => | ||
Hostd({ | ||
api: `${h.apiAddress}/api`, | ||
password: h.password, | ||
}) | ||
), | ||
walletds: walletds.map((w) => | ||
Walletd({ | ||
api: `${w.apiAddress}/api`, | ||
password: w.password, | ||
}) | ||
), | ||
} | ||
const { baseUrl } = await startWebServerCluster({ | ||
exploredAddress: daemons.explored.apiAddress, | ||
}) | ||
console.log(` | ||
webServerUrl: ${baseUrl} | ||
clusterd: http://localhost:${clusterd.managementPort} | ||
explored: ${daemons.explored.apiAddress} | ||
renterd: ${daemons.renterd.apiAddress} | ||
hostds: ${daemons.hostds.map((h) => h.apiAddress)} | ||
walletds: ${daemons.walletds.map((w) => w.apiAddress)} | ||
`) | ||
return { | ||
webServerUrl: baseUrl, | ||
clusterd, | ||
apis, | ||
daemons, | ||
} | ||
} | ||
|
||
export async function stopCluster() { | ||
stopWebServer() | ||
teardownCluster() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Walletd } from '@siafoundation/walletd-js' | ||
import { blake2bHex } from 'blakejs' | ||
import { initSDK, getSDK } from '@siafoundation/sdk' | ||
import { WalletAddressMetadata } from '@siafoundation/walletd-types' | ||
import { to } from '@siafoundation/request' | ||
import { clusterd, mine } from '@siafoundation/clusterd' | ||
import { Bus } from '@siafoundation/renterd-js' | ||
import { humanSiacoin } from '@siafoundation/units' | ||
|
||
export async function addWalletToWalletd(walletd: ReturnType<typeof Walletd>) { | ||
await initSDK() | ||
const sdk = getSDK() | ||
const { phrase: mnemonic } = sdk.wallet.generateSeedPhrase() | ||
const mnemonicHash = blake2bHex(mnemonic) | ||
const [wallet, walletError] = await to( | ||
walletd.walletAdd({ | ||
data: { | ||
name: 'test', | ||
description: 'test', | ||
metadata: { | ||
type: 'seed', | ||
mnemonicHash, | ||
}, | ||
}, | ||
}) | ||
) | ||
if (!wallet || walletError) { | ||
throw new Error(`Failed to add wallet: ${walletError}`) | ||
} | ||
const kp = sdk.wallet.keyPairFromSeedPhrase(mnemonic, 0) | ||
const suh = sdk.wallet.standardUnlockHash(kp.publicKey) | ||
const uc = sdk.wallet.standardUnlockConditions(kp.publicKey) | ||
const metadata: WalletAddressMetadata = { | ||
index: 0, | ||
} | ||
const [, addressError] = await to( | ||
walletd.walletAddressAdd({ | ||
params: { | ||
id: wallet.id, | ||
}, | ||
data: { | ||
address: suh.address, | ||
description: '', | ||
spendPolicy: { | ||
type: 'uc', | ||
policy: uc.unlockConditions, | ||
}, | ||
metadata, | ||
}, | ||
}) | ||
) | ||
if (addressError) { | ||
throw new Error(`Failed to add address: ${addressError}`) | ||
} | ||
return { wallet, address: suh.address } | ||
} | ||
|
||
// The renterd node receives all the initial funds so we can use it to fund | ||
// other wallets. | ||
export async function sendSiacoinFromRenterd(address: string, amount: string) { | ||
console.log(`Sending ${humanSiacoin(amount)} from renterd to:`, address) | ||
const renterdNode = clusterd.nodes.find((n) => n.type === 'renterd') | ||
if (!renterdNode) { | ||
throw new Error('Renterd node not found') | ||
} | ||
const bus = Bus({ | ||
api: renterdNode.apiAddress + '/api', | ||
password: renterdNode.password, | ||
}) | ||
|
||
try { | ||
// Send some funds to the wallet. | ||
await bus.walletSend({ | ||
data: { | ||
address, | ||
amount, | ||
subtractMinerFee: false, | ||
}, | ||
}) | ||
await mine(1) | ||
} catch (e) { | ||
console.log('error sending siacoin', e) | ||
} | ||
} | ||
|
||
export async function getRenterdAddress() { | ||
const renterdNode = clusterd.nodes.find((n) => n.type === 'renterd') | ||
if (!renterdNode) { | ||
throw new Error('Renterd node not found') | ||
} | ||
return renterdNode.walletAddress | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { ChildProcess, spawn } from 'child_process' | ||
import { workspaceRoot } from '@nx/devkit' | ||
import net from 'net' | ||
|
||
let server: ChildProcess | ||
let baseUrl: string | ||
|
||
// Starts the explorer app webserver configured to run against the testnet | ||
// cluster provided via the NEXT_PUBLIC_EXPLORED_ADDRESS environment variable. | ||
export async function startWebServerCluster({ | ||
exploredAddress, | ||
}: { | ||
exploredAddress: string | ||
}) { | ||
const port = await findFreePort() | ||
server = spawn( | ||
'npx', | ||
[ | ||
'nx', | ||
'run', | ||
'explorer:serve:development-testnet-cluster', | ||
'--port', | ||
port.toString(), | ||
], | ||
{ | ||
cwd: workspaceRoot, | ||
shell: true, | ||
env: { | ||
...process.env, | ||
NEXT_PUBLIC_EXPLORED_ADDRESS: exploredAddress, | ||
}, | ||
} | ||
) | ||
|
||
server.stdout?.on('data', (data) => { | ||
console.log(data.toString()) | ||
}) | ||
|
||
server.stderr?.on('data', (data) => { | ||
console.error(data.toString()) | ||
}) | ||
|
||
// Wait until stdout prints "Ready", eg: | ||
// ✓ Starting... | ||
// ✓ Ready in 1606ms | ||
await new Promise((resolve) => { | ||
server.stdout?.on('data', (data) => { | ||
if (data.toString().includes('Ready')) { | ||
console.log('Server ready') | ||
resolve(true) | ||
} | ||
}) | ||
}) | ||
baseUrl = `http://localhost:${port}` | ||
return { | ||
baseUrl, | ||
} | ||
} | ||
|
||
export function stopWebServer() { | ||
console.log('Stopping webserver: ', baseUrl) | ||
server.kill() // Kill the server after each test | ||
} | ||
|
||
async function findFreePort(): Promise<number> { | ||
return new Promise((res) => { | ||
const srv = net.createServer() | ||
srv.listen(0, () => { | ||
const addr = srv.address() | ||
if (typeof addr === 'string') { | ||
throw new Error('Address is a string') | ||
} | ||
const port = addr?.port | ||
if (!port) { | ||
throw new Error('Port is undefined') | ||
} | ||
srv.close(() => res(port)) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.