mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
Add unit test for send tx sign for block signer.
This commit is contained in:
parent
4e3cb79ff2
commit
00df3d6cc3
3 changed files with 81 additions and 11 deletions
|
|
@ -43,11 +43,8 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and send tx to smart contract for sign validate block.
|
// Create and send tx to smart contract for sign validate block.
|
||||||
blockHex := common.LeftPadBytes(block.Number().Bytes(), 32)
|
|
||||||
data := common.Hex2Bytes(HexSignMethod)
|
|
||||||
inputData := append(data, blockHex...)
|
|
||||||
nonce := pool.State().GetNonce(account.Address)
|
nonce := pool.State().GetNonce(account.Address)
|
||||||
tx := types.NewTransaction(nonce, common.HexToAddress(common.BlockSigners), big.NewInt(0), 100000, big.NewInt(0), inputData)
|
tx := CreateTxSign(block.Number(), nonce, common.HexToAddress(common.BlockSigners))
|
||||||
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
|
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fail to create tx sign", "error", err)
|
log.Error("Fail to create tx sign", "error", err)
|
||||||
|
|
@ -60,13 +57,18 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create tx sign.
|
||||||
|
func CreateTxSign(blockNumber *big.Int, nonce uint64, blockSigner common.Address) *types.Transaction {
|
||||||
|
blockHex := common.LeftPadBytes(blockNumber.Bytes(), 32)
|
||||||
|
data := common.Hex2Bytes(HexSignMethod)
|
||||||
|
inputData := append(data, blockHex...)
|
||||||
|
tx := types.NewTransaction(nonce, blockSigner, big.NewInt(0), 100000, big.NewInt(0), inputData)
|
||||||
|
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
|
||||||
// Get signers signed for blockNumber from blockSigner contract.
|
// Get signers signed for blockNumber from blockSigner contract.
|
||||||
func GetSignersFromContract(ctx *node.ServiceContext, blockNumber uint64) ([]common.Address, error) {
|
func GetSignersFromContract(client bind.ContractBackend, blockNumber uint64) ([]common.Address, error) {
|
||||||
client, err := GetEthClient(ctx)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Fail to connect IPC from blockSigner", "error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
addr := common.HexToAddress(common.BlockSigners)
|
addr := common.HexToAddress(common.BlockSigners)
|
||||||
blockSigner, err := contract.NewBlockSigner(addr, client)
|
blockSigner, err := contract.NewBlockSigner(addr, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
63
contracts/utils_test.go
Normal file
63
contracts/utils_test.go
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
package contracts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"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/contracts/blocksigner"
|
||||||
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
"math/big"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSendTxSign(t *testing.T) {
|
||||||
|
acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
||||||
|
acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
|
||||||
|
acc3Key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
|
acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey)
|
||||||
|
acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey)
|
||||||
|
acc3Addr := crypto.PubkeyToAddress(acc3Key.PublicKey)
|
||||||
|
accounts := []common.Address{acc2Addr, acc3Addr}
|
||||||
|
keys := []*ecdsa.PrivateKey{acc2Key, acc3Key}
|
||||||
|
|
||||||
|
signer := types.HomesteadSigner{}
|
||||||
|
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000)}}
|
||||||
|
backend := backends.NewSimulatedBackend(genesis)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
transactOpts := bind.NewKeyedTransactor(acc1Key)
|
||||||
|
addrBlockSigner, blockSigner, err := blocksigner.DeployBlockSigner(transactOpts, backend)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't deploy block signer: %v", err)
|
||||||
|
}
|
||||||
|
backend.Commit()
|
||||||
|
|
||||||
|
nonces := make(map[*ecdsa.PrivateKey]int)
|
||||||
|
oldBlock := make([]common.Address, 100)
|
||||||
|
|
||||||
|
for i := uint64(0); i < 100; i++ {
|
||||||
|
rand := rand.Intn(len(keys))
|
||||||
|
accKey := keys[rand]
|
||||||
|
tx, _ := types.SignTx(CreateTxSign(new(big.Int).SetUint64(i), uint64(nonces[accKey]), addrBlockSigner), signer, accKey)
|
||||||
|
backend.SendTransaction(ctx, tx)
|
||||||
|
backend.Commit()
|
||||||
|
nonces[accKey]++
|
||||||
|
oldBlock[i] = accounts[rand]
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := uint64(0); i < 100; i++ {
|
||||||
|
signers, err := blockSigner.GetSigners(new(big.Int).SetUint64(i))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't get signers: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if signers[0].String() != oldBlock[i].String() {
|
||||||
|
t.Errorf("Tx sign for block signer not match %v - %v", signers[0].String(), oldBlock[i].String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -208,7 +208,12 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
|
|
||||||
for i := startBlockNumber; i <= endBlockNumber; i++ {
|
for i := startBlockNumber; i <= endBlockNumber; i++ {
|
||||||
// Get signers in blockSigner smartcontract.
|
// Get signers in blockSigner smartcontract.
|
||||||
addrs, err := contracts.GetSignersFromContract(ctx, i)
|
client, err := contracts.GetEthClient(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail to connect IPC from blockSigner", "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
addrs, err := contracts.GetSignersFromContract(client, i
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i)
|
log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i)
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue