|
| 1 | +// VulcanizeDB |
| 2 | +// Copyright © 2022 Vulcanize |
| 3 | + |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package file_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/jmoiron/sqlx" |
| 29 | + "github.com/multiformats/go-multihash" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | + |
| 32 | + "github.com/ethereum/go-ethereum/statediff/indexer/database/file" |
| 33 | + "github.com/ethereum/go-ethereum/statediff/indexer/database/file/types" |
| 34 | + "github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres" |
| 35 | + "github.com/ethereum/go-ethereum/statediff/indexer/interfaces" |
| 36 | + "github.com/ethereum/go-ethereum/statediff/indexer/ipld" |
| 37 | +) |
| 38 | + |
| 39 | +const dbDirectory = "/file_indexer" |
| 40 | +const pgCopyStatement = `COPY %s FROM '%s' CSV` |
| 41 | + |
| 42 | +func setupCSVLegacy(t *testing.T) { |
| 43 | + mockLegacyBlock = legacyData.MockBlock |
| 44 | + legacyHeaderCID, _ = ipld.RawdataToCid(ipld.MEthHeader, legacyData.MockHeaderRlp, multihash.KECCAK_256) |
| 45 | + file.CSVTestConfig.OutputDir = "./statediffing_legacy_test" |
| 46 | + |
| 47 | + if _, err := os.Stat(file.CSVTestConfig.OutputDir); !errors.Is(err, os.ErrNotExist) { |
| 48 | + err := os.RemoveAll(file.CSVTestConfig.OutputDir) |
| 49 | + require.NoError(t, err) |
| 50 | + } |
| 51 | + |
| 52 | + ind, err := file.NewStateDiffIndexer(context.Background(), legacyData.Config, file.CSVTestConfig) |
| 53 | + require.NoError(t, err) |
| 54 | + var tx interfaces.Batch |
| 55 | + tx, err = ind.PushBlock( |
| 56 | + mockLegacyBlock, |
| 57 | + legacyData.MockReceipts, |
| 58 | + legacyData.MockBlock.Difficulty()) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + defer func() { |
| 62 | + if err := tx.Submit(err); err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + if err := ind.Close(); err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + }() |
| 69 | + |
| 70 | + for _, node := range legacyData.StateDiffs { |
| 71 | + err = ind.PushStateNode(tx, node, legacyData.MockBlock.Hash().String()) |
| 72 | + require.NoError(t, err) |
| 73 | + } |
| 74 | + |
| 75 | + require.Equal(t, legacyData.BlockNumber.String(), tx.(*file.BatchTx).BlockNumber) |
| 76 | + |
| 77 | + connStr := postgres.DefaultConfig.DbConnectionString() |
| 78 | + sqlxdb, err = sqlx.Connect("postgres", connStr) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("failed to connect to db with connection string: %s err: %v", connStr, err) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func dumpCSVFileData(t *testing.T) { |
| 85 | + outputDir := filepath.Join(dbDirectory, file.CSVTestConfig.OutputDir) |
| 86 | + |
| 87 | + for _, tbl := range file.Tables { |
| 88 | + var stmt string |
| 89 | + varcharColumns := tbl.VarcharColumns() |
| 90 | + if len(varcharColumns) > 0 { |
| 91 | + stmt = fmt.Sprintf( |
| 92 | + pgCopyStatement+" FORCE NOT NULL %s", |
| 93 | + tbl.Name, |
| 94 | + file.TableFilePath(outputDir, tbl.Name), |
| 95 | + strings.Join(varcharColumns, ", "), |
| 96 | + ) |
| 97 | + } else { |
| 98 | + stmt = fmt.Sprintf(pgCopyStatement, tbl.Name, file.TableFilePath(outputDir, tbl.Name)) |
| 99 | + } |
| 100 | + |
| 101 | + _, err = sqlxdb.Exec(stmt) |
| 102 | + require.NoError(t, err) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func dumpWatchedAddressesCSVFileData(t *testing.T) { |
| 107 | + outputFilePath := filepath.Join(dbDirectory, file.CSVTestConfig.WatchedAddressesFilePath) |
| 108 | + stmt := fmt.Sprintf(pgCopyStatement, types.TableWatchedAddresses.Name, outputFilePath) |
| 109 | + |
| 110 | + _, err = sqlxdb.Exec(stmt) |
| 111 | + require.NoError(t, err) |
| 112 | +} |
| 113 | + |
| 114 | +func tearDownCSV(t *testing.T) { |
| 115 | + file.TearDownDB(t, sqlxdb) |
| 116 | + |
| 117 | + err := os.RemoveAll(file.CSVTestConfig.OutputDir) |
| 118 | + require.NoError(t, err) |
| 119 | + |
| 120 | + if err := os.Remove(file.CSVTestConfig.WatchedAddressesFilePath); !errors.Is(err, os.ErrNotExist) { |
| 121 | + require.NoError(t, err) |
| 122 | + } |
| 123 | + |
| 124 | + err = sqlxdb.Close() |
| 125 | + require.NoError(t, err) |
| 126 | +} |
| 127 | + |
| 128 | +func TestCSVFileIndexerLegacy(t *testing.T) { |
| 129 | + t.Run("Publish and index header IPLDs", func(t *testing.T) { |
| 130 | + setupCSVLegacy(t) |
| 131 | + dumpCSVFileData(t) |
| 132 | + defer tearDownCSV(t) |
| 133 | + testLegacyPublishAndIndexHeaderIPLDs(t) |
| 134 | + }) |
| 135 | +} |
0 commit comments