This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
/
plugin.go
165 lines (144 loc) · 5.59 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// VulcanizeDB
// Copyright © 2019 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package integration_tests
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"plugin"
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
"github.com/vulcanize/vulcanizedb/libraries/shared/watcher"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
p2 "github.com/vulcanize/vulcanizedb/pkg/plugin"
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
"github.com/vulcanize/account_transformers/transformers/account/shared/test_helpers"
)
var accountConfig = config.Plugin{
Home: "github.com/vulcanize/account_transformers",
Transformers: map[string]config.Transformer{
"account": {
Path: "transformers/account/light/initializer",
Type: config.EthContract,
MigrationPath: "db/migrations",
MigrationRank: 0,
RepositoryPath: "github.com/vulcanize/account_transformers",
},
},
FileName: "testAccountTransformer",
FilePath: "$GOPATH/src/github.com/vulcanize/account_transformers/transformers/integration_tests/plugin",
Save: false,
}
var dbConfig = config.Database{
Hostname: "localhost",
Port: 5432,
Name: "vulcanize_private",
}
type Exporter interface {
Export() ([]transformer.EventTransformerInitializer, []transformer.StorageTransformerInitializer, []transformer.ContractTransformerInitializer)
}
var _ = Describe("Plugin test", func() {
var g p2.Generator
var goPath, soPath string
var err error
var bc core.BlockChain
var db *postgres.DB
var hr repositories.HeaderRepository
Describe("Account Transformer Plugin", func() {
BeforeEach(func() {
goPath, soPath, err = accountConfig.GetPluginPaths()
Expect(err).ToNot(HaveOccurred())
g, err = p2.NewGenerator(accountConfig, dbConfig)
Expect(err).ToNot(HaveOccurred())
err = g.GenerateExporterPlugin()
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
err := helpers.ClearFiles(goPath, soPath)
Expect(err).ToNot(HaveOccurred())
})
Describe("GenerateTransformerPlugin", func() {
It("It bundles the specified TransformerInitializers into a Exporter object and creates .so", func() {
plug, err := plugin.Open(soPath)
Expect(err).ToNot(HaveOccurred())
symExporter, err := plug.Lookup("Exporter")
Expect(err).ToNot(HaveOccurred())
exporter, ok := symExporter.(Exporter)
Expect(ok).To(Equal(true))
event, storage, contract := exporter.Export()
Expect(len(event)).To(Equal(0))
Expect(len(storage)).To(Equal(0))
Expect(len(contract)).To(Equal(1))
})
It("Loads our generated Exporter and uses it to import an arbitrary set of TransformerInitializers that we can execute over", func() {
db, bc = test_helpers.SetupDBandBC()
defer test_helpers.TearDown(db)
hr = repositories.NewHeaderRepository(db)
header1, err := bc.GetHeaderByNumber(6791668)
Expect(err).ToNot(HaveOccurred())
_, err = hr.CreateOrUpdateHeader(header1)
Expect(err).ToNot(HaveOccurred())
plug, err := plugin.Open(soPath)
Expect(err).ToNot(HaveOccurred())
symExporter, err := plug.Lookup("Exporter")
Expect(err).ToNot(HaveOccurred())
exporter, ok := symExporter.(Exporter)
Expect(ok).To(Equal(true))
_, _, contractInitializers := exporter.Export()
w := watcher.NewContractWatcher(db, bc)
w.AddTransformers(contractInitializers)
err = w.Execute()
Expect(err).ToNot(HaveOccurred())
type coinBalanceRecord struct {
Address []byte `db:"address_hash"`
BlockNumber int64 `db:"block_number"`
Value string
}
var coinRecords []coinBalanceRecord
rows, err := db.Queryx(`SELECT * FROM accounts.address_coin_balances WHERE block_number = $1`, 6791668)
Expect(err).ToNot(HaveOccurred())
for rows.Next() {
record := new(coinBalanceRecord)
err = rows.StructScan(record)
Expect(err).ToNot(HaveOccurred())
coinRecords = append(coinRecords, *record)
}
err = rows.Err()
Expect(err).ToNot(HaveOccurred())
rows.Close()
type tokenBalanceRecord struct {
Address []byte `db:"address_hash"`
BlockNumber int64 `db:"block_number"`
ContractAddress []byte `db:"token_contract_address_hash"`
Value string
}
var tokenRecords []tokenBalanceRecord
rows, err = db.Queryx(`SELECT * FROM accounts.address_token_balances WHERE block_number = $1`, 6791668)
Expect(err).ToNot(HaveOccurred())
for rows.Next() {
record := new(tokenBalanceRecord)
err = rows.StructScan(record)
Expect(err).ToNot(HaveOccurred())
tokenRecords = append(tokenRecords, *record)
}
err = rows.Err()
Expect(err).ToNot(HaveOccurred())
rows.Close()
// This init at the end is done to send a quit message to the getBalanceRecord goroutine
err = w.Transformers[0].Init()
Expect(err).ToNot(HaveOccurred())
})
})
})
})