Fixed add unit test tx sign for validators.

This commit is contained in:
parmarrushabh 2018-09-09 18:01:19 +05:30
parent 00df3d6cc3
commit 9b480b261d

View file

@ -28,26 +28,39 @@ func TestSendTxSign(t *testing.T) {
signer := types.HomesteadSigner{} signer := types.HomesteadSigner{}
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000)}} genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000)}}
backend := backends.NewSimulatedBackend(genesis) backend := backends.NewSimulatedBackend(genesis)
backend.Commit()
ctx := context.Background() ctx := context.Background()
transactOpts := bind.NewKeyedTransactor(acc1Key) transactOpts := bind.NewKeyedTransactor(acc1Key)
addrBlockSigner, blockSigner, err := blocksigner.DeployBlockSigner(transactOpts, backend) blockSignerAddr, blockSigner, err := blocksigner.DeployBlockSigner(transactOpts, backend)
if err != nil { if err != nil {
t.Fatalf("Can't deploy block signer: %v", err) t.Fatalf("Can't get block signer: %v", err)
} }
backend.Commit() backend.Commit()
nonces := make(map[*ecdsa.PrivateKey]int) nonces := make(map[*ecdsa.PrivateKey]int)
oldBlock := make([]common.Address, 100) oldBlock := make([]common.Address, 100)
for i := uint64(0); i < 100; i++ { signTx := func(ctx context.Context, backend *backends.SimulatedBackend, signer types.HomesteadSigner, nonces map[*ecdsa.PrivateKey]int, accKey *ecdsa.PrivateKey, i uint64) {
rand := rand.Intn(len(keys)) tx, _ := types.SignTx(CreateTxSign(new(big.Int).SetUint64(i), uint64(nonces[accKey]), blockSignerAddr), signer, accKey)
accKey := keys[rand]
tx, _ := types.SignTx(CreateTxSign(new(big.Int).SetUint64(i), uint64(nonces[accKey]), addrBlockSigner), signer, accKey)
backend.SendTransaction(ctx, tx) backend.SendTransaction(ctx, tx)
backend.Commit() backend.Commit()
nonces[accKey]++ nonces[accKey]++
oldBlock[i] = accounts[rand] }
// Tx sign for signer.
for i := uint64(0); i < 100; i++ {
randIndex := rand.Intn(len(keys))
accKey := keys[randIndex]
signTx(ctx, backend, signer, nonces, accKey, i)
oldBlock[i] = accounts[randIndex]
// Tx sign for validators.
for _, key := range keys {
if key != accKey {
signTx(ctx, backend, signer, nonces, key, i)
}
}
} }
for i := uint64(0); i < 100; i++ { for i := uint64(0); i < 100; i++ {
@ -59,5 +72,9 @@ func TestSendTxSign(t *testing.T) {
if signers[0].String() != oldBlock[i].String() { if signers[0].String() != oldBlock[i].String() {
t.Errorf("Tx sign for block signer not match %v - %v", signers[0].String(), oldBlock[i].String()) t.Errorf("Tx sign for block signer not match %v - %v", signers[0].String(), oldBlock[i].String())
} }
if len(signers) != len(keys) {
t.Error("Tx sign for block validators not match")
}
} }
} }