Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit f6ab938

Browse files
Address repo updates (#134)
* Factor out get or create address into one sql string * Factor out getChecksumAddress method in address repo * Update address repo methods to not need a receiver * Move address repository to libraries/shared
1 parent 0310431 commit f6ab938

File tree

8 files changed

+115
-110
lines changed

8 files changed

+115
-110
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// VulcanizeDB
2+
// Copyright © 2019 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+
// This program is free software: you can redistribute it and/or modify
18+
// it under the terms of the GNU Affero General Public License as published by
19+
// the Free Software Foundation, either version 3 of the License, or
20+
// (at your option) any later version.
21+
22+
// This program is distributed in the hope that it will be useful,
23+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
// GNU Affero General Public License for more details.
26+
27+
// You should have received a copy of the GNU Affero General Public License
28+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
29+
30+
package repository
31+
32+
import (
33+
"github.com/ethereum/go-ethereum/common"
34+
"github.com/jmoiron/sqlx"
35+
36+
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
37+
)
38+
39+
const getOrCreateAddressQuery = `WITH addressId AS (
40+
INSERT INTO addresses (address) VALUES ($1) ON CONFLICT DO NOTHING RETURNING id
41+
)
42+
SELECT id FROM addresses WHERE address = $1
43+
UNION
44+
SELECT id FROM addressId`
45+
46+
func GetOrCreateAddress(db *postgres.DB, address string) (int64, error) {
47+
checksumAddress := getChecksumAddress(address)
48+
49+
var addressId int64
50+
getOrCreateErr := db.Get(&addressId, getOrCreateAddressQuery, checksumAddress)
51+
52+
return addressId, getOrCreateErr
53+
}
54+
55+
func GetOrCreateAddressInTransaction(tx *sqlx.Tx, address string) (int64, error) {
56+
checksumAddress := getChecksumAddress(address)
57+
58+
var addressId int64
59+
getOrCreateErr := tx.Get(&addressId, getOrCreateAddressQuery, checksumAddress)
60+
61+
return addressId, getOrCreateErr
62+
}
63+
64+
func GetAddressById(db *postgres.DB, id int64) (string, error) {
65+
var address string
66+
getErr := db.Get(&address, `SELECT address FROM public.addresses WHERE id = $1`, id)
67+
return address, getErr
68+
}
69+
70+
func getChecksumAddress(address string) string {
71+
stringAddressToCommonAddress := common.HexToAddress(address)
72+
return stringAddressToCommonAddress.Hex()
73+
}

pkg/datastore/postgres/repositories/address_repository_test.go renamed to libraries/shared/repository/address_repository_test.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
// VulcanizeDB
22
// Copyright © 2019 Vulcanize
3-
3+
//
44
// This program is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU Affero General Public License as published by
66
// the Free Software Foundation, either version 3 of the License, or
77
// (at your option) any later version.
8-
8+
//
99
// This program is distributed in the hope that it will be useful,
1010
// but WITHOUT ANY WARRANTY; without even the implied warranty of
1111
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1212
// GNU Affero General Public License for more details.
13-
13+
//
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package repositories_test
17+
package repository_test
1818

1919
import (
20+
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
2021
"strings"
2122

2223
"github.com/jmoiron/sqlx"
2324
. "github.com/onsi/ginkgo"
2425
. "github.com/onsi/gomega"
2526

2627
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
27-
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
2828
"github.com/vulcanize/vulcanizedb/pkg/fakes"
2929
"github.com/vulcanize/vulcanizedb/test_config"
3030
)
3131

3232
var _ = Describe("address lookup", func() {
3333
var (
3434
db *postgres.DB
35-
repo repositories.AddressRepository
3635
address = fakes.FakeAddress.Hex()
3736
)
3837
BeforeEach(func() {
3938
db = test_config.NewTestDB(test_config.NewTestNode())
4039
test_config.CleanTestDB(db)
41-
repo = repositories.AddressRepository{}
4240
})
4341

4442
AfterEach(func() {
@@ -52,7 +50,7 @@ var _ = Describe("address lookup", func() {
5250

5351
Describe("GetOrCreateAddress", func() {
5452
It("creates an address record", func() {
55-
addressId, createErr := repo.GetOrCreateAddress(db, address)
53+
addressId, createErr := repository.GetOrCreateAddress(db, address)
5654
Expect(createErr).NotTo(HaveOccurred())
5755

5856
var actualAddress dbAddress
@@ -63,10 +61,10 @@ var _ = Describe("address lookup", func() {
6361
})
6462

6563
It("returns the existing record id if the address already exists", func() {
66-
createId, createErr := repo.GetOrCreateAddress(db, address)
64+
createId, createErr := repository.GetOrCreateAddress(db, address)
6765
Expect(createErr).NotTo(HaveOccurred())
6866

69-
getId, getErr := repo.GetOrCreateAddress(db, address)
67+
getId, getErr := repository.GetOrCreateAddress(db, address)
7068
Expect(getErr).NotTo(HaveOccurred())
7169

7270
var addressCount int
@@ -78,20 +76,20 @@ var _ = Describe("address lookup", func() {
7876

7977
It("gets upper-cased addresses", func() {
8078
upperAddress := strings.ToUpper(address)
81-
upperAddressId, createErr := repo.GetOrCreateAddress(db, upperAddress)
79+
upperAddressId, createErr := repository.GetOrCreateAddress(db, upperAddress)
8280
Expect(createErr).NotTo(HaveOccurred())
8381

84-
mixedCaseAddressId, getErr := repo.GetOrCreateAddress(db, address)
82+
mixedCaseAddressId, getErr := repository.GetOrCreateAddress(db, address)
8583
Expect(getErr).NotTo(HaveOccurred())
8684
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
8785
})
8886

8987
It("gets lower-cased addresses", func() {
9088
lowerAddress := strings.ToLower(address)
91-
upperAddressId, createErr := repo.GetOrCreateAddress(db, lowerAddress)
89+
upperAddressId, createErr := repository.GetOrCreateAddress(db, lowerAddress)
9290
Expect(createErr).NotTo(HaveOccurred())
9391

94-
mixedCaseAddressId, getErr := repo.GetOrCreateAddress(db, address)
92+
mixedCaseAddressId, getErr := repository.GetOrCreateAddress(db, address)
9593
Expect(getErr).NotTo(HaveOccurred())
9694
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
9795
})
@@ -112,7 +110,7 @@ var _ = Describe("address lookup", func() {
112110
})
113111

114112
It("creates an address record", func() {
115-
addressId, createErr := repo.GetOrCreateAddressInTransaction(tx, address)
113+
addressId, createErr := repository.GetOrCreateAddressInTransaction(tx, address)
116114
Expect(createErr).NotTo(HaveOccurred())
117115
commitErr := tx.Commit()
118116
Expect(commitErr).NotTo(HaveOccurred())
@@ -125,10 +123,10 @@ var _ = Describe("address lookup", func() {
125123
})
126124

127125
It("returns the existing record id if the address already exists", func() {
128-
_, createErr := repo.GetOrCreateAddressInTransaction(tx, address)
126+
_, createErr := repository.GetOrCreateAddressInTransaction(tx, address)
129127
Expect(createErr).NotTo(HaveOccurred())
130128

131-
_, getErr := repo.GetOrCreateAddressInTransaction(tx, address)
129+
_, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
132130
Expect(getErr).NotTo(HaveOccurred())
133131
tx.Commit()
134132

@@ -139,10 +137,10 @@ var _ = Describe("address lookup", func() {
139137

140138
It("gets upper-cased addresses", func() {
141139
upperAddress := strings.ToUpper(address)
142-
upperAddressId, createErr := repo.GetOrCreateAddressInTransaction(tx, upperAddress)
140+
upperAddressId, createErr := repository.GetOrCreateAddressInTransaction(tx, upperAddress)
143141
Expect(createErr).NotTo(HaveOccurred())
144142

145-
mixedCaseAddressId, getErr := repo.GetOrCreateAddressInTransaction(tx, address)
143+
mixedCaseAddressId, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
146144
Expect(getErr).NotTo(HaveOccurred())
147145
tx.Commit()
148146

@@ -151,10 +149,10 @@ var _ = Describe("address lookup", func() {
151149

152150
It("gets lower-cased addresses", func() {
153151
lowerAddress := strings.ToLower(address)
154-
upperAddressId, createErr := repo.GetOrCreateAddressInTransaction(tx, lowerAddress)
152+
upperAddressId, createErr := repository.GetOrCreateAddressInTransaction(tx, lowerAddress)
155153
Expect(createErr).NotTo(HaveOccurred())
156154

157-
mixedCaseAddressId, getErr := repo.GetOrCreateAddressInTransaction(tx, address)
155+
mixedCaseAddressId, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
158156
Expect(getErr).NotTo(HaveOccurred())
159157
tx.Commit()
160158

@@ -164,16 +162,16 @@ var _ = Describe("address lookup", func() {
164162

165163
Describe("GetAddressById", func() {
166164
It("gets and address by it's id", func() {
167-
addressId, createErr := repo.GetOrCreateAddress(db, address)
165+
addressId, createErr := repository.GetOrCreateAddress(db, address)
168166
Expect(createErr).NotTo(HaveOccurred())
169167

170-
actualAddress, getErr := repo.GetAddressById(db, addressId)
168+
actualAddress, getErr := repository.GetAddressById(db, addressId)
171169
Expect(getErr).NotTo(HaveOccurred())
172170
Expect(actualAddress).To(Equal(address))
173171
})
174172

175173
It("returns an error if the id doesn't exist", func() {
176-
_, getErr := repo.GetAddressById(db, 0)
174+
_, getErr := repository.GetAddressById(db, 0)
177175
Expect(getErr).To(HaveOccurred())
178176
Expect(getErr).To(MatchError("sql: no rows in result set"))
179177
})

pkg/contract_watcher/full/retriever/block_retriever.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package retriever
1818

1919
import (
2020
"database/sql"
21+
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
2122
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
22-
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
2323
)
2424

2525
// Block retriever is used to retrieve the first block for a given contract and the most recent block
@@ -55,7 +55,7 @@ func (r *blockRetriever) RetrieveFirstBlock(contractAddr string) (int64, error)
5555
// For some contracts the contract creation transaction receipt doesn't have the contract address so this doesn't work (e.g. Sai)
5656
func (r *blockRetriever) retrieveFirstBlockFromReceipts(contractAddr string) (int64, error) {
5757
var firstBlock int64
58-
addressId, getAddressErr := addressRepository().GetOrCreateAddress(r.db, contractAddr)
58+
addressId, getAddressErr := repository.GetOrCreateAddress(r.db, contractAddr)
5959
if getAddressErr != nil {
6060
return firstBlock, getAddressErr
6161
}
@@ -72,10 +72,6 @@ func (r *blockRetriever) retrieveFirstBlockFromReceipts(contractAddr string) (in
7272
return firstBlock, err
7373
}
7474

75-
func addressRepository() repositories.AddressRepository {
76-
return repositories.AddressRepository{}
77-
}
78-
7975
// In which case this servers as a heuristic to find the first block by finding the first contract event log
8076
func (r *blockRetriever) retrieveFirstBlockFromLogs(contractAddr string) (int64, error) {
8177
var firstBlock int

pkg/datastore/postgres/repositories/address_repository.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

pkg/datastore/postgres/repositories/full_sync_receipt_repository.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"database/sql"
2121
"github.com/jmoiron/sqlx"
2222
"github.com/sirupsen/logrus"
23+
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
2324
"github.com/vulcanize/vulcanizedb/pkg/core"
2425
"github.com/vulcanize/vulcanizedb/pkg/datastore"
2526
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
@@ -84,7 +85,7 @@ func createLogs(logs []core.FullSyncLog, receiptId int64, tx *sqlx.Tx) error {
8485

8586
func (FullSyncReceiptRepository) CreateFullSyncReceiptInTx(blockId int64, receipt core.Receipt, tx *sqlx.Tx) (int64, error) {
8687
var receiptId int64
87-
addressId, getAddressErr := AddressRepository{}.GetOrCreateAddressInTransaction(tx, receipt.ContractAddress)
88+
addressId, getAddressErr := repository.GetOrCreateAddressInTransaction(tx, receipt.ContractAddress)
8889
if getAddressErr != nil {
8990
logrus.Error("createReceipt: Error getting address id: ", getAddressErr)
9091
return receiptId, getAddressErr

0 commit comments

Comments
 (0)