mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
fix unit test, safe cancel context
This commit is contained in:
parent
dceb579b68
commit
004b6e0045
4 changed files with 96 additions and 6 deletions
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/big"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -29,6 +28,14 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"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.
|
// 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("Which consensus engine to use? (default = clique)")
|
||||||
fmt.Println(" 1. Ethash - proof-of-work")
|
fmt.Println(" 1. Ethash - proof-of-work")
|
||||||
fmt.Println(" 2. Clique - proof-of-authority")
|
fmt.Println(" 2. Clique - proof-of-authority")
|
||||||
|
fmt.Println(" 3. Xdpos - proof-of-stake-voting")
|
||||||
|
|
||||||
choice := w.read()
|
choice := w.read()
|
||||||
switch {
|
switch {
|
||||||
|
|
@ -107,6 +115,84 @@ func (w *wizard) makeGenesis() {
|
||||||
fmt.Println("How many blocks per checkpoint? (default = 990)")
|
fmt.Println("How many blocks per checkpoint? (default = 990)")
|
||||||
genesis.Config.Clique.RewardCheckpoint = uint64(w.readDefaultInt(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:
|
default:
|
||||||
log.Crit("Invalid consensus engine choice", "choice", choice)
|
log.Crit("Invalid consensus engine choice", "choice", choice)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ package blocksigner
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
|
"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/core"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
@ -21,14 +22,15 @@ package blocksigner
|
||||||
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
|
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
|
||||||
transactOpts := bind.NewKeyedTransactor(key)
|
transactOpts := bind.NewKeyedTransactor(key)
|
||||||
|
|
||||||
_, blockSigner, err := DeployBlockSigner(transactOpts, contractBackend)
|
blockSignerAddress, blockSigner, err := DeployBlockSigner(transactOpts, contractBackend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't deploy root registry: %v", err)
|
t.Fatalf("can't deploy root registry: %v", err)
|
||||||
}
|
}
|
||||||
contractBackend.Commit()
|
contractBackend.Commit()
|
||||||
|
|
||||||
d := time.Now().Add(1000 * time.Millisecond)
|
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)
|
code, _ := contractBackend.CodeAt(ctx, blockSignerAddress, nil)
|
||||||
t.Log("contract code", common.ToHex(code))
|
t.Log("contract code", common.ToHex(code))
|
||||||
f := func(key, val common.Hash) bool {
|
f := func(key, val common.Hash) bool {
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,8 @@ func TestRandomize(t *testing.T) {
|
||||||
contractBackend.Commit()
|
contractBackend.Commit()
|
||||||
|
|
||||||
d := time.Now().Add(1000 * time.Millisecond)
|
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)
|
code, _ := contractBackend.CodeAt(ctx, randomizeAddress, nil)
|
||||||
t.Log("contract code", common.ToHex(code))
|
t.Log("contract code", common.ToHex(code))
|
||||||
f := func(key, val common.Hash) bool {
|
f := func(key, val common.Hash) bool {
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,8 @@ func TestValidator(t *testing.T) {
|
||||||
contractBackend.Commit()
|
contractBackend.Commit()
|
||||||
|
|
||||||
d := time.Now().Add(1000 * time.Millisecond)
|
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)
|
code, _ := contractBackend.CodeAt(ctx, validatorAddress, nil)
|
||||||
t.Log("contract code", common.ToHex(code))
|
t.Log("contract code", common.ToHex(code))
|
||||||
f := func(key, val common.Hash) bool {
|
f := func(key, val common.Hash) bool {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue