mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-11 15:34:26 +00:00
updated VALIDATOR_TEST.GO
This commit is contained in:
parent
6e4c9d9387
commit
f7fac4ec91
1 changed files with 6 additions and 143 deletions
|
|
@ -1,72 +1,31 @@
|
||||||
// Copyright (c) 2018 XDCchain
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package validator
|
package validator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"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/common"
|
||||||
"github.com/ethereum/go-ethereum/contracts"
|
|
||||||
"github.com/ethereum/go-ethereum/contracts/validator/contract"
|
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"math/rand"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
addr = crypto.PubkeyToAddress(key.PublicKey)
|
addr = crypto.PubkeyToAddress(key.PublicKey)
|
||||||
acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
|
||||||
acc2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
|
|
||||||
acc3Key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
||||||
acc4Key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee04aefe388d1e14474d32c45c72ce7b7a")
|
|
||||||
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey)
|
|
||||||
acc2Addr = crypto.PubkeyToAddress(acc2Key.PublicKey)
|
|
||||||
acc3Addr = crypto.PubkeyToAddress(acc3Key.PublicKey)
|
|
||||||
acc4Addr = crypto.PubkeyToAddress(acc4Key.PublicKey)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValidator(t *testing.T) {
|
func TestValidator(t *testing.T) {
|
||||||
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)
|
||||||
|
|
||||||
validatorCap := new(big.Int)
|
_, validator, err := DeployValidator(transactOpts, contractBackend, []common.Address{addr}, []*big.Int{big.NewInt(0)})
|
||||||
validatorCap.SetString("50000000000000000000000", 10)
|
|
||||||
validatorAddress, validator, err := DeployValidator(transactOpts, contractBackend, []common.Address{addr}, []*big.Int{validatorCap}, addr)
|
|
||||||
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)
|
|
||||||
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 {
|
|
||||||
t.Log(key.Hex(), val.Hex())
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
contractBackend.ForEachStorageAt(ctx, validatorAddress, nil, f)
|
|
||||||
|
|
||||||
candidates, err := validator.GetCandidates()
|
candidates, err := validator.GetCandidates()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't get candidates: %v", err)
|
t.Fatalf("can't get candidates: %v", err)
|
||||||
|
|
@ -74,104 +33,8 @@ func TestValidator(t *testing.T) {
|
||||||
for _, it := range candidates {
|
for _, it := range candidates {
|
||||||
cap, _ := validator.GetCandidateCap(it)
|
cap, _ := validator.GetCandidateCap(it)
|
||||||
t.Log("candidate", it.String(), "cap", cap)
|
t.Log("candidate", it.String(), "cap", cap)
|
||||||
owner, _ := validator.GetCandidateOwner(it)
|
backer, _ := validator.GetCandidateBacker(it)
|
||||||
t.Log("candidate", it.String(), "validator owner", owner.String())
|
t.Log("candidate", it.String(), "backer", backer.String())
|
||||||
}
|
}
|
||||||
contractBackend.Commit()
|
contractBackend.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRewardBalance(t *testing.T) {
|
|
||||||
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{
|
|
||||||
acc1Addr: {Balance: new(big.Int).SetUint64(10000000)},
|
|
||||||
acc2Addr: {Balance: new(big.Int).SetUint64(10000000)},
|
|
||||||
acc4Addr: {Balance: new(big.Int).SetUint64(10000000)},
|
|
||||||
})
|
|
||||||
acc1Opts := bind.NewKeyedTransactor(acc1Key)
|
|
||||||
acc2Opts := bind.NewKeyedTransactor(acc2Key)
|
|
||||||
accounts := []*bind.TransactOpts{acc1Opts, acc2Opts}
|
|
||||||
transactOpts := bind.NewKeyedTransactor(acc1Key)
|
|
||||||
|
|
||||||
// validatorAddr, _, baseValidator, err := contract.DeployXDCValidator(transactOpts, contractBackend, big.NewInt(50000), big.NewInt(99), big.NewInt(100), big.NewInt(100))
|
|
||||||
validatorCap := new(big.Int)
|
|
||||||
validatorCap.SetString("50000000000000000000000", 10)
|
|
||||||
validatorAddr, _, baseValidator, err := contract.DeployXDCValidator(
|
|
||||||
transactOpts,
|
|
||||||
contractBackend,
|
|
||||||
[]common.Address{addr},
|
|
||||||
[]*big.Int{validatorCap},
|
|
||||||
addr,
|
|
||||||
big.NewInt(50000),
|
|
||||||
big.NewInt(1),
|
|
||||||
big.NewInt(99),
|
|
||||||
big.NewInt(100),
|
|
||||||
big.NewInt(100),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("can't deploy root registry: %v", err)
|
|
||||||
}
|
|
||||||
contractBackend.Commit()
|
|
||||||
|
|
||||||
// Propose master node acc3Addr.
|
|
||||||
opts := bind.NewKeyedTransactor(acc4Key)
|
|
||||||
opts.Value = new(big.Int).SetUint64(50000)
|
|
||||||
acc4Validator, _ := NewValidator(opts, validatorAddr, contractBackend)
|
|
||||||
acc4Validator.Propose(acc3Addr)
|
|
||||||
contractBackend.Commit()
|
|
||||||
|
|
||||||
totalVote := 0
|
|
||||||
type logCap struct {
|
|
||||||
Addr string
|
|
||||||
Balance int
|
|
||||||
}
|
|
||||||
logCaps := make(map[int]*logCap)
|
|
||||||
for i := 0; i <= 10; i++ {
|
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
|
||||||
randIndex := rand.Intn(len(accounts))
|
|
||||||
randCap := rand.Intn(10) * 1000
|
|
||||||
if randCap <= 0 {
|
|
||||||
randCap = 1000
|
|
||||||
}
|
|
||||||
totalVote += randCap
|
|
||||||
accounts[randIndex].Value = new(big.Int).SetInt64(int64(randCap))
|
|
||||||
validator, err := NewValidator(accounts[randIndex], validatorAddr, contractBackend)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("can't get current validator: %v", err)
|
|
||||||
}
|
|
||||||
validator.Vote(acc3Addr)
|
|
||||||
contractBackend.Commit()
|
|
||||||
logCaps[i] = &logCap{accounts[randIndex].From.String(), randCap}
|
|
||||||
}
|
|
||||||
|
|
||||||
foundationAddr := common.HexToAddress(common.FoudationAddr)
|
|
||||||
totalReward := new(big.Int).SetInt64(15 * 1000)
|
|
||||||
rewards, err := contracts.GetRewardBalancesRate(foundationAddr, acc3Addr, totalReward, baseValidator)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Fail to get reward balances rate.", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
afterReward := new(big.Int)
|
|
||||||
for _, value := range rewards {
|
|
||||||
afterReward = new(big.Int).Add(afterReward, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
if totalReward.Int64()+5 < afterReward.Int64() || totalReward.Int64()-5 > afterReward.Int64() {
|
|
||||||
callOpts := new(bind.CallOpts)
|
|
||||||
voters, err := baseValidator.GetVoters(callOpts, acc3Addr)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("Can not get voters in validator contract.", err)
|
|
||||||
}
|
|
||||||
for addr, capacity := range logCaps {
|
|
||||||
t.Errorf("from %v - %v", addr, capacity)
|
|
||||||
}
|
|
||||||
for _, voter := range voters {
|
|
||||||
voteCap, _ := baseValidator.GetVoterCap(callOpts, acc3Addr, voter)
|
|
||||||
t.Errorf("vote %v - %v", voter.String(), voteCap)
|
|
||||||
}
|
|
||||||
for addr, value := range rewards {
|
|
||||||
t.Errorf("reaward %v - %v", addr.String(), value)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Errorf("reward total %v - %v", totalReward, afterReward)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue