Skip to content

Commit

Permalink
safebox: always rollback on tx/block validation failure
Browse files Browse the repository at this point in the history
  • Loading branch information
xiphon committed Mar 31, 2019
1 parent 1b9d563 commit 5d71231
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 0 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ func (this *Blockchain) ProcessNewBlocks(blocks []safebox.SerializedBlock, preSa
defer this.lock.Unlock()

if err := this.processNewBlocksUnsafe(blocks, preSave); err != nil {
this.safebox.Rollback()
return err
}
this.safebox.Merge()
Expand Down
16 changes: 16 additions & 0 deletions safebox/safebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (s *Safebox) Rollback() {
s.lock.Lock()
defer s.lock.Unlock()

s.rollbackUnsafe()
}

func (s *Safebox) rollbackUnsafe() {
s.accounter.Rollback()
}

Expand All @@ -141,6 +145,7 @@ func (s *Safebox) GetUpdatedPacks() []uint32 {
return s.accounter.GetUpdatedPacks()
}

func (this *Safebox) processOperationsUnsafe(miner *crypto.Public, timestamp uint32, operations []tx.CommonOperation, difficulty *big.Int) (map[*accounter.Account]map[uint32]uint32, error) {
blockIndex := this.accounter.GetHeight()
height := blockIndex + 1
reward := getReward(height)
Expand Down Expand Up @@ -185,6 +190,17 @@ func (s *Safebox) GetUpdatedPacks() []uint32 {
return affectedByTxes, nil
}

func (s *Safebox) ProcessOperations(miner *crypto.Public, timestamp uint32, operations []tx.CommonOperation, difficulty *big.Int) (map[*accounter.Account]map[uint32]uint32, error) {
s.lock.Lock()
defer s.lock.Unlock()

affectedByTxes, err := s.processOperationsUnsafe(miner, timestamp, operations, difficulty)
if err != nil {
s.rollbackUnsafe()
}
return affectedByTxes, err
}

func (this *Safebox) GetLastTimestamps(count uint32) (timestamps []uint32) {
this.lock.RLock()
defer this.lock.RUnlock()
Expand Down

0 comments on commit 5d71231

Please sign in to comment.