Skip to content

Commit

Permalink
add patch to factory (#3187)
Browse files Browse the repository at this point in the history
Co-authored-by: Haaai <55118568+Liuhaai@users.noreply.github.com>
  • Loading branch information
CoderZhi and Liuhaai committed Mar 16, 2022
1 parent 887ebab commit ad44742
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion chainservice/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ func New(
}
sf, err = factory.NewStateDB(cfg, opts...)
} else {
sf, err = factory.NewFactory(cfg, factory.DefaultTrieOption(), factory.RegistryOption(registry))
sf, err = factory.NewFactory(
cfg,
factory.DefaultTrieOption(),
factory.RegistryOption(registry),
factory.DefaultTriePatchOption(),
)
}
if err != nil {
return nil, errors.Wrapf(err, "Failed to create state factory")
Expand Down
19 changes: 19 additions & 0 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ func SkipBlockValidationOption() Option {
}
}

// DefaultTriePatchOption loads patchs
func DefaultTriePatchOption() Option {
return func(sf *factory, cfg config.Config) (err error) {
sf.ps, err = newPatchStore(cfg.Chain.TrieDBPatchFile)
return
}
}

// NewFactory creates a new state factory
func NewFactory(cfg config.Config, opts ...Option) (Factory, error) {
sf := &factory{
Expand Down Expand Up @@ -278,6 +286,17 @@ func (sf *factory) newWorkingSet(ctx context.Context, height uint64) (*workingSe
if err := store.Start(ctx); err != nil {
return nil, err
}
for _, p := range sf.ps.Get(height) {
if p.Type == _Delete {
if err := store.Delete(p.Namespace, p.Key); err != nil {
return nil, err
}
} else {
if err := store.Put(p.Namespace, p.Key, p.Value); err != nil {
return nil, err
}
}
}

return newWorkingSet(height, store), nil
}
Expand Down

0 comments on commit ad44742

Please sign in to comment.