Run smart contract before putBlock() in commitBlock()
@@ -644,6 +644,14 @@ func (bc *blockchain) Validator() Validator {
//=====================================
// commitBlock commits a block to the chain
func (bc *blockchain) commitBlock(blk *Block) error {
+ // update state factory
+ if bc.sf != nil {
+ ExecuteContracts(blk, bc)
+ if err := bc.sf.CommitStateChanges(blk.Height(), blk.Transfers, blk.Votes, blk.Executions); err != nil {
+ return err
+ }
+ }
+ // write block into DB
if err := bc.dao.putBlock(blk); err != nil {
return err
}
@@ -652,14 +660,6 @@ func (bc *blockchain) commitBlock(blk *Block) error {
defer bc.mu.Unlock()
bc.tipHeight = blk.Header.height
bc.tipHash = blk.HashBlock()
-
- // update state factory
- if bc.sf == nil {
- return nil
- }
- ExecuteContracts(blk, bc)
- err := bc.sf.CommitStateChanges(blk.Height(), blk.Transfers, blk.Votes, blk.Executions)
-
logger.Info().Uint64("height", blk.Header.height).Msg("commit a block")
- return err
+ return nil
}
