From 004b6e004504b02ebd3734af4fd48e12551614de Mon Sep 17 00:00:00 2001 From: parmarrushabh Date: Mon, 22 Oct 2018 18:14:27 +0530 Subject: [PATCH] fix unit test, safe cancel context --- cmd/puppeth/wizard_genesis.go | 88 ++++++++++++++++++++++- contracts/blocksigner/blocksigner_test.go | 8 ++- contracts/randomize/randomize_test.go | 3 +- contracts/validator/validator_test.go | 3 +- 4 files changed, 96 insertions(+), 6 deletions(-) diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index 324224094f..e4c1b6b266 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. Xdpos - proof-of-stake-voting") choice := w.read() switch { @@ -107,6 +115,84 @@ 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") + } + contractBackend.Commit() + + d := time.Now().Add(1000 * time.Millisecond) + ctx, cancel := context.WithDeadline(context.Background(), d) + defer cancel() + 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) } diff --git a/contracts/blocksigner/blocksigner_test.go b/contracts/blocksigner/blocksigner_test.go index 6ab04cea4c..277a3244fa 100644 --- a/contracts/blocksigner/blocksigner_test.go +++ b/contracts/blocksigner/blocksigner_test.go @@ -8,6 +8,7 @@ package blocksigner "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/crypto" ) @@ -21,14 +22,15 @@ package blocksigner contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}) transactOpts := bind.NewKeyedTransactor(key) - _, blockSigner, err := DeployBlockSigner(transactOpts, contractBackend) - if err != nil { + blockSignerAddress, blockSigner, err := DeployBlockSigner(transactOpts, contractBackend) + if err != nil { t.Fatalf("can't deploy root registry: %v", err) } contractBackend.Commit() d := time.Now().Add(1000 * time.Millisecond) - ctx, _ := context.WithDeadline(context.Background(), d) + ctx, cancel := context.WithDeadline(context.Background(), d) + defer cancel() code, _ := contractBackend.CodeAt(ctx, blockSignerAddress, nil) t.Log("contract code", common.ToHex(code)) f := func(key, val common.Hash) bool { diff --git a/contracts/randomize/randomize_test.go b/contracts/randomize/randomize_test.go index 1d6f62f67e..b30140e278 100644 --- a/contracts/randomize/randomize_test.go +++ b/contracts/randomize/randomize_test.go @@ -31,7 +31,8 @@ func TestRandomize(t *testing.T) { contractBackend.Commit() d := time.Now().Add(1000 * time.Millisecond) - ctx, _ := context.WithDeadline(context.Background(), d) + ctx, cancel := context.WithDeadline(context.Background(), d) + defer cancel() code, _ := contractBackend.CodeAt(ctx, randomizeAddress, nil) t.Log("contract code", common.ToHex(code)) f := func(key, val common.Hash) bool { diff --git a/contracts/validator/validator_test.go b/contracts/validator/validator_test.go index 5548336146..6c6103b4dd 100644 --- a/contracts/validator/validator_test.go +++ b/contracts/validator/validator_test.go @@ -41,7 +41,8 @@ func TestValidator(t *testing.T) { contractBackend.Commit() d := time.Now().Add(1000 * time.Millisecond) - ctx, _ := context.WithDeadline(context.Background(), d) + ctx, cancel := context.WithDeadline(context.Background(), d) + defer cancel() code, _ := contractBackend.CodeAt(ctx, validatorAddress, nil) t.Log("contract code", common.ToHex(code)) f := func(key, val common.Hash) bool {