mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
push Randomization implementation
This commit is contained in:
parent
944494f6e4
commit
deb79f4a23
21 changed files with 326 additions and 240 deletions
|
|
@ -77,7 +77,7 @@ var daoProForkGenesis = `{
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
var daoGenesisHash = common.HexToHash("5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0")
|
var daoGenesisHash = common.HexToHash("f2ea0466bf5a07cb7407474d9fbaae6e275127f038ca57a673b833234204f4fd")
|
||||||
var daoGenesisForkBlock = big.NewInt(314)
|
var daoGenesisForkBlock = big.NewInt(314)
|
||||||
|
|
||||||
// TestDAOForkBlockNewChain tests that the DAO hard-fork number and the nodes support/opposition is correctly
|
// TestDAOForkBlockNewChain tests that the DAO hard-fork number and the nodes support/opposition is correctly
|
||||||
|
|
@ -127,7 +127,7 @@ func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBloc
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
genesisHash := common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
|
genesisHash := common.HexToHash("2efa267fef46877ac2659209e2299f97b0afc2a797ee8672db21920a5151e0aa")
|
||||||
if genesis != "" {
|
if genesis != "" {
|
||||||
genesisHash = daoGenesisHash
|
genesisHash = daoGenesisHash
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
common/constants.go
Normal file
14
common/constants.go
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
const (
|
||||||
|
RewardMasterPercent = 40
|
||||||
|
RewardVoterPercent = 50
|
||||||
|
RewardFoundationPercent = 10
|
||||||
|
HexSignMethod = "e341eaa4"
|
||||||
|
HexSetSecret = "34d38600"
|
||||||
|
HexSetOpening = "e11f5ba2"
|
||||||
|
EpocBlockSecret = 800
|
||||||
|
EpocBlockOpening = 850
|
||||||
|
EpocBlockRandomize = 900
|
||||||
|
MaxMasternodes = 150
|
||||||
|
)
|
||||||
|
|
@ -17,7 +17,7 @@ import (
|
||||||
const SafeMathABI = "[]"
|
const SafeMathABI = "[]"
|
||||||
|
|
||||||
// SafeMathBin is the compiled bytecode used for deploying new contracts.
|
// SafeMathBin is the compiled bytecode used for deploying new contracts.
|
||||||
const SafeMathBin = `0x604c602c600b82828239805160001a60731460008114601c57601e565bfe5b5030600052607381538281f30073000000000000000000000000000000000000000030146060604052600080fd00a165627a7a72305820b9407d48ebc7efee5c9f08b3b3a957df2939281f5913225e8c1291f069b900490029`
|
const SafeMathBin = `0x604c602c600b82828239805160001a60731460008114601c57601e565bfe5b5030600052607381538281f30073000000000000000000000000000000000000000030146080604052600080fd00a165627a7a72305820a3f63b465e1cf25f306b1eb1efefc8dac3c38993a7340f69d8b470c3bf599ff30029`
|
||||||
|
|
||||||
// DeploySafeMath deploys a new Ethereum contract, binding an instance of SafeMath to it.
|
// DeploySafeMath deploys a new Ethereum contract, binding an instance of SafeMath to it.
|
||||||
func DeploySafeMath(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *SafeMath, error) {
|
func DeploySafeMath(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *SafeMath, error) {
|
||||||
|
|
@ -178,7 +178,7 @@ func (_SafeMath *SafeMathTransactorRaw) Transact(opts *bind.TransactOpts, method
|
||||||
const XDCRandomizeABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"_validator\",\"type\":\"address\"}],\"name\":\"getSecret\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_secret\",\"type\":\"bytes32[]\"}],\"name\":\"setSecret\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"randomNumber\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_validator\",\"type\":\"address\"}],\"name\":\"getOpening\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_opening\",\"type\":\"bytes32\"}],\"name\":\"setOpening\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"_randomNumber\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]"
|
const XDCRandomizeABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"_validator\",\"type\":\"address\"}],\"name\":\"getSecret\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_secret\",\"type\":\"bytes32[]\"}],\"name\":\"setSecret\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"randomNumber\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_validator\",\"type\":\"address\"}],\"name\":\"getOpening\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_opening\",\"type\":\"bytes32\"}],\"name\":\"setOpening\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"_randomNumber\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]"
|
||||||
|
|
||||||
// XDCRandomizeBin is the compiled bytecode used for deploying new contracts.
|
// XDCRandomizeBin is the compiled bytecode used for deploying new contracts.
|
||||||
const XDCRandomizeBin = `0x6060604052341561000f57600080fd5b604051602080610359833981016040528080516000555050610323806100366000396000f30060606040526004361061006c5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663284180fc811461007157806334d38600146100e3578063ccbac9f514610134578063d442d6cc14610159578063e11f5ba214610178575b600080fd5b341561007c57600080fd5b610090600160a060020a036004351661018e565b60405160208082528190810183818151815260200191508051906020019060200280838360005b838110156100cf5780820151838201526020016100b7565b505050509050019250505060405180910390f35b34156100ee57600080fd5b610132600460248135818101908301358060208181020160405190810160405280939291908181526020018383602002808284375094965061021295505050505050565b005b341561013f57600080fd5b61014761023f565b60405190815260200160405180910390f35b341561016457600080fd5b610147600160a060020a0360043516610245565b341561018357600080fd5b610132600435610260565b61019661027b565b6001600083600160a060020a0316600160a060020a0316815260200190815260200160002080548060200260200160405190810160405280929190818152602001828054801561020657602002820191906000526020600020905b815481526001909101906020018083116101f1575b50505050509050919050565b600160a060020a033316600090815260016020526040902081805161023b92916020019061028d565b5050565b60005481565b600160a060020a031660009081526002602052604090205490565b600160a060020a033316600090815260026020526040902055565b60206040519081016040526000815290565b8280548282559060005260206000209081019282156102ca579160200282015b828111156102ca57825182556020909201916001909101906102ad565b506102d69291506102da565b5090565b6102f491905b808211156102d657600081556001016102e0565b905600a165627a7a7230582043a345787b9942e3361515ce1a0a4dbaab0ed6ce42c6ca0344119134351d9ffe0029`
|
const XDCRandomizeBin = `0x608060405234801561001057600080fd5b5060405160208061035b8339810160405251600055610327806100346000396000f30060806040526004361061006c5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663284180fc811461007157806334d38600146100ef578063ccbac9f514610146578063d442d6cc1461016d578063e11f5ba21461019b575b600080fd5b34801561007d57600080fd5b5061009f73ffffffffffffffffffffffffffffffffffffffff600435166101b3565b60408051602080825283518183015283519192839290830191858101910280838360005b838110156100db5781810151838201526020016100c3565b505050509050019250505060405180910390f35b3480156100fb57600080fd5b50604080516020600480358082013583810280860185019096528085526101449536959394602494938501929182918501908490808284375094975061022d9650505050505050565b005b34801561015257600080fd5b5061015b610251565b60408051918252519081900360200190f35b34801561017957600080fd5b5061015b73ffffffffffffffffffffffffffffffffffffffff60043516610257565b3480156101a757600080fd5b5061014460043561027f565b73ffffffffffffffffffffffffffffffffffffffff811660009081526001602090815260409182902080548351818402810184019094528084526060939283018282801561022157602002820191906000526020600020905b8154815260019091019060200180831161020c575b50505050509050919050565b336000908152600160209081526040909120825161024d92840190610291565b5050565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526002602052604090205490565b33600090815260026020526040902055565b8280548282559060005260206000209081019282156102ce579160200282015b828111156102ce57825182556020909201916001909101906102b1565b506102da9291506102de565b5090565b6102f891905b808211156102da57600081556001016102e4565b905600a165627a7a7230582027c1c755e5d546967a2057a4338eb36cf7ec5972758d7f7a8427abdfc890bd590029`
|
||||||
|
|
||||||
// DeployXDCRandomize deploys a new Ethereum contract, binding an instance of XDCRandomize to it.
|
// DeployXDCRandomize deploys a new Ethereum contract, binding an instance of XDCRandomize to it.
|
||||||
func DeployXDCRandomize(auth *bind.TransactOpts, backend bind.ContractBackend, _randomNumber *big.Int) (common.Address, *types.Transaction, *XDCRandomize, error) {
|
func DeployXDCRandomize(auth *bind.TransactOpts, backend bind.ContractBackend, _randomNumber *big.Int) (common.Address, *types.Transaction, *XDCRandomize, error) {
|
||||||
|
|
|
||||||
|
|
@ -9,19 +9,25 @@ import (
|
||||||
"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/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
epocNumber = int64(12)
|
||||||
addr = crypto.PubkeyToAddress(key.PublicKey)
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
byte0 = make([][32]byte, 2)
|
addr = crypto.PubkeyToAddress(key.PublicKey)
|
||||||
|
byte0 = make([][32]byte, epocNumber)
|
||||||
|
acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
||||||
|
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey)
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRandomize(t *testing.T) {
|
func TestRandomize(t *testing.T) {
|
||||||
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
|
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(100000000000000)}})
|
||||||
transactOpts := bind.NewKeyedTransactor(key)
|
transactOpts := bind.NewKeyedTransactor(key)
|
||||||
|
transactOpts.GasLimit = 1000000
|
||||||
|
|
||||||
randomizeAddress, randomize, err := DeployRandomize(transactOpts, contractBackend, big.NewInt(2))
|
randomizeAddress, randomize, err := DeployRandomize(transactOpts, contractBackend, big.NewInt(2))
|
||||||
t.Log("contract address", randomizeAddress.String())
|
t.Log("contract address", randomizeAddress.String())
|
||||||
|
|
@ -40,11 +46,80 @@ func TestRandomize(t *testing.T) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
contractBackend.ForEachStorageAt(ctx, randomizeAddress, nil, f)
|
contractBackend.ForEachStorageAt(ctx, randomizeAddress, nil, f)
|
||||||
|
|
||||||
s, err := randomize.SetSecret(byte0)
|
s, err := randomize.SetSecret(byte0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't get secret: %v", err)
|
t.Fatalf("can't set secret: %v", err)
|
||||||
}
|
}
|
||||||
t.Log("tx data", s)
|
t.Log("tx data", s)
|
||||||
contractBackend.Commit()
|
contractBackend.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSendTxRandomizeSecretAndOpening(t *testing.T) {
|
||||||
|
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}}
|
||||||
|
backend := backends.NewSimulatedBackend(genesis)
|
||||||
|
backend.Commit()
|
||||||
|
signer := types.HomesteadSigner{}
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
transactOpts := bind.NewKeyedTransactor(acc1Key)
|
||||||
|
transactOpts.GasLimit = 4200000
|
||||||
|
epocNumber := uint64(900)
|
||||||
|
randomizeAddr, randomizeContract, err := DeployRandomize(transactOpts, backend, new(big.Int).SetInt64(0))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't deploy randomize SC: %v", err)
|
||||||
|
}
|
||||||
|
backend.Commit()
|
||||||
|
|
||||||
|
nonce := uint64(1)
|
||||||
|
randomizeKeyValue := contracts.RandStringByte(32)
|
||||||
|
tx, err := contracts.BuildTxSecretRandomize(nonce, randomizeAddr, epocNumber, randomizeKeyValue)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't create tx randomize secret: %v", err)
|
||||||
|
}
|
||||||
|
tx, err = types.SignTx(tx, signer, acc1Key)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't sign tx randomize secret: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = backend.SendTransaction(ctx, tx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't send tx for create randomize secret: %v", err)
|
||||||
|
}
|
||||||
|
backend.Commit()
|
||||||
|
// Increment nonce.
|
||||||
|
nonce++
|
||||||
|
// Set opening.
|
||||||
|
tx, err = contracts.BuildTxOpeningRandomize(nonce, randomizeAddr, randomizeKeyValue)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't create tx randomize opening: %v", err)
|
||||||
|
}
|
||||||
|
tx, err = types.SignTx(tx, signer, acc1Key)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't sign tx randomize opening: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = backend.SendTransaction(ctx, tx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't send tx for create randomize opening: %v", err)
|
||||||
|
}
|
||||||
|
backend.Commit()
|
||||||
|
|
||||||
|
// Get randomize secret from SC.
|
||||||
|
secrets, err := randomizeContract.GetSecret(acc1Addr)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Fail get secrets from randomize", err)
|
||||||
|
}
|
||||||
|
if len(secrets) <= 0 {
|
||||||
|
t.Error("Empty get secrets from SC", err)
|
||||||
|
}
|
||||||
|
// Decrypt randomize from SC.
|
||||||
|
opening, err := randomizeContract.GetOpening(acc1Addr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't get secret from SC: %v", err)
|
||||||
|
}
|
||||||
|
randomize, err := contracts.DecryptRandomizeFromSecretsAndOpening(secrets, opening)
|
||||||
|
t.Log("randomize", randomize)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Can't decrypt secret and opening", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,237 +1,195 @@
|
||||||
package contracts
|
package contracts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"bytes"
|
||||||
"github.com/ethereum/go-ethereum/accounts"
|
"context"
|
||||||
|
"crypto/ecdsa"
|
||||||
"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/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/consensus"
|
"github.com/ethereum/go-ethereum/contracts/blocksigner"
|
||||||
"github.com/ethereum/go-ethereum/contracts/blocksigner/contract"
|
|
||||||
contractValidator "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/core/state"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
HexSignMethod = "e341eaa4"
|
acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
||||||
RewardMasterPercent = 40
|
acc2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
|
||||||
RewardVoterPercent = 50
|
acc3Key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
RewardFoundationPercent = 10
|
acc4Key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee04aefe388d1e14474d32c45c72ce7b7a")
|
||||||
|
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey)
|
||||||
|
acc2Addr = crypto.PubkeyToAddress(acc2Key.PublicKey)
|
||||||
|
acc3Addr = crypto.PubkeyToAddress(acc3Key.PublicKey)
|
||||||
|
acc4Addr = crypto.PubkeyToAddress(acc4Key.PublicKey)
|
||||||
)
|
)
|
||||||
|
|
||||||
type rewardLog struct {
|
func getCommonBackend() *backends.SimulatedBackend {
|
||||||
Sign uint64 `json:"sign"`
|
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}}
|
||||||
Reward *big.Int `json:"reward"`
|
backend := backends.NewSimulatedBackend(genesis)
|
||||||
|
backend.Commit()
|
||||||
|
|
||||||
|
return backend
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send tx sign for block number to smart contract blockSigner.
|
func TestSendTxSign(t *testing.T) {
|
||||||
func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block) error {
|
accounts := []common.Address{acc2Addr, acc3Addr, acc4Addr}
|
||||||
if chainConfig.XDPoS != nil {
|
keys := []*ecdsa.PrivateKey{acc2Key, acc3Key, acc4Key}
|
||||||
// Find active account.
|
backend := getCommonBackend()
|
||||||
account := accounts.Account{}
|
signer := types.HomesteadSigner{}
|
||||||
var wallet accounts.Wallet
|
ctx := context.Background()
|
||||||
if wallets := manager.Wallets(); len(wallets) > 0 {
|
|
||||||
wallet = wallets[0]
|
transactOpts := bind.NewKeyedTransactor(acc1Key)
|
||||||
if accts := wallets[0].Accounts(); len(accts) > 0 {
|
blockSignerAddr, blockSigner, err := blocksigner.DeployBlockSigner(transactOpts, backend, big.NewInt(99))
|
||||||
account = accts[0]
|
if err != nil {
|
||||||
|
t.Fatalf("Can't get block signer: %v", err)
|
||||||
|
}
|
||||||
|
backend.Commit()
|
||||||
|
|
||||||
|
nonces := make(map[*ecdsa.PrivateKey]int)
|
||||||
|
oldBlocks := make(map[common.Hash]common.Address)
|
||||||
|
|
||||||
|
signTx := func(ctx context.Context, backend *backends.SimulatedBackend, signer types.HomesteadSigner, nonces map[*ecdsa.PrivateKey]int, accKey *ecdsa.PrivateKey, blockNumber *big.Int, blockHash common.Hash) *types.Transaction {
|
||||||
|
tx, _ := types.SignTx(CreateTxSign(blockNumber, blockHash, uint64(nonces[accKey]), blockSignerAddr), signer, accKey)
|
||||||
|
backend.SendTransaction(ctx, tx)
|
||||||
|
backend.Commit()
|
||||||
|
nonces[accKey]++
|
||||||
|
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tx sign for signer.
|
||||||
|
signCount := int64(0)
|
||||||
|
blockHashes := make([]common.Hash, 10)
|
||||||
|
for i := int64(0); i < 10; i++ {
|
||||||
|
blockHash := randomHash()
|
||||||
|
blockHashes[i] = blockHash
|
||||||
|
randIndex := rand.Intn(len(keys))
|
||||||
|
accKey := keys[randIndex]
|
||||||
|
signTx(ctx, backend, signer, nonces, accKey, new(big.Int).SetInt64(i), blockHash)
|
||||||
|
oldBlocks[blockHash] = accounts[randIndex]
|
||||||
|
signCount++
|
||||||
|
|
||||||
|
// Tx sign for validators.
|
||||||
|
for _, key := range keys {
|
||||||
|
if key != accKey {
|
||||||
|
signTx(ctx, backend, signer, nonces, key, new(big.Int).SetInt64(i), blockHash)
|
||||||
|
signCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create and send tx to smart contract for sign validate block.
|
for _, blockHash := range blockHashes {
|
||||||
nonce := pool.State().GetNonce(account.Address)
|
signers, err := blockSigner.GetSigners(blockHash)
|
||||||
tx := CreateTxSign(block.Number(), block.Hash(), nonce, common.HexToAddress(common.BlockSigners))
|
|
||||||
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fail to create tx sign", "error", err)
|
t.Fatalf("Can't get signers: %v", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add tx signed to local tx pool.
|
if signers[0].String() != oldBlocks[blockHash].String() {
|
||||||
pool.AddLocal(txSigned)
|
t.Errorf("Tx sign for block signer not match %v - %v", signers[0].String(), oldBlocks[blockHash].String())
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create tx sign.
|
|
||||||
func CreateTxSign(blockNumber *big.Int, blockHash common.Hash, nonce uint64, blockSigner common.Address) *types.Transaction {
|
|
||||||
data := common.Hex2Bytes(HexSignMethod)
|
|
||||||
inputData := append(data, common.LeftPadBytes(blockNumber.Bytes(), 32)...)
|
|
||||||
inputData = append(inputData, common.LeftPadBytes(blockHash.Bytes(), 32)...)
|
|
||||||
tx := types.NewTransaction(nonce, blockSigner, big.NewInt(0), 200000, big.NewInt(0), inputData)
|
|
||||||
|
|
||||||
return tx
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get signers signed for blockNumber from blockSigner contract.
|
|
||||||
func GetSignersFromContract(addrBlockSigner common.Address, client bind.ContractBackend, blockHash common.Hash) ([]common.Address, error) {
|
|
||||||
blockSigner, err := contract.NewBlockSigner(addrBlockSigner, client)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Fail get instance of blockSigner", "error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
opts := new(bind.CallOpts)
|
|
||||||
addrs, err := blockSigner.GetSigners(opts, blockHash)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Fail get block signers", "error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return addrs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate reward for reward checkpoint.
|
|
||||||
func GetRewardForCheckpoint(chain consensus.ChainReader, blockSignerAddr common.Address, number uint64, rCheckpoint uint64, client bind.ContractBackend, totalSigner *uint64) (map[common.Address]*rewardLog, error) {
|
|
||||||
// Not reward for singer of genesis block and only calculate reward at checkpoint block.
|
|
||||||
startBlockNumber := number - (rCheckpoint * 2) + 1
|
|
||||||
endBlockNumber := startBlockNumber + rCheckpoint - 1
|
|
||||||
signers := make(map[common.Address]*rewardLog)
|
|
||||||
|
|
||||||
for i := startBlockNumber; i <= endBlockNumber; i++ {
|
|
||||||
block := chain.GetHeaderByNumber(i)
|
|
||||||
addrs, err := GetSignersFromContract(blockSignerAddr, client, block.Hash())
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i)
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
// Filter duplicate address.
|
|
||||||
if len(addrs) > 0 {
|
if len(signers) != len(keys) {
|
||||||
addrSigners := make(map[common.Address]bool)
|
t.Error("Tx sign for block validators not match")
|
||||||
for _, addr := range addrs {
|
}
|
||||||
if _, ok := addrSigners[addr]; !ok {
|
}
|
||||||
addrSigners[addr] = true
|
}
|
||||||
}
|
|
||||||
}
|
// Generate random string.
|
||||||
for addr := range addrSigners {
|
func randomHash() common.Hash {
|
||||||
_, exist := signers[addr]
|
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
|
||||||
if exist {
|
var b common.Hash
|
||||||
signers[addr].Sign++
|
for i := range b {
|
||||||
} else {
|
rand.Seed(time.Now().UnixNano())
|
||||||
signers[addr] = &rewardLog{1, new(big.Int)}
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||||
}
|
}
|
||||||
*totalSigner++
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unit test for get random position of masternodes.
|
||||||
|
func TestRandomMasterNode(t *testing.T) {
|
||||||
|
oldSlice := NewSlice(0, 10, 1)
|
||||||
|
newSlice := Shuffle(oldSlice)
|
||||||
|
for _, newNumber := range newSlice {
|
||||||
|
for i, oldNumber := range oldSlice {
|
||||||
|
if oldNumber == newNumber {
|
||||||
|
// Delete find element.
|
||||||
|
oldSlice = append(oldSlice[:i], oldSlice[i+1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(oldSlice) != 0 {
|
||||||
log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber)
|
t.Errorf("Test generate random masternode fail %v - %v", oldSlice, newSlice)
|
||||||
|
}
|
||||||
return signers, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate reward for signers.
|
func TestEncryptDecrypt(t *testing.T) {
|
||||||
func CalculateRewardForSigner(chainReward *big.Int, signers map[common.Address]*rewardLog, totalSigner uint64) (map[common.Address]*big.Int, error) {
|
//byteInteger := common.LeftPadBytes([]byte(new(big.Int).SetInt64(4).String()), 32)
|
||||||
resultSigners := make(map[common.Address]*big.Int)
|
randomByte := RandStringByte(32)
|
||||||
// Add reward for signers.
|
encrypt := Encrypt(randomByte, new(big.Int).SetInt64(4).String())
|
||||||
if totalSigner > 0 {
|
decrypt := Decrypt(randomByte, encrypt)
|
||||||
for signer, rLog := range signers {
|
t.Log("Encrypt", encrypt, "Test", string(randomByte), "Decrypt", decrypt, "trim", string(bytes.TrimLeft([]byte(decrypt), "\x00")))
|
||||||
// Add reward for signer.
|
|
||||||
calcReward := new(big.Int)
|
|
||||||
calcReward.Div(chainReward, new(big.Int).SetUint64(totalSigner))
|
|
||||||
calcReward.Mul(calcReward, new(big.Int).SetUint64(rLog.Sign))
|
|
||||||
rLog.Reward = calcReward
|
|
||||||
|
|
||||||
resultSigners[signer] = calcReward
|
|
||||||
}
|
|
||||||
}
|
|
||||||
jsonSigners, err := json.Marshal(signers)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Fail to parse json signers", "error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
log.Info("Signers data", "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
|
|
||||||
|
|
||||||
return resultSigners, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get candidate owner by address.
|
func isArrayEqual(a [][]int64, b [][]int64) bool {
|
||||||
func GetCandidatesOwnerBySigner(validator *contractValidator.XDCValidator, signerAddr common.Address) common.Address {
|
if len(a) != len(b) {
|
||||||
owner := signerAddr
|
return false
|
||||||
opts := new(bind.CallOpts)
|
|
||||||
owner, err := validator.GetCandidateOwner(opts, signerAddr)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Fail get candidate owner", "error", err)
|
|
||||||
return owner
|
|
||||||
}
|
}
|
||||||
|
for i, vs := range a {
|
||||||
return owner
|
for j, v := range vs {
|
||||||
}
|
if v != b[i][j] {
|
||||||
|
return false
|
||||||
// Calculate reward for holders.
|
|
||||||
func CalculateRewardForHolders(foudationWalletAddr common.Address, validator *contractValidator.XDCValidator, state *state.StateDB, signer common.Address, calcReward *big.Int) error {
|
|
||||||
rewards, err := GetRewardBalancesRate(foudationWalletAddr, signer, calcReward, validator)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(rewards) > 0 {
|
|
||||||
for holder, reward := range rewards {
|
|
||||||
state.AddBalance(holder, reward)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get reward balance rates for master node, founder and holders.
|
|
||||||
func GetRewardBalancesRate(foudationWalletAddr common.Address, masterAddr common.Address, totalReward *big.Int, validator *contractValidator.XDCValidator) (map[common.Address]*big.Int, error) {
|
|
||||||
owner := GetCandidatesOwnerBySigner(validator, masterAddr)
|
|
||||||
balances := make(map[common.Address]*big.Int)
|
|
||||||
rewardMaster := new(big.Int).Mul(totalReward, new(big.Int).SetInt64(RewardMasterPercent))
|
|
||||||
rewardMaster = new(big.Int).Div(rewardMaster, new(big.Int).SetInt64(100))
|
|
||||||
balances[owner] = rewardMaster
|
|
||||||
// Get voters for masternode.
|
|
||||||
opts := new(bind.CallOpts)
|
|
||||||
voters, err := validator.GetVoters(opts, masterAddr)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Fail to get voters", "error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(voters) > 0 {
|
|
||||||
totalVoterReward := new(big.Int).Mul(totalReward, new(big.Int).SetUint64(RewardVoterPercent))
|
|
||||||
totalVoterReward = new(big.Int).Div(totalVoterReward, new(big.Int).SetUint64(100))
|
|
||||||
totalCap := new(big.Int)
|
|
||||||
// Get voters capacities.
|
|
||||||
voterCaps := make(map[common.Address]*big.Int)
|
|
||||||
for _, voteAddr := range voters {
|
|
||||||
voterCap, err := validator.GetVoterCap(opts, masterAddr, voteAddr)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Fail to get vote capacity", "error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
totalCap.Add(totalCap, voterCap)
|
|
||||||
voterCaps[voteAddr] = voterCap
|
|
||||||
}
|
|
||||||
if totalCap.Cmp(new(big.Int).SetInt64(0)) > 0 {
|
|
||||||
for addr, voteCap := range voterCaps {
|
|
||||||
// Only valid voter has cap > 0.
|
|
||||||
if voteCap.Cmp(new(big.Int).SetInt64(0)) > 0 {
|
|
||||||
rcap := new(big.Int).Mul(totalVoterReward, voteCap)
|
|
||||||
rcap = new(big.Int).Div(rcap, totalCap)
|
|
||||||
if balances[addr] != nil {
|
|
||||||
balances[addr].Add(balances[addr], rcap)
|
|
||||||
} else {
|
|
||||||
balances[addr] = rcap
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
foudationReward := new(big.Int).Mul(totalReward, new(big.Int).SetInt64(RewardFoundationPercent))
|
// Unit test for
|
||||||
foudationReward = new(big.Int).Div(foudationReward, new(big.Int).SetInt64(100))
|
func TestGenM2FromRandomize(t *testing.T) {
|
||||||
balances[foudationWalletAddr] = foudationReward
|
var a []int64
|
||||||
|
for i := 0; i <= 10; i++ {
|
||||||
jsonHolders, err := json.Marshal(balances)
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
if err != nil {
|
a = append(a, int64(rand.Intn(9999)))
|
||||||
log.Error("Fail to parse json holders", "error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
log.Info("Holders reward", "holders", string(jsonHolders), "master node", masterAddr.String())
|
b, err := GenM2FromRandomize(a, common.MaxMasternodes)
|
||||||
|
t.Log("randomize", b, "len", len(b))
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Fail to test gen m2 for randomize.", err)
|
||||||
|
}
|
||||||
|
// Test Permutation Without Fixed-point.
|
||||||
|
M1List := NewSlice(int64(0), common.MaxMasternodes, 1)
|
||||||
|
for i, m1 := range M1List {
|
||||||
|
if m1 == b[i] {
|
||||||
|
t.Errorf("Error check Permutation Without Fixed-point %v - %v - %v", i, b[i], a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return balances, nil
|
// Unit test for validator m2.
|
||||||
|
func TestBuildValidatorFromM2(t *testing.T) {
|
||||||
|
a := []int64{84, 58, 27, 96, 127, 60, 136, 20, 121, 31, 87, 85, 40, 120, 149, 109, 141, 145, 11, 110, 147, 35, 76, 46, 34, 108, 72, 103, 102, 12, 23, 47, 70, 86, 125, 112, 128, 13, 130, 98, 126, 62, 132, 111, 134, 6, 106, 67, 24, 91, 101, 50, 94, 43, 77, 73, 129, 71, 51, 10, 92, 29, 80, 95, 33, 100, 124, 75, 38, 133, 79, 83, 61, 36, 122, 99, 16, 28, 18, 116, 140, 97, 119, 82, 148, 48, 56, 32, 93, 107, 69, 68, 123, 81, 22, 137, 25, 115, 44, 8, 42, 131, 143, 17, 55, 89, 9, 15, 19, 59, 146, 54, 5, 30, 41, 144, 117, 1, 104, 49, 105, 45, 88, 78, 74, 135, 0, 21, 57, 3, 66, 52, 63, 138, 4, 114, 37, 118, 14, 2, 26, 7, 65, 139, 39, 64, 90, 142, 53, 113}
|
||||||
|
b := BuildValidatorFromM2(a)
|
||||||
|
c := ExtractValidatorsFromBytes(b)
|
||||||
|
if !isArrayEqual([][]int64{a}, [][]int64{c}) {
|
||||||
|
t.Errorf("Fail to get m2 result %v", b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unit test for decode validator string data.
|
||||||
|
func TestDecodeValidatorsHexData(t *testing.T) {
|
||||||
|
a := "0x000000310000003000000032000000310000003000000032000000310000003000000032000000310000003000000031000000320000003000000031000000320000003000000031000000320000003000000030000000310000003200000030000000310000003200000030000000310000003200000030000000300000003100000032000000300000003100000032000000300000003100000032000000300000003200000030000000310000003200000030000000310000003200000030000000310000003000000030"
|
||||||
|
b, err := DecodeValidatorsHexData(a)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Fail to decode validator from hex string", err)
|
||||||
|
}
|
||||||
|
c := []int64{1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 2, 0, 1, 2, 0, 1, 2, 0, 1, 0, 0}
|
||||||
|
if !isArrayEqual([][]int64{b}, [][]int64{c}) {
|
||||||
|
t.Errorf("Fail to get m2 result %v", b)
|
||||||
|
}
|
||||||
|
t.Log("b", b)
|
||||||
}
|
}
|
||||||
|
|
@ -64,7 +64,6 @@ const (
|
||||||
maxTimeFutureBlocks = 30
|
maxTimeFutureBlocks = 30
|
||||||
badBlockLimit = 10
|
badBlockLimit = 10
|
||||||
triesInMemory = 128
|
triesInMemory = 128
|
||||||
masterNodeLimit = 150
|
|
||||||
|
|
||||||
// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
|
// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
|
||||||
BlockChainVersion = 3
|
BlockChainVersion = 3
|
||||||
|
|
@ -1646,8 +1645,8 @@ func (bc *BlockChain) UpdateM1() error {
|
||||||
})
|
})
|
||||||
// update masternodes
|
// update masternodes
|
||||||
log.Info("Updating new set of masternodes")
|
log.Info("Updating new set of masternodes")
|
||||||
if len(ms) > masterNodeLimit {
|
if len(ms) > common.MaxMasternodes {
|
||||||
err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms[:masterNodeLimit])
|
err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms[:common.MaxMasternodes])
|
||||||
} else {
|
} else {
|
||||||
err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms)
|
err = engine.UpdateMasternodes(bc, bc.CurrentHeader(), ms)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ func TestDefaultGenesisBlock(t *testing.T) {
|
||||||
|
|
||||||
func TestSetupGenesis(t *testing.T) {
|
func TestSetupGenesis(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50")
|
customghash = common.HexToHash("0xa1cd36b0c6db6940b1184a28cea11f4f74d823589c6a7e7f1398a5c176835598")
|
||||||
customg = Genesis{
|
customg = Genesis{
|
||||||
Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)},
|
Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)},
|
||||||
Alloc: GenesisAlloc{
|
Alloc: GenesisAlloc{
|
||||||
|
|
|
||||||
|
|
@ -251,15 +251,19 @@ func (l *txList) Overlaps(tx *types.Transaction) bool {
|
||||||
func (l *txList) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Transaction) {
|
func (l *txList) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Transaction) {
|
||||||
// If there's an older better transaction, abort
|
// If there's an older better transaction, abort
|
||||||
old := l.txs.Get(tx.Nonce())
|
old := l.txs.Get(tx.Nonce())
|
||||||
if old != nil {
|
|
||||||
threshold := new(big.Int).Div(new(big.Int).Mul(old.GasPrice(), big.NewInt(100+int64(priceBump))), big.NewInt(100))
|
if (tx.To() != nil && tx.To().String() != common.RandomizeSMC) || tx.To() == nil {
|
||||||
// Have to ensure that the new gas price is higher than the old gas
|
if old != nil {
|
||||||
// price as well as checking the percentage threshold to ensure that
|
threshold := new(big.Int).Div(new(big.Int).Mul(old.GasPrice(), big.NewInt(100+int64(priceBump))), big.NewInt(100))
|
||||||
// this is accurate for low (Wei-level) gas price replacements
|
// Have to ensure that the new gas price is higher than the old gas
|
||||||
if old.GasPrice().Cmp(tx.GasPrice()) >= 0 || threshold.Cmp(tx.GasPrice()) > 0 {
|
// price as well as checking the percentage threshold to ensure that
|
||||||
return false, nil
|
// this is accurate for low (Wei-level) gas price replacements
|
||||||
|
if old.GasPrice().Cmp(tx.GasPrice()) >= 0 || threshold.Cmp(tx.GasPrice()) > 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise overwrite the old transaction with the current one
|
// Otherwise overwrite the old transaction with the current one
|
||||||
l.txs.Put(tx)
|
l.txs.Put(tx)
|
||||||
if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
|
if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
|
||||||
|
|
|
||||||
|
|
@ -589,7 +589,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
|
||||||
return ErrInsufficientFunds
|
return ErrInsufficientFunds
|
||||||
}
|
}
|
||||||
|
|
||||||
if tx.To() == nil || (tx.To() != nil && tx.To().String() != common.BlockSigners) {
|
if tx.To() != nil && tx.To().String() != common.BlockSigners && tx.To().String() != common.RandomizeSMC {
|
||||||
intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
|
intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -17,19 +17,19 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// from bcValidBlockTest.json, "SimpleTx"
|
// from bcValidBlockTest.json, "SimpleTx"
|
||||||
func TestBlockEncoding(t *testing.T) {
|
func TestBlockEncoding(t *testing.T) {
|
||||||
blockEnc := common.FromHex("f90260f901f9a083cafc574e1f51ba9dc0568fc617a08ea2429fb384059c972f13b19fa1c8dd55a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c4f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1c0")
|
blockEnc := common.FromHex("f90261f901faa00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c480f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1c0")
|
||||||
var block Block
|
var block Block
|
||||||
if err := rlp.DecodeBytes(blockEnc, &block); err != nil {
|
if err := rlp.DecodeBytes(blockEnc, &block); err != nil {
|
||||||
t.Fatal("decode error: ", err)
|
t.Fatal("decode error: ", err)
|
||||||
|
|
@ -46,7 +46,7 @@ func TestBlockEncoding(t *testing.T) {
|
||||||
check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1"))
|
check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1"))
|
||||||
check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
|
check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
|
||||||
check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
|
check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
|
||||||
check("Hash", block.Hash(), common.HexToHash("0a5843ac1cb04865017cb35a57b50b07084e5fcee39b5acadade33149f4fff9e"))
|
check("Hash", block.Hash(), common.HexToHash("ed8e5b071b309a4c965a2b4aabd1f8f7818276627331ca37e753a3e97ec87081"))
|
||||||
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
|
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
|
||||||
check("Time", block.Time(), big.NewInt(1426516743))
|
check("Time", block.Time(), big.NewInt(1426516743))
|
||||||
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
|
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
|
||||||
|
|
@ -64,6 +64,7 @@ func TestBlockEncoding(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("encode error: ", err)
|
t.Fatal("encode error: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(ourBlockEnc, blockEnc) {
|
if !bytes.Equal(ourBlockEnc, blockEnc) {
|
||||||
t.Errorf("encoded block mismatch:\ngot: %x\nwant: %x", ourBlockEnc, blockEnc)
|
t.Errorf("encoded block mismatch:\ngot: %x\nwant: %x", ourBlockEnc, blockEnc)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -201,7 +201,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, authorized := snap.Signers[eth.etherbase]; authorized {
|
if _, authorized := snap.Signers[eth.etherbase]; authorized {
|
||||||
if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block); err != nil {
|
if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block, chainDb); err != nil {
|
||||||
log.Error("Fail to create tx sign for imported block", "error", err)
|
log.Error("Fail to create tx sign for imported block", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -251,6 +251,40 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check m2 exists on chaindb.
|
||||||
|
// Get secrets and opening at epoc block checkpoint.
|
||||||
|
if number > 0 && number%common.EpocBlockRandomize == 0 {
|
||||||
|
var candidates []int64
|
||||||
|
// Get signers from snapshot.
|
||||||
|
snap, err := c.GetSnapshot(eth.blockchain, chain.CurrentHeader())
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail to get snapshot for get secret and opening.", "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
signers := snap.Signers
|
||||||
|
lenSigners := int64(len(signers))
|
||||||
|
|
||||||
|
if lenSigners > 0 {
|
||||||
|
for addr := range signers {
|
||||||
|
random, err := contracts.GetRandomizeFromContract(client, addr)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail to get random m2 from contract.", "error", err)
|
||||||
|
}
|
||||||
|
candidates = append(candidates, random)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get randomize m2 list.
|
||||||
|
m2, err := contracts.GenM2FromRandomize(candidates, lenSigners)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Can not get m2 from randomize SC", "error", err)
|
||||||
|
}
|
||||||
|
if len(m2) > 0 {
|
||||||
|
header.Validators = contracts.BuildValidatorFromM2(m2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
eth/tracers/testdata/call_tracer_create.json
vendored
2
eth/tracers/testdata/call_tracer_create.json
vendored
|
|
@ -26,7 +26,7 @@
|
||||||
"chainId": 3,
|
"chainId": 3,
|
||||||
"daoForkSupport": true,
|
"daoForkSupport": true,
|
||||||
"eip150Block": 0,
|
"eip150Block": 0,
|
||||||
"eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
|
"eip150Hash": "0xfc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a",
|
||||||
"eip155Block": 10,
|
"eip155Block": 10,
|
||||||
"eip158Block": 10,
|
"eip158Block": 10,
|
||||||
"ethash": {},
|
"ethash": {},
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@
|
||||||
"chainId": 3,
|
"chainId": 3,
|
||||||
"daoForkSupport": true,
|
"daoForkSupport": true,
|
||||||
"eip150Block": 0,
|
"eip150Block": 0,
|
||||||
"eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
|
"eip150Hash": "0xfc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a",
|
||||||
"eip155Block": 10,
|
"eip155Block": 10,
|
||||||
"eip158Block": 10,
|
"eip158Block": 10,
|
||||||
"ethash": {},
|
"ethash": {},
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@
|
||||||
"chainId": 3,
|
"chainId": 3,
|
||||||
"daoForkSupport": true,
|
"daoForkSupport": true,
|
||||||
"eip150Block": 0,
|
"eip150Block": 0,
|
||||||
"eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
|
"eip150Hash": "0xfc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a",
|
||||||
"eip155Block": 10,
|
"eip155Block": 10,
|
||||||
"eip158Block": 10,
|
"eip158Block": 10,
|
||||||
"ethash": {},
|
"ethash": {},
|
||||||
|
|
|
||||||
2
eth/tracers/testdata/call_tracer_oog.json
vendored
2
eth/tracers/testdata/call_tracer_oog.json
vendored
|
|
@ -28,7 +28,7 @@
|
||||||
"chainId": 3,
|
"chainId": 3,
|
||||||
"daoForkSupport": true,
|
"daoForkSupport": true,
|
||||||
"eip150Block": 0,
|
"eip150Block": 0,
|
||||||
"eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
|
"eip150Hash": "0xfc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a",
|
||||||
"eip155Block": 10,
|
"eip155Block": 10,
|
||||||
"eip158Block": 10,
|
"eip158Block": 10,
|
||||||
"ethash": {},
|
"ethash": {},
|
||||||
|
|
|
||||||
2
eth/tracers/testdata/call_tracer_revert.json
vendored
2
eth/tracers/testdata/call_tracer_revert.json
vendored
|
|
@ -26,7 +26,7 @@
|
||||||
"chainId": 3,
|
"chainId": 3,
|
||||||
"daoForkSupport": true,
|
"daoForkSupport": true,
|
||||||
"eip150Block": 0,
|
"eip150Block": 0,
|
||||||
"eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
|
"eip150Hash": "0xfc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a",
|
||||||
"eip155Block": 10,
|
"eip155Block": 10,
|
||||||
"eip158Block": 10,
|
"eip158Block": 10,
|
||||||
"ethash": {},
|
"ethash": {},
|
||||||
|
|
|
||||||
2
eth/tracers/testdata/call_tracer_simple.json
vendored
2
eth/tracers/testdata/call_tracer_simple.json
vendored
|
|
@ -37,7 +37,7 @@
|
||||||
"chainId": 3,
|
"chainId": 3,
|
||||||
"daoForkSupport": true,
|
"daoForkSupport": true,
|
||||||
"eip150Block": 0,
|
"eip150Block": 0,
|
||||||
"eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
|
"eip150Hash": "0xfc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a",
|
||||||
"eip155Block": 10,
|
"eip155Block": 10,
|
||||||
"eip158Block": 10,
|
"eip158Block": 10,
|
||||||
"ethash": {},
|
"ethash": {},
|
||||||
|
|
|
||||||
2
eth/tracers/testdata/call_tracer_throw.json
vendored
2
eth/tracers/testdata/call_tracer_throw.json
vendored
|
|
@ -30,7 +30,7 @@
|
||||||
"chainId": 3,
|
"chainId": 3,
|
||||||
"daoForkSupport": true,
|
"daoForkSupport": true,
|
||||||
"eip150Block": 0,
|
"eip150Block": 0,
|
||||||
"eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
|
"eip150Hash": "0xfc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a",
|
||||||
"eip155Block": 10,
|
"eip155Block": 10,
|
||||||
"eip158Block": 10,
|
"eip158Block": 10,
|
||||||
"ethash": {},
|
"ethash": {},
|
||||||
|
|
|
||||||
|
|
@ -816,6 +816,7 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
|
||||||
"timestamp": (*hexutil.Big)(head.Time),
|
"timestamp": (*hexutil.Big)(head.Time),
|
||||||
"transactionsRoot": head.TxHash,
|
"transactionsRoot": head.TxHash,
|
||||||
"receiptsRoot": head.ReceiptHash,
|
"receiptsRoot": head.ReceiptHash,
|
||||||
|
"validators": hexutil.Bytes(head.Validators),
|
||||||
}
|
}
|
||||||
|
|
||||||
if inclTx {
|
if inclTx {
|
||||||
|
|
|
||||||
|
|
@ -364,7 +364,7 @@ func (self *worker) wait() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Send tx sign to smart contract blockSigners.
|
// Send tx sign to smart contract blockSigners.
|
||||||
if err := contracts.CreateTransactionSign(self.config, self.eth.TxPool(), self.eth.AccountManager(), block); err != nil {
|
if err := contracts.CreateTransactionSign(self.config, self.eth.TxPool(), self.eth.AccountManager(), block, self.chainDb); err != nil {
|
||||||
log.Error("Fail to create tx sign for signer", "error", "err")
|
log.Error("Fail to create tx sign for signer", "error", "err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3") // Mainnet genesis hash to enforce below configs on
|
MainnetGenesisHash = common.HexToHash("2efa267fef46877ac2659209e2299f97b0afc2a797ee8672db21920a5151e0aa") // Mainnet genesis hash to enforce below configs on
|
||||||
TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d") // Testnet genesis hash to enforce below configs on
|
TestnetGenesisHash = common.HexToHash("fc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a") // Testnet genesis hash to enforce below configs on
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -51,7 +51,7 @@ var (
|
||||||
DAOForkBlock: nil,
|
DAOForkBlock: nil,
|
||||||
DAOForkSupport: true,
|
DAOForkSupport: true,
|
||||||
EIP150Block: big.NewInt(0),
|
EIP150Block: big.NewInt(0),
|
||||||
EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
|
EIP150Hash: common.HexToHash("0xfc673c99f7650df67bf769ab5b923ef60deefdbb49a6fe0dea14420f6790736a"),
|
||||||
EIP155Block: big.NewInt(10),
|
EIP155Block: big.NewInt(10),
|
||||||
EIP158Block: big.NewInt(10),
|
EIP158Block: big.NewInt(10),
|
||||||
ByzantiumBlock: big.NewInt(1700000),
|
ByzantiumBlock: big.NewInt(1700000),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue