Skip to content

Commit

Permalink
[workingset] add GetDirty() to retrieve latest value
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Jan 16, 2025
1 parent 4157ba3 commit 1a4069f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 36 deletions.
8 changes: 8 additions & 0 deletions db/kvstorewithbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
// and transaction with multiple writes
KVStoreWithBuffer interface {
KVStore
GetDirty(string, []byte) ([]byte, bool)
withBuffer
}

Expand Down Expand Up @@ -176,6 +177,13 @@ func (kvb *kvStoreWithBuffer) Get(ns string, key []byte) ([]byte, error) {
return value, err
}

func (kvb *kvStoreWithBuffer) GetDirty(ns string, key []byte) ([]byte, bool) {
if value, err := kvb.buffer.Get(ns, key); err == nil {
return value, true
}
return nil, false
}

func (kvb *kvStoreWithBuffer) Put(ns string, key, value []byte) error {
kvb.buffer.Put(ns, key, value, fmt.Sprintf("faild to put %x in %s", key, ns))
return nil
Expand Down
10 changes: 2 additions & 8 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (sf *factory) Validate(ctx context.Context, blk *block.Block) error {
if err := ws.ValidateBlock(ctx, blk); err != nil {
return errors.Wrap(err, "failed to validate block with workingset in factory")
}
sf.putIntoWorkingSets(key, ws)
sf.workingsets.Add(key, ws)
}
receipts, err := ws.Receipts()
if err != nil {
Expand Down Expand Up @@ -398,7 +398,7 @@ func (sf *factory) NewBlockBuilder(

blkCtx := protocol.MustGetBlockCtx(ctx)
key := generateWorkingSetCacheKey(blkBuilder.GetCurrentBlockHeader(), blkCtx.Producer.String())
sf.putIntoWorkingSets(key, ws)
sf.workingsets.Add(key, ws)
return blkBuilder, nil
}

Expand Down Expand Up @@ -591,9 +591,3 @@ func (sf *factory) getFromWorkingSets(ctx context.Context, key hash.Hash256) (*w
}
return ws, false, nil
}

func (sf *factory) putIntoWorkingSets(key hash.Hash256, ws *workingSet) {
sf.mutex.Lock()
defer sf.mutex.Unlock()
sf.workingsets.Add(key, ws)
}
28 changes: 0 additions & 28 deletions state/factory/workingset.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,6 @@ func (ws *workingSet) validate(ctx context.Context) error {
return nil
}

func (ws *workingSet) runActions(
ctx context.Context,
elps []*action.SealedEnvelope,
) ([]*action.Receipt, error) {
// Handle actions
receipts := make([]*action.Receipt, 0)
blkCtx := protocol.MustGetBlockCtx(ctx)
fCtx := protocol.MustGetFeatureCtx(ctx)
for _, elp := range elps {
ctxWithActionContext, err := withActionCtx(ctx, elp)
if err != nil {
return nil, err
}
receipt, err := ws.runAction(protocol.WithBlockCtx(ctxWithActionContext, blkCtx), elp)
if err != nil {
return nil, errors.Wrap(err, "error when run action")
}
receipts = append(receipts, receipt)
if fCtx.EnableDynamicFeeTx && receipt.PriorityFee() != nil {
(&blkCtx.AccumulatedTips).Add(&blkCtx.AccumulatedTips, receipt.PriorityFee())
}
}
if fCtx.CorrectTxLogIndex {
updateReceiptIndex(receipts)
}
return receipts, nil
}

func withActionCtx(ctx context.Context, selp *action.SealedEnvelope) (context.Context, error) {
var actionCtx protocol.ActionCtx
var err error
Expand Down
5 changes: 5 additions & 0 deletions state/factory/workingsetstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type (
workingSetStore interface {
db.KVStoreBasic
GetDirty(string, []byte) ([]byte, bool)
Commit() error
States(string, [][]byte) ([][]byte, [][]byte, error)
Digest() hash.Hash256
Expand All @@ -39,6 +40,10 @@ func (store *workingSetStoreCommon) WriteView(name string, value interface{}) er
return store.view.Write(name, value)
}

func (store *workingSetStoreCommon) GetDirty(ns string, key []byte) ([]byte, bool) {
return store.flusher.KVStoreWithBuffer().GetDirty(ns, key)
}

func (store *workingSetStoreCommon) Put(ns string, key []byte, value []byte) error {
store.flusher.KVStoreWithBuffer().MustPut(ns, key, value)
return nil
Expand Down

0 comments on commit 1a4069f

Please sign in to comment.