diff --git a/cmd/parseStorageDiffs.go b/cmd/parseStorageDiffs.go index f826d3cba..d1aee3985 100644 --- a/cmd/parseStorageDiffs.go +++ b/cmd/parseStorageDiffs.go @@ -58,6 +58,7 @@ func parseStorageDiffs() { // TODO: configure transformers watcher := shared.NewStorageWatcher(tailer, db) watcher.AddTransformers([]storage.TransformerInitializer{ + transformers.GetCatStorageTransformer().NewTransformer, transformers.GetPitStorageTransformer().NewTransformer, transformers.GetVatStorageTransformer().NewTransformer, transformers.GetVowStorageTransformer().NewTransformer, diff --git a/db/migrations/20190215160236_create_cat_storage_tables.sql b/db/migrations/20190215160236_create_cat_storage_tables.sql new file mode 100644 index 000000000..d613b459c --- /dev/null +++ b/db/migrations/20190215160236_create_cat_storage_tables.sql @@ -0,0 +1,106 @@ +-- +goose Up +CREATE TABLE maker.cat_nflip ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + nflip NUMERIC NOT NULL +); + +CREATE TABLE maker.cat_live ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + live NUMERIC NOT NULL +); + +CREATE TABLE maker.cat_vat ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + vat TEXT +); + +CREATE TABLE maker.cat_pit ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + pit TEXT +); + +CREATE TABLE maker.cat_vow ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + vow TEXT +); + +CREATE TABLE maker.cat_ilk_flip ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + ilk TEXT, + flip TEXT +); + +CREATE TABLE maker.cat_ilk_chop ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + ilk TEXT, + chop NUMERIC NOT NULL +); + +CREATE TABLE maker.cat_ilk_lump ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + ilk TEXT, + lump NUMERIC NOT NULL +); + +CREATE TABLE maker.cat_flip_ilk ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + flip NUMERIC NOT NULL, + ilk TEXT +); + +CREATE TABLE maker.cat_flip_urn ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + flip NUMERIC NOT NULL, + urn TEXT +); + +CREATE TABLE maker.cat_flip_ink ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + flip NUMERIC NOT NULL, + ink NUMERIC NOT NULL +); + +CREATE TABLE maker.cat_flip_tab ( + id SERIAL PRIMARY KEY, + block_number BIGINT, + block_hash TEXT, + flip NUMERIC NOT NULL, + tab NUMERIC NOT NULL +); + + +-- +goose Down +DROP TABLE maker.cat_nflip; +DROP TABLE maker.cat_live; +DROP TABLE maker.cat_vat; +DROP TABLE maker.cat_pit; +DROP TABLE maker.cat_vow; +DROP TABLE maker.cat_ilk_flip; +DROP TABLE maker.cat_ilk_chop; +DROP TABLE maker.cat_ilk_lump; +DROP TABLE maker.cat_flip_ilk; +DROP TABLE maker.cat_flip_urn; +DROP TABLE maker.cat_flip_ink; +DROP TABLE maker.cat_flip_tab; \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index 46f371181..6cbdcd762 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -2,8 +2,8 @@ -- PostgreSQL database dump -- --- Dumped from database version 10.4 --- Dumped by pg_dump version 10.4 +-- Dumped from database version 10.5 +-- Dumped by pg_dump version 10.5 SET statement_timeout = 0; SET lock_timeout = 0; @@ -204,6 +204,397 @@ CREATE SEQUENCE maker.cat_file_pit_vow_id_seq ALTER SEQUENCE maker.cat_file_pit_vow_id_seq OWNED BY maker.cat_file_pit_vow.id; +-- +-- Name: cat_flip_ilk; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_flip_ilk ( + id integer NOT NULL, + block_number bigint, + block_hash text, + flip numeric NOT NULL, + ilk text +); + + +-- +-- Name: cat_flip_ilk_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_flip_ilk_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_flip_ilk_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_flip_ilk_id_seq OWNED BY maker.cat_flip_ilk.id; + + +-- +-- Name: cat_flip_ink; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_flip_ink ( + id integer NOT NULL, + block_number bigint, + block_hash text, + flip numeric NOT NULL, + ink numeric NOT NULL +); + + +-- +-- Name: cat_flip_ink_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_flip_ink_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_flip_ink_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_flip_ink_id_seq OWNED BY maker.cat_flip_ink.id; + + +-- +-- Name: cat_flip_tab; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_flip_tab ( + id integer NOT NULL, + block_number bigint, + block_hash text, + flip numeric NOT NULL, + tab numeric NOT NULL +); + + +-- +-- Name: cat_flip_tab_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_flip_tab_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_flip_tab_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_flip_tab_id_seq OWNED BY maker.cat_flip_tab.id; + + +-- +-- Name: cat_flip_urn; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_flip_urn ( + id integer NOT NULL, + block_number bigint, + block_hash text, + flip numeric NOT NULL, + urn text +); + + +-- +-- Name: cat_flip_urn_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_flip_urn_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_flip_urn_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_flip_urn_id_seq OWNED BY maker.cat_flip_urn.id; + + +-- +-- Name: cat_ilk_chop; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_ilk_chop ( + id integer NOT NULL, + block_number bigint, + block_hash text, + ilk text, + chop numeric NOT NULL +); + + +-- +-- Name: cat_ilk_chop_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_ilk_chop_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_ilk_chop_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_ilk_chop_id_seq OWNED BY maker.cat_ilk_chop.id; + + +-- +-- Name: cat_ilk_flip; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_ilk_flip ( + id integer NOT NULL, + block_number bigint, + block_hash text, + ilk text, + flip text +); + + +-- +-- Name: cat_ilk_flip_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_ilk_flip_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_ilk_flip_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_ilk_flip_id_seq OWNED BY maker.cat_ilk_flip.id; + + +-- +-- Name: cat_ilk_lump; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_ilk_lump ( + id integer NOT NULL, + block_number bigint, + block_hash text, + ilk text, + lump numeric NOT NULL +); + + +-- +-- Name: cat_ilk_lump_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_ilk_lump_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_ilk_lump_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_ilk_lump_id_seq OWNED BY maker.cat_ilk_lump.id; + + +-- +-- Name: cat_live; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_live ( + id integer NOT NULL, + block_number bigint, + block_hash text, + live numeric NOT NULL +); + + +-- +-- Name: cat_live_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_live_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_live_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_live_id_seq OWNED BY maker.cat_live.id; + + +-- +-- Name: cat_nflip; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_nflip ( + id integer NOT NULL, + block_number bigint, + block_hash text, + nflip numeric NOT NULL +); + + +-- +-- Name: cat_nflip_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_nflip_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_nflip_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_nflip_id_seq OWNED BY maker.cat_nflip.id; + + +-- +-- Name: cat_pit; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_pit ( + id integer NOT NULL, + block_number bigint, + block_hash text, + pit text +); + + +-- +-- Name: cat_pit_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_pit_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_pit_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_pit_id_seq OWNED BY maker.cat_pit.id; + + +-- +-- Name: cat_vat; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_vat ( + id integer NOT NULL, + block_number bigint, + block_hash text, + vat text +); + + +-- +-- Name: cat_vat_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_vat_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_vat_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_vat_id_seq OWNED BY maker.cat_vat.id; + + +-- +-- Name: cat_vow; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.cat_vow ( + id integer NOT NULL, + block_number bigint, + block_hash text, + vow text +); + + +-- +-- Name: cat_vow_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.cat_vow_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cat_vow_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.cat_vow_id_seq OWNED BY maker.cat_vow.id; + + -- -- Name: deal; Type: TABLE; Schema: maker; Owner: - -- @@ -2440,6 +2831,90 @@ ALTER TABLE ONLY maker.cat_file_flip ALTER COLUMN id SET DEFAULT nextval('maker. ALTER TABLE ONLY maker.cat_file_pit_vow ALTER COLUMN id SET DEFAULT nextval('maker.cat_file_pit_vow_id_seq'::regclass); +-- +-- Name: cat_flip_ilk id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_flip_ilk ALTER COLUMN id SET DEFAULT nextval('maker.cat_flip_ilk_id_seq'::regclass); + + +-- +-- Name: cat_flip_ink id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_flip_ink ALTER COLUMN id SET DEFAULT nextval('maker.cat_flip_ink_id_seq'::regclass); + + +-- +-- Name: cat_flip_tab id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_flip_tab ALTER COLUMN id SET DEFAULT nextval('maker.cat_flip_tab_id_seq'::regclass); + + +-- +-- Name: cat_flip_urn id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_flip_urn ALTER COLUMN id SET DEFAULT nextval('maker.cat_flip_urn_id_seq'::regclass); + + +-- +-- Name: cat_ilk_chop id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_ilk_chop ALTER COLUMN id SET DEFAULT nextval('maker.cat_ilk_chop_id_seq'::regclass); + + +-- +-- Name: cat_ilk_flip id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_ilk_flip ALTER COLUMN id SET DEFAULT nextval('maker.cat_ilk_flip_id_seq'::regclass); + + +-- +-- Name: cat_ilk_lump id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_ilk_lump ALTER COLUMN id SET DEFAULT nextval('maker.cat_ilk_lump_id_seq'::regclass); + + +-- +-- Name: cat_live id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_live ALTER COLUMN id SET DEFAULT nextval('maker.cat_live_id_seq'::regclass); + + +-- +-- Name: cat_nflip id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_nflip ALTER COLUMN id SET DEFAULT nextval('maker.cat_nflip_id_seq'::regclass); + + +-- +-- Name: cat_pit id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_pit ALTER COLUMN id SET DEFAULT nextval('maker.cat_pit_id_seq'::regclass); + + +-- +-- Name: cat_vat id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_vat ALTER COLUMN id SET DEFAULT nextval('maker.cat_vat_id_seq'::regclass); + + +-- +-- Name: cat_vow id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_vow ALTER COLUMN id SET DEFAULT nextval('maker.cat_vow_id_seq'::regclass); + + -- -- Name: deal id; Type: DEFAULT; Schema: maker; Owner: - -- @@ -2938,6 +3413,102 @@ ALTER TABLE ONLY maker.cat_file_pit_vow ADD CONSTRAINT cat_file_pit_vow_pkey PRIMARY KEY (id); +-- +-- Name: cat_flip_ilk cat_flip_ilk_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_flip_ilk + ADD CONSTRAINT cat_flip_ilk_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_flip_ink cat_flip_ink_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_flip_ink + ADD CONSTRAINT cat_flip_ink_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_flip_tab cat_flip_tab_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_flip_tab + ADD CONSTRAINT cat_flip_tab_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_flip_urn cat_flip_urn_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_flip_urn + ADD CONSTRAINT cat_flip_urn_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_ilk_chop cat_ilk_chop_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_ilk_chop + ADD CONSTRAINT cat_ilk_chop_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_ilk_flip cat_ilk_flip_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_ilk_flip + ADD CONSTRAINT cat_ilk_flip_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_ilk_lump cat_ilk_lump_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_ilk_lump + ADD CONSTRAINT cat_ilk_lump_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_live cat_live_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_live + ADD CONSTRAINT cat_live_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_nflip cat_nflip_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_nflip + ADD CONSTRAINT cat_nflip_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_pit cat_pit_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_pit + ADD CONSTRAINT cat_pit_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_vat cat_vat_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_vat + ADD CONSTRAINT cat_vat_pkey PRIMARY KEY (id); + + +-- +-- Name: cat_vow cat_vow_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.cat_vow + ADD CONSTRAINT cat_vow_pkey PRIMARY KEY (id); + + -- -- Name: deal deal_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- diff --git a/pkg/transformers/storage_diffs/maker/cat/cat_suite_test.go b/pkg/transformers/storage_diffs/maker/cat/cat_suite_test.go new file mode 100644 index 000000000..98207fa0f --- /dev/null +++ b/pkg/transformers/storage_diffs/maker/cat/cat_suite_test.go @@ -0,0 +1,19 @@ +package cat_test + +import ( + "github.com/sirupsen/logrus" + "io/ioutil" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestCat(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Cat Suite") +} + +var _ = BeforeSuite(func() { + logrus.SetOutput(ioutil.Discard) +}) diff --git a/pkg/transformers/storage_diffs/maker/cat/mappings.go b/pkg/transformers/storage_diffs/maker/cat/mappings.go new file mode 100644 index 000000000..46ade0082 --- /dev/null +++ b/pkg/transformers/storage_diffs/maker/cat/mappings.go @@ -0,0 +1,198 @@ +package cat + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/sirupsen/logrus" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared" + "strconv" +) + +const ( + NFlip = "nflip" + Live = "live" + Vat = "vat" + Pit = "pit" + Vow = "vow" + + IlkFlip = "flip" + IlkChop = "chop" + IlkLump = "lump" + + FlipIlk = "ilk" + FlipUrn = "urn" + FlipInk = "ink" + FlipTab = "tab" +) + +var ( + // wards takes up index 0 + IlksMappingIndex = storage_diffs.IndexOne // bytes32 => flip address; chop (ray), lump (wad) uint256 + FlipsMappingIndex = storage_diffs.IndexTwo // uint256 => ilk, urn bytes32; ink, tab uint256 (both wad) + + NFlipKey = common.HexToHash(storage_diffs.IndexThree) + NFlipMetadata = shared.GetStorageValueMetadata(NFlip, nil, shared.Uint256) + + LiveKey = common.HexToHash(storage_diffs.IndexFour) + LiveMetadata = shared.GetStorageValueMetadata(Live, nil, shared.Uint256) + + VatKey = common.HexToHash(storage_diffs.IndexFive) + VatMetadata = shared.GetStorageValueMetadata(Vat, nil, shared.Address) + + PitKey = common.HexToHash(storage_diffs.IndexSix) + PitMetadata = shared.GetStorageValueMetadata(Pit, nil, shared.Address) + + VowKey = common.HexToHash(storage_diffs.IndexSeven) + VowMetadata = shared.GetStorageValueMetadata(Vow, nil, shared.Address) +) + +type CatMappings struct { + StorageRepository maker.IMakerStorageRepository + mappings map[common.Hash]shared.StorageValueMetadata +} + +func (mappings CatMappings) Lookup(key common.Hash) (shared.StorageValueMetadata, error) { + metadata, ok := mappings.mappings[key] + if !ok { + err := mappings.loadMappings() + if err != nil { + return metadata, err + } + metadata, ok = mappings.mappings[key] + if !ok { + return metadata, shared.ErrStorageKeyNotFound{Key: key.Hex()} + } + } + return metadata, nil +} + +func (mappings *CatMappings) SetDB(db *postgres.DB) { + mappings.StorageRepository.SetDB(db) +} + +func (mappings *CatMappings) loadMappings() error { + mappings.mappings = loadStaticMappings() + ilkErr := mappings.loadIlkKeys() + if ilkErr != nil { + return ilkErr + } + + flipsErr := mappings.loadFlipsKeys() + if flipsErr != nil { + return flipsErr + } + + return nil +} + +func loadStaticMappings() map[common.Hash]shared.StorageValueMetadata { + mappings := make(map[common.Hash]shared.StorageValueMetadata) + mappings[NFlipKey] = NFlipMetadata + mappings[LiveKey] = LiveMetadata + mappings[VatKey] = VatMetadata + mappings[PitKey] = PitMetadata + mappings[VowKey] = VowMetadata + return mappings +} + +// Ilks +func (mappings *CatMappings) loadIlkKeys() error { + ilks, err := mappings.StorageRepository.GetIlks() + if err != nil { + return err + } + for _, ilk := range ilks { + mappings.mappings[getIlkFlipKey(ilk)] = getIlkFlipMetadata(ilk) + mappings.mappings[getIlkChopKey(ilk)] = getIlkChopMetadata(ilk) + mappings.mappings[getIlkLumpKey(ilk)] = getIlkLumpMetadata(ilk) + } + return nil +} + +func getIlkFlipKey(ilk string) common.Hash { + return storage_diffs.GetMapping(IlksMappingIndex, ilk) +} + +func getIlkFlipMetadata(ilk string) shared.StorageValueMetadata { + keys := map[shared.Key]string{shared.Ilk: ilk} + return shared.GetStorageValueMetadata(IlkFlip, keys, shared.Address) +} + +func getIlkChopKey(ilk string) common.Hash { + return storage_diffs.GetIncrementedKey(getIlkFlipKey(ilk), 1) +} + +func getIlkChopMetadata(ilk string) shared.StorageValueMetadata { + keys := map[shared.Key]string{shared.Ilk: ilk} + return shared.GetStorageValueMetadata(IlkChop, keys, shared.Uint256) +} + +func getIlkLumpKey(ilk string) common.Hash { + return storage_diffs.GetIncrementedKey(getIlkFlipKey(ilk), 2) +} + +func getIlkLumpMetadata(ilk string) shared.StorageValueMetadata { + keys := map[shared.Key]string{shared.Ilk: ilk} + return shared.GetStorageValueMetadata(IlkLump, keys, shared.Uint256) +} + +// Flip ID increments each time it happens, so we just need the biggest flip ID from the DB +// and we can interpolate the sequence [0..max]. This makes sure we track all earlier flips, +// even if we've missed events +func (mappings CatMappings) loadFlipsKeys() error { + maxFlip, err := mappings.StorageRepository.GetMaxFlip() + if err != nil { + logrus.Error("loadFlipsKeys: error getting max flip: ", err) + return err + } else if maxFlip == nil { // No flips occurred yet + return nil + } + + last := maxFlip.Int64() + for flip := 0; int64(flip) <= last; flip++ { + flipStr := strconv.Itoa(flip) + mappings.mappings[getFlipIlkKey(flipStr)] = getFlipIlkMetadata(flipStr) + mappings.mappings[getFlipUrnKey(flipStr)] = getFlipUrnMetadata(flipStr) + mappings.mappings[getFlipInkKey(flipStr)] = getFlipInkMetadata(flipStr) + mappings.mappings[getFlipTabKey(flipStr)] = getFlipTabMetadata(flipStr) + } + return nil +} + +func getFlipIlkKey(flip string) common.Hash { + return storage_diffs.GetMapping(FlipsMappingIndex, flip) +} + +func getFlipIlkMetadata(flip string) shared.StorageValueMetadata { + keys := map[shared.Key]string{shared.Flip: flip} + return shared.GetStorageValueMetadata(FlipIlk, keys, shared.Bytes32) +} + +func getFlipUrnKey(flip string) common.Hash { + return storage_diffs.GetIncrementedKey(getFlipIlkKey(flip), 1) +} + +func getFlipUrnMetadata(flip string) shared.StorageValueMetadata { + keys := map[shared.Key]string{shared.Flip: flip} + return shared.GetStorageValueMetadata(FlipUrn, keys, shared.Bytes32) +} + +func getFlipInkKey(flip string) common.Hash { + return storage_diffs.GetIncrementedKey(getFlipIlkKey(flip), 2) +} + +func getFlipInkMetadata(flip string) shared.StorageValueMetadata { + keys := map[shared.Key]string{shared.Flip: flip} + return shared.GetStorageValueMetadata(FlipInk, keys, shared.Uint256) +} + +func getFlipTabKey(flip string) common.Hash { + return storage_diffs.GetIncrementedKey(getFlipIlkKey(flip), 3) +} + +func getFlipTabMetadata(flip string) shared.StorageValueMetadata { + keys := map[shared.Key]string{shared.Flip: flip} + return shared.GetStorageValueMetadata(FlipTab, keys, shared.Uint256) +} diff --git a/pkg/transformers/storage_diffs/maker/cat/mappings_test.go b/pkg/transformers/storage_diffs/maker/cat/mappings_test.go new file mode 100644 index 000000000..4cf725b7d --- /dev/null +++ b/pkg/transformers/storage_diffs/maker/cat/mappings_test.go @@ -0,0 +1,177 @@ +package cat_test + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/fakes" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/cat" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared" + "math/big" +) + +var _ = Describe("Cat storage mappings", func() { + const ( + fakeIlk = "fakeIlk" + fakeFlip = "2" + ) + + var ( + storageRepository *test_helpers.MockMakerStorageRepository + mappings cat.CatMappings + ) + + BeforeEach(func() { + storageRepository = &test_helpers.MockMakerStorageRepository{} + mappings = cat.CatMappings{StorageRepository: storageRepository} + }) + + Describe("looking up static keys", func() { + It("returns value metadata if key exists", func() { + Expect(mappings.Lookup(cat.NFlipKey)).To(Equal(cat.NFlipMetadata)) + Expect(mappings.Lookup(cat.LiveKey)).To(Equal(cat.LiveMetadata)) + Expect(mappings.Lookup(cat.VatKey)).To(Equal(cat.VatMetadata)) + Expect(mappings.Lookup(cat.PitKey)).To(Equal(cat.PitMetadata)) + Expect(mappings.Lookup(cat.VowKey)).To(Equal(cat.VowMetadata)) + }) + + It("returns error if key does not exist", func() { + _, err := mappings.Lookup(common.HexToHash(fakes.FakeHash.Hex())) + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(shared.ErrStorageKeyNotFound{Key: fakes.FakeHash.Hex()})) + }) + }) + + Describe("looking up dynamic keys", func() { + It("refreshes mappings from repository if key not found", func() { + _, _ = mappings.Lookup(fakes.FakeHash) + + Expect(storageRepository.GetIlksCalled).To(BeTrue()) + Expect(storageRepository.GetMaxFlipCalled).To(BeTrue()) + }) + + It("returns error if ilks lookup fails", func() { + storageRepository.GetIlksError = fakes.FakeError + + _, err := mappings.Lookup(fakes.FakeHash) + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("returns error if max flip lookup fails", func() { + storageRepository.GetMaxFlipError = fakes.FakeError + + _, err := mappings.Lookup(fakes.FakeHash) + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("interpolates flips up to max", func() { + storageRepository.MaxFlip = big.NewInt(1) + + _, err := mappings.Lookup(storage_diffs.GetMapping(storage_diffs.IndexTwo, "0")) + Expect(err).NotTo(HaveOccurred()) + + _, err = mappings.Lookup(storage_diffs.GetMapping(storage_diffs.IndexTwo, "1")) + Expect(err).NotTo(HaveOccurred()) + }) + + Describe("ilk", func() { + var ilkFlipKey = common.BytesToHash(crypto.Keccak256(common.FromHex(fakeIlk + cat.IlksMappingIndex))) + + BeforeEach(func() { + storageRepository.Ilks = []string{fakeIlk} + }) + + It("returns value metadata for ilk flip", func() { + expectedMetadata := shared.StorageValueMetadata{ + Name: cat.IlkFlip, + Keys: map[shared.Key]string{shared.Ilk: fakeIlk}, + Type: shared.Address, + } + Expect(mappings.Lookup(ilkFlipKey)).To(Equal(expectedMetadata)) + }) + + It("returns value metadata for ilk chop", func() { + ilkChopKey := storage_diffs.GetIncrementedKey(ilkFlipKey, 1) + expectedMetadata := shared.StorageValueMetadata{ + Name: cat.IlkChop, + Keys: map[shared.Key]string{shared.Ilk: fakeIlk}, + Type: shared.Uint256, + } + Expect(mappings.Lookup(ilkChopKey)).To(Equal(expectedMetadata)) + }) + + It("returns value metadata for ilk lump", func() { + ilkLumpKey := storage_diffs.GetIncrementedKey(ilkFlipKey, 2) + expectedMetadata := shared.StorageValueMetadata{ + Name: cat.IlkLump, + Keys: map[shared.Key]string{shared.Ilk: fakeIlk}, + Type: shared.Uint256, + } + Expect(mappings.Lookup(ilkLumpKey)).To(Equal(expectedMetadata)) + }) + }) + + Describe("flip", func() { + var flipIlkKey = common.BytesToHash(crypto.Keccak256(common.FromHex(fakeFlip + cat.FlipsMappingIndex))) + + BeforeEach(func() { + storageRepository.MaxFlip = big.NewInt(2) + }) + + It("returns value metadata for flip ilk", func() { + expectedMetadata := shared.StorageValueMetadata{ + Name: cat.FlipIlk, + Keys: map[shared.Key]string{shared.Flip: fakeFlip}, + Type: shared.Bytes32, + } + actualMetadata, err := mappings.Lookup(flipIlkKey) + Expect(err).NotTo(HaveOccurred()) + Expect(actualMetadata).To(Equal(expectedMetadata)) + }) + + It("returns value metadata for flip urn", func() { + flipUrnKey := storage_diffs.GetIncrementedKey(flipIlkKey, 1) + expectedMetadata := shared.StorageValueMetadata{ + Name: cat.FlipUrn, + Keys: map[shared.Key]string{shared.Flip: fakeFlip}, + Type: shared.Bytes32, + } + actualMetadata, err := mappings.Lookup(flipUrnKey) + Expect(err).NotTo(HaveOccurred()) + Expect(actualMetadata).To(Equal(expectedMetadata)) + }) + + It("returns value metadata for flip ink", func() { + flipInkKey := storage_diffs.GetIncrementedKey(flipIlkKey, 2) + expectedMetadata := shared.StorageValueMetadata{ + Name: cat.FlipInk, + Keys: map[shared.Key]string{shared.Flip: fakeFlip}, + Type: shared.Uint256, + } + actualMetadata, err := mappings.Lookup(flipInkKey) + Expect(err).NotTo(HaveOccurred()) + Expect(actualMetadata).To(Equal(expectedMetadata)) + }) + + It("returns value metadata for flip tab", func() { + flipTabKey := storage_diffs.GetIncrementedKey(flipIlkKey, 3) + expectedMetadata := shared.StorageValueMetadata{ + Name: cat.FlipTab, + Keys: map[shared.Key]string{shared.Flip: fakeFlip}, + Type: shared.Uint256, + } + actualMetadata, err := mappings.Lookup(flipTabKey) + Expect(err).NotTo(HaveOccurred()) + Expect(actualMetadata).To(Equal(expectedMetadata)) + }) + }) + }) +}) diff --git a/pkg/transformers/storage_diffs/maker/cat/repository.go b/pkg/transformers/storage_diffs/maker/cat/repository.go new file mode 100644 index 000000000..37a60b16d --- /dev/null +++ b/pkg/transformers/storage_diffs/maker/cat/repository.go @@ -0,0 +1,176 @@ +package cat + +import ( + "fmt" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared" +) + +type CatStorageRepository struct { + db *postgres.DB +} + +func (repository *CatStorageRepository) Create(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, value interface{}) error { + switch metadata.Name { + case NFlip: + return repository.insertNFlip(blockNumber, blockHash, value.(string)) + case Live: + return repository.insertLive(blockNumber, blockHash, value.(string)) + case Vat: + return repository.insertVat(blockNumber, blockHash, value.(string)) + case Pit: + return repository.insertPit(blockNumber, blockHash, value.(string)) + case Vow: + return repository.insertVow(blockNumber, blockHash, value.(string)) + case IlkFlip: + return repository.insertIlkFlip(blockNumber, blockHash, metadata, value.(string)) + case IlkChop: + return repository.insertIlkChop(blockNumber, blockHash, metadata, value.(string)) + case IlkLump: + return repository.insertIlkLump(blockNumber, blockHash, metadata, value.(string)) + case FlipIlk: + return repository.insertFlipIlk(blockNumber, blockHash, metadata, value.(string)) + case FlipUrn: + return repository.insertFlipUrn(blockNumber, blockHash, metadata, value.(string)) + case FlipInk: + return repository.insertFlipInk(blockNumber, blockHash, metadata, value.(string)) + case FlipTab: + return repository.insertFlipTab(blockNumber, blockHash, metadata, value.(string)) + default: + panic(fmt.Sprintf("unrecognized cat contract storage name: %s", metadata.Name)) + } +} + +func (repository *CatStorageRepository) SetDB(db *postgres.DB) { + repository.db = db +} + +func (repository *CatStorageRepository) insertNFlip(blockNumber int, blockHash string, nflip string) error { + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_nflip (block_number, block_hash, nflip) VALUES ($1, $2, $3)`, + blockNumber, blockHash, nflip) + return writeErr +} + +func (repository *CatStorageRepository) insertLive(blockNumber int, blockHash string, live string) error { + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_live (block_number, block_hash, live) VALUES ($1, $2, $3 )`, + blockNumber, blockHash, live) + return writeErr +} + +func (repository *CatStorageRepository) insertVat(blockNumber int, blockHash string, vat string) error { + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_vat (block_number, block_hash, vat) VALUES ($1, $2, $3 )`, + blockNumber, blockHash, vat) + return writeErr +} + +func (repository *CatStorageRepository) insertPit(blockNumber int, blockHash string, pit string) error { + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_pit (block_number, block_hash, pit) VALUES ($1, $2, $3 )`, + blockNumber, blockHash, pit) + return writeErr +} + +func (repository *CatStorageRepository) insertVow(blockNumber int, blockHash string, vow string) error { + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_vow (block_number, block_hash, vow) VALUES ($1, $2, $3 )`, + blockNumber, blockHash, vow) + return writeErr +} + +// Ilks mapping: bytes32 => flip address; chop (ray), lump (wad) uint256 +func (repository *CatStorageRepository) insertIlkFlip(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, flip string) error { + ilk, err := getIlk(metadata.Keys) + if err != nil { + return err + } + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_ilk_flip (block_number, block_hash, ilk, flip) VALUES ($1, $2, $3, $4)`, + blockNumber, blockHash, ilk, flip) + return writeErr +} + +func (repository *CatStorageRepository) insertIlkChop(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, chop string) error { + ilk, err := getIlk(metadata.Keys) + if err != nil { + return err + } + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_ilk_chop (block_number, block_hash, ilk, chop) VALUES ($1, $2, $3, $4)`, + blockNumber, blockHash, ilk, chop) + return writeErr +} + +func (repository *CatStorageRepository) insertIlkLump(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, lump string) error { + ilk, err := getIlk(metadata.Keys) + if err != nil { + return err + } + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_ilk_lump (block_number, block_hash, ilk, lump) VALUES ($1, $2, $3, $4)`, + blockNumber, blockHash, ilk, lump) + return writeErr +} + +// Flips mapping: uint256 => ilk, urn bytes32; ink, tab uint256 (both wad) +func (repository *CatStorageRepository) insertFlipIlk(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, ilk string) error { + flip, err := getFlip(metadata.Keys) + if err != nil { + return err + } + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_flip_ilk (block_number, block_hash, flip, ilk) VALUES ($1, $2, $3, $4)`, + blockNumber, blockHash, flip, ilk) + return writeErr +} + +func (repository *CatStorageRepository) insertFlipUrn(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, urn string) error { + flip, err := getFlip(metadata.Keys) + if err != nil { + return err + } + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_flip_urn (block_number, block_hash, flip, urn) VALUES ($1, $2, $3, $4)`, + blockNumber, blockHash, flip, urn) + return writeErr +} + +func (repository *CatStorageRepository) insertFlipInk(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, ink string) error { + flip, err := getFlip(metadata.Keys) + if err != nil { + return err + } + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_flip_ink (block_number, block_hash, flip, ink) VALUES ($1, $2, $3, $4)`, + blockNumber, blockHash, flip, ink) + return writeErr +} + +func (repository *CatStorageRepository) insertFlipTab(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, tab string) error { + flip, err := getFlip(metadata.Keys) + if err != nil { + return err + } + _, writeErr := repository.db.Exec( + `INSERT INTO maker.cat_flip_tab (block_number, block_hash, flip, tab) VALUES ($1, $2, $3, $4)`, + blockNumber, blockHash, flip, tab) + return writeErr +} + +func getIlk(keys map[shared.Key]string) (string, error) { + ilk, ok := keys[shared.Ilk] + if !ok { + return "", shared.ErrMetadataMalformed{MissingData: shared.Ilk} + } + return ilk, nil +} + +func getFlip(keys map[shared.Key]string) (string, error) { + flip, ok := keys[shared.Flip] + if !ok { + return "", shared.ErrMetadataMalformed{MissingData: shared.Flip} + } + return flip, nil +} diff --git a/pkg/transformers/storage_diffs/maker/cat/repository_test.go b/pkg/transformers/storage_diffs/maker/cat/repository_test.go new file mode 100644 index 000000000..8424a444b --- /dev/null +++ b/pkg/transformers/storage_diffs/maker/cat/repository_test.go @@ -0,0 +1,248 @@ +package cat_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/cat" + . "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared" + "github.com/vulcanize/vulcanizedb/test_config" +) + +var _ = Describe("Cat storage repository", func() { + var ( + db *postgres.DB + repo cat.CatStorageRepository + fakeBlockNumber = 123 + fakeBlockHash = "expected_block_hash" + fakeAddress = "0x12345" + fakeIlk = "fake_ilk" + fakeUint256 = "12345" + fakeBytes32 = "fake_bytes32" + ) + + BeforeEach(func() { + db = test_config.NewTestDB(test_config.NewTestNode()) + test_config.CleanTestDB(db) + repo = cat.CatStorageRepository{} + repo.SetDB(db) + }) + + Describe("Variable", func() { + var result VariableRes + + Describe("NFlip", func() { + It("writes a row", func() { + nFlipMetadata := shared.GetStorageValueMetadata(cat.NFlip, nil, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, nFlipMetadata, fakeUint256) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, nflip AS value FROM maker.cat_nflip`) + Expect(err).NotTo(HaveOccurred()) + AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeUint256) + }) + }) + + Describe("Live", func() { + It("writes a row", func() { + liveMetadata := shared.GetStorageValueMetadata(cat.Live, nil, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, liveMetadata, fakeUint256) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, live AS value FROM maker.cat_live`) + Expect(err).NotTo(HaveOccurred()) + AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeUint256) + }) + }) + + Describe("Vat", func() { + It("writes a row", func() { + vatMetadata := shared.GetStorageValueMetadata(cat.Vat, nil, shared.Address) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, vatMetadata, fakeAddress) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, vat AS value FROM maker.cat_vat`) + Expect(err).NotTo(HaveOccurred()) + AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeAddress) + }) + }) + + Describe("Pit", func() { + It("writes a row", func() { + pitMetadata := shared.GetStorageValueMetadata(cat.Pit, nil, shared.Address) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, pitMetadata, fakeAddress) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, pit AS value FROM maker.cat_pit`) + Expect(err).NotTo(HaveOccurred()) + AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeAddress) + }) + }) + + Describe("Vow", func() { + It("writes a row", func() { + vowMetadata := shared.GetStorageValueMetadata(cat.Vow, nil, shared.Address) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, vowMetadata, fakeAddress) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, vow AS value FROM maker.cat_vow`) + Expect(err).NotTo(HaveOccurred()) + AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeAddress) + }) + }) + }) + + Describe("Ilk", func() { + var result MappingRes + + Describe("Flip", func() { + It("writes a row", func() { + ilkFlipMetadata := shared.GetStorageValueMetadata(cat.IlkFlip, map[shared.Key]string{shared.Ilk: fakeIlk}, shared.Address) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, ilkFlipMetadata, fakeAddress) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, flip AS value FROM maker.cat_ilk_flip`) + Expect(err).NotTo(HaveOccurred()) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeAddress) + }) + + It("returns an error if metadata missing ilk", func() { + malformedIlkFlipMetadata := shared.GetStorageValueMetadata(cat.IlkFlip, map[shared.Key]string{}, shared.Address) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedIlkFlipMetadata, fakeAddress) + Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Ilk})) + }) + }) + + Describe("Chop", func() { + It("writes a row", func() { + ilkChopMetadata := shared.GetStorageValueMetadata(cat.IlkChop, map[shared.Key]string{shared.Ilk: fakeIlk}, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, ilkChopMetadata, fakeUint256) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, chop AS value FROM maker.cat_ilk_chop`) + Expect(err).NotTo(HaveOccurred()) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256) + }) + + It("returns an error if metadata missing ilk", func() { + malformedIlkChopMetadata := shared.GetStorageValueMetadata(cat.IlkChop, map[shared.Key]string{}, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedIlkChopMetadata, fakeAddress) + Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Ilk})) + }) + }) + + Describe("Lump", func() { + It("writes a row", func() { + ilkLumpMetadata := shared.GetStorageValueMetadata(cat.IlkLump, map[shared.Key]string{shared.Ilk: fakeIlk}, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, ilkLumpMetadata, fakeUint256) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, lump AS value FROM maker.cat_ilk_lump`) + Expect(err).NotTo(HaveOccurred()) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256) + }) + + It("returns an error if metadata missing ilk", func() { + malformedIlkLumpMetadata := shared.GetStorageValueMetadata(cat.IlkLump, map[shared.Key]string{}, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedIlkLumpMetadata, fakeAddress) + Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Ilk})) + }) + }) + }) + + Describe("Flip", func() { + var result MappingRes + + Describe("FlipIlk", func() { + It("writes a row", func() { + flipIlkMetadata := shared.GetStorageValueMetadata(cat.FlipIlk, map[shared.Key]string{shared.Flip: fakeUint256}, shared.Bytes32) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, flipIlkMetadata, fakeBytes32) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, flip AS key, ilk AS value FROM maker.cat_flip_ilk`) + Expect(err).NotTo(HaveOccurred()) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeBytes32) + }) + + It("returns an error if metadata missing flip", func() { + malformedFlipIlkMetadata := shared.GetStorageValueMetadata(cat.FlipIlk, map[shared.Key]string{}, shared.Bytes32) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedFlipIlkMetadata, fakeBytes32) + Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Flip})) + }) + }) + + Describe("FlipUrn", func() { + It("writes a row", func() { + flipUrnMetadata := shared.GetStorageValueMetadata(cat.FlipUrn, map[shared.Key]string{shared.Flip: fakeUint256}, shared.Bytes32) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, flipUrnMetadata, fakeBytes32) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, flip AS key, urn AS value FROM maker.cat_flip_urn`) + Expect(err).NotTo(HaveOccurred()) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeBytes32) + }) + + It("returns an error if metadata missing flip", func() { + malformedFlipUrnMetadata := shared.GetStorageValueMetadata(cat.FlipUrn, map[shared.Key]string{}, shared.Bytes32) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedFlipUrnMetadata, fakeBytes32) + Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Flip})) + }) + }) + + Describe("FlipInk", func() { + It("writes a row", func() { + flipInkMetadata := shared.GetStorageValueMetadata(cat.FlipInk, map[shared.Key]string{shared.Flip: fakeUint256}, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, flipInkMetadata, fakeUint256) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, flip AS key, ink AS value FROM maker.cat_flip_ink`) + Expect(err).NotTo(HaveOccurred()) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeUint256) + }) + + It("returns an error if metadata missing flip", func() { + malformedFlipInkMetadata := shared.GetStorageValueMetadata(cat.FlipInk, map[shared.Key]string{}, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedFlipInkMetadata, fakeUint256) + Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Flip})) + }) + }) + + Describe("FlipTab", func() { + It("writes a row", func() { + flipTabMetadata := shared.GetStorageValueMetadata(cat.FlipTab, map[shared.Key]string{shared.Flip: fakeUint256}, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, flipTabMetadata, fakeUint256) + Expect(err).NotTo(HaveOccurred()) + + err = db.Get(&result, `SELECT block_number, block_hash, flip AS key, tab AS value FROM maker.cat_flip_tab`) + Expect(err).NotTo(HaveOccurred()) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeUint256) + }) + + It("returns an error if metadata missing flip", func() { + malformedFlipTabMetadata := shared.GetStorageValueMetadata(cat.FlipTab, map[shared.Key]string{}, shared.Uint256) + + err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedFlipTabMetadata, fakeUint256) + Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Flip})) + }) + }) + }) +}) diff --git a/pkg/transformers/storage_diffs/maker/maker_storage_repository.go b/pkg/transformers/storage_diffs/maker/maker_storage_repository.go index 44ff49981..2615d3f76 100644 --- a/pkg/transformers/storage_diffs/maker/maker_storage_repository.go +++ b/pkg/transformers/storage_diffs/maker/maker_storage_repository.go @@ -16,7 +16,11 @@ package maker -import "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" +import ( + "database/sql" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "math/big" +) type Urn struct { Ilk string @@ -25,6 +29,7 @@ type Urn struct { type IMakerStorageRepository interface { GetDaiKeys() ([]string, error) + GetMaxFlip() (*big.Int, error) GetGemKeys() ([]Urn, error) GetIlks() ([]string, error) GetSinKeys() ([]string, error) @@ -48,6 +53,16 @@ func (repository *MakerStorageRepository) GetDaiKeys() ([]string, error) { return daiKeys, err } +func (repository *MakerStorageRepository) GetMaxFlip() (*big.Int, error) { + var maxFlip big.Int + err := repository.db.Get(&maxFlip, `SELECT MAX(nflip) FROM maker.cat_nflip`) + if err == sql.ErrNoRows { + // No flips have occurred; this is different from flip 0 having occurred + return nil, nil + } + return &maxFlip, err +} + func (repository *MakerStorageRepository) GetGemKeys() ([]Urn, error) { var gems []Urn err := repository.db.Select(&gems, ` diff --git a/pkg/transformers/storage_diffs/maker/pit/repository_test.go b/pkg/transformers/storage_diffs/maker/pit/repository_test.go index 84e63653b..3b9e6ddf8 100644 --- a/pkg/transformers/storage_diffs/maker/pit/repository_test.go +++ b/pkg/transformers/storage_diffs/maker/pit/repository_test.go @@ -21,6 +21,7 @@ import ( . "github.com/onsi/gomega" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/pit" + . "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -54,18 +55,11 @@ var _ = Describe("Pit storage repository", func() { err = repo.Create(blockNumber, blockHash, ilkLineMetadata, expectedLine) Expect(err).NotTo(HaveOccurred()) - type IlkLine struct { - BlockMetadata - Ilk string - Line string - } - var result IlkLine - err = db.Get(&result, `SELECT block_number, block_hash, ilk, line FROM maker.pit_ilk_line`) + + var result MappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, line AS value FROM maker.pit_ilk_line`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Ilk).To(Equal(expectedIlk)) - Expect(result.Line).To(Equal(expectedLine)) + AssertMapping(result, blockNumber, blockHash, expectedIlk, expectedLine) }) It("persists an ilk spot", func() { @@ -79,18 +73,11 @@ var _ = Describe("Pit storage repository", func() { err = repo.Create(blockNumber, blockHash, ilkSpotMetadata, expectedSpot) Expect(err).NotTo(HaveOccurred()) - type IlkSpot struct { - BlockMetadata - Ilk string - Spot string - } - var result IlkSpot - err = db.Get(&result, `SELECT block_number, block_hash, ilk, spot FROM maker.pit_ilk_spot`) + + var result MappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, spot AS value FROM maker.pit_ilk_spot`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Ilk).To(Equal(expectedIlk)) - Expect(result.Spot).To(Equal(expectedSpot)) + AssertMapping(result, blockNumber, blockHash, expectedIlk, expectedSpot) }) It("persists a pit drip", func() { @@ -99,16 +86,11 @@ var _ = Describe("Pit storage repository", func() { err = repo.Create(blockNumber, blockHash, pit.DripMetadata, expectedDrip) Expect(err).NotTo(HaveOccurred()) - type PitDrip struct { - BlockMetadata - Drip string - } - var result PitDrip - err = db.Get(&result, `SELECT block_number, block_hash, drip FROM maker.pit_drip`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, drip AS value FROM maker.pit_drip`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Drip).To(Equal(expectedDrip)) + AssertVariable(result, blockNumber, blockHash, expectedDrip) }) It("persists a pit line", func() { @@ -117,16 +99,11 @@ var _ = Describe("Pit storage repository", func() { err = repo.Create(blockNumber, blockHash, pit.LineMetadata, expectedLine) Expect(err).NotTo(HaveOccurred()) - type PitLine struct { - BlockMetadata - Line string - } - var result PitLine - err = db.Get(&result, `SELECT block_number, block_hash, line FROM maker.pit_line`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, line AS value FROM maker.pit_line`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Line).To(Equal(expectedLine)) + AssertVariable(result, blockNumber, blockHash, expectedLine) }) It("persists a pit live", func() { @@ -135,16 +112,11 @@ var _ = Describe("Pit storage repository", func() { err = repo.Create(blockNumber, blockHash, pit.LiveMetadata, expectedLive) Expect(err).NotTo(HaveOccurred()) - type PitLive struct { - BlockMetadata - Live string - } - var result PitLive - err = db.Get(&result, `SELECT block_number, block_hash, live FROM maker.pit_live`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, live AS value FROM maker.pit_live`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Live).To(Equal(expectedLive)) + AssertVariable(result, blockNumber, blockHash, expectedLive) }) It("persists a pit vat", func() { @@ -153,20 +125,10 @@ var _ = Describe("Pit storage repository", func() { err = repo.Create(blockNumber, blockHash, pit.VatMetadata, expectedVat) Expect(err).NotTo(HaveOccurred()) - type PitVat struct { - BlockMetadata - Vat string - } - var result PitVat - err = db.Get(&result, `SELECT block_number, block_hash, vat FROM maker.pit_vat`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, vat AS value FROM maker.pit_vat`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Vat).To(Equal(expectedVat)) + AssertVariable(result, blockNumber, blockHash, expectedVat) }) }) - -type BlockMetadata struct { - BlockNumber int `db:"block_number"` - BlockHash string `db:"block_hash"` -} diff --git a/pkg/transformers/storage_diffs/maker/test_helpers/datatypes.go b/pkg/transformers/storage_diffs/maker/test_helpers/datatypes.go new file mode 100644 index 000000000..09c6bcc63 --- /dev/null +++ b/pkg/transformers/storage_diffs/maker/test_helpers/datatypes.go @@ -0,0 +1,49 @@ +package test_helpers + +import ( + . "github.com/onsi/gomega" +) + +type BlockMetadata struct { + BlockNumber int `db:"block_number"` + BlockHash string `db:"block_hash"` +} + +type VariableRes struct { + BlockMetadata + Value string +} + +type MappingRes struct { + BlockMetadata + Key string + Value string +} + +type DoubleMappingRes struct { + BlockMetadata + KeyOne string `db:"key_one"` + KeyTwo string `db:"key_two"` + Value string +} + +func AssertVariable(res VariableRes, blockNumber int, blockHash, value string) { + Expect(res.BlockNumber).To(Equal(blockNumber)) + Expect(res.BlockHash).To(Equal(blockHash)) + Expect(res.Value).To(Equal(value)) +} + +func AssertMapping(res MappingRes, blockNumber int, blockHash, key, value string) { + Expect(res.BlockNumber).To(Equal(blockNumber)) + Expect(res.BlockHash).To(Equal(blockHash)) + Expect(res.Key).To(Equal(key)) + Expect(res.Value).To(Equal(value)) +} + +func AssertDoubleMapping(res DoubleMappingRes, blockNumber int, blockHash, keyOne, keyTwo, value string) { + Expect(res.BlockNumber).To(Equal(blockNumber)) + Expect(res.BlockHash).To(Equal(blockHash)) + Expect(res.KeyOne).To(Equal(keyOne)) + Expect(res.KeyTwo).To(Equal(keyTwo)) + Expect(res.Value).To(Equal(value)) +} diff --git a/pkg/transformers/storage_diffs/maker/test_helpers/maker_storage_repository.go b/pkg/transformers/storage_diffs/maker/test_helpers/maker_storage_repository.go index 161cac53e..3ce7c08cf 100644 --- a/pkg/transformers/storage_diffs/maker/test_helpers/maker_storage_repository.go +++ b/pkg/transformers/storage_diffs/maker/test_helpers/maker_storage_repository.go @@ -3,6 +3,7 @@ package test_helpers import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker" + "math/big" ) type MockMakerStorageRepository struct { @@ -14,11 +15,14 @@ type MockMakerStorageRepository struct { GetGemKeysError error GetIlksCalled bool GetIlksError error + GetMaxFlipCalled bool + GetMaxFlipError error GetSinKeysCalled bool GetSinKeysError error GetUrnsCalled bool GetUrnsError error Ilks []string + MaxFlip *big.Int SinKeys []string Urns []maker.Urn } @@ -38,6 +42,11 @@ func (repository *MockMakerStorageRepository) GetIlks() ([]string, error) { return repository.Ilks, repository.GetIlksError } +func (repository *MockMakerStorageRepository) GetMaxFlip() (*big.Int, error) { + repository.GetMaxFlipCalled = true + return repository.MaxFlip, repository.GetMaxFlipError +} + func (repository *MockMakerStorageRepository) GetSinKeys() ([]string, error) { repository.GetSinKeysCalled = true return repository.SinKeys, repository.GetSinKeysError diff --git a/pkg/transformers/storage_diffs/maker/vat/repository_test.go b/pkg/transformers/storage_diffs/maker/vat/repository_test.go index fcee9e08a..a139c1320 100644 --- a/pkg/transformers/storage_diffs/maker/vat/repository_test.go +++ b/pkg/transformers/storage_diffs/maker/vat/repository_test.go @@ -4,6 +4,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + . "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/vat" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared" "github.com/vulcanize/vulcanizedb/test_config" @@ -38,18 +39,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, daiMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type Dai struct { - BlockMetadata - Guy string - Dai string - } - var result Dai - err = db.Get(&result, `SELECT block_number, block_hash, guy, dai FROM maker.vat_dai`) + + var result MappingRes + err = db.Get(&result, `SELECT block_number, block_hash, guy AS key, dai AS value FROM maker.vat_dai`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Guy).To(Equal(fakeGuy)) - Expect(result.Dai).To(Equal(fakeUint256)) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeGuy, fakeUint256) }) It("returns error if metadata missing guy", func() { @@ -77,20 +71,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, gemMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type Gem struct { - BlockMetadata - Ilk string - Guy string - Gem string - } - var result Gem - err = db.Get(&result, `SELECT block_number, block_hash, ilk, guy, gem FROM maker.vat_gem`) + + var result DoubleMappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key_one, guy AS key_two, gem AS value FROM maker.vat_gem`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Ilk).To(Equal(fakeIlk)) - Expect(result.Guy).To(Equal(fakeGuy)) - Expect(result.Gem).To(Equal(fakeUint256)) + AssertDoubleMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeGuy, fakeUint256) }) It("returns error if metadata missing ilk", func() { @@ -131,18 +116,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, ilkArtMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type IlkArt struct { - BlockMetadata - Ilk string - Art string - } - var result IlkArt - err = db.Get(&result, `SELECT block_number, block_hash, ilk, art FROM maker.vat_ilk_art`) + + var result MappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, art AS value FROM maker.vat_ilk_art`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Ilk).To(Equal(fakeIlk)) - Expect(result.Art).To(Equal(fakeUint256)) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256) }) It("returns error if metadata missing ilk", func() { @@ -170,18 +148,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, ilkInkMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type IlkInk struct { - BlockMetadata - Ilk string - Ink string - } - var result IlkInk - err = db.Get(&result, `SELECT block_number, block_hash, ilk, ink FROM maker.vat_ilk_ink`) + + var result MappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, ink AS value FROM maker.vat_ilk_ink`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Ilk).To(Equal(fakeIlk)) - Expect(result.Ink).To(Equal(fakeUint256)) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256) }) It("returns error if metadata missing ilk", func() { @@ -209,18 +180,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, ilkRateMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type IlkRate struct { - BlockMetadata - Ilk string - Rate string - } - var result IlkRate - err = db.Get(&result, `SELECT block_number, block_hash, ilk, rate FROM maker.vat_ilk_rate`) + + var result MappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, rate AS value FROM maker.vat_ilk_rate`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Ilk).To(Equal(fakeIlk)) - Expect(result.Rate).To(Equal(fakeUint256)) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256) }) It("returns error if metadata missing ilk", func() { @@ -248,18 +212,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, ilkTakeMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type IlkTake struct { - BlockMetadata - Ilk string - Take string - } - var result IlkTake - err = db.Get(&result, `SELECT block_number, block_hash, ilk, take FROM maker.vat_ilk_take`) + + var result MappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, take AS value FROM maker.vat_ilk_take`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Ilk).To(Equal(fakeIlk)) - Expect(result.Take).To(Equal(fakeUint256)) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256) }) It("returns error if metadata missing ilk", func() { @@ -287,18 +244,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, sinMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type Sin struct { - BlockMetadata - Guy string - Sin string - } - var result Sin - err = db.Get(&result, `SELECT block_number, block_hash, guy, sin FROM maker.vat_sin`) + + var result MappingRes + err = db.Get(&result, `SELECT block_number, block_hash, guy AS key, sin AS value FROM maker.vat_sin`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Guy).To(Equal(fakeGuy)) - Expect(result.Sin).To(Equal(fakeUint256)) + AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeGuy, fakeUint256) }) It("returns error if metadata missing guy", func() { @@ -326,20 +276,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, urnArtMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type UrnArt struct { - BlockMetadata - Ilk string - Urn string - Art string - } - var result UrnArt - err = db.Get(&result, `SELECT block_number, block_hash, ilk, urn, art FROM maker.vat_urn_art`) + + var result DoubleMappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key_one, urn AS key_two, art AS value FROM maker.vat_urn_art`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Ilk).To(Equal(fakeIlk)) - Expect(result.Urn).To(Equal(fakeGuy)) - Expect(result.Art).To(Equal(fakeUint256)) + AssertDoubleMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeGuy, fakeUint256) }) It("returns error if metadata missing ilk", func() { @@ -380,20 +321,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, urnInkMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type UrnInk struct { - BlockMetadata - Ilk string - Urn string - Ink string - } - var result UrnInk - err = db.Get(&result, `SELECT block_number, block_hash, ilk, urn, ink FROM maker.vat_urn_ink`) + + var result DoubleMappingRes + err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key_one, urn AS key_two, ink AS value FROM maker.vat_urn_ink`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Ilk).To(Equal(fakeIlk)) - Expect(result.Urn).To(Equal(fakeGuy)) - Expect(result.Ink).To(Equal(fakeUint256)) + AssertDoubleMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeGuy, fakeUint256) }) It("returns error if metadata missing ilk", func() { @@ -433,16 +365,11 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, debtMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type Debt struct { - BlockMetadata - Debt string - } - var result Debt - err = db.Get(&result, `SELECT block_number, block_hash, debt FROM maker.vat_debt`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, debt AS value FROM maker.vat_debt`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Debt).To(Equal(fakeUint256)) + AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeUint256) }) It("persists vat vice", func() { @@ -455,20 +382,10 @@ var _ = Describe("Vat storage repository", func() { err := repo.Create(fakeBlockNumber, fakeBlockHash, viceMetadata, fakeUint256) Expect(err).NotTo(HaveOccurred()) - type Vice struct { - BlockMetadata - Vice string - } - var result Vice - err = db.Get(&result, `SELECT block_number, block_hash, vice FROM maker.vat_vice`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, vice AS value FROM maker.vat_vice`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(fakeBlockNumber)) - Expect(result.BlockHash).To(Equal(fakeBlockHash)) - Expect(result.Vice).To(Equal(fakeUint256)) + AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeUint256) }) }) - -type BlockMetadata struct { - BlockNumber int `db:"block_number"` - BlockHash string `db:"block_hash"` -} diff --git a/pkg/transformers/storage_diffs/maker/vow/repository_test.go b/pkg/transformers/storage_diffs/maker/vow/repository_test.go index e7540106b..eb1163429 100644 --- a/pkg/transformers/storage_diffs/maker/vow/repository_test.go +++ b/pkg/transformers/storage_diffs/maker/vow/repository_test.go @@ -19,6 +19,7 @@ package vow_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + . "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/vow" @@ -49,16 +50,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.VatMetadata, expectedVat) Expect(err).NotTo(HaveOccurred()) - type VowVat struct { - BlockMetadata - Vat string - } - var result VowVat - err = db.Get(&result, `SELECT block_number, block_hash, vat from maker.vow_vat`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, vat AS value from maker.vow_vat`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Vat).To(Equal(expectedVat)) + AssertVariable(result, blockNumber, blockHash, expectedVat) }) It("persists a vow cow", func() { @@ -67,16 +63,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.CowMetadata, expectedCow) Expect(err).NotTo(HaveOccurred()) - type VowCow struct { - BlockMetadata - Cow string - } - var result VowCow - err = db.Get(&result, `SELECT block_number, block_hash, cow from maker.vow_cow`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, cow AS value from maker.vow_cow`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Cow).To(Equal(expectedCow)) + AssertVariable(result, blockNumber, blockHash, expectedCow) }) It("persists a vow row", func() { @@ -85,16 +76,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.RowMetadata, expectedRow) Expect(err).NotTo(HaveOccurred()) - type VowRow struct { - BlockMetadata - Row string - } - var result VowRow - err = db.Get(&result, `SELECT block_number, block_hash, row from maker.vow_row`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, row AS value from maker.vow_row`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Row).To(Equal(expectedRow)) + AssertVariable(result, blockNumber, blockHash, expectedRow) }) It("persists a vow Sin", func() { @@ -103,16 +89,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.SinMetadata, expectedSow) Expect(err).NotTo(HaveOccurred()) - type VowSin struct { - BlockMetadata - Sin string - } - var result VowSin - err = db.Get(&result, `SELECT block_number, block_hash, sin from maker.vow_sin`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, sin AS value from maker.vow_sin`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Sin).To(Equal(expectedSow)) + AssertVariable(result, blockNumber, blockHash, expectedSow) }) It("persists a vow woe", func() { @@ -121,16 +102,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.WoeMetadata, expectedWoe) Expect(err).NotTo(HaveOccurred()) - type VowWoe struct { - BlockMetadata - Woe string - } - var result VowWoe - err = db.Get(&result, `SELECT block_number, block_hash, woe from maker.vow_woe`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, woe AS value from maker.vow_woe`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Woe).To(Equal(expectedWoe)) + AssertVariable(result, blockNumber, blockHash, expectedWoe) }) It("persists a vow Ash", func() { @@ -139,16 +115,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.AshMetadata, expectedAsh) Expect(err).NotTo(HaveOccurred()) - type VowAsh struct { - BlockMetadata - Ash string - } - var result VowAsh - err = db.Get(&result, `SELECT block_number, block_hash, ash from maker.vow_ash`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, ash AS value from maker.vow_ash`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Ash).To(Equal(expectedAsh)) + AssertVariable(result, blockNumber, blockHash, expectedAsh) }) It("persists a vow Wait", func() { @@ -157,16 +128,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.WaitMetadata, expectedWait) Expect(err).NotTo(HaveOccurred()) - type VowWait struct { - BlockMetadata - Wait string - } - var result VowWait - err = db.Get(&result, `SELECT block_number, block_hash, wait from maker.vow_wait`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, wait AS value from maker.vow_wait`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Wait).To(Equal(expectedWait)) + AssertVariable(result, blockNumber, blockHash, expectedWait) }) It("persists a vow Bump", func() { @@ -175,16 +141,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.BumpMetadata, expectedBump) Expect(err).NotTo(HaveOccurred()) - type VowBump struct { - BlockMetadata - Bump string - } - var result VowBump - err = db.Get(&result, `SELECT block_number, block_hash, bump from maker.vow_bump`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, bump AS value from maker.vow_bump`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Bump).To(Equal(expectedBump)) + AssertVariable(result, blockNumber, blockHash, expectedBump) }) It("persists a vow Sump", func() { @@ -193,16 +154,11 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.SumpMetadata, expectedSump) Expect(err).NotTo(HaveOccurred()) - type VowSump struct { - BlockMetadata - Sump string - } - var result VowSump - err = db.Get(&result, `SELECT block_number, block_hash, sump from maker.vow_sump`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, sump AS value from maker.vow_sump`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Sump).To(Equal(expectedSump)) + AssertVariable(result, blockNumber, blockHash, expectedSump) }) It("persists a vow Hump", func() { @@ -211,20 +167,10 @@ var _ = Describe("Vow storage repository test", func() { err = repo.Create(blockNumber, blockHash, vow.HumpMetadata, expectedHump) Expect(err).NotTo(HaveOccurred()) - type VowHump struct { - BlockMetadata - Hump string - } - var result VowHump - err = db.Get(&result, `SELECT block_number, block_hash, hump from maker.vow_hump`) + + var result VariableRes + err = db.Get(&result, `SELECT block_number, block_hash, hump AS value from maker.vow_hump`) Expect(err).NotTo(HaveOccurred()) - Expect(result.BlockNumber).To(Equal(blockNumber)) - Expect(result.BlockHash).To(Equal(blockHash)) - Expect(result.Hump).To(Equal(expectedHump)) + AssertVariable(result, blockNumber, blockHash, expectedHump) }) }) - -type BlockMetadata struct { - BlockNumber int `db:"block_number"` - BlockHash string `db:"block_hash"` -} diff --git a/pkg/transformers/storage_diffs/shared/decoder.go b/pkg/transformers/storage_diffs/shared/decoder.go index 753b24168..7591f8c08 100644 --- a/pkg/transformers/storage_diffs/shared/decoder.go +++ b/pkg/transformers/storage_diffs/shared/decoder.go @@ -27,6 +27,8 @@ func Decode(row StorageDiffRow, metadata StorageValueMetadata) (interface{}, err return decodeUint256(row.StorageValue.Bytes()), nil case Address: return decodeAddress(row.StorageValue.Bytes()), nil + case Bytes32: + return row.StorageValue.Hex(), nil default: return nil, ErrTypeNotFound{} } diff --git a/pkg/transformers/storage_diffs/shared/value.go b/pkg/transformers/storage_diffs/shared/value.go index 4bc59d7f8..e5543bcec 100644 --- a/pkg/transformers/storage_diffs/shared/value.go +++ b/pkg/transformers/storage_diffs/shared/value.go @@ -27,8 +27,9 @@ const ( type Key string const ( - Ilk Key = "ilk" - Guy Key = "guy" + Ilk Key = "ilk" + Guy Key = "guy" + Flip Key = "flip" ) type StorageValueMetadata struct { diff --git a/pkg/transformers/storage_transformers.go b/pkg/transformers/storage_transformers.go index a0d80d1dc..48e2771bc 100644 --- a/pkg/transformers/storage_transformers.go +++ b/pkg/transformers/storage_transformers.go @@ -5,11 +5,20 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/transformers/factories/storage" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker" + "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/cat" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/pit" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/vat" "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/vow" ) +func GetCatStorageTransformer() storage.Transformer { + return storage.Transformer{ + Address: common.HexToAddress(constants.CatContractAddress()), + Mappings: &cat.CatMappings{StorageRepository: &maker.MakerStorageRepository{}}, + Repository: &cat.CatStorageRepository{}, + } +} + func GetPitStorageTransformer() storage.Transformer { return storage.Transformer{ Address: common.HexToAddress(constants.PitContractAddress()), diff --git a/test_config/test_config.go b/test_config/test_config.go index e11ddea34..b760be063 100644 --- a/test_config/test_config.go +++ b/test_config/test_config.go @@ -130,6 +130,17 @@ func CleanTestDB(db *postgres.DB) { db.MustExec("DELETE FROM receipts") db.MustExec("DELETE FROM transactions") db.MustExec("DELETE FROM watched_contracts") + db.MustExec("DELETE FROM maker.cat_nflip") + db.MustExec("DELETE FROM maker.cat_live") + db.MustExec("DELETE FROM maker.cat_vat") + db.MustExec("DELETE FROM maker.cat_pit") + db.MustExec("DELETE FROM maker.cat_vow") + db.MustExec("DELETE FROM maker.cat_ilk_flip") + db.MustExec("DELETE FROM maker.cat_ilk_chop") + db.MustExec("DELETE FROM maker.cat_ilk_lump") + db.MustExec("DELETE FROM maker.cat_flip_ilk") + db.MustExec("DELETE FROM maker.cat_flip_ink") + db.MustExec("DELETE FROM maker.cat_flip_tab") } // Returns a new test node, with the same ID