From dd353d6bb581bfbb72e482675f8f239b2033b361 Mon Sep 17 00:00:00 2001 From: Nguyen Sy Thanh Son Date: Thu, 12 Jul 2018 10:06:00 +0000 Subject: [PATCH] add option genesis generator for POSV --- cmd/puppeth/wizard_genesis.go | 87 ++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index 89423e8b55..eb89a4ec71 100644 --- a/cmd/puppeth/wizard_genesis.go +++ b/cmd/puppeth/wizard_genesis.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "math/big" "math/rand" "time" @@ -29,6 +28,14 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" + + "context" + "math/big" + + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" + validatorContract "github.com/ethereum/go-ethereum/contracts/validator" + "github.com/ethereum/go-ethereum/crypto" ) // makeGenesis creates a new genesis struct based on some user input. @@ -52,6 +59,7 @@ func (w *wizard) makeGenesis() { fmt.Println("Which consensus engine to use? (default = clique)") fmt.Println(" 1. Ethash - proof-of-work") fmt.Println(" 2. Clique - proof-of-authority") + fmt.Println(" 3. Tomo - proof-of-stake-voting") choice := w.read() switch { @@ -107,6 +115,83 @@ func (w *wizard) makeGenesis() { fmt.Println("How many blocks per checkpoint? (default = 990)") genesis.Config.Clique.RewardCheckpoint = uint64(w.readDefaultInt(990)) + case choice == "3": + genesis.Difficulty = big.NewInt(1) + genesis.Config.Clique = ¶ms.CliqueConfig{ + Period: 15, + Epoch: 30000, + Reward: 0, + } + fmt.Println() + fmt.Println("How many seconds should blocks take? (default = 2)") + genesis.Config.Clique.Period = uint64(w.readDefaultInt(2)) + + fmt.Println() + fmt.Println("How many Ethers should be rewarded to masternode? (default = 10)") + genesis.Config.Clique.Reward = uint64(w.readDefaultInt(10)) + + fmt.Println() + fmt.Println("Who own the first masternodes? (mandatory)") + w.readAddress() + + // We also need the initial list of signers + fmt.Println() + fmt.Println("Which accounts are allowed to seal (signers)? (mandatory at least one)") + + var signers []common.Address + for { + if address := w.readAddress(); address != nil { + signers = append(signers, *address) + continue + } + if len(signers) > 0 { + break + } + } + // Sort the signers and embed into the extra-data section + for i := 0; i < len(signers); i++ { + for j := i + 1; j < len(signers); j++ { + if bytes.Compare(signers[i][:], signers[j][:]) > 0 { + signers[i], signers[j] = signers[j], signers[i] + } + } + } + genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65) + for i, signer := range signers { + copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:]) + } + + fmt.Println() + fmt.Println("How many blocks per checkpoint? (default = 990)") + genesis.Config.Clique.RewardCheckpoint = uint64(w.readDefaultInt(990)) + + // Smart Contract Code + key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + addr := crypto.PubkeyToAddress(key.PublicKey) + contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}) + transactOpts := bind.NewKeyedTransactor(key) + + validatorAddress, _, err := validatorContract.DeployValidator(transactOpts, contractBackend) + if err != nil { + fmt.Println("can't deploy root registry: %v", err) + } + contractBackend.Commit() + + d := time.Now().Add(1000 * time.Millisecond) + ctx, _ := context.WithDeadline(context.Background(), d) + code, _ := contractBackend.CodeAt(ctx, validatorAddress, nil) + storage := make(map[common.Hash]common.Hash) + f := func(key, val common.Hash) bool { + storage[key] = val + return true + } + contractBackend.ForEachStorageAt(ctx, validatorAddress, nil, f) + genesis.Alloc[common.StringToAddress("0x0000000000000000000000000000000000000089")] = core.GenesisAccount{ + Balance: big.NewInt(0), + Code: code, + Storage: storage, + } + default: log.Crit("Invalid consensus engine choice", "choice", choice) }