Fixed unit test for new m2 block header.

This commit is contained in:
parmarrushabh 2018-11-09 16:29:54 +05:30
parent 07b4be1284
commit 1396b1a10f
12 changed files with 126 additions and 46 deletions

View file

@ -77,7 +77,7 @@ var daoProForkGenesis = `{
}
}`
var daoGenesisHash = common.HexToHash("e99f371cb126efa0a5bf4dc0660114a607ee3b5458c7c82b044164923f61069e")
var daoGenesisHash = common.HexToHash("29a4b5d743bfbda3a7461974d49c62bf23ba5df9c8b01de8256e2ac2a9ae1cd8")
var daoGenesisForkBlock = big.NewInt(314)
// 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()
genesisHash := common.HexToHash("205f64c83f065fe851db5703843304e46115c42c96434da316ce567ab9d5cc22")
genesisHash := common.HexToHash("8d13370621558f4ed0da587934473c0404729f28b0ff1d50e5fdd840457a2f17")
if genesis != "" {
genesisHash = daoGenesisHash
}

View file

@ -1,6 +1,7 @@
package contracts
import (
"bytes"
"context"
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
@ -10,28 +11,37 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/consensus/XDPoS"
"math/big"
"math/rand"
"testing"
"time"
)
func TestSendTxSign(t *testing.T) {
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)
accounts := []common.Address{acc2Addr, acc3Addr, acc4Addr}
keys := []*ecdsa.PrivateKey{acc2Key, acc3Key, acc4Key}
var (
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)
)
signer := types.HomesteadSigner{}
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000)}}
func getCommonBackend() *backends.SimulatedBackend {
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}}
backend := backends.NewSimulatedBackend(genesis)
backend.Commit()
return backend
}
func TestSendTxSign(t *testing.T) {
accounts := []common.Address{acc2Addr, acc3Addr, acc4Addr}
keys := []*ecdsa.PrivateKey{acc2Key, acc3Key, acc4Key}
backend := getCommonBackend()
signer := types.HomesteadSigner{}
ctx := context.Background()
transactOpts := bind.NewKeyedTransactor(acc1Key)
@ -99,4 +109,88 @@ func randomHash() common.Hash {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
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 {
t.Errorf("Test generate random masternode fail %v - %v", oldSlice, newSlice)
}
}
func TestEncryptDecrypt(t *testing.T) {
//byteInteger := common.LeftPadBytes([]byte(new(big.Int).SetInt64(4).String()), 32)
randomByte := RandStringByte(32)
encrypt := Encrypt(randomByte, new(big.Int).SetInt64(4).String())
decrypt := Decrypt(randomByte, encrypt)
t.Log("Encrypt", encrypt, "Test", string(randomByte), "Decrypt", decrypt, "trim", string(bytes.TrimLeft([]byte(decrypt), "\x00")))
}
func isArrayEqual(a [][]int64, b [][]int64) bool {
if len(a) != len(b) {
return false
}
for i, vs := range a {
for j, v := range vs {
if v != b[i][j] {
return false
}
}
}
return true
}
// Unit test for
func TestGenM2FromRandomize(t *testing.T) {
var a []int64
for i := 0; i <= 10; i++ {
rand.Seed(time.Now().UTC().UnixNano())
a = append(a, int64(rand.Intn(9999)))
}
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)
}
}
}
// 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 := XDPoS.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)
}

View file

@ -366,7 +366,7 @@ func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
common.BytesToAddress([]byte{6}): {Balance: big.NewInt(1)}, // ECAdd
common.BytesToAddress([]byte{7}): {Balance: big.NewInt(1)}, // ECScalarMul
common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing
faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
},
}
}

View file

@ -32,11 +32,11 @@ import (
func TestDefaultGenesisBlock(t *testing.T) {
block := DefaultGenesisBlock().ToBlock(nil)
if block.Hash() != params.MainnetGenesisHash {
t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash().String(), params.MainnetGenesisHash.String())
}
block = DefaultTestnetGenesisBlock().ToBlock(nil)
if block.Hash() != params.TestnetGenesisHash {
t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestnetGenesisHash)
t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash().String(), params.TestnetGenesisHash.String())
}
}

View file

@ -327,7 +327,7 @@ func (b *Block) ReceiptHash() common.Hash { return b.header.ReceiptHash }
func (b *Block) UncleHash() common.Hash { return b.header.UncleHash }
func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Extra) }
func (b *Block) Penalties() []byte { return common.CopyBytes(b.header.Penalties) }
func (b *Block) Validator() []byte { return common.CopyBytes(b.header.Validator) }
func (b *Block) Validator() []byte { return common.CopyBytes(b.header.Validator) }
func (b *Block) Header() *Header { return CopyHeader(b.header) }

View file

@ -21,7 +21,6 @@ import (
"testing"
"bytes"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"reflect"
@ -29,7 +28,7 @@ import (
// from bcValidBlockTest.json, "SimpleTx"
func TestBlockEncoding(t *testing.T) {
blockEnc := common.FromHex("f90265f901fea00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000832fefd8832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c48080f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1c0")
blockEnc := common.FromHex("f90201f901fca00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c4808080c0c0")
var block Block
if err := rlp.DecodeBytes(blockEnc, &block); err != nil {
t.Fatal("decode error: ", err)
@ -45,21 +44,12 @@ func TestBlockEncoding(t *testing.T) {
check("GasUsed", block.GasUsed(), uint64(21000))
check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1"))
check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
check("Hash", block.Hash(), common.HexToHash("4f6c7eff9b02b689c1f99f81656dad19dfbc0d9f163f900ddfa940974aea72cb"))
check("Root", block.Root().String(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017").String())
check("Hash", block.Hash().String(), common.HexToHash("e8d9d473fdeddd3079988fa7be58f582b7b2800e90917d4bb6f11155ce4dba30").String())
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
check("Time", block.Time(), big.NewInt(1426516743))
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil)
tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100"))
fmt.Println(block.Transactions()[0].Hash())
fmt.Println(tx1.data)
fmt.Println(tx1.Hash())
check("len(Transactions)", len(block.Transactions()), 1)
check("Transactions[0].Hash", block.Transactions()[0].Hash().String(), tx1.Hash().String())
ourBlockEnc, err := rlp.EncodeToBytes(&block)
if err != nil {
t.Fatal("encode error: ", err)

View file

@ -220,9 +220,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
sighash, _ := wallet.SignHash(accounts.Account{Address: eb}, XDPoS.SigHash(header).Bytes())
header.Validator = sighash
block = types.NewBlockWithHeader(header)
//c := eth.engine.(*XDPoS.XDPoS)
//validator, _ := c.RecoverValidator(block.Header())
//log.Error("addr", "addr", validator)
}
return block, nil

View file

@ -40,8 +40,8 @@ type PublicDownloaderAPI struct {
// installSyncSubscription channel.
func NewPublicDownloaderAPI(d *Downloader, m *event.TypeMux) *PublicDownloaderAPI {
api := &PublicDownloaderAPI{
d: d,
mux: m,
d: d,
mux: m,
installSyncSubscription: make(chan chan interface{}),
uninstallSyncSubscription: make(chan *uninstallSyncSubscriptionRequest),
}

View file

@ -654,8 +654,6 @@ func (f *Fetcher) insert(peer string, block *types.Block) {
// Quickly validate the header and propagate the block if it passes
switch err := f.verifyHeader(block.Header()); err {
case nil:
// Append m2 to block header.
// Invoke the dv hook to run double validation layer
if f.appendM2HeaderHook != nil {
if block, err = f.appendM2HeaderHook(block); err != nil {
log.Error("Append m2 to block header fail", "err", err)

View file

@ -242,10 +242,10 @@ func testGetBlockBodies(t *testing.T, protocol int) {
available []bool // Availability of explicitly requested blocks
expected int // Total number of existing blocks to expect
}{
{1, nil, nil, 1}, // A single random block should be retrievable
{10, nil, nil, 10}, // Multiple random blocks should be retrievable
{limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
{limit + 1, nil, nil, limit}, // No more than the possible block count should be returned
{1, nil, nil, 1}, // A single random block should be retrievable
{10, nil, nil, 10}, // Multiple random blocks should be retrievable
{limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
{limit + 1, nil, nil, limit}, // No more than the possible block count should be returned
{0, []common.Hash{pm.blockchain.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
{0, []common.Hash{pm.blockchain.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
{0, []common.Hash{{}}, []bool{false}, 0}, // A non existent block should not be returned

View file

@ -24,8 +24,8 @@ import (
)
var (
MainnetGenesisHash = common.HexToHash("205f64c83f065fe851db5703843304e46115c42c96434da316ce567ab9d5cc22") // Mainnet genesis hash to enforce below configs on
TestnetGenesisHash = common.HexToHash("62e0fde86e34c263e250fbcd5ca4598ba8ca10a1d166c8526bb127e10b313311") // Testnet genesis hash to enforce below configs on
MainnetGenesisHash = common.HexToHash("8d13370621558f4ed0da587934473c0404729f28b0ff1d50e5fdd840457a2f17") // Mainnet genesis hash to enforce below configs on
TestnetGenesisHash = common.HexToHash("dffc8ae3b45965404b4fd73ce7f0e13e822ac0fc23ce7e95b42bc5f1e57023a5") // Testnet genesis hash to enforce below configs on
)
var (

View file

@ -71,7 +71,7 @@ func initErrHandling() {
multipleChoicesPage := GetMultipleChoicesErrorPage()
//map the codes to the available pages
tnames := map[int]string{
0: genErrPage, //default
0: genErrPage, //default
http.StatusBadRequest: genErrPage,
http.StatusNotFound: notFoundPage,
http.StatusMultipleChoices: multipleChoicesPage,