erigon-pulse/common/dbutils/composite_keys_test.go
ledgerwatch 5ea590c18e
State cache switching writes to reads during commit (#1368)
* State cache init

* More code

* Fix lint

* More tests

* More tests

* More tests

* Fix test

* Transformations

* remove writeQueue, before fixing the tests

* Fix tests

* Add more tests, incarnation to the code items

* Fix lint

* Fix lint

* Remove shards prototype, add incarnation to the state reader code

* Clean up and replace cache in call_traces stage

* fix flaky test

* Save changes

* Readers to use addrHash, writes - addresses

* Fix lint

* Fix lint

* More accurate tracking of size

* Optimise for smaller write batches

* Attempt to integrate state cache into Execution stage

* cacheSize to default flags

* Print correct cache sizes and batch sizes

* cacheSize in the integration

* Fix tests

* Fix lint

* Remove print

* Fix exec stage

* Fix test

* Refresh sequence on write

* No double increment

* heap.Remove

* Try to fix alignment

* Refactoring, adding hashItems

* More changes

* Fix compile errors

* Fix lint

* Wrapping cached reader

* Wrap writer into cached writer

* Turn state cache off by default

* Fix plain state writer

* Fix for code/storage mixup

* Fix tests

* Fix clique test

* Better fix for the tests

* Add test and fix some more

* Fix compile error|

* More functions

* Fixes

* Fix for the tests

* sepatate DeletedFlag and AbsentFlag

* Minor fixes

* Test refactoring

* More changes

* Fix some tests

* More test fixes

* More test fixes

* Fix lint

* Move blockchain_test to be able to use stagedsync

* More fixes

* Fixes and cleanup

* Fix tests in turbo/stages

* Fix lint

* Fix lint

* Intemediate

* Fix tests

* Intemediate

* More fixes

* Compilation fixes

* More fixes

* Fix compile errors

* More test fixes

* More fixes

* More test fixes

* Fix compile error

* Fixes

* Fix

* Fix

* More fixes

* Fixes

* More fixes and cleanup

* Further fix

* Check gas used and bloom with header

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
2020-12-08 09:44:29 +00:00

97 lines
4.0 KiB
Go

package dbutils
import (
"testing"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/stretchr/testify/assert"
)
func TestHeaderTypeDetection(t *testing.T) {
// good input
headerHashKey := common.Hex2Bytes("00000000000000006e")
assert.False(t, IsHeaderKey(headerHashKey))
assert.False(t, IsHeaderTDKey(headerHashKey))
assert.True(t, IsHeaderHashKey(headerHashKey))
headerKey := common.Hex2Bytes("0000000000004321ed7240d411782ae438adfd85f7edad373cea722318c6e7f5f5b30f9abc9b36fd")
assert.True(t, IsHeaderKey(headerKey))
assert.False(t, IsHeaderTDKey(headerKey))
assert.False(t, IsHeaderHashKey(headerKey))
headerTdKey := common.Hex2Bytes("0000000000004321ed7240d411782ae438adfd85f7edad373cea722318c6e7f5f5b30f9abc9b36fd74")
assert.False(t, IsHeaderKey(headerTdKey))
assert.True(t, IsHeaderTDKey(headerTdKey))
assert.False(t, IsHeaderHashKey(headerTdKey))
// bad input
emptyKey := common.Hex2Bytes("")
assert.False(t, IsHeaderKey(emptyKey))
assert.False(t, IsHeaderTDKey(emptyKey))
assert.False(t, IsHeaderHashKey(emptyKey))
tooLongKey := common.Hex2Bytes("0000000000004321ed7240d411782ae438adfd85f7edad373cea722318c6e7f5f5b30f9abc9b36fd0000000000004321ed7240d411782ae438adfd85f7edad373cea722318c6e7f5f5b30f9abc9b36fd0000000000004321ed7240d411782ae438adfd85f7edad373cea722318c6e7f5f5b30f9abc9b36fd0000000000004321ed7240d411782ae438adfd85f7edad373cea722318c6e7f5f5b30f9abc9b36fd")
assert.False(t, IsHeaderKey(tooLongKey))
assert.False(t, IsHeaderTDKey(tooLongKey))
assert.False(t, IsHeaderHashKey(tooLongKey))
notRelatedInput := common.Hex2Bytes("alex")
assert.False(t, IsHeaderKey(notRelatedInput))
assert.False(t, IsHeaderTDKey(notRelatedInput))
assert.False(t, IsHeaderHashKey(notRelatedInput))
}
func TestPlainParseStoragePrefix(t *testing.T) {
expectedAddr := common.HexToAddress("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c")
expectedIncarnation := uint64(999000999)
prefix := PlainGenerateStoragePrefix(expectedAddr[:], expectedIncarnation)
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) {
expectedAddr := common.HexToAddress("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c")
expectedIncarnation := uint64(999000999)
expectedKey := common.HexToHash("0x58833f949125129fb8c6c93d2c6003c5bab7c0b116d695f4ca137b1debf4e472")
compositeKey := PlainGenerateCompositeStorageKey(expectedAddr.Bytes(), expectedIncarnation, expectedKey.Bytes())
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) {
expectedAddrHash, _ := common.HashData(common.HexToAddress("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c").Bytes())
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) {
expectedAddrHash, _ := common.HashData(common.HexToAddress("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c").Bytes())
expectedIncarnation := uint64(999000999)
expectedKey := common.HexToHash("0x58833f949125129fb8c6c93d2c6003c5bab7c0b116d695f4ca137b1debf4e472")
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")
}