-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
subcommand generate to generate chainspec file (#212)
* subcommand generate to generate chainspec file * fix codec and chainspec * fix
- Loading branch information
Showing
26 changed files
with
540 additions
and
206 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
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
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
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,44 @@ | ||
import ArgumentParser | ||
import Codec | ||
import Foundation | ||
import Node | ||
import Utils | ||
|
||
enum OutputFormat: String, ExpressibleByArgument { | ||
case json | ||
case binary | ||
} | ||
|
||
struct Generate: AsyncParsableCommand { | ||
static let configuration = CommandConfiguration( | ||
abstract: "Generate new chainspec file" | ||
) | ||
|
||
@Argument(help: "output file") | ||
var output: String | ||
|
||
@Option(name: .long, help: "A preset config or path to chain config file.") | ||
var chain: Genesis = .preset(.minimal) | ||
|
||
@Option(name: .long, help: "The output format. json or binary.") | ||
var format: OutputFormat = .json | ||
|
||
@Option(name: .long, help: "The chain name.") | ||
var name: String = "Devnet" | ||
|
||
@Option(name: .long, help: "The chain id.") | ||
var id: String = "dev" | ||
|
||
func run() async throws { | ||
let chainspec = try await chain.load() | ||
let data = switch format { | ||
case .json: | ||
try chainspec.encode() | ||
case .binary: | ||
try chainspec.encodeBinary() | ||
} | ||
try data.write(to: URL(fileURLWithPath: output)) | ||
|
||
print("Chainspec generated at \(output)") | ||
} | ||
} |
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
42 changes: 0 additions & 42 deletions
42
Boka/Tests/BokaTests/chainfiles/devnet_allconfig_spec.json
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
Boka/Tests/BokaTests/chainfiles/mainnet_someconfig_spec.json
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
import Foundation | ||
|
||
struct KeyValuePair<Key: Codable & Hashable & Comparable, Value: Codable>: Codable { | ||
var key: Key | ||
var value: Value | ||
} | ||
|
||
public struct SortedKeyValues<Key: Codable & Hashable & Comparable, Value: Codable>: Codable, CodableAlias { | ||
public typealias Alias = [Key: Value] | ||
|
||
public var alias: Alias | ||
|
||
public init(alias: Alias) { | ||
self.alias = alias | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
let array = try container.decode([KeyValuePair<Key, Value>].self) | ||
|
||
// ensure array is sorted and unique | ||
var previous: KeyValuePair<Key, Value>? | ||
for item in array { | ||
guard previous == nil || item.key > previous!.key else { | ||
throw DecodingError.dataCorrupted( | ||
DecodingError.Context( | ||
codingPath: container.codingPath, | ||
debugDescription: "Array is not sorted" | ||
) | ||
) | ||
} | ||
previous = item | ||
} | ||
|
||
alias = .init(uniqueKeysWithValues: array.map { ($0.key, $0.value) }) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
var array = alias.map { KeyValuePair(key: $0.key, value: $0.value) } | ||
array.sort { $0.key < $1.key } | ||
try container.encode(array) | ||
} | ||
} | ||
|
||
extension SortedKeyValues: Sendable where Key: Sendable, Value: Sendable, Alias: Sendable {} | ||
|
||
extension SortedKeyValues: Equatable where Key: Equatable, Value: Equatable, Alias: Equatable {} |
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
Oops, something went wrong.