forked from cerc-io/ipld-eth-server
(VDB-925) Add hashed address to address table
This commit is contained in:
parent
184603bf49
commit
2dc8ace69b
@ -1,8 +1,9 @@
|
|||||||
-- +goose Up
|
-- +goose Up
|
||||||
CREATE TABLE public.addresses
|
CREATE TABLE public.addresses
|
||||||
(
|
(
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
address character varying(42),
|
address character varying(42),
|
||||||
|
hashed_address character varying(66),
|
||||||
UNIQUE (address)
|
UNIQUE (address)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -26,7 +26,8 @@ SET default_with_oids = false;
|
|||||||
|
|
||||||
CREATE TABLE public.addresses (
|
CREATE TABLE public.addresses (
|
||||||
id integer NOT NULL,
|
id integer NOT NULL,
|
||||||
address character varying(42)
|
address character varying(42),
|
||||||
|
hashed_address character varying(66)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,12 +32,12 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
const getOrCreateAddressQuery = `WITH addressId AS (
|
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
|
SELECT id FROM addresses WHERE address = $1
|
||||||
UNION
|
UNION
|
||||||
@ -45,18 +45,20 @@ const getOrCreateAddressQuery = `WITH addressId AS (
|
|||||||
|
|
||||||
func GetOrCreateAddress(db *postgres.DB, address string) (int64, error) {
|
func GetOrCreateAddress(db *postgres.DB, address string) (int64, error) {
|
||||||
checksumAddress := getChecksumAddress(address)
|
checksumAddress := getChecksumAddress(address)
|
||||||
|
hashedAddress := utils.HexToKeccak256Hash(checksumAddress).Hex()
|
||||||
|
|
||||||
var addressId int64
|
var addressId int64
|
||||||
getOrCreateErr := db.Get(&addressId, getOrCreateAddressQuery, checksumAddress)
|
getOrCreateErr := db.Get(&addressId, getOrCreateAddressQuery, checksumAddress, hashedAddress)
|
||||||
|
|
||||||
return addressId, getOrCreateErr
|
return addressId, getOrCreateErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOrCreateAddressInTransaction(tx *sqlx.Tx, address string) (int64, error) {
|
func GetOrCreateAddressInTransaction(tx *sqlx.Tx, address string) (int64, error) {
|
||||||
checksumAddress := getChecksumAddress(address)
|
checksumAddress := getChecksumAddress(address)
|
||||||
|
hashedAddress := utils.HexToKeccak256Hash(checksumAddress).Hex()
|
||||||
|
|
||||||
var addressId int64
|
var addressId int64
|
||||||
getOrCreateErr := tx.Get(&addressId, getOrCreateAddressQuery, checksumAddress)
|
getOrCreateErr := tx.Get(&addressId, getOrCreateAddressQuery, checksumAddress, hashedAddress)
|
||||||
|
|
||||||
return addressId, getOrCreateErr
|
return addressId, getOrCreateErr
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package repository_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
|
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
|
||||||
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
@ -44,8 +45,9 @@ var _ = Describe("address lookup", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
type dbAddress struct {
|
type dbAddress struct {
|
||||||
Id int64
|
Id int64
|
||||||
Address string
|
Address string
|
||||||
|
HashedAddress string `db:"hashed_address"`
|
||||||
}
|
}
|
||||||
|
|
||||||
Describe("GetOrCreateAddress", func() {
|
Describe("GetOrCreateAddress", func() {
|
||||||
@ -54,9 +56,10 @@ var _ = Describe("address lookup", func() {
|
|||||||
Expect(createErr).NotTo(HaveOccurred())
|
Expect(createErr).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var actualAddress dbAddress
|
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())
|
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))
|
Expect(actualAddress).To(Equal(expectedAddress))
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -116,9 +119,10 @@ var _ = Describe("address lookup", func() {
|
|||||||
Expect(commitErr).NotTo(HaveOccurred())
|
Expect(commitErr).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var actualAddress dbAddress
|
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())
|
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))
|
Expect(actualAddress).To(Equal(expectedAddress))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user