Use map for key in storage value metadata

- Enables inserting multiple keys for one storage row (e.g. ilk and urn)
This commit is contained in:
Rob Mulholand 2019-02-05 16:22:26 -06:00
parent 09abcb841a
commit dba4e48a3e
5 changed files with 19 additions and 13 deletions

View File

@ -40,7 +40,7 @@ var (
DripKey = common.HexToHash(storage_diffs.IndexFive) DripKey = common.HexToHash(storage_diffs.IndexFive)
DripMetadata = shared.StorageValueMetadata{ DripMetadata = shared.StorageValueMetadata{
Name: PitDrip, Name: PitDrip,
Key: "", Keys: nil,
Type: shared.Address, Type: shared.Address,
} }
@ -50,7 +50,7 @@ var (
LineKey = common.HexToHash(storage_diffs.IndexThree) LineKey = common.HexToHash(storage_diffs.IndexThree)
LineMetadata = shared.StorageValueMetadata{ LineMetadata = shared.StorageValueMetadata{
Name: PitLine, Name: PitLine,
Key: "", Keys: nil,
Type: shared.Uint256, Type: shared.Uint256,
} }
@ -58,7 +58,7 @@ var (
LiveKey = common.HexToHash(storage_diffs.IndexTwo) LiveKey = common.HexToHash(storage_diffs.IndexTwo)
LiveMetadata = shared.StorageValueMetadata{ LiveMetadata = shared.StorageValueMetadata{
Name: PitLive, Name: PitLive,
Key: "", Keys: nil,
Type: shared.Uint256, Type: shared.Uint256,
} }
@ -66,7 +66,7 @@ var (
VatKey = common.HexToHash(storage_diffs.IndexFour) VatKey = common.HexToHash(storage_diffs.IndexFour)
VatMetadata = shared.StorageValueMetadata{ VatMetadata = shared.StorageValueMetadata{
Name: PitVat, Name: PitVat,
Key: "", Keys: nil,
Type: shared.Address, Type: shared.Address,
} }
) )
@ -126,7 +126,7 @@ func getSpotKey(ilk string) common.Hash {
func getSpotMetadata(ilk string) shared.StorageValueMetadata { func getSpotMetadata(ilk string) shared.StorageValueMetadata {
return shared.StorageValueMetadata{ return shared.StorageValueMetadata{
Name: IlkSpot, Name: IlkSpot,
Key: ilk, Keys: map[shared.Key]string{shared.Ilk: ilk},
Type: shared.Uint256, Type: shared.Uint256,
} }
} }
@ -140,7 +140,7 @@ func getLineKey(ilk string) common.Hash {
func getLineMetadata(ilk string) shared.StorageValueMetadata { func getLineMetadata(ilk string) shared.StorageValueMetadata {
return shared.StorageValueMetadata{ return shared.StorageValueMetadata{
Name: IlkLine, Name: IlkLine,
Key: ilk, Keys: map[shared.Key]string{shared.Ilk: ilk},
Type: shared.Uint256, Type: shared.Uint256,
} }
} }

View File

@ -52,7 +52,7 @@ var _ = Describe("Pit storage mappings", func() {
ilkSpotKey := common.BytesToHash(crypto.Keccak256(common.FromHex("0x" + fakeIlk + pit.IlkSpotIndex))) ilkSpotKey := common.BytesToHash(crypto.Keccak256(common.FromHex("0x" + fakeIlk + pit.IlkSpotIndex)))
expectedMetadata := shared.StorageValueMetadata{ expectedMetadata := shared.StorageValueMetadata{
Name: pit.IlkSpot, Name: pit.IlkSpot,
Key: fakeIlk, Keys: map[shared.Key]string{shared.Ilk: fakeIlk},
Type: shared.Uint256, Type: shared.Uint256,
} }
@ -70,7 +70,7 @@ var _ = Describe("Pit storage mappings", func() {
ilkLineKey := common.BytesToHash(incrementedIlkSpot.Bytes()) ilkLineKey := common.BytesToHash(incrementedIlkSpot.Bytes())
expectedMetadata := shared.StorageValueMetadata{ expectedMetadata := shared.StorageValueMetadata{
Name: pit.IlkLine, Name: pit.IlkLine,
Key: fakeIlk, Keys: map[shared.Key]string{shared.Ilk: fakeIlk},
Type: shared.Uint256, Type: shared.Uint256,
} }

View File

@ -32,9 +32,9 @@ func (repository *PitStorageRepository) SetDB(db *postgres.DB) {
func (repository PitStorageRepository) Create(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, value interface{}) error { func (repository PitStorageRepository) Create(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, value interface{}) error {
switch metadata.Name { switch metadata.Name {
case IlkLine: case IlkLine:
return repository.insertIlkLine(blockNumber, blockHash, metadata.Key, value.(string)) return repository.insertIlkLine(blockNumber, blockHash, metadata.Keys[shared.Ilk], value.(string))
case IlkSpot: case IlkSpot:
return repository.insertIlkSpot(blockNumber, blockHash, metadata.Key, value.(string)) return repository.insertIlkSpot(blockNumber, blockHash, metadata.Keys[shared.Ilk], value.(string))
case PitDrip: case PitDrip:
return repository.insertPitDrip(blockNumber, blockHash, value.(string)) return repository.insertPitDrip(blockNumber, blockHash, value.(string))
case PitLine: case PitLine:

View File

@ -48,7 +48,7 @@ var _ = Describe("Pit storage repository", func() {
expectedLine := "12345" expectedLine := "12345"
ilkLineMetadata := shared.StorageValueMetadata{ ilkLineMetadata := shared.StorageValueMetadata{
Name: pit.IlkLine, Name: pit.IlkLine,
Key: expectedIlk, Keys: map[shared.Key]string{shared.Ilk: expectedIlk},
Type: shared.Uint256, Type: shared.Uint256,
} }
err = repo.Create(blockNumber, blockHash, ilkLineMetadata, expectedLine) err = repo.Create(blockNumber, blockHash, ilkLineMetadata, expectedLine)
@ -73,7 +73,7 @@ var _ = Describe("Pit storage repository", func() {
expectedSpot := "12345" expectedSpot := "12345"
ilkSpotMetadata := shared.StorageValueMetadata{ ilkSpotMetadata := shared.StorageValueMetadata{
Name: pit.IlkSpot, Name: pit.IlkSpot,
Key: expectedIlk, Keys: map[shared.Key]string{shared.Ilk: expectedIlk},
Type: shared.Uint256, Type: shared.Uint256,
} }
err = repo.Create(blockNumber, blockHash, ilkSpotMetadata, expectedSpot) err = repo.Create(blockNumber, blockHash, ilkSpotMetadata, expectedSpot)

View File

@ -24,8 +24,14 @@ const (
Address Address
) )
type Key int
const (
Ilk Key = iota
)
type StorageValueMetadata struct { type StorageValueMetadata struct {
Name string Name string
Key string Keys map[Key]string
Type ValueType Type ValueType
} }