diff --git a/common/changeset/storage_changeset.go b/common/changeset/storage_changeset.go index 79aa55cb8..c33cd9d31 100644 --- a/common/changeset/storage_changeset.go +++ b/common/changeset/storage_changeset.go @@ -5,8 +5,9 @@ import ( "encoding/binary" "errors" "fmt" - "github.com/ledgerwatch/turbo-geth/common" "sort" + + "github.com/ledgerwatch/turbo-geth/common" ) const ( @@ -32,11 +33,10 @@ func NewStorageChangeSet() *ChangeSet { Storage ChangeSet is serialized in the following manner in order to facilitate binary search: numOfElements uint32 -numOfUniqAddrHashes uint32 -numOfUniqKeys uint32 +numOfUniqAddrHashes uint16 [addrHashes] []common.Hash [idOfAddr:idOfKey] [uint8/uint16/uint32:common.Hash...] (depends on numOfUniqAddrHashes) -numOfUint8Values uint8 +numOfUint8Values uint16 numOfUint16Values uint16 numOfUint32Values uint16 [len(val0), len(val0)+len(val1), ..., len(val0)+len(val1)+...+len(val_{numOfUint8Values-1})] []uint8 @@ -526,12 +526,18 @@ func (b StorageChangeSetBytes) Find(k []byte) ([]byte, error) { //here should be binary search too elemLength := uint32(getNumOfBytesByLen(int(numOfUniqueItems))) + encodedAddHashID := make([]byte, elemLength) + writeKeyRow(addHashID, encodedAddHashID) for i := uint32(0); i < numOfItems; i++ { elemStart := storageEnodingStartElem + storageEnodingLengthOfDict + uint32(numOfUniqueItems)*(common.HashLength) + i*(elemLength+common.HashLength) + if !bytes.Equal(encodedAddHashID, b[elemStart:elemStart+elemLength]) { + continue + } + if !bytes.Equal(k[common.HashLength+common.IncarnationLength:2*common.HashLength+common.IncarnationLength], b[elemStart+elemLength:elemStart+elemLength+common.HashLength]) { continue } diff --git a/common/changeset/storage_changeset_test.go b/common/changeset/storage_changeset_test.go index 6f0cb7158..b18dd4eac 100644 --- a/common/changeset/storage_changeset_test.go +++ b/common/changeset/storage_changeset_test.go @@ -3,14 +3,16 @@ package changeset import ( "bytes" "fmt" - "github.com/ledgerwatch/turbo-geth/common" - "github.com/ledgerwatch/turbo-geth/common/dbutils" - "github.com/stretchr/testify/assert" "math/rand" "reflect" "sort" "strconv" "testing" + + "github.com/ledgerwatch/turbo-geth/common" + "github.com/ledgerwatch/turbo-geth/common/dbutils" + "github.com/ledgerwatch/turbo-geth/common/hexutil" + "github.com/stretchr/testify/assert" ) const ( @@ -223,3 +225,12 @@ func TestEncodingStorageWithoutNotDefaultIncarnationFind(t *testing.T) { f(t, 10000) }) } + +func TestFind(t *testing.T) { + // storage changes at block 51385 + changes := hexutil.MustDecode("0x0000000a0002353e456a1b25b4640cbf753b6094458a4e38929a0c5bbe22904d9d08abc6d11adf396ae6730bdcd2e30c871da8978de3251900d45eaf15c0ba4d8a691c1d251300290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563003f4920f7f194a9a91a5d5422dc6313c329b82e533bce5e6614fbd13d4da7a32800b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56301405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace0159bd035209cfbd05133d9c61cd860212636c4146228286761610b6e8811e537a018db697c2abd4284e7bb9aae7273fd67d061dd6ed4282b8382a3ed29d9cfaa1bb0198da4b407718e49fb0fe900da3b7fb2c3e0fed30f4148729225f24534e3e471b01b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601c2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b000a000000000101020a0b0b0b0b0c0d130e0429d069189e0013041115") + key := hexutil.MustDecode("0xdf396ae6730bdcd2e30c871da8978de3251900d45eaf15c0ba4d8a691c1d2513fffffffffffffffeb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6") + val, err := StorageChangeSetBytes(changes).Find(key) + assert.NoError(t, err) + assert.Equal(t, hexutil.MustDecode("0x11"), val) +} diff --git a/common/dbutils/history_index.go b/common/dbutils/history_index.go index 179bba6f7..54404bdf7 100644 --- a/common/dbutils/history_index.go +++ b/common/dbutils/history_index.go @@ -2,7 +2,7 @@ package dbutils import ( "encoding/binary" - "fmt" + "github.com/ledgerwatch/turbo-geth/common/math" ) @@ -112,7 +112,6 @@ Loop: func (hi *HistoryIndexBytes) Search(v uint64) (uint64, bool) { if len(*hi) < 4 { - fmt.Println(1) return 0, false } numOfElements := binary.LittleEndian.Uint32((*hi)[0:LenBytes]) @@ -120,7 +119,6 @@ func (hi *HistoryIndexBytes) Search(v uint64) (uint64, bool) { var itemLen uint32 if numOfElements == 0 { - fmt.Println(2) return 0, false }