(VDB-925) Add hashed address to address table

This commit is contained in:
Gabe Laughlin 2019-10-23 13:14:49 -05:00
parent 184603bf49
commit 2dc8ace69b
No known key found for this signature in database
GPG Key ID: BCB00D9B64310AB5
4 changed files with 21 additions and 13 deletions

View File

@ -3,6 +3,7 @@ CREATE TABLE public.addresses
(
id SERIAL PRIMARY KEY,
address character varying(42),
hashed_address character varying(66),
UNIQUE (address)
);

View File

@ -26,7 +26,8 @@ SET default_with_oids = false;
CREATE TABLE public.addresses (
id integer NOT NULL,
address character varying(42)
address character varying(42),
hashed_address character varying(66)
);

View File

@ -32,12 +32,12 @@ package repository
import (
"github.com/ethereum/go-ethereum/common"
"github.com/jmoiron/sqlx"
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
const getOrCreateAddressQuery = `WITH addressId AS (
INSERT INTO addresses (address) VALUES ($1) ON CONFLICT DO NOTHING RETURNING id
INSERT INTO addresses (address, hashed_address) VALUES ($1, $2) ON CONFLICT DO NOTHING RETURNING id
)
SELECT id FROM addresses WHERE address = $1
UNION
@ -45,18 +45,20 @@ const getOrCreateAddressQuery = `WITH addressId AS (
func GetOrCreateAddress(db *postgres.DB, address string) (int64, error) {
checksumAddress := getChecksumAddress(address)
hashedAddress := utils.HexToKeccak256Hash(checksumAddress).Hex()
var addressId int64
getOrCreateErr := db.Get(&addressId, getOrCreateAddressQuery, checksumAddress)
getOrCreateErr := db.Get(&addressId, getOrCreateAddressQuery, checksumAddress, hashedAddress)
return addressId, getOrCreateErr
}
func GetOrCreateAddressInTransaction(tx *sqlx.Tx, address string) (int64, error) {
checksumAddress := getChecksumAddress(address)
hashedAddress := utils.HexToKeccak256Hash(checksumAddress).Hex()
var addressId int64
getOrCreateErr := tx.Get(&addressId, getOrCreateAddressQuery, checksumAddress)
getOrCreateErr := tx.Get(&addressId, getOrCreateAddressQuery, checksumAddress, hashedAddress)
return addressId, getOrCreateErr
}

View File

@ -18,6 +18,7 @@ package repository_test
import (
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
"strings"
"github.com/jmoiron/sqlx"
@ -46,6 +47,7 @@ var _ = Describe("address lookup", func() {
type dbAddress struct {
Id int64
Address string
HashedAddress string `db:"hashed_address"`
}
Describe("GetOrCreateAddress", func() {
@ -54,9 +56,10 @@ var _ = Describe("address lookup", func() {
Expect(createErr).NotTo(HaveOccurred())
var actualAddress dbAddress
getErr := db.Get(&actualAddress, `SELECT id, address FROM public.addresses LIMIT 1`)
getErr := db.Get(&actualAddress, `SELECT id, address, hashed_address FROM public.addresses LIMIT 1`)
Expect(getErr).NotTo(HaveOccurred())
expectedAddress := dbAddress{Id: addressId, Address: address}
hashedAddress := utils.HexToKeccak256Hash(address).Hex()
expectedAddress := dbAddress{Id: addressId, Address: address, HashedAddress: hashedAddress}
Expect(actualAddress).To(Equal(expectedAddress))
})
@ -116,9 +119,10 @@ var _ = Describe("address lookup", func() {
Expect(commitErr).NotTo(HaveOccurred())
var actualAddress dbAddress
getErr := db.Get(&actualAddress, `SELECT id, address FROM public.addresses LIMIT 1`)
getErr := db.Get(&actualAddress, `SELECT id, address, hashed_address FROM public.addresses LIMIT 1`)
Expect(getErr).NotTo(HaveOccurred())
expectedAddress := dbAddress{Id: addressId, Address: address}
hashedAddress := utils.HexToKeccak256Hash(address).Hex()
expectedAddress := dbAddress{Id: addressId, Address: address, HashedAddress: hashedAddress}
Expect(actualAddress).To(Equal(expectedAddress))
})