From 2dc8ace69b44eef653634c2d0182833c2b8ff9fe Mon Sep 17 00:00:00 2001 From: Gabe Laughlin Date: Wed, 23 Oct 2019 13:14:49 -0500 Subject: [PATCH] (VDB-925) Add hashed address to address table --- db/migrations/00013_add_address_table.sql | 5 +++-- db/schema.sql | 3 ++- .../shared/repository/address_repository.go | 10 ++++++---- .../shared/repository/address_repository_test.go | 16 ++++++++++------ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/db/migrations/00013_add_address_table.sql b/db/migrations/00013_add_address_table.sql index 22ee7b3e..6866f6e1 100644 --- a/db/migrations/00013_add_address_table.sql +++ b/db/migrations/00013_add_address_table.sql @@ -1,8 +1,9 @@ -- +goose Up CREATE TABLE public.addresses ( - id SERIAL PRIMARY KEY, - address character varying(42), + id SERIAL PRIMARY KEY, + address character varying(42), + hashed_address character varying(66), UNIQUE (address) ); diff --git a/db/schema.sql b/db/schema.sql index 18ceaf84..ebd2988d 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -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) ); diff --git a/libraries/shared/repository/address_repository.go b/libraries/shared/repository/address_repository.go index 40f064dc..e2c0bf50 100644 --- a/libraries/shared/repository/address_repository.go +++ b/libraries/shared/repository/address_repository.go @@ -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 } diff --git a/libraries/shared/repository/address_repository_test.go b/libraries/shared/repository/address_repository_test.go index aa81aaf4..ad13bbd1 100644 --- a/libraries/shared/repository/address_repository_test.go +++ b/libraries/shared/repository/address_repository_test.go @@ -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" @@ -44,8 +45,9 @@ var _ = Describe("address lookup", func() { }) type dbAddress struct { - Id int64 - Address string + 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)) })