remove pkg/transformers; extract shared files needed to

libraries/shared; work on automated db migration management
This commit is contained in:
Ian Norden
2019-02-24 21:38:47 -06:00
parent 55fa9b8364
commit decc2a3caf
545 changed files with 431 additions and 15922 deletions
@@ -1,134 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package pit
import (
"github.com/ethereum/go-ethereum/common"
"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"
)
const (
IlkLine = "line"
IlkSpot = "spot"
PitDrip = "drip"
PitLine = "Line"
PitLive = "live"
PitVat = "vat"
)
var (
// storage key and value metadata for "drip" on the Pit contract
DripKey = common.HexToHash(storage_diffs.IndexFive)
DripMetadata = shared.StorageValueMetadata{
Name: PitDrip,
Keys: nil,
Type: shared.Address,
}
IlkSpotIndex = storage_diffs.IndexOne
// storage key and value metadata for "Spot" on the Pit contract
LineKey = common.HexToHash(storage_diffs.IndexThree)
LineMetadata = shared.StorageValueMetadata{
Name: PitLine,
Keys: nil,
Type: shared.Uint256,
}
// storage key and value metadata for "live" on the Pit contract
LiveKey = common.HexToHash(storage_diffs.IndexTwo)
LiveMetadata = shared.StorageValueMetadata{
Name: PitLive,
Keys: nil,
Type: shared.Uint256,
}
// storage key and value metadata for "vat" on the Pit contract
VatKey = common.HexToHash(storage_diffs.IndexFour)
VatMetadata = shared.StorageValueMetadata{
Name: PitVat,
Keys: nil,
Type: shared.Address,
}
)
type PitMappings struct {
StorageRepository maker.IMakerStorageRepository
mappings map[common.Hash]shared.StorageValueMetadata
}
func (mappings *PitMappings) SetDB(db *postgres.DB) {
mappings.StorageRepository.SetDB(db)
}
func (mappings *PitMappings) 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 *PitMappings) loadMappings() error {
mappings.mappings = getStaticMappings()
ilks, err := mappings.StorageRepository.GetIlks()
if err != nil {
return err
}
for _, ilk := range ilks {
mappings.mappings[getSpotKey(ilk)] = getSpotMetadata(ilk)
mappings.mappings[getLineKey(ilk)] = getLineMetadata(ilk)
}
return nil
}
func getStaticMappings() map[common.Hash]shared.StorageValueMetadata {
mappings := make(map[common.Hash]shared.StorageValueMetadata)
mappings[DripKey] = DripMetadata
mappings[LineKey] = LineMetadata
mappings[LiveKey] = LiveMetadata
mappings[VatKey] = VatMetadata
return mappings
}
func getSpotKey(ilk string) common.Hash {
return storage_diffs.GetMapping(IlkSpotIndex, ilk)
}
func getSpotMetadata(ilk string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk}
return shared.GetStorageValueMetadata(IlkSpot, keys, shared.Uint256)
}
func getLineKey(ilk string) common.Hash {
return storage_diffs.GetIncrementedKey(getSpotKey(ilk), 1)
}
func getLineMetadata(ilk string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk}
return shared.GetStorageValueMetadata(IlkLine, keys, shared.Uint256)
}
@@ -1,90 +0,0 @@
package pit_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/maker/pit"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared"
"math/big"
)
var _ = Describe("Pit storage mappings", func() {
Describe("looking up static keys", func() {
It("returns value metadata if key exists", func() {
storageRepository := &test_helpers.MockMakerStorageRepository{}
mappings := pit.PitMappings{StorageRepository: storageRepository}
Expect(mappings.Lookup(pit.DripKey)).To(Equal(pit.DripMetadata))
Expect(mappings.Lookup(pit.LineKey)).To(Equal(pit.LineMetadata))
Expect(mappings.Lookup(pit.LiveKey)).To(Equal(pit.LiveMetadata))
Expect(mappings.Lookup(pit.VatKey)).To(Equal(pit.VatMetadata))
})
It("returns error if key does not exist", func() {
mappings := pit.PitMappings{StorageRepository: &test_helpers.MockMakerStorageRepository{}}
_, 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() {
storageRepository := &test_helpers.MockMakerStorageRepository{}
mappings := pit.PitMappings{StorageRepository: storageRepository}
mappings.Lookup(fakes.FakeHash)
Expect(storageRepository.GetIlksCalled).To(BeTrue())
})
It("returns value metadata for spot when ilk in the DB", func() {
storageRepository := &test_helpers.MockMakerStorageRepository{}
fakeIlk := "fakeIlk"
storageRepository.Ilks = []string{fakeIlk}
mappings := pit.PitMappings{StorageRepository: storageRepository}
ilkSpotKey := common.BytesToHash(crypto.Keccak256(common.FromHex("0x" + fakeIlk + pit.IlkSpotIndex)))
expectedMetadata := shared.StorageValueMetadata{
Name: pit.IlkSpot,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk},
Type: shared.Uint256,
}
Expect(mappings.Lookup(ilkSpotKey)).To(Equal(expectedMetadata))
})
It("returns value metadata for line when ilk in the DB", func() {
storageRepository := &test_helpers.MockMakerStorageRepository{}
fakeIlk := "fakeIlk"
storageRepository.Ilks = []string{fakeIlk}
mappings := pit.PitMappings{StorageRepository: storageRepository}
ilkSpotKeyBytes := crypto.Keccak256(common.FromHex("0x" + fakeIlk + pit.IlkSpotIndex))
ilkSpotAsInt := big.NewInt(0).SetBytes(ilkSpotKeyBytes)
incrementedIlkSpot := big.NewInt(0).Add(ilkSpotAsInt, big.NewInt(1))
ilkLineKey := common.BytesToHash(incrementedIlkSpot.Bytes())
expectedMetadata := shared.StorageValueMetadata{
Name: pit.IlkLine,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk},
Type: shared.Uint256,
}
Expect(mappings.Lookup(ilkLineKey)).To(Equal(expectedMetadata))
})
It("returns error if key not found", func() {
storageRepository := &test_helpers.MockMakerStorageRepository{}
mappings := pit.PitMappings{StorageRepository: storageRepository}
_, err := mappings.Lookup(fakes.FakeHash)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(shared.ErrStorageKeyNotFound{Key: fakes.FakeHash.Hex()}))
})
})
})
@@ -1,241 +0,0 @@
package vat
import (
"github.com/ethereum/go-ethereum/common"
"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"
)
const (
Dai = "dai"
Gem = "gem"
IlkArt = "Art"
IlkInk = "Ink"
IlkRate = "rate"
IlkTake = "take"
Sin = "sin"
UrnArt = "art"
UrnInk = "ink"
VatDebt = "debt"
VatVice = "vice"
)
var (
DebtKey = common.HexToHash(storage_diffs.IndexSix)
DebtMetadata = shared.StorageValueMetadata{
Name: VatDebt,
Keys: nil,
Type: 0,
}
IlksMappingIndex = storage_diffs.IndexOne
UrnsMappingIndex = storage_diffs.IndexTwo
GemsMappingIndex = storage_diffs.IndexThree
DaiMappingIndex = storage_diffs.IndexFour
SinMappingIndex = storage_diffs.IndexFive
ViceKey = common.HexToHash(storage_diffs.IndexSeven)
ViceMetadata = shared.StorageValueMetadata{
Name: VatVice,
Keys: nil,
Type: 0,
}
)
type VatMappings struct {
StorageRepository maker.IMakerStorageRepository
mappings map[common.Hash]shared.StorageValueMetadata
}
func (mappings VatMappings) 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 *VatMappings) SetDB(db *postgres.DB) {
mappings.StorageRepository.SetDB(db)
}
func (mappings *VatMappings) loadMappings() error {
mappings.mappings = loadStaticMappings()
daiErr := mappings.loadDaiKeys()
if daiErr != nil {
return daiErr
}
gemErr := mappings.loadGemKeys()
if gemErr != nil {
return gemErr
}
ilkErr := mappings.loadIlkKeys()
if ilkErr != nil {
return ilkErr
}
sinErr := mappings.loadSinKeys()
if sinErr != nil {
return sinErr
}
urnErr := mappings.loadUrnKeys()
if urnErr != nil {
return urnErr
}
return nil
}
func loadStaticMappings() map[common.Hash]shared.StorageValueMetadata {
mappings := make(map[common.Hash]shared.StorageValueMetadata)
mappings[DebtKey] = DebtMetadata
mappings[ViceKey] = ViceMetadata
return mappings
}
func (mappings *VatMappings) loadDaiKeys() error {
daiKeys, err := mappings.StorageRepository.GetDaiKeys()
if err != nil {
return err
}
for _, d := range daiKeys {
mappings.mappings[getDaiKey(d)] = getDaiMetadata(d)
}
return nil
}
func (mappings *VatMappings) loadGemKeys() error {
gemKeys, err := mappings.StorageRepository.GetGemKeys()
if err != nil {
return err
}
for _, gem := range gemKeys {
mappings.mappings[getGemKey(gem.Ilk, gem.Guy)] = getGemMetadata(gem.Ilk, gem.Guy)
}
return nil
}
func (mappings *VatMappings) loadIlkKeys() error {
ilks, err := mappings.StorageRepository.GetIlks()
if err != nil {
return err
}
for _, ilk := range ilks {
mappings.mappings[getIlkTakeKey(ilk)] = getIlkTakeMetadata(ilk)
mappings.mappings[getIlkRateKey(ilk)] = getIlkRateMetadata(ilk)
mappings.mappings[getIlkInkKey(ilk)] = getIlkInkMetadata(ilk)
mappings.mappings[getIlkArtKey(ilk)] = getIlkArtMetadata(ilk)
}
return nil
}
func (mappings *VatMappings) loadSinKeys() error {
sinKeys, err := mappings.StorageRepository.GetSinKeys()
if err != nil {
return err
}
for _, s := range sinKeys {
mappings.mappings[getSinKey(s)] = getSinMetadata(s)
}
return nil
}
func (mappings *VatMappings) loadUrnKeys() error {
urns, err := mappings.StorageRepository.GetUrns()
if err != nil {
return err
}
for _, urn := range urns {
mappings.mappings[getUrnInkKey(urn.Ilk, urn.Guy)] = getUrnInkMetadata(urn.Ilk, urn.Guy)
mappings.mappings[getUrnArtKey(urn.Ilk, urn.Guy)] = getUrnArtMetadata(urn.Ilk, urn.Guy)
}
return nil
}
func getIlkTakeKey(ilk string) common.Hash {
return storage_diffs.GetMapping(IlksMappingIndex, ilk)
}
func getIlkTakeMetadata(ilk string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk}
return shared.GetStorageValueMetadata(IlkTake, keys, shared.Uint256)
}
func getIlkRateKey(ilk string) common.Hash {
return storage_diffs.GetIncrementedKey(getIlkTakeKey(ilk), 1)
}
func getIlkRateMetadata(ilk string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk}
return shared.GetStorageValueMetadata(IlkRate, keys, shared.Uint256)
}
func getIlkInkKey(ilk string) common.Hash {
return storage_diffs.GetIncrementedKey(getIlkTakeKey(ilk), 2)
}
func getIlkInkMetadata(ilk string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk}
return shared.GetStorageValueMetadata(IlkInk, keys, shared.Uint256)
}
func getIlkArtKey(ilk string) common.Hash {
return storage_diffs.GetIncrementedKey(getIlkTakeKey(ilk), 3)
}
func getIlkArtMetadata(ilk string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk}
return shared.GetStorageValueMetadata(IlkArt, keys, shared.Uint256)
}
func getUrnInkKey(ilk, guy string) common.Hash {
return storage_diffs.GetNestedMapping(UrnsMappingIndex, ilk, guy)
}
func getUrnInkMetadata(ilk, guy string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk, shared.Guy: guy}
return shared.GetStorageValueMetadata(UrnInk, keys, shared.Uint256)
}
func getUrnArtKey(ilk, guy string) common.Hash {
return storage_diffs.GetIncrementedKey(getUrnInkKey(ilk, guy), 1)
}
func getUrnArtMetadata(ilk, guy string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk, shared.Guy: guy}
return shared.GetStorageValueMetadata(UrnArt, keys, shared.Uint256)
}
func getGemKey(ilk, guy string) common.Hash {
return storage_diffs.GetNestedMapping(GemsMappingIndex, ilk, guy)
}
func getGemMetadata(ilk, guy string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Ilk: ilk, shared.Guy: guy}
return shared.GetStorageValueMetadata(Gem, keys, shared.Uint256)
}
func getDaiKey(guy string) common.Hash {
return storage_diffs.GetMapping(DaiMappingIndex, guy)
}
func getDaiMetadata(guy string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Guy: guy}
return shared.GetStorageValueMetadata(Dai, keys, shared.Uint256)
}
func getSinKey(guy string) common.Hash {
return storage_diffs.GetMapping(SinMappingIndex, guy)
}
func getSinMetadata(guy string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Guy: guy}
return shared.GetStorageValueMetadata(Sin, keys, shared.Uint256)
}
@@ -1,233 +0,0 @@
package vat_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/maker"
"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"
"math/big"
)
var _ = Describe("Vat storage mappings", func() {
var (
fakeIlk = "fakeIlk"
fakeGuy = "fakeGuy"
storageRepository *test_helpers.MockMakerStorageRepository
mappings vat.VatMappings
)
BeforeEach(func() {
storageRepository = &test_helpers.MockMakerStorageRepository{}
mappings = vat.VatMappings{StorageRepository: storageRepository}
})
Describe("looking up static keys", func() {
It("returns value metadata if key exists", func() {
Expect(mappings.Lookup(vat.DebtKey)).To(Equal(vat.DebtMetadata))
Expect(mappings.Lookup(vat.ViceKey)).To(Equal(vat.ViceMetadata))
})
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.GetDaiKeysCalled).To(BeTrue())
Expect(storageRepository.GetGemKeysCalled).To(BeTrue())
Expect(storageRepository.GetIlksCalled).To(BeTrue())
Expect(storageRepository.GetSinKeysCalled).To(BeTrue())
Expect(storageRepository.GetUrnsCalled).To(BeTrue())
})
It("returns error if dai keys lookup fails", func() {
storageRepository.GetDaiKeysError = fakes.FakeError
_, err := mappings.Lookup(fakes.FakeHash)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
It("returns error if gem keys lookup fails", func() {
storageRepository.GetGemKeysError = fakes.FakeError
_, err := mappings.Lookup(fakes.FakeHash)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
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 sin keys lookup fails", func() {
storageRepository.GetSinKeysError = fakes.FakeError
_, err := mappings.Lookup(fakes.FakeHash)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
It("returns error if urns lookup fails", func() {
storageRepository.GetUrnsError = fakes.FakeError
_, err := mappings.Lookup(fakes.FakeHash)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
Describe("ilk", func() {
It("returns value metadata for ilk take", func() {
storageRepository.Ilks = []string{fakeIlk}
ilkTakeKey := common.BytesToHash(crypto.Keccak256(common.FromHex("0x" + fakeIlk + vat.IlksMappingIndex)))
expectedMetadata := shared.StorageValueMetadata{
Name: vat.IlkTake,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk},
Type: shared.Uint256,
}
Expect(mappings.Lookup(ilkTakeKey)).To(Equal(expectedMetadata))
})
It("returns value metadata for ilk rate", func() {
storageRepository.Ilks = []string{fakeIlk}
ilkTakeBytes := crypto.Keccak256(common.FromHex("0x" + fakeIlk + vat.IlksMappingIndex))
ilkTakeAsInt := big.NewInt(0).SetBytes(ilkTakeBytes)
incrementedIlkTake := big.NewInt(0).Add(ilkTakeAsInt, big.NewInt(1))
ilkRateKey := common.BytesToHash(incrementedIlkTake.Bytes())
expectedMetadata := shared.StorageValueMetadata{
Name: vat.IlkRate,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk},
Type: shared.Uint256,
}
Expect(mappings.Lookup(ilkRateKey)).To(Equal(expectedMetadata))
})
It("returns value metadata for ilk Ink", func() {
storageRepository.Ilks = []string{fakeIlk}
ilkTakeBytes := crypto.Keccak256(common.FromHex("0x" + fakeIlk + vat.IlksMappingIndex))
ilkTakeAsInt := big.NewInt(0).SetBytes(ilkTakeBytes)
doubleIncrementedIlkTake := big.NewInt(0).Add(ilkTakeAsInt, big.NewInt(2))
ilkInkKey := common.BytesToHash(doubleIncrementedIlkTake.Bytes())
expectedMetadata := shared.StorageValueMetadata{
Name: vat.IlkInk,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk},
Type: shared.Uint256,
}
Expect(mappings.Lookup(ilkInkKey)).To(Equal(expectedMetadata))
})
It("returns value metadata for ilk Art", func() {
storageRepository.Ilks = []string{fakeIlk}
ilkTakeBytes := crypto.Keccak256(common.FromHex("0x" + fakeIlk + vat.IlksMappingIndex))
ilkTakeAsInt := big.NewInt(0).SetBytes(ilkTakeBytes)
tripleIncrementedIlkTake := big.NewInt(0).Add(ilkTakeAsInt, big.NewInt(3))
ilkArtKey := common.BytesToHash(tripleIncrementedIlkTake.Bytes())
expectedMetadata := shared.StorageValueMetadata{
Name: vat.IlkArt,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk},
Type: shared.Uint256,
}
Expect(mappings.Lookup(ilkArtKey)).To(Equal(expectedMetadata))
})
})
Describe("urn", func() {
It("returns value metadata for urn ink", func() {
storageRepository.Urns = []maker.Urn{{Ilk: fakeIlk, Guy: fakeGuy}}
encodedPrimaryMapIndex := crypto.Keccak256(common.FromHex("0x" + fakeIlk + vat.UrnsMappingIndex))
encodedSecondaryMapIndex := crypto.Keccak256(common.FromHex(fakeGuy), encodedPrimaryMapIndex)
urnInkKey := common.BytesToHash(encodedSecondaryMapIndex)
expectedMetadata := shared.StorageValueMetadata{
Name: vat.UrnInk,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk, shared.Guy: fakeGuy},
Type: shared.Uint256,
}
Expect(mappings.Lookup(urnInkKey)).To(Equal(expectedMetadata))
})
It("returns value metadata for urn art", func() {
storageRepository.Urns = []maker.Urn{{Ilk: fakeIlk, Guy: fakeGuy}}
encodedPrimaryMapIndex := crypto.Keccak256(common.FromHex("0x" + fakeIlk + vat.UrnsMappingIndex))
urnInkAsInt := big.NewInt(0).SetBytes(crypto.Keccak256(common.FromHex(fakeGuy), encodedPrimaryMapIndex))
incrementedUrnInk := big.NewInt(0).Add(urnInkAsInt, big.NewInt(1))
urnArtKey := common.BytesToHash(incrementedUrnInk.Bytes())
expectedMetadata := shared.StorageValueMetadata{
Name: vat.UrnArt,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk, shared.Guy: fakeGuy},
Type: shared.Uint256,
}
Expect(mappings.Lookup(urnArtKey)).To(Equal(expectedMetadata))
})
})
Describe("gem", func() {
It("returns value metadata for gem", func() {
storageRepository.GemKeys = []maker.Urn{{Ilk: fakeIlk, Guy: fakeGuy}}
encodedPrimaryMapIndex := crypto.Keccak256(common.FromHex("0x" + fakeIlk + vat.GemsMappingIndex))
encodedSecondaryMapIndex := crypto.Keccak256(common.FromHex(fakeGuy), encodedPrimaryMapIndex)
gemKey := common.BytesToHash(encodedSecondaryMapIndex)
expectedMetadata := shared.StorageValueMetadata{
Name: vat.Gem,
Keys: map[shared.Key]string{shared.Ilk: fakeIlk, shared.Guy: fakeGuy},
Type: shared.Uint256,
}
Expect(mappings.Lookup(gemKey)).To(Equal(expectedMetadata))
})
})
Describe("dai", func() {
It("returns value metadata for dai", func() {
storageRepository.DaiKeys = []string{fakeGuy}
daiKey := common.BytesToHash(crypto.Keccak256(common.FromHex("0x" + fakeGuy + vat.DaiMappingIndex)))
expectedMetadata := shared.StorageValueMetadata{
Name: vat.Dai,
Keys: map[shared.Key]string{shared.Guy: fakeGuy},
Type: shared.Uint256,
}
Expect(mappings.Lookup(daiKey)).To(Equal(expectedMetadata))
})
})
Describe("when sin key exists in the db", func() {
It("returns value metadata for sin", func() {
storageRepository.SinKeys = []string{fakeGuy}
sinKey := common.BytesToHash(crypto.Keccak256(common.FromHex("0x" + fakeGuy + vat.SinMappingIndex)))
expectedMetadata := shared.StorageValueMetadata{
Name: vat.Sin,
Keys: map[shared.Key]string{shared.Guy: fakeGuy},
Type: shared.Uint256,
}
Expect(mappings.Lookup(sinKey)).To(Equal(expectedMetadata))
})
})
})
})
@@ -1,27 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package storage_diffs
import (
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared"
)
type Repository interface {
Create(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, value interface{}) error
SetDB(db *postgres.DB)
}
@@ -1,45 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package shared
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"math/big"
)
func Decode(row StorageDiffRow, metadata StorageValueMetadata) (interface{}, error) {
switch metadata.Type {
case Uint256:
return decodeUint256(row.StorageValue.Bytes()), nil
case Address:
return decodeAddress(row.StorageValue.Bytes()), nil
case Bytes32:
return row.StorageValue.Hex(), nil
default:
panic(fmt.Sprintf("can't decode unknown type: %d", metadata.Type))
}
}
func decodeUint256(raw []byte) string {
n := big.NewInt(0).SetBytes(raw)
return n.String()
}
func decodeAddress(raw []byte) string {
return common.BytesToAddress(raw).Hex()
}
@@ -1,49 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package shared_test
import (
"github.com/ethereum/go-ethereum/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared"
"math/big"
)
var _ = Describe("Storage decoder", func() {
It("decodes uint256", func() {
fakeInt := common.HexToHash("0000000000000000000000000000000000000000000000000000000000000539")
row := shared.StorageDiffRow{StorageValue: fakeInt}
metadata := shared.StorageValueMetadata{Type: shared.Uint256}
result, err := shared.Decode(row, metadata)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(big.NewInt(0).SetBytes(fakeInt.Bytes()).String()))
})
It("decodes address", func() {
fakeAddress := common.HexToAddress("0x12345")
row := shared.StorageDiffRow{StorageValue: fakeAddress.Hash()}
metadata := shared.StorageValueMetadata{Type: shared.Address}
result, err := shared.Decode(row, metadata)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(fakeAddress.Hex()))
})
})
@@ -1,53 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package shared
import (
"fmt"
)
type ErrContractNotFound struct {
Contract string
}
func (e ErrContractNotFound) Error() string {
return fmt.Sprintf("transformer not found for contract: %s", e.Contract)
}
type ErrMetadataMalformed struct {
MissingData Key
}
func (e ErrMetadataMalformed) Error() string {
return fmt.Sprintf("storage metadata malformed: missing %s", e.MissingData)
}
type ErrRowMalformed struct {
Length int
}
func (e ErrRowMalformed) Error() string {
return fmt.Sprintf("storage row malformed: length %d, expected %d", e.Length, ExpectedRowLength)
}
type ErrStorageKeyNotFound struct {
Key string
}
func (e ErrStorageKeyNotFound) Error() string {
return fmt.Sprintf("unknown storage key: %s", e.Key)
}
@@ -1,49 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package shared
import (
"github.com/ethereum/go-ethereum/common"
"strconv"
)
const ExpectedRowLength = 5
type StorageDiffRow struct {
Contract common.Address
BlockHash common.Hash `db:"block_hash"`
BlockHeight int `db:"block_height"`
StorageKey common.Hash `db:"storage_key"`
StorageValue common.Hash `db:"storage_value"`
}
func FromStrings(csvRow []string) (StorageDiffRow, error) {
if len(csvRow) != ExpectedRowLength {
return StorageDiffRow{}, ErrRowMalformed{Length: len(csvRow)}
}
height, err := strconv.Atoi(csvRow[2])
if err != nil {
return StorageDiffRow{}, err
}
return StorageDiffRow{
Contract: common.HexToAddress(csvRow[0]),
BlockHash: common.HexToHash(csvRow[1]),
BlockHeight: height,
StorageKey: common.HexToHash(csvRow[3]),
StorageValue: common.HexToHash(csvRow[4]),
}, nil
}
@@ -1,57 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package shared_test
import (
"github.com/ethereum/go-ethereum/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared"
)
var _ = Describe("Storage row parsing", func() {
It("converts an array of strings to a row struct", func() {
contract := "0x123"
blockHash := "0x456"
blockHeight := "789"
storageKey := "0x987"
storageValue := "0x654"
data := []string{contract, blockHash, blockHeight, storageKey, storageValue}
result, err := shared.FromStrings(data)
Expect(err).NotTo(HaveOccurred())
Expect(result.Contract).To(Equal(common.HexToAddress(contract)))
Expect(result.BlockHash).To(Equal(common.HexToHash(blockHash)))
Expect(result.BlockHeight).To(Equal(789))
Expect(result.StorageKey).To(Equal(common.HexToHash(storageKey)))
Expect(result.StorageValue).To(Equal(common.HexToHash(storageValue)))
})
It("returns an error if row is missing data", func() {
_, err := shared.FromStrings([]string{"0x123"})
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(shared.ErrRowMalformed{Length: 1}))
})
It("returns error if block height malformed", func() {
_, err := shared.FromStrings([]string{"", "", "", "", ""})
Expect(err).To(HaveOccurred())
})
})
@@ -1,35 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package shared_test
import (
"github.com/sirupsen/logrus"
"io/ioutil"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestShared(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Shared Suite")
}
var _ = BeforeSuite(func() {
logrus.SetOutput(ioutil.Discard)
})
@@ -1,47 +0,0 @@
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package shared
type ValueType int
const (
Uint256 ValueType = iota
Bytes32
Address
)
type Key string
const (
Ilk Key = "ilk"
Guy Key = "guy"
Flip Key = "flip"
)
type StorageValueMetadata struct {
Name string
Keys map[Key]string
Type ValueType
}
func GetStorageValueMetadata(name string, keys map[Key]string, t ValueType) StorageValueMetadata {
return StorageValueMetadata{
Name: name,
Keys: keys,
Type: t,
}
}