-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[facotry] add factoryWithHeight for archive mode
- Loading branch information
Showing
11 changed files
with
288 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2020 IoTeX Foundation | ||
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability | ||
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. | ||
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file. | ||
|
||
package factory | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/iotexproject/iotex-core/v2/action/protocol" | ||
"github.com/iotexproject/iotex-core/v2/state" | ||
) | ||
|
||
type factoryWithHeight struct { | ||
*factory | ||
height uint64 | ||
} | ||
|
||
func (sf *factoryWithHeight) State(s interface{}, opts ...protocol.StateOption) (uint64, error) { | ||
sf.mutex.RLock() | ||
defer sf.mutex.RUnlock() | ||
cfg, err := processOptions(opts...) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if cfg.Keys != nil { | ||
return 0, errors.Wrap(ErrNotSupported, "Read state with keys option has not been implemented yet") | ||
} | ||
if sf.height > sf.currentChainHeight { | ||
return 0, errors.Errorf("query height %d is higher than tip height %d", sf.height, sf.currentChainHeight) | ||
} | ||
return sf.height, sf.stateAtHeight(sf.height, cfg.Namespace, cfg.Key, s) | ||
} | ||
|
||
func (sf *factoryWithHeight) stateAtHeight(height uint64, ns string, key []byte, s interface{}) error { | ||
if !sf.saveHistory { | ||
return ErrNoArchiveData | ||
} | ||
tlt, err := newTwoLayerTrie(ArchiveTrieNamespace, sf.dao, fmt.Sprintf("%s-%d", ArchiveTrieRootKey, height), false) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to generate trie for %d", height) | ||
} | ||
if err := tlt.Start(context.Background()); err != nil { | ||
return err | ||
} | ||
defer tlt.Stop(context.Background()) | ||
|
||
value, err := readStateFromTLT(tlt, ns, key) | ||
if err != nil { | ||
return err | ||
} | ||
return state.Deserialize(s, value) | ||
} | ||
|
||
func (sf *factoryWithHeight) States(opts ...protocol.StateOption) (uint64, state.Iterator, error) { | ||
sf.mutex.RLock() | ||
defer sf.mutex.RUnlock() | ||
if sf.height > sf.currentChainHeight { | ||
return 0, nil, errors.Errorf("query height %d is higher than tip height %d", sf.height, sf.currentChainHeight) | ||
} | ||
cfg, err := processOptions(opts...) | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
if cfg.Keys != nil { | ||
return 0, nil, errors.Wrap(ErrNotSupported, "Read states with keys option has not been implemented yet") | ||
} | ||
tlt, err := newTwoLayerTrie(ArchiveTrieNamespace, sf.dao, fmt.Sprintf("%s-%d", ArchiveTrieRootKey, sf.height), false) | ||
if err != nil { | ||
return 0, nil, errors.Wrapf(err, "failed to generate trie for %d", sf.height) | ||
} | ||
if err := tlt.Start(context.Background()); err != nil { | ||
return 0, nil, err | ||
} | ||
defer tlt.Stop(context.Background()) | ||
keys, values, err := readStatesFromTLT(tlt, cfg.Namespace, cfg.Keys) | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
iter, err := state.NewIterator(keys, values) | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
return sf.height, iter, err | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.