-
Notifications
You must be signed in to change notification settings - Fork 6
Experimenting with new smoldot version #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8fdb755
experimenting with new smoldot
x3c41a bb7fd36
decreased log level to 4(debug), read chainspec from Bob, connect to …
x3c41a 79e5042
added comments
x3c41a d47037c
set protocol ID to null in chainSpec
x3c41a 6395f7a
chenged result param
x3c41a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,131 @@ | ||
| import * as smoldot from 'smoldot'; | ||
| import fs from 'fs'; | ||
| import { ApiPromise } from "@polkadot/api"; | ||
| import { Keyring } from "@polkadot/keyring"; | ||
| import { WsProvider } from "@polkadot/api"; | ||
|
|
||
|
|
||
| async function main() { | ||
| const ws = new WsProvider('ws://localhost:12346'); | ||
| const bobApi = await ApiPromise.create({ provider: ws }); | ||
| await bobApi.isReady; | ||
| const chainSpec = (await bobApi.rpc.syncstate.genSyncSpec(true)).toString(); | ||
|
|
||
| // Bob's address | ||
| const provider = new WsProvider('ws://localhost:10000'); | ||
| const api = await ApiPromise.create({ provider }); | ||
| await api.isReady; | ||
|
|
||
|
|
||
| // Check if chainSpec has bootnodes | ||
| const chainSpecObj = JSON.parse(chainSpec); | ||
| console.log("🔗 Bootnodes in chainSpec:", chainSpecObj.bootNodes || []); | ||
| if (!chainSpecObj.bootNodes || chainSpecObj.bootNodes.length === 0) { | ||
| console.warn("⚠️ No bootnodes found! Smoldot won't be able to sync."); | ||
| } | ||
|
|
||
| const keyring = new Keyring({ type: 'sr25519' }); | ||
| const sudo_pair = keyring.addFromUri('//Alice'); | ||
| const who_pair = keyring.addFromUri('//Alice'); | ||
|
|
||
| // data | ||
| const who = who_pair.address; | ||
| const transactions = 32; | ||
| const bytes = 64 * 1024 * 1024; // 64 MB | ||
|
|
||
| const authorizeTx = api.tx.transactionStorage.authorizeAccount( | ||
| who, | ||
| transactions, | ||
| bytes | ||
| ); | ||
|
|
||
| // Wrap in sudo since authorizeAccount requires root privileges | ||
| const sudoTx = api.tx.sudo.sudo(authorizeTx); | ||
| const signedTx = await sudoTx.signAsync(sudo_pair); | ||
| console.log("✅ Signed transaction:", signedTx.toHex()); | ||
|
|
||
| // Start smoldot with logging enabled | ||
| const client = smoldot.start({ | ||
| maxLogLevel: 4, // 0=off, 1=error, 2=warn, 3=info, 4=debug, 5=trace | ||
| logCallback: (level, target, message) => { | ||
| const levelNames = ['ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE']; | ||
| const levelName = levelNames[level - 1] || 'UNKNOWN'; | ||
| console.log(`[smoldot:${levelName}] ${target}: ${message}`); | ||
| } | ||
| }); | ||
| await client | ||
| .addChain({ chainSpec }) | ||
| .then(async (chain) => { | ||
| // Give smoldot a moment to sync | ||
| console.log("⏳ Waiting for smoldot to sync..."); | ||
| await new Promise(resolve => setTimeout(resolve, 2000)); | ||
|
|
||
| // First, test with a simple storage query | ||
| console.log("🔍 Testing smoldot with a storage query..."); | ||
| chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"chain_getBlockHash","params":[0]}'); | ||
| const queryResponse = await chain.nextJsonRpcResponse(); | ||
| const queryParsed = JSON.parse(queryResponse); | ||
| console.log("✅ Genesis block hash:", queryParsed.result); | ||
|
|
||
| // Check current head with timeout | ||
| console.log("🔍 Checking smoldot's current head..."); | ||
| chain.sendJsonRpc('{"jsonrpc":"2.0","id":3,"method":"chain_getHead","params":[]}'); | ||
|
|
||
| const headResponse = await Promise.race([ | ||
| chain.nextJsonRpcResponse(), | ||
| new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout waiting for head")), 12000)) | ||
| ]).catch(err => { | ||
| throw err; | ||
| }); | ||
|
|
||
| const headParsed = JSON.parse(headResponse); | ||
| console.log("Current head hash:", headParsed.result); | ||
|
|
||
| // Get the block number for current head | ||
| chain.sendJsonRpc(`{"jsonrpc":"2.0","id":4,"method":"chain_getHeader","params":["${headParsed.result}"]}`); | ||
| const headerResponse = await chain.nextJsonRpcResponse(); | ||
| const headerParsed = JSON.parse(headerResponse); | ||
| console.log("Current head block number:", parseInt(headerParsed.result.number, 16)); | ||
|
|
||
| // Now try a simple balance transfer instead of sudo | ||
| console.log("Creating a simple balance transfer..."); | ||
| const bob = keyring.addFromUri('//Bob'); | ||
| const transferTx = api.tx.balances.transferKeepAlive(bob.address, 1000000000000); | ||
| const signedTransfer = await transferTx.signAsync(sudo_pair); | ||
|
|
||
| console.log("Submitting transfer transaction..."); | ||
| chain.sendJsonRpc(`{"jsonrpc":"2.0","id":2,"method":"author_submitAndWatchExtrinsic","params":["${signedTransfer.toHex()}"]}`); | ||
| return chain; | ||
| }) | ||
| .then(async (chain) => { | ||
| // Get subscription ID | ||
| const response = await chain.nextJsonRpcResponse(); | ||
| console.log("Subscription ID:", JSON.parse(response).result); | ||
| // Listen for transaction status updates | ||
| while (true) { | ||
| const statusUpdate = await chain.nextJsonRpcResponse(); | ||
| const parsed = JSON.parse(statusUpdate); | ||
| console.log("Transaction status:", parsed); | ||
|
|
||
| // Check if transaction is finalized | ||
| if (parsed.params?.result?.Finalized) { | ||
| console.log("✅ Transaction finalized in block:", parsed.params.result.Finalized); | ||
| break; | ||
| } | ||
|
|
||
| if (parsed.params?.result === 'dropped' || parsed.params?.result === 'invalid') { | ||
| console.error("❌ Transaction failed:", parsed.params.result); | ||
| break; | ||
| } | ||
| if (parsed.params?.result?.Invalid || parsed.params?.result?.Dropped) { | ||
| console.error("❌ Transaction failed:", parsed.params.result); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return chain; | ||
| }) | ||
| .then(() => client.terminate()) | ||
| } | ||
|
|
||
| await main(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.