forked from cerc-io/ipld-eth-server
Merge pull request #162 from vulcanize/vdb-925-hashed-address
(VDB-925) Add hashed address to address table
This commit is contained in:
commit
58a5f06094
@ -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)
|
||||
);
|
||||
|
||||
|
@ -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)
|
||||
);
|
||||
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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))
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user