Skip to content

Commit

Permalink
Merge pull request optimism-java#167 from fearlessfe/fix-state-storage
Browse files Browse the repository at this point in the history
fix: state storage err
  • Loading branch information
fearlessfe authored Sep 23, 2024
2 parents a8464fc + df92775 commit 0dd477d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
15 changes: 6 additions & 9 deletions portalnetwork/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func (s *StateStorage) Put(contentKey []byte, contentId []byte, content []byte)
keyType := contentKey[0]
switch keyType {
case AccountTrieNodeType:
return s.putAccountTrieNode(contentKey[1:], content)
return s.putAccountTrieNode(contentKey[1:], contentId, content)
case ContractStorageTrieNodeType:
return s.putContractStorageTrieNode(contentKey[1:], content)
return s.putContractStorageTrieNode(contentKey[1:], contentId, content)
case ContractByteCodeType:
return s.putContractBytecode(contentKey[1:], content)
return s.putContractBytecode(contentKey[1:], contentId, content)
}
return errors.New("unknown content type")
}
Expand All @@ -55,7 +55,7 @@ func (s *StateStorage) Radius() *uint256.Int {
return s.store.Radius()
}

func (s *StateStorage) putAccountTrieNode(contentKey []byte, content []byte) error {
func (s *StateStorage) putAccountTrieNode(contentKey []byte, contentId []byte, content []byte) error {
accountKey := &AccountTrieNodeKey{}
err := accountKey.Deserialize(codec.NewDecodingReader(bytes.NewReader(contentKey), uint64(len(contentKey))))
if err != nil {
Expand All @@ -81,15 +81,14 @@ func (s *StateStorage) putAccountTrieNode(contentKey []byte, content []byte) err
if err != nil {
return err
}
contentId := defaultContentIdFunc(contentKey)
err = s.store.Put(contentId, contentId, contentValueBuf.Bytes())
if err != nil {
s.log.Error("failed to save data after validate", "type", contentKey[0], "key", contentKey[1:], "value", content)
}
return nil
}

func (s *StateStorage) putContractStorageTrieNode(contentKey []byte, content []byte) error {
func (s *StateStorage) putContractStorageTrieNode(contentKey []byte, contentId []byte, content []byte) error {
contractStorageKey := &ContractStorageTrieNodeKey{}
err := contractStorageKey.Deserialize(codec.NewDecodingReader(bytes.NewReader(contentKey), uint64(len(contentKey))))
if err != nil {
Expand All @@ -116,15 +115,14 @@ func (s *StateStorage) putContractStorageTrieNode(contentKey []byte, content []b
if err != nil {
return err
}
contentId := defaultContentIdFunc(contentKey)
err = s.store.Put(contentId, contentId, contentValueBuf.Bytes())
if err != nil {
s.log.Error("failed to save data after validate", "type", contentKey[0], "key", contentKey[1:], "value", content)
}
return nil
}

func (s *StateStorage) putContractBytecode(contentKey []byte, content []byte) error {
func (s *StateStorage) putContractBytecode(contentKey []byte, contentId []byte, content []byte) error {
contractByteCodeKey := &ContractBytecodeKey{}
err := contractByteCodeKey.Deserialize(codec.NewDecodingReader(bytes.NewReader(contentKey), uint64(len(contentKey))))
if err != nil {
Expand All @@ -147,7 +145,6 @@ func (s *StateStorage) putContractBytecode(contentKey []byte, content []byte) er
if err != nil {
return err
}
contentId := defaultContentIdFunc(contentKey)
err = s.store.Put(contentId, contentId, contentValueBuf.Bytes())
if err != nil {
s.log.Error("failed to save data after validate", "type", contentKey[0], "key", contentKey[1:], "value", content)
Expand Down
4 changes: 2 additions & 2 deletions portalnetwork/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func TestStorage(t *testing.T) {
require.NoError(t, err)
for _, tt := range cases {
contentKey := hexutil.MustDecode(tt.ContentKey)
contentId := defaultContentIdFunc(contentKey[1:])
contentId := defaultContentIdFunc(contentKey)
err = stateStorage.Put(contentKey, contentId, hexutil.MustDecode(tt.ContentValueOffer))
require.NoError(t, err)
res, err := stateStorage.Get(contentKey[1:], contentId)
res, err := stateStorage.Get(contentKey, contentId)
require.NoError(t, err)
require.Equal(t, hexutil.MustDecode(tt.ContentValueRetrieval), res)
}
Expand Down

0 comments on commit 0dd477d

Please sign in to comment.