Skip to content
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

admin_importChain rpc module #8042

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Nethermind/Nethermind.Cli/Modules/AdminCliModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ public AdminCliModule(ICliEngine engine, INodeManager nodeManager) : base(engine

[CliFunction("admin", "removePeer", Description = "Removes given node from the static nodes")]
public string? RemovePeer(string enode, bool removeFromStaticNodes = false) => NodeManager.Post<string>("admin_removePeer", enode, removeFromStaticNodes).Result;

[CliFunction("admin", "exportChain", Description = "Exports the current blockchain into a local file.")]
public bool ExportChain(string filePath, ulong? firstBlock = null, ulong? lastBlock = null)
=> NodeManager.Post<bool>("admin_exportChain", filePath, firstBlock, lastBlock).Result;
}
}
23 changes: 23 additions & 0 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,27 @@ public ResultWrapper<string> admin_verifyTrie(BlockParameter block)

return ResultWrapper<string>.Success("Starting.");
}

public async Task<ResultWrapper<bool>> admin_exportChain(string filePath, ulong? firstBlock = null, ulong? lastBlock = null)
{
try
{
await Task.Run(() =>
{
// TODO: export logic goes here.
// 1. Open filePath for writing.
// 2. Determine the range of blocks (firstBlock ?? 0) up to (lastBlock ?? chainHead).
// 3. Iterate over each block, serialize it, and write to file.
// 4. Close the file, return success.
});

return ResultWrapper<bool>.Success(true);
}
catch (Exception ex)
{
// Return a failure message or a generic error
return ResultWrapper<bool>.Fail($"Export failed: {ex.Message}");
}
}

}
13 changes: 13 additions & 0 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Admin/IAdminRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ ResultWrapper<PeerInfo[]> admin_peers(
ExampleResponse = "\"Starting\"",
IsImplemented = true)]
ResultWrapper<string> admin_verifyTrie(BlockParameter block);

[JsonRpcMethod(
Description = "Exports the current blockchain into a local file. Optionally specify a block range (firstBlock, lastBlock).",
IsImplemented = false
)]
Task<ResultWrapper<bool>> admin_exportChain(
[JsonRpcParameter(Description = "File path to store the exported blocks", ExampleValue = "\"/data/mychain.blocks\"")]
string filePath,
[JsonRpcParameter(Description = "Optional start block", ExampleValue = "0")]
ulong? firstBlock = null,
[JsonRpcParameter(Description = "Optional end block", ExampleValue = "10000")]
ulong? lastBlock = null
);
}