This is an automated email from the ASF dual-hosted git repository. toulmean pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/main by this push: new 7afd849b Tweak the serialization of the genesis file, allow to export the csv of the validators new 328b2828 Merge pull request #396 from atoulme/fix_genesis 7afd849b is described below commit 7afd849b675e0af911f39ed0d1004b9f97e163c8 Author: Antoine Toulme <anto...@lunar-ocean.com> AuthorDate: Sat Apr 2 23:48:40 2022 -0700 Tweak the serialization of the genesis file, allow to export the csv of the validators --- .../src/main/kotlin/org/apache/tuweni/genesis/Genesis.kt | 3 ++- genesis/src/main/kotlin/org/apache/tuweni/genesis/Main.kt | 8 +++++--- genesis/src/main/kotlin/org/apache/tuweni/genesis/Quorum.kt | 13 ++++++++++--- .../test/kotlin/org/apache/tuweni/genesis/GenesisTest.kt | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/genesis/src/main/kotlin/org/apache/tuweni/genesis/Genesis.kt b/genesis/src/main/kotlin/org/apache/tuweni/genesis/Genesis.kt index e61d8206..aece56d4 100644 --- a/genesis/src/main/kotlin/org/apache/tuweni/genesis/Genesis.kt +++ b/genesis/src/main/kotlin/org/apache/tuweni/genesis/Genesis.kt @@ -58,6 +58,7 @@ open class GenesisConfig( "gasUsed", "mixHash", "coinbase", + "parentHash", "alloc" ) class Genesis( @@ -69,7 +70,7 @@ class Genesis( private val timestamp: Long, val extraData: Bytes, private val gasLimit: Long, - val alloc: Map<Address, UInt256>, + val alloc: Map<Address, Map<String, UInt256>>, val config: GenesisConfig, ) { diff --git a/genesis/src/main/kotlin/org/apache/tuweni/genesis/Main.kt b/genesis/src/main/kotlin/org/apache/tuweni/genesis/Main.kt index 2dcd823b..c095d9c4 100644 --- a/genesis/src/main/kotlin/org/apache/tuweni/genesis/Main.kt +++ b/genesis/src/main/kotlin/org/apache/tuweni/genesis/Main.kt @@ -17,6 +17,7 @@ package org.apache.tuweni.genesis import com.fasterxml.jackson.databind.json.JsonMapper +import org.apache.tuweni.bytes.Bytes import org.apache.tuweni.bytes.Bytes32 import org.apache.tuweni.eth.EthJsonModule import org.apache.tuweni.units.bigints.UInt256 @@ -29,9 +30,9 @@ fun main(args: Array<String>) { Security.addProvider(BouncyCastleProvider()) val config = QuorumConfig.generate( mixHash = Bytes32.random(), config = QuorumGenesisConfig(chainId = args[0].toInt()), - numberValidators = 10, - numberAllocations = 10, - amount = UInt256.valueOf(3000) + numberValidators = 4, + numberAllocations = 100, + amount = UInt256.fromBytes(Bytes32.rightPad(Bytes.fromHexString("0x10"))) ) val mapper = JsonMapper() @@ -39,4 +40,5 @@ fun main(args: Array<String>) { val contents = mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(config.genesis) Files.write(Paths.get("genesis.json"), contents) Files.write(Paths.get("accounts.csv"), config.allocsToCsv().toByteArray()) + Files.write(Paths.get("validators.csv"), config.validatorsToCsv().toByteArray()) } diff --git a/genesis/src/main/kotlin/org/apache/tuweni/genesis/Quorum.kt b/genesis/src/main/kotlin/org/apache/tuweni/genesis/Quorum.kt index 70f3f887..6063574b 100644 --- a/genesis/src/main/kotlin/org/apache/tuweni/genesis/Quorum.kt +++ b/genesis/src/main/kotlin/org/apache/tuweni/genesis/Quorum.kt @@ -28,6 +28,7 @@ import org.apache.tuweni.units.bigints.UInt256 class QuorumConfig(val genesis: Genesis, val validators: List<SECP256K1.KeyPair>, val allocations: List<Allocation>) { companion object { + val validatorHeader = "Public key,Secret key\n" val header = "User,Public key,Address,Secret key\n" fun generate( @@ -46,7 +47,7 @@ class QuorumConfig(val genesis: Genesis, val validators: List<SECP256K1.KeyPair> ): QuorumConfig { val allocations = AllocationGenerator().createAllocations(numberAllocations, amount) - val validators = (0..numberValidators).map { + val validators = (0 until numberValidators).map { SECP256K1.KeyPair.random() } @@ -78,9 +79,9 @@ class QuorumConfig(val genesis: Genesis, val validators: List<SECP256K1.KeyPair> allocations: List<Allocation>, validators: List<SECP256K1.KeyPair>, ): QuorumConfig { - val allocs = mutableMapOf<Address, UInt256>() + val allocs = mutableMapOf<Address, Map<String, UInt256>>() for (alloc in allocations) { - allocs[alloc.address] = alloc.amount + allocs[alloc.address] = mapOf(Pair("balance", alloc.amount)) } val genesis = Genesis( nonce = nonce, @@ -99,6 +100,12 @@ class QuorumConfig(val genesis: Genesis, val validators: List<SECP256K1.KeyPair> } } + fun validatorsToCsv(): String { + return validatorHeader + validators.map { + it.publicKey().toHexString() + "," + it.secretKey().bytes().toHexString() + }.joinToString("\n") + } + fun allocsToCsv(): String { val lines = allocations.map { "Unclaimed,${ diff --git a/genesis/src/test/kotlin/org/apache/tuweni/genesis/GenesisTest.kt b/genesis/src/test/kotlin/org/apache/tuweni/genesis/GenesisTest.kt index bb5a36f7..5a072e85 100644 --- a/genesis/src/test/kotlin/org/apache/tuweni/genesis/GenesisTest.kt +++ b/genesis/src/test/kotlin/org/apache/tuweni/genesis/GenesisTest.kt @@ -43,7 +43,7 @@ class GenesisTest { extraData = Bytes.EMPTY, gasLimit = 0L, parentHash = Bytes32.leftPad(Bytes.fromHexString("0x00ff")), - alloc = mapOf(Pair(Address.fromHexString("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"), UInt256.ONE)), + alloc = mapOf(Pair(Address.fromHexString("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"), mapOf(Pair("balance", UInt256.ONE)))), config = GenesisConfig(chainId = 1337) ) val mapper = JsonMapper() --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@tuweni.apache.org For additional commands, e-mail: commits-h...@tuweni.apache.org