Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rollback outputs #2008

Merged
merged 3 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/block/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,25 @@ func GetRollbacksHashWithDiffArr(dbTx *sqldb.DbTransaction, bId int64) ([]byte,
if err != nil {
return nil, err
}
arr := make([]string, 0, len(rollbackTxs))
arr := make([]string, 0)
for _, row := range rollbackTxs {
data, err := json.Marshal(row)
if err != nil {
continue
}
arr = append(arr, crypto.HashHex(data))
}
spentInfos, err := sqldb.GetBlockOutputs(dbTx, bId)
if err != nil {
return nil, err
}
for _, row := range spentInfos {
data, err := json.Marshal(row)
if err != nil {
continue
}
arr = append(arr, crypto.HashHex(data))
}
sort.Strings(arr)
marshal, _ := json.Marshal(arr)
return crypto.Hash(marshal), nil
Expand Down
6 changes: 6 additions & 0 deletions packages/rollback/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func rollbackBlock(dbTx *sqldb.DbTransaction, block *block.Block) error {
return err
}
}

err := sqldb.RollbackOutputs(block.Header.BlockId, dbTx, logger)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("updating outputs by block id")
return err
}

return nil
}
24 changes: 24 additions & 0 deletions packages/storage/sqldb/spent_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package sqldb

import (
"github.com/IBAX-io/go-ibax/packages/consts"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -75,3 +77,25 @@ func GetTxOutputs(db *DbTransaction, keyIds []int64) ([]SpentInfo, error) {
}
return result, nil
}

func RollbackOutputs(blockID int64, db *DbTransaction, logger *log.Entry) error {
err := GetDB(db).Exec(`UPDATE spent_info SET input_tx_hash= null , input_index=0 WHERE input_tx_hash in ( SELECT output_tx_hash FROM "spent_info" WHERE block_id = ? )`, blockID).Error
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Errorf("updating input_tx_hash rollback outputs by blockID : %d", blockID)
return err
}

err = GetDB(db).Exec(`DELETE FROM spent_info WHERE block_id = ? `, blockID).Error
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Errorf("deleting rollback outputs by blockID : %d", blockID)
return err
}

return nil
}

func GetBlockOutputs(dbTx *DbTransaction, blockID int64) ([]SpentInfo, error) {
var result []SpentInfo
err := GetDB(dbTx).Where("block_id = ?", blockID).Find(&result).Error
return result, err
}