fix unit test, safe cancel context

This commit is contained in:
parmarrushabh 2018-10-22 18:14:27 +05:30
parent dceb579b68
commit 004b6e0045
4 changed files with 96 additions and 6 deletions

View file

@ -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 = &params.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)
}

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {