|
| 1 | +package staking |
| 2 | + |
| 3 | +import ( |
| 4 | + "flare-indexer/database" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func testPChainTxData(id uint64, txID string, inputAddress string, inputIndex int) database.PChainTxData { |
| 9 | + tx := database.PChainTxData{ |
| 10 | + PChainTx: database.PChainTx{ |
| 11 | + BaseEntity: database.BaseEntity{ID: id}, |
| 12 | + TxID: &txID, |
| 13 | + }, |
| 14 | + InputAddress: inputAddress, |
| 15 | + InputIndex: uint32(inputIndex), |
| 16 | + } |
| 17 | + return tx |
| 18 | +} |
| 19 | + |
| 20 | +func TestDedupeTxs(t *testing.T) { |
| 21 | + testPChainTxData := []database.PChainTxData{ |
| 22 | + testPChainTxData(1, "tx1", "addr2", 0), |
| 23 | + testPChainTxData(2, "tx1", "addr1", 0), |
| 24 | + testPChainTxData(3, "tx1", "addr3", 1), |
| 25 | + testPChainTxData(4, "tx2", "addr4", 0), |
| 26 | + testPChainTxData(6, "tx3", "addr1", 0), |
| 27 | + testPChainTxData(5, "tx3", "addr2", 0), |
| 28 | + testPChainTxData(7, "tx3", "addr6", 1), |
| 29 | + testPChainTxData(8, "tx3", "addr7", 1), |
| 30 | + testPChainTxData(9, "tx4", "addr1", 0), |
| 31 | + testPChainTxData(10, "tx4", "addr2", 0), |
| 32 | + } |
| 33 | + |
| 34 | + dedupedTxs := DedupeTxs(testPChainTxData) |
| 35 | + |
| 36 | + expectedSequence := [][]string{ |
| 37 | + {"tx1", "addr1"}, |
| 38 | + {"tx2", "addr4"}, |
| 39 | + {"tx3", "addr1"}, |
| 40 | + {"tx4", "addr1"}, |
| 41 | + } |
| 42 | + |
| 43 | + if len(dedupedTxs) != len(expectedSequence) { |
| 44 | + t.Fatalf("Expected %d deduped txs, got %d", len(expectedSequence), len(dedupedTxs)) |
| 45 | + } |
| 46 | + |
| 47 | + for i, tx := range dedupedTxs { |
| 48 | + expectedTxID := expectedSequence[i][0] |
| 49 | + expectedAddress := expectedSequence[i][1] |
| 50 | + if *tx.TxID != expectedTxID { |
| 51 | + t.Errorf("At index %d, expected TxID %s, got %s", i, expectedTxID, *tx.TxID) |
| 52 | + } |
| 53 | + if tx.InputAddress != expectedAddress { |
| 54 | + t.Errorf("At index %d, expected InputAddress %s, got %s", i, expectedAddress, tx.InputAddress) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments