-
Notifications
You must be signed in to change notification settings - Fork 328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[factory] simplify interface for archive mode #4474
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,8 +87,6 @@ type ( | |
NewBlockBuilder(context.Context, actpool.ActPool, func(action.Envelope) (*action.SealedEnvelope, error)) (*block.Builder, error) | ||
PutBlock(context.Context, *block.Block) error | ||
DeleteTipBlock(context.Context, *block.Block) error | ||
StateAtHeight(uint64, interface{}, ...protocol.StateOption) error | ||
StatesAtHeight(uint64, ...protocol.StateOption) (state.Iterator, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these 2 are included in |
||
WorkingSet(context.Context) (protocol.StateManager, error) | ||
WorkingSetAtHeight(context.Context, uint64) (protocol.StateManager, error) | ||
} | ||
|
@@ -272,6 +270,31 @@ func (sf *factory) newWorkingSet(ctx context.Context, height uint64) (*workingSe | |
if err != nil { | ||
return nil, err | ||
} | ||
return sf.createSfWorkingSet(ctx, height, store) | ||
} | ||
|
||
func (sf *factory) newWorkingSetAtHeight(ctx context.Context, height uint64) (*workingSet, error) { | ||
span := tracer.SpanFromContext(ctx) | ||
span.AddEvent("factory.newWorkingSet") | ||
defer span.End() | ||
|
||
g := genesis.MustExtractGenesisContext(ctx) | ||
flusher, err := db.NewKVStoreFlusher( | ||
sf.dao, | ||
batch.NewCachedBatch(), | ||
sf.flusherOptions(!g.IsEaster(height))..., | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
store, err := newFactoryWorkingSetStoreAtHeight(sf.protocolView, flusher, height) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return sf.createSfWorkingSet(ctx, height, store) | ||
} | ||
|
||
func (sf *factory) createSfWorkingSet(ctx context.Context, height uint64, store workingSetStore) (*workingSet, error) { | ||
if err := store.Start(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -286,7 +309,6 @@ func (sf *factory) newWorkingSet(ctx context.Context, height uint64) (*workingSe | |
} | ||
} | ||
} | ||
|
||
return newWorkingSet(height, store), nil | ||
} | ||
|
||
|
@@ -388,16 +410,20 @@ func (sf *factory) WorkingSet(ctx context.Context) (protocol.StateManager, error | |
} | ||
|
||
func (sf *factory) WorkingSetAtHeight(ctx context.Context, height uint64) (protocol.StateManager, error) { | ||
if !sf.saveHistory { | ||
return nil, ErrNoArchiveData | ||
} | ||
sf.mutex.Lock() | ||
defer sf.mutex.Unlock() | ||
return sf.newWorkingSet(ctx, height+1) | ||
if height > sf.currentChainHeight { | ||
return nil, errors.Errorf("query height %d is higher than tip height %d", height, sf.currentChainHeight) | ||
} | ||
return sf.newWorkingSetAtHeight(ctx, height) | ||
} | ||
|
||
// PutBlock persists all changes in RunActions() into the DB | ||
func (sf *factory) PutBlock(ctx context.Context, blk *block.Block) error { | ||
sf.mutex.Lock() | ||
timer := sf.timerFactory.NewTimer("Commit") | ||
sf.mutex.Unlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when reviewing the usage of |
||
defer timer.End() | ||
producer := blk.PublicKey().Address() | ||
if producer == nil { | ||
|
@@ -456,52 +482,6 @@ func (sf *factory) DeleteTipBlock(_ context.Context, _ *block.Block) error { | |
return errors.Wrap(ErrNotSupported, "cannot delete tip block from factory") | ||
} | ||
|
||
// StateAtHeight returns a confirmed state at height -- archive mode | ||
func (sf *factory) StateAtHeight(height uint64, s interface{}, opts ...protocol.StateOption) error { | ||
sf.mutex.RLock() | ||
defer sf.mutex.RUnlock() | ||
cfg, err := processOptions(opts...) | ||
if err != nil { | ||
return err | ||
} | ||
if cfg.Keys != nil { | ||
return errors.Wrap(ErrNotSupported, "Read state with keys option has not been implemented yet") | ||
} | ||
if height > sf.currentChainHeight { | ||
return errors.Errorf("query height %d is higher than tip height %d", height, sf.currentChainHeight) | ||
} | ||
return sf.stateAtHeight(height, cfg.Namespace, cfg.Key, s) | ||
} | ||
|
||
// StatesAtHeight returns a set states in the state factory at height -- archive mode | ||
func (sf *factory) StatesAtHeight(height uint64, opts ...protocol.StateOption) (state.Iterator, error) { | ||
sf.mutex.RLock() | ||
defer sf.mutex.RUnlock() | ||
if height > sf.currentChainHeight { | ||
return nil, errors.Errorf("query height %d is higher than tip height %d", height, sf.currentChainHeight) | ||
} | ||
cfg, err := processOptions(opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if cfg.Keys != nil { | ||
return 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, height), false) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to generate trie for %d", height) | ||
} | ||
if err := tlt.Start(context.Background()); err != nil { | ||
return nil, err | ||
} | ||
defer tlt.Stop(context.Background()) | ||
keys, values, err := readStatesFromTLT(tlt, cfg.Namespace, cfg.Keys) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return state.NewIterator(keys, values) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. included in |
||
// State returns a confirmed state in the state factory | ||
func (sf *factory) State(s interface{}, opts ...protocol.StateOption) (uint64, error) { | ||
sf.mutex.RLock() | ||
|
@@ -573,26 +553,6 @@ func legacyKeyLen() int { | |
return 20 | ||
} | ||
|
||
func (sf *factory) 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 *factory) createGenesisStates(ctx context.Context) error { | ||
ws, err := sf.newWorkingSet(ctx, 0) | ||
if err != nil { | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -343,6 +343,9 @@ func (ws *workingSet) State(s interface{}, opts ...protocol.StateOption) (uint64 | |
if err != nil { | ||
return ws.height, err | ||
} | ||
if cfg.Keys != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why add this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's in the removed |
||
return 0, errors.Wrap(ErrNotSupported, "Read state with keys option has not been implemented yet") | ||
} | ||
value, err := ws.store.Get(cfg.Namespace, cfg.Key) | ||
if err != nil { | ||
return ws.height, err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can combine codes after getting the specified StateReader