@@ -40,6 +40,8 @@ type batchService struct {
4040
4141 checksum hash.Hash // checksum hasher
4242 resync bool
43+
44+ pendingChainState * postage.ChainState
4345}
4446
4547type Interface interface {
@@ -95,15 +97,22 @@ func New(
9597 }
9698 }
9799
98- return & batchService {stateStore , storer , logger .WithName (loggerName ).Register (), listener , owner , batchListener , sum , resync }, nil
100+ return & batchService {stateStore : stateStore , storer : storer , logger : logger .WithName (loggerName ).Register (), listener : listener , owner : owner , batchListener : batchListener , checksum : sum , resync : resync }, nil
101+ }
102+
103+ func (svc * batchService ) getChainState () * postage.ChainState {
104+ if svc .pendingChainState != nil {
105+ return svc .pendingChainState
106+ }
107+ return svc .storer .GetChainState ()
99108}
100109
101110// Create will create a new batch with the given ID, owner value and depth and
102111// stores it in the BatchedStore.
103112func (svc * batchService ) Create (id , owner []byte , totalAmout , normalisedBalance * big.Int , depth , bucketDepth uint8 , immutable bool , txHash common.Hash ) error {
104- // don't add batches which have value which equals total cumulative
113+ // dont add batches which have value which equals total cumulative
105114 // payout or that are going to expire already within the next couple of blocks
106- val := big .NewInt (0 ).Add (svc .storer . GetChainState ().TotalAmount , svc .storer . GetChainState ().CurrentPrice )
115+ val := big .NewInt (0 ).Add (svc .getChainState ().TotalAmount , svc .getChainState ().CurrentPrice )
107116 if normalisedBalance .Cmp (val ) <= 0 {
108117 // don't do anything
109118 return fmt .Errorf ("batch service: batch %x: %w" , id , ErrZeroValueBatch )
@@ -112,7 +121,7 @@ func (svc *batchService) Create(id, owner []byte, totalAmout, normalisedBalance
112121 ID : id ,
113122 Owner : owner ,
114123 Value : normalisedBalance ,
115- Start : svc .storer . GetChainState ().Block ,
124+ Start : svc .getChainState ().Block ,
116125 Depth : depth ,
117126 BucketDepth : bucketDepth ,
118127 Immutable : immutable ,
@@ -196,10 +205,13 @@ func (svc *batchService) UpdateDepth(id []byte, depth uint8, normalisedBalance *
196205// UpdatePrice implements the EventUpdater interface. It sets the current
197206// price from the chain in the service chain state.
198207func (svc * batchService ) UpdatePrice (price * big.Int , txHash common.Hash ) error {
199- cs := svc .storer . GetChainState ()
208+ cs := svc .getChainState ()
200209 cs .CurrentPrice = price
201- if err := svc .storer .PutChainState (cs ); err != nil {
202- return fmt .Errorf ("put chain state: %w" , err )
210+
211+ if svc .pendingChainState == nil {
212+ if err := svc .storer .PutChainState (cs ); err != nil {
213+ return fmt .Errorf ("put chain state: %w" , err )
214+ }
203215 }
204216
205217 sum , err := svc .updateChecksum (txHash )
@@ -212,7 +224,7 @@ func (svc *batchService) UpdatePrice(price *big.Int, txHash common.Hash) error {
212224}
213225
214226func (svc * batchService ) UpdateBlockNumber (blockNumber uint64 ) error {
215- cs := svc .storer . GetChainState ()
227+ cs := svc .getChainState ()
216228 if blockNumber == cs .Block {
217229 return nil
218230 }
@@ -223,17 +235,27 @@ func (svc *batchService) UpdateBlockNumber(blockNumber uint64) error {
223235
224236 cs .TotalAmount .Add (cs .TotalAmount , diff .Mul (diff , cs .CurrentPrice ))
225237 cs .Block = blockNumber
226- if err := svc .storer .PutChainState (cs ); err != nil {
227- return fmt .Errorf ("put chain state: %w" , err )
238+
239+ if svc .pendingChainState == nil {
240+ if err := svc .storer .PutChainState (cs ); err != nil {
241+ return fmt .Errorf ("put chain state: %w" , err )
242+ }
228243 }
229244
230245 svc .logger .Debug ("block height updated" , "new_block" , blockNumber )
231246 return nil
232247}
233248func (svc * batchService ) TransactionStart () error {
249+ svc .pendingChainState = svc .storer .GetChainState ()
234250 return svc .stateStore .Put (dirtyDBKey , true )
235251}
236252func (svc * batchService ) TransactionEnd () error {
253+ if svc .pendingChainState != nil {
254+ if err := svc .storer .PutChainState (svc .pendingChainState ); err != nil {
255+ return fmt .Errorf ("put chain state: %w" , err )
256+ }
257+ svc .pendingChainState = nil
258+ }
237259 return svc .stateStore .Delete (dirtyDBKey )
238260}
239261
0 commit comments