| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | package core | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"crypto/ecdsa" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/crypto" | 
					
						
							| 
									
										
										
										
											2015-01-07 13:17:48 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/ethdb" | 
					
						
							| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/ethutil" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/event" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/state" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // State query interface | 
					
						
							| 
									
										
										
										
											2015-01-07 13:17:48 +01:00
										 |  |  | type stateQuery struct{ db ethutil.Database } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func SQ() stateQuery { | 
					
						
							|  |  |  | 	db, _ := ethdb.NewMemDatabase() | 
					
						
							|  |  |  | 	return stateQuery{db: db} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (self stateQuery) GetAccount(addr []byte) *state.StateObject { | 
					
						
							| 
									
										
										
										
											2015-01-07 13:17:48 +01:00
										 |  |  | 	return state.NewStateObject(addr, self.db) | 
					
						
							| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func transaction() *types.Transaction { | 
					
						
							|  |  |  | 	return types.NewTransactionMessage(make([]byte, 20), ethutil.Big0, ethutil.Big0, ethutil.Big0, nil) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup() (*TxPool, *ecdsa.PrivateKey) { | 
					
						
							|  |  |  | 	var m event.TypeMux | 
					
						
							|  |  |  | 	key, _ := crypto.GenerateKey() | 
					
						
							| 
									
										
										
										
											2015-01-02 12:26:55 +01:00
										 |  |  | 	return NewTxPool(&m), key | 
					
						
							| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestTxAdding(t *testing.T) { | 
					
						
							|  |  |  | 	pool, key := setup() | 
					
						
							|  |  |  | 	tx1 := transaction() | 
					
						
							|  |  |  | 	tx1.SignECDSA(key) | 
					
						
							|  |  |  | 	err := pool.Add(tx1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = pool.Add(tx1) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		t.Error("added tx twice") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAddInvalidTx(t *testing.T) { | 
					
						
							|  |  |  | 	pool, _ := setup() | 
					
						
							|  |  |  | 	tx1 := transaction() | 
					
						
							|  |  |  | 	err := pool.Add(tx1) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		t.Error("expected error") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRemoveSet(t *testing.T) { | 
					
						
							|  |  |  | 	pool, _ := setup() | 
					
						
							|  |  |  | 	tx1 := transaction() | 
					
						
							| 
									
										
										
										
											2015-01-06 13:18:09 +01:00
										 |  |  | 	pool.addTx(tx1) | 
					
						
							| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | 	pool.RemoveSet(types.Transactions{tx1}) | 
					
						
							|  |  |  | 	if pool.Size() > 0 { | 
					
						
							|  |  |  | 		t.Error("expected pool size to be 0") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRemoveInvalid(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-01-02 12:18:23 +01:00
										 |  |  | 	pool, key := setup() | 
					
						
							| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | 	tx1 := transaction() | 
					
						
							| 
									
										
										
										
											2015-01-06 13:18:09 +01:00
										 |  |  | 	pool.addTx(tx1) | 
					
						
							| 
									
										
										
										
											2015-01-07 13:17:48 +01:00
										 |  |  | 	pool.RemoveInvalid(SQ()) | 
					
						
							| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | 	if pool.Size() > 0 { | 
					
						
							|  |  |  | 		t.Error("expected pool size to be 0") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-02 12:18:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tx1.SetNonce(1) | 
					
						
							|  |  |  | 	tx1.SignECDSA(key) | 
					
						
							| 
									
										
										
										
											2015-01-06 13:18:09 +01:00
										 |  |  | 	pool.addTx(tx1) | 
					
						
							| 
									
										
										
										
											2015-01-07 13:17:48 +01:00
										 |  |  | 	pool.RemoveInvalid(SQ()) | 
					
						
							| 
									
										
										
										
											2015-01-02 12:18:23 +01:00
										 |  |  | 	if pool.Size() != 1 { | 
					
						
							|  |  |  | 		t.Error("expected pool size to be 1, is", pool.Size()) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-02 12:09:38 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-31 17:22:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestInvalidSender(t *testing.T) { | 
					
						
							|  |  |  | 	pool, _ := setup() | 
					
						
							|  |  |  | 	tx := new(types.Transaction) | 
					
						
							|  |  |  | 	tx.V = 28 | 
					
						
							|  |  |  | 	err := pool.ValidateTransaction(tx) | 
					
						
							|  |  |  | 	if err != ErrInvalidSender { | 
					
						
							|  |  |  | 		t.Error("expected %v, got %v", ErrInvalidSender, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |