From 81ff6421d0db475b801311cc2561456598f039f7 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 19 Jan 2024 08:22:24 +0800 Subject: [PATCH] fix Panic In ProcessOrderPending Leads To Chain Halt (#392) Co-authored-by: Liam Lai --- core/types/order_transaction.go | 3 +++ core/types/order_transaction_test.go | 31 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 core/types/order_transaction_test.go diff --git a/core/types/order_transaction.go b/core/types/order_transaction.go index c064444d7f67..907ed7dbe9c0 100644 --- a/core/types/order_transaction.go +++ b/core/types/order_transaction.go @@ -304,6 +304,9 @@ func NewOrderTransactionByNonce(signer OrderSigner, txs map[common.Address]Order // Initialize a price based heap with the head transactions heads := make(OrderTxByNonce, 0, len(txs)) for from, accTxs := range txs { + if len(accTxs) == 0 { + continue + } heads = append(heads, accTxs[0]) // Ensure the sender address is from the signer acc, _ := OrderSender(signer, accTxs[0]) diff --git a/core/types/order_transaction_test.go b/core/types/order_transaction_test.go new file mode 100644 index 000000000000..4b3537a95900 --- /dev/null +++ b/core/types/order_transaction_test.go @@ -0,0 +1,31 @@ +package types + +import ( + "crypto/ecdsa" + "math/big" + "testing" + + "github.com/XinFinOrg/XDPoSChain/common" + "github.com/XinFinOrg/XDPoSChain/crypto" +) + +func TestNewOrderTransactionByNonce(t *testing.T) { + // Generate a batch of accounts to start with + keys := make([]*ecdsa.PrivateKey, 1) + for i := 0; i < len(keys); i++ { + keys[i], _ = crypto.GenerateKey() + } + + groups := map[common.Address]OrderTransactions{} + for start, key := range keys { + addr := crypto.PubkeyToAddress(key.PublicKey) + for i := 0; i < 1; i++ { + //tx, _ := SignTx(NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), 100, big.NewInt(int64(start+i)), nil), signer, key) + orderTx := NewOrderTransaction(uint64(start+i), big.NewInt(1), big.NewInt(2), common.Address{}, common.Address{}, common.Address{}, common.Address{}, "new", "BID", "test", common.Hash{}, 1001) + + groups[addr] = append(groups[addr], orderTx) + } + } + tx := NewOrderTransactionByNonce(OrderTxSigner{}, groups) + t.Log(tx) +}