2019-12-06 09:02:43 +00:00
|
|
|
package dbutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2019-12-06 09:02:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2019-12-06 09:02:43 +00:00
|
|
|
)
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func TestPlainParseStoragePrefix(t *testing.T) {
|
2023-01-13 18:12:18 +00:00
|
|
|
expectedAddr := libcommon.HexToAddress("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c")
|
2020-05-15 07:52:45 +00:00
|
|
|
expectedIncarnation := uint64(999000999)
|
|
|
|
|
2020-06-09 05:52:38 +00:00
|
|
|
prefix := PlainGenerateStoragePrefix(expectedAddr[:], expectedIncarnation)
|
2020-05-15 07:52:45 +00:00
|
|
|
|
|
|
|
addr, incarnation := PlainParseStoragePrefix(prefix)
|
|
|
|
|
|
|
|
assert.Equal(t, expectedAddr, addr, "address should be extracted")
|
|
|
|
assert.Equal(t, expectedIncarnation, incarnation, "incarnation should be extracted")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlainParseCompositeStorageKey(t *testing.T) {
|
2023-01-13 18:12:18 +00:00
|
|
|
expectedAddr := libcommon.HexToAddress("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c")
|
2020-05-15 07:52:45 +00:00
|
|
|
expectedIncarnation := uint64(999000999)
|
2023-01-13 18:12:18 +00:00
|
|
|
expectedKey := libcommon.HexToHash("0x58833f949125129fb8c6c93d2c6003c5bab7c0b116d695f4ca137b1debf4e472")
|
2020-05-15 07:52:45 +00:00
|
|
|
|
2020-12-08 09:44:29 +00:00
|
|
|
compositeKey := PlainGenerateCompositeStorageKey(expectedAddr.Bytes(), expectedIncarnation, expectedKey.Bytes())
|
2020-05-15 07:52:45 +00:00
|
|
|
|
|
|
|
addr, incarnation, key := PlainParseCompositeStorageKey(compositeKey)
|
|
|
|
|
|
|
|
assert.Equal(t, expectedAddr, addr, "address should be extracted")
|
|
|
|
assert.Equal(t, expectedIncarnation, incarnation, "incarnation should be extracted")
|
|
|
|
assert.Equal(t, expectedKey, key, "key should be extracted")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseStoragePrefix(t *testing.T) {
|
2023-01-13 18:12:18 +00:00
|
|
|
expectedAddrHash, _ := common.HashData(libcommon.HexToAddress("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c").Bytes())
|
2020-05-15 07:52:45 +00:00
|
|
|
expectedIncarnation := uint64(999000999)
|
|
|
|
|
|
|
|
prefix := GenerateStoragePrefix(expectedAddrHash[:], expectedIncarnation)
|
|
|
|
|
|
|
|
addrHash, incarnation := ParseStoragePrefix(prefix)
|
|
|
|
|
|
|
|
assert.Equal(t, expectedAddrHash, addrHash, "address should be extracted")
|
|
|
|
assert.Equal(t, expectedIncarnation, incarnation, "incarnation should be extracted")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCompositeStorageKey(t *testing.T) {
|
2023-01-13 18:12:18 +00:00
|
|
|
expectedAddrHash, _ := common.HashData(libcommon.HexToAddress("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c").Bytes())
|
2020-05-15 07:52:45 +00:00
|
|
|
expectedIncarnation := uint64(999000999)
|
2023-01-13 18:12:18 +00:00
|
|
|
expectedKey := libcommon.HexToHash("0x58833f949125129fb8c6c93d2c6003c5bab7c0b116d695f4ca137b1debf4e472")
|
2020-05-15 07:52:45 +00:00
|
|
|
|
|
|
|
compositeKey := GenerateCompositeStorageKey(expectedAddrHash, expectedIncarnation, expectedKey)
|
|
|
|
|
|
|
|
addrHash, incarnation, key := ParseCompositeStorageKey(compositeKey)
|
|
|
|
|
|
|
|
assert.Equal(t, expectedAddrHash, addrHash, "address should be extracted")
|
|
|
|
assert.Equal(t, expectedIncarnation, incarnation, "incarnation should be extracted")
|
|
|
|
assert.Equal(t, expectedKey, key, "key should be extracted")
|
|
|
|
}
|