diff --git a/chainservice/chainservice.go b/chainservice/chainservice.go index 9bfc00e12b..1df5ee33f9 100644 --- a/chainservice/chainservice.go +++ b/chainservice/chainservice.go @@ -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") diff --git a/state/factory/factory.go b/state/factory/factory.go index 093906d6b2..8afcd046bb 100644 --- a/state/factory/factory.go +++ b/state/factory/factory.go @@ -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{ @@ -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 }