Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Add tests for Cat storage diff transformer
Browse files Browse the repository at this point in the history
Update schema after rebase migrations

Fix small issues from review
  • Loading branch information
m0ar committed Feb 20, 2019
1 parent cd6611d commit 5746de7
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 21 deletions.
4 changes: 2 additions & 2 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions pkg/transformers/storage_diffs/maker/cat/cat_suite_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
35 changes: 17 additions & 18 deletions pkg/transformers/storage_diffs/maker/cat/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ func (mappings CatMappings) loadFlipsKeys() error {
}

last := maxFlip.Int64()
var flipStr string
for flip := 0; int64(flip) <= last; flip++ {
flipStr = strconv.Itoa(flip)
flipStr := strconv.Itoa(flip)
mappings.mappings[getFlipIlkKey(flipStr)] = getFlipIlkMetadata(flipStr)
mappings.mappings[getFlipUrnKey(flipStr)] = getFlipUrnMetadata(flipStr)
mappings.mappings[getFlipInkKey(flipStr)] = getFlipInkMetadata(flipStr)
Expand All @@ -162,38 +161,38 @@ func (mappings CatMappings) loadFlipsKeys() error {
return nil
}

func getFlipIlkKey(nflip string) common.Hash {
return storage_diffs.GetMapping(FlipsMappingIndex, nflip)
func getFlipIlkKey(flip string) common.Hash {
return storage_diffs.GetMapping(FlipsMappingIndex, flip)
}

func getFlipIlkMetadata(nflip string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Flip: nflip}
func getFlipIlkMetadata(flip string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Flip: flip}
return shared.GetStorageValueMetadata(FlipIlk, keys, shared.Bytes32)
}

func getFlipUrnKey(nflip string) common.Hash {
return storage_diffs.GetIncrementedKey(getFlipIlkKey(nflip), 1)
func getFlipUrnKey(flip string) common.Hash {
return storage_diffs.GetIncrementedKey(getFlipIlkKey(flip), 1)
}

func getFlipUrnMetadata(nflip string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Flip: nflip}
func getFlipUrnMetadata(flip string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Flip: flip}
return shared.GetStorageValueMetadata(FlipUrn, keys, shared.Bytes32)
}

func getFlipInkKey(nflip string) common.Hash {
return storage_diffs.GetIncrementedKey(getFlipIlkKey(nflip), 2)
func getFlipInkKey(flip string) common.Hash {
return storage_diffs.GetIncrementedKey(getFlipIlkKey(flip), 2)
}

func getFlipInkMetadata(nflip string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Flip: nflip}
func getFlipInkMetadata(flip string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Flip: flip}
return shared.GetStorageValueMetadata(FlipInk, keys, shared.Uint256)
}

func getFlipTabKey(nflip string) common.Hash {
return storage_diffs.GetIncrementedKey(getFlipIlkKey(nflip), 3)
func getFlipTabKey(flip string) common.Hash {
return storage_diffs.GetIncrementedKey(getFlipIlkKey(flip), 3)
}

func getFlipTabMetadata(nflip string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Flip: nflip}
func getFlipTabMetadata(flip string) shared.StorageValueMetadata {
keys := map[shared.Key]string{shared.Flip: flip}
return shared.GetStorageValueMetadata(FlipTab, keys, shared.Uint256)
}
177 changes: 177 additions & 0 deletions pkg/transformers/storage_diffs/maker/cat/mappings_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})
})
2 changes: 1 addition & 1 deletion pkg/transformers/storage_diffs/maker/cat/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (repository *CatStorageRepository) Create(blockNumber int, blockHash string
case FlipTab:
return repository.insertFlipTab(blockNumber, blockHash, metadata, value.(string))
default:
panic(fmt.Sprintf("unrecognized vat contract storage name: %s", metadata.Name))
panic(fmt.Sprintf("unrecognized cat contract storage name: %s", metadata.Name))
}
}

Expand Down
Loading

0 comments on commit 5746de7

Please sign in to comment.