From d5047f28fb5172fb45426deeaab3829de42784b6 Mon Sep 17 00:00:00 2001 From: ReyisaRuby Date: Tue, 7 Jan 2025 08:20:57 +0800 Subject: [PATCH 1/6] support sql write/double read Signed-off-by: ReyisaRuby --- config/storage.go | 3 ++ core/startup/startup.go | 6 ++- core/txdb/txdb.go | 105 +++++++++++++++++++++++++++++++++++----- 3 files changed, 99 insertions(+), 15 deletions(-) diff --git a/config/storage.go b/config/storage.go index bd693fe..db730b4 100644 --- a/config/storage.go +++ b/config/storage.go @@ -7,6 +7,9 @@ type KVconf struct { Path string `toml:"path"` // distributed kvdb Hosts []string `toml:"hosts"` + + UseSQlDbConf bool `toml:"use_sql_db"` + SQLDbConf SqlDbConf `toml:"sql_db"` } type SqlDbConf struct { diff --git a/core/startup/startup.go b/core/startup/startup.go index 9b0fb04..c53aec6 100644 --- a/core/startup/startup.go +++ b/core/startup/startup.go @@ -1,8 +1,12 @@ package startup import ( + "os" + "path" + "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" + "github.com/yu-org/yu/apps/synchronizer" "github.com/yu-org/yu/config" "github.com/yu-org/yu/core/blockchain" @@ -17,8 +21,6 @@ import ( "github.com/yu-org/yu/infra/p2p" "github.com/yu-org/yu/infra/storage/kv" "github.com/yu-org/yu/utils/codec" - "os" - "path" ) var ( diff --git a/core/txdb/txdb.go b/core/txdb/txdb.go index 5bd103c..013a983 100644 --- a/core/txdb/txdb.go +++ b/core/txdb/txdb.go @@ -2,9 +2,12 @@ package txdb import ( "github.com/sirupsen/logrus" + . "github.com/yu-org/yu/common" + "github.com/yu-org/yu/config" . "github.com/yu-org/yu/core/types" "github.com/yu-org/yu/infra/storage/kv" + "github.com/yu-org/yu/infra/storage/sql" ) const ( @@ -13,23 +16,57 @@ const ( ) type TxDB struct { - nodeType int + nodeType int + txnKV kv.KV receiptKV kv.KV + + enableUseSql bool + db sql.SqlDB +} + +type TxnDBSchema struct { + Type string `gorm:"type:varchar(10)"` + Key string `gorm:"primaryKey;type:text"` + Value string `gorm:"type:text"` } -func NewTxDB(nodeTyp int, kvdb kv.Kvdb) ItxDB { - return &TxDB{ +func (TxnDBSchema) TableName() string { + return "txndb" +} + +func NewTxDB(nodeTyp int, kvdb kv.Kvdb, kvdbConf *config.KVconf) (ItxDB, error) { + txdb := &TxDB{ nodeType: nodeTyp, txnKV: kvdb.New(Txns), receiptKV: kvdb.New(Results), } + if kvdbConf.UseSQlDbConf { + db, err := sql.NewSqlDB(&kvdbConf.SQLDbConf) + if err != nil { + return nil, err + } + txdb.db = db + txdb.enableUseSql = true + } + if err := txdb.db.AutoMigrate(&TxnDBSchema{}); err != nil { + return nil, err + } + return txdb, nil } func (bb *TxDB) GetTxn(txnHash Hash) (*SignedTxn, error) { if bb.nodeType == LightNode { return nil, nil } + if bb.enableUseSql { + var records []TxnDBSchema + err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "txn", string(txnHash.Bytes())).Find(&records).Error + // find result in sql database + if err == nil && len(records) > 0 { + return DecodeSignedTxn([]byte(records[0].Value)) + } + } byt, err := bb.txnKV.Get(txnHash.Bytes()) if err != nil { return nil, err @@ -46,18 +83,11 @@ func (bb *TxDB) GetTxns(txnHashes []Hash) ([]*SignedTxn, error) { } txns := make([]*SignedTxn, 0) for _, txnHash := range txnHashes { - byt, err := bb.txnKV.Get(txnHash.Bytes()) - if err != nil { - return nil, err - } - if byt == nil { - continue - } - signedTxn, err := DecodeSignedTxn(byt) + result, err := bb.GetTxn(txnHash) if err != nil { return nil, err } - txns = append(txns, signedTxn) + txns = append(txns, result) } return txns, nil } @@ -66,6 +96,13 @@ func (bb *TxDB) ExistTxn(txnHash Hash) bool { if bb.nodeType == LightNode { return false } + if bb.enableUseSql { + var records []TxnDBSchema + err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "txn", string(txnHash.Bytes())).Find(&records).Error + if err == nil && len(records) > 0 { + return true + } + } return bb.txnKV.Exist(txnHash.Bytes()) } @@ -73,6 +110,20 @@ func (bb *TxDB) SetTxns(txns []*SignedTxn) error { if bb.nodeType == LightNode { return nil } + if bb.enableUseSql { + for _, txn := range txns { + txbyt, err := txn.Encode() + if err != nil { + logrus.Errorf("TxDB.SetTxns set tx(%s) failed: %v", txn.TxnHash.String(), err) + return err + } + if err := bb.db.Db().Exec("insert into txndb (type,key,value) values (?,?,?)", "txn", string(txn.TxnHash.Bytes()), string(txbyt)).Error; err != nil { + logrus.Errorf("Insert TxDB.SetTxns tx(%s) failed: %v", txn.TxnHash.String(), err) + return err + } + } + return nil + } kvtx, err := bb.txnKV.NewKvTxn() if err != nil { return err @@ -92,11 +143,18 @@ func (bb *TxDB) SetTxns(txns []*SignedTxn) error { } func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) error { + if bb.enableUseSql { + for txHash, receipt := range receipts { + if err := bb.SetReceipt(txHash, receipt); err != nil { + return err + } + } + return nil + } kvtx, err := bb.receiptKV.NewKvTxn() if err != nil { return err } - for txHash, receipt := range receipts { byt, err := receipt.Encode() if err != nil { @@ -112,6 +170,16 @@ func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) error { } func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) error { + if bb.enableUseSql { + byt, err := receipt.Encode() + if err != nil { + return err + } + if err := bb.db.Db().Exec("insert into txndb (type,key,value) values (?,?,?)", "receipt", string(txHash.Bytes()), string(byt)).Error; err != nil { + return err + } + return nil + } byt, err := receipt.Encode() if err != nil { return err @@ -120,6 +188,17 @@ func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) error { } func (bb *TxDB) GetReceipt(txHash Hash) (*Receipt, error) { + if bb.enableUseSql { + var records []TxnDBSchema + err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "receipt", string(txHash.Bytes())).Find(&records).Error + if err == nil && len(records) > 0 { + receipt := new(Receipt) + err = receipt.Decode([]byte(records[0].Value)) + if err == nil { + return receipt, nil + } + } + } byt, err := bb.receiptKV.Get(txHash.Bytes()) if err != nil { logrus.Errorf("TxDB.GetReceipt(%s), failed: %s, error: %v", txHash.String(), string(byt), err) From 070ef339315d468f148215d15b7c00c2e6b13fc3 Mon Sep 17 00:00:00 2001 From: ReyisaRuby Date: Tue, 7 Jan 2025 11:28:53 +0800 Subject: [PATCH 2/6] fix Signed-off-by: ReyisaRuby --- core/startup/startup.go | 3 ++- core/txdb/txdb.go | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/startup/startup.go b/core/startup/startup.go index c53aec6..965ae6b 100644 --- a/core/startup/startup.go +++ b/core/startup/startup.go @@ -70,7 +70,8 @@ func InitKernel(cfg *config.KernelConf) *kernel.Kernel { } if TxnDB == nil { - TxnDB = txdb.NewTxDB(cfg.NodeType, kvdb) + TxnDB, err = txdb.NewTxDB(cfg.NodeType, kvdb, &cfg.KVDB) + logrus.Fatal("init kvdb error: ", err) } if Chain == nil { Chain = blockchain.NewBlockChain(cfg.NodeType, &cfg.BlockChain, TxnDB) diff --git a/core/txdb/txdb.go b/core/txdb/txdb.go index 013a983..02686e4 100644 --- a/core/txdb/txdb.go +++ b/core/txdb/txdb.go @@ -41,16 +41,16 @@ func NewTxDB(nodeTyp int, kvdb kv.Kvdb, kvdbConf *config.KVconf) (ItxDB, error) txnKV: kvdb.New(Txns), receiptKV: kvdb.New(Results), } - if kvdbConf.UseSQlDbConf { + if kvdbConf != nil && kvdbConf.UseSQlDbConf { db, err := sql.NewSqlDB(&kvdbConf.SQLDbConf) if err != nil { return nil, err } txdb.db = db txdb.enableUseSql = true - } - if err := txdb.db.AutoMigrate(&TxnDBSchema{}); err != nil { - return nil, err + if err := txdb.db.AutoMigrate(&TxnDBSchema{}); err != nil { + return nil, err + } } return txdb, nil } From e4c96d2c5254af3e212e6231a5ae9b7ae1fc1f38 Mon Sep 17 00:00:00 2001 From: ReyisaRuby Date: Tue, 7 Jan 2025 14:13:07 +0800 Subject: [PATCH 3/6] add lock Signed-off-by: ReyisaRuby --- core/txdb/txdb.go | 149 ++++++++++++++++++++++++++++++---------------- 1 file changed, 98 insertions(+), 51 deletions(-) diff --git a/core/txdb/txdb.go b/core/txdb/txdb.go index 02686e4..19bf5a1 100644 --- a/core/txdb/txdb.go +++ b/core/txdb/txdb.go @@ -1,6 +1,8 @@ package txdb import ( + "sync" + "github.com/sirupsen/logrus" . "github.com/yu-org/yu/common" @@ -18,13 +20,58 @@ const ( type TxDB struct { nodeType int - txnKV kv.KV - receiptKV kv.KV + txnKV *txnkvdb + receiptKV *receipttxnkvdb enableUseSql bool db sql.SqlDB } +type txnkvdb struct { + sync.RWMutex + txnKV kv.KV +} + +func (t *txnkvdb) GetTxn(txnHash Hash) (*SignedTxn, error) { + t.RLock() + defer t.RUnlock() + byt, err := t.txnKV.Get(txnHash.Bytes()) + if err != nil { + return nil, err + } + if byt == nil { + return nil, nil + } + return DecodeSignedTxn(byt) +} + +func (t *txnkvdb) ExistTxn(txnHash Hash) bool { + t.RLock() + defer t.RUnlock() + return t.txnKV.Exist(txnHash.Bytes()) +} + +func (t *txnkvdb) SetTxns(txns []*SignedTxn) error { + t.Lock() + defer t.Unlock() + kvtx, err := t.txnKV.NewKvTxn() + if err != nil { + return err + } + for _, txn := range txns { + txbyt, err := txn.Encode() + if err != nil { + logrus.Errorf("TxDB.SetTxns set tx(%s) failed: %v", txn.TxnHash.String(), err) + return err + } + err = kvtx.Set(txn.TxnHash.Bytes(), txbyt) + if err != nil { + return err + } + } + return kvtx.Commit() +} + type TxnDBSchema struct { Type string `gorm:"type:varchar(10)"` Key string `gorm:"primaryKey;type:text"` @@ -38,8 +85,8 @@ func (TxnDBSchema) TableName() string { func NewTxDB(nodeTyp int, kvdb kv.Kvdb, kvdbConf *config.KVconf) (ItxDB, error) { txdb := &TxDB{ nodeType: nodeTyp, - txnKV: kvdb.New(Txns), - receiptKV: kvdb.New(Results), + txnKV: &txnkvdb{txnKV: kvdb.New(Txns)}, + receiptKV: &receipttxnkvdb{receiptKV: kvdb.New(Results)}, } if kvdbConf != nil && kvdbConf.UseSQlDbConf { db, err := sql.NewSqlDB(&kvdbConf.SQLDbConf) @@ -67,14 +114,7 @@ func (bb *TxDB) GetTxn(txnHash Hash) (*SignedTxn, error) { return DecodeSignedTxn([]byte(records[0].Value)) } } - byt, err := bb.txnKV.Get(txnHash.Bytes()) - if err != nil { - return nil, err - } - if byt == nil { - return nil, nil - } - return DecodeSignedTxn(byt) + return bb.txnKV.GetTxn(txnHash) } func (bb *TxDB) GetTxns(txnHashes []Hash) ([]*SignedTxn, error) { @@ -103,7 +143,7 @@ func (bb *TxDB) ExistTxn(txnHash Hash) bool { return true } } - return bb.txnKV.Exist(txnHash.Bytes()) + return bb.txnKV.ExistTxn(txnHash) } func (bb *TxDB) SetTxns(txns []*SignedTxn) error { @@ -124,22 +164,7 @@ func (bb *TxDB) SetTxns(txns []*SignedTxn) error { } return nil } - kvtx, err := bb.txnKV.NewKvTxn() - if err != nil { - return err - } - for _, txn := range txns { - txbyt, err := txn.Encode() - if err != nil { - logrus.Errorf("TxDB.SetTxns set tx(%s) failed: %v", txn.TxnHash.String(), err) - return err - } - err = kvtx.Set(txn.TxnHash.Bytes(), txbyt) - if err != nil { - return err - } - } - return kvtx.Commit() + return bb.txnKV.SetTxns(txns) } func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) error { @@ -151,22 +176,7 @@ func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) error { } return nil } - kvtx, err := bb.receiptKV.NewKvTxn() - if err != nil { - return err - } - for txHash, receipt := range receipts { - byt, err := receipt.Encode() - if err != nil { - return err - } - err = kvtx.Set(txHash.Bytes(), byt) - if err != nil { - return err - } - } - - return kvtx.Commit() + return bb.receiptKV.SetReceipts(receipts) } func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) error { @@ -180,11 +190,7 @@ func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) error { } return nil } - byt, err := receipt.Encode() - if err != nil { - return err - } - return bb.receiptKV.Set(txHash.Bytes(), byt) + return bb.receiptKV.SetReceipt(txHash, receipt) } func (bb *TxDB) GetReceipt(txHash Hash) (*Receipt, error) { @@ -199,7 +205,18 @@ func (bb *TxDB) GetReceipt(txHash Hash) (*Receipt, error) { } } } - byt, err := bb.receiptKV.Get(txHash.Bytes()) + return bb.receiptKV.GetReceipt(txHash) +} + +type receipttxnkvdb struct { + sync.RWMutex + receiptKV kv.KV +} + +func (r *receipttxnkvdb) GetReceipt(txHash Hash) (*Receipt, error) { + r.RLock() + defer r.RUnlock() + byt, err := r.receiptKV.Get(txHash.Bytes()) if err != nil { logrus.Errorf("TxDB.GetReceipt(%s), failed: %s, error: %v", txHash.String(), string(byt), err) return nil, err @@ -214,3 +231,33 @@ func (bb *TxDB) GetReceipt(txHash Hash) (*Receipt, error) { } return receipt, err } + +func (r *receipttxnkvdb) SetReceipt(txHash Hash, receipt *Receipt) error { + r.Lock() + defer r.Unlock() + byt, err := receipt.Encode() + if err != nil { + return err + } + return r.receiptKV.Set(txHash.Bytes(), byt) +} + +func (r *receipttxnkvdb) SetReceipts(receipts map[Hash]*Receipt) error { + r.Lock() + defer r.Unlock() + kvtx, err := r.receiptKV.NewKvTxn() + if err != nil { + return err + } + for txHash, receipt := range receipts { + byt, err := receipt.Encode() + if err != nil { + return err + } + err = kvtx.Set(txHash.Bytes(), byt) + if err != nil { + return err + } + } + return kvtx.Commit() +} From c745a0fa99de822cf320f37e93fab6f0152dae07 Mon Sep 17 00:00:00 2001 From: ReyisaRuby Date: Tue, 7 Jan 2025 14:21:07 +0800 Subject: [PATCH 4/6] add metrics Signed-off-by: ReyisaRuby --- core/txdb/txdb.go | 49 ++++++++++++++++++++++++++++++++++------- metrics/metrics.go | 1 + metrics/txdb_metrics.go | 22 ++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 metrics/txdb_metrics.go diff --git a/core/txdb/txdb.go b/core/txdb/txdb.go index 19bf5a1..9b1d185 100644 --- a/core/txdb/txdb.go +++ b/core/txdb/txdb.go @@ -10,6 +10,7 @@ import ( . "github.com/yu-org/yu/core/types" "github.com/yu-org/yu/infra/storage/kv" "github.com/yu-org/yu/infra/storage/sql" + "github.com/yu-org/yu/metrics" ) const ( @@ -32,7 +33,21 @@ type txnkvdb struct { txnKV kv.KV } -func (t *txnkvdb) GetTxn(txnHash Hash) (*SignedTxn, error) { +const ( + txnType = "txn" + receiptType = "receipt" + successStatus = "success" + errStatus = "err" +) + +func getStatusValue(err error) string { + if err == nil { + return successStatus + } + return err.Error() +} + +func (t *txnkvdb) GetTxn(txnHash Hash) (txn *SignedTxn, err error) { t.RLock() defer t.RUnlock() byt, err := t.txnKV.Get(txnHash.Bytes()) @@ -51,7 +66,7 @@ func (t *txnkvdb) ExistTxn(txnHash Hash) bool { return t.txnKV.Exist(txnHash.Bytes()) } -func (t *txnkvdb) SetTxns(txns []*SignedTxn) error { +func (t *txnkvdb) SetTxns(txns []*SignedTxn) (err error) { t.Lock() defer t.Unlock() kvtx, err := t.txnKV.NewKvTxn() @@ -102,7 +117,10 @@ func NewTxDB(nodeTyp int, kvdb kv.Kvdb, kvdbConf *config.KVconf) (ItxDB, error) return txdb, nil } -func (bb *TxDB) GetTxn(txnHash Hash) (*SignedTxn, error) { +func (bb *TxDB) GetTxn(txnHash Hash) (stxn *SignedTxn, err error) { + defer func() { + metrics.TxnDBCounter.WithLabelValues(txnType, "getTxn", getStatusValue(err)).Inc() + }() if bb.nodeType == LightNode { return nil, nil } @@ -117,7 +135,10 @@ func (bb *TxDB) GetTxn(txnHash Hash) (*SignedTxn, error) { return bb.txnKV.GetTxn(txnHash) } -func (bb *TxDB) GetTxns(txnHashes []Hash) ([]*SignedTxn, error) { +func (bb *TxDB) GetTxns(txnHashes []Hash) (stxns []*SignedTxn, err error) { + defer func() { + metrics.TxnDBCounter.WithLabelValues(txnType, "getTxn", getStatusValue(err)).Inc() + }() if bb.nodeType == LightNode { return nil, nil } @@ -146,7 +167,10 @@ func (bb *TxDB) ExistTxn(txnHash Hash) bool { return bb.txnKV.ExistTxn(txnHash) } -func (bb *TxDB) SetTxns(txns []*SignedTxn) error { +func (bb *TxDB) SetTxns(txns []*SignedTxn) (err error) { + defer func() { + metrics.TxnDBCounter.WithLabelValues(txnType, "setTxns", getStatusValue(err)).Inc() + }() if bb.nodeType == LightNode { return nil } @@ -167,7 +191,10 @@ func (bb *TxDB) SetTxns(txns []*SignedTxn) error { return bb.txnKV.SetTxns(txns) } -func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) error { +func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) (err error) { + defer func() { + metrics.TxnDBCounter.WithLabelValues(receiptType, "setReceipts", getStatusValue(err)).Inc() + }() if bb.enableUseSql { for txHash, receipt := range receipts { if err := bb.SetReceipt(txHash, receipt); err != nil { @@ -179,7 +206,10 @@ func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) error { return bb.receiptKV.SetReceipts(receipts) } -func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) error { +func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) (err error) { + defer func() { + metrics.TxnDBCounter.WithLabelValues(receiptType, "setReceipt", getStatusValue(err)).Inc() + }() if bb.enableUseSql { byt, err := receipt.Encode() if err != nil { @@ -193,7 +223,10 @@ func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) error { return bb.receiptKV.SetReceipt(txHash, receipt) } -func (bb *TxDB) GetReceipt(txHash Hash) (*Receipt, error) { +func (bb *TxDB) GetReceipt(txHash Hash) (rec *Receipt, err error) { + defer func() { + metrics.TxnDBCounter.WithLabelValues(receiptType, "getReceipt", getStatusValue(err)).Inc() + }() if bb.enableUseSql { var records []TxnDBSchema err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "receipt", string(txHash.Bytes())).Find(&records).Error diff --git a/metrics/metrics.go b/metrics/metrics.go index 2e92a81..9ae268c 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -89,4 +89,5 @@ func init() { prometheus.MustRegister(TxpoolSizeGauge) // prometheus.MustRegister(AppendBlockDuration, StartBlockDuration, EndBlockDuration, FinalizeBlockDuration) prometheus.MustRegister(StateCommitDuration) + initTxnDBMetrics() } diff --git a/metrics/txdb_metrics.go b/metrics/txdb_metrics.go new file mode 100644 index 0000000..a818f5f --- /dev/null +++ b/metrics/txdb_metrics.go @@ -0,0 +1,22 @@ +package metrics + +import "github.com/prometheus/client_golang/prometheus" + +const ( + TypeLbl = "type" + OpLabel = "op" + StatusLbl = "status" +) + +var ( + TxnDBCounter = prometheus.NewCounterVec(prometheus.CounterOpts{ + Namespace: "yu", + Subsystem: "txndb", + Name: "op_counter", + Help: "Counter of txnDB", + }, []string{TypeLbl, OpLabel, StatusLbl}) +) + +func initTxnDBMetrics() { + prometheus.MustRegister(TxnDBCounter) +} From 45f049c084dd4e75afebbeb666f9575baf439df0 Mon Sep 17 00:00:00 2001 From: ReyisaRuby Date: Tue, 7 Jan 2025 14:25:56 +0800 Subject: [PATCH 5/6] fix Signed-off-by: ReyisaRuby --- core/txdb/txdb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/txdb/txdb.go b/core/txdb/txdb.go index 9b1d185..4d87f08 100644 --- a/core/txdb/txdb.go +++ b/core/txdb/txdb.go @@ -44,7 +44,7 @@ func getStatusValue(err error) string { if err == nil { return successStatus } - return err.Error() + return errStatus } func (t *txnkvdb) GetTxn(txnHash Hash) (txn *SignedTxn, err error) { From f88951920a08c1b1e48d17545c6313f7e6cd8095 Mon Sep 17 00:00:00 2001 From: ReyisaRuby Date: Tue, 7 Jan 2025 16:02:27 +0800 Subject: [PATCH 6/6] address the comment Signed-off-by: ReyisaRuby --- core/startup/startup.go | 4 +++- core/txdb/txdb.go | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/core/startup/startup.go b/core/startup/startup.go index 965ae6b..c182f87 100644 --- a/core/startup/startup.go +++ b/core/startup/startup.go @@ -71,7 +71,9 @@ func InitKernel(cfg *config.KernelConf) *kernel.Kernel { if TxnDB == nil { TxnDB, err = txdb.NewTxDB(cfg.NodeType, kvdb, &cfg.KVDB) - logrus.Fatal("init kvdb error: ", err) + if err != nil { + logrus.Fatal("init kvdb error: ", err) + } } if Chain == nil { Chain = blockchain.NewBlockChain(cfg.NodeType, &cfg.BlockChain, TxnDB) diff --git a/core/txdb/txdb.go b/core/txdb/txdb.go index 4d87f08..2b33a0e 100644 --- a/core/txdb/txdb.go +++ b/core/txdb/txdb.go @@ -126,7 +126,7 @@ func (bb *TxDB) GetTxn(txnHash Hash) (stxn *SignedTxn, err error) { } if bb.enableUseSql { var records []TxnDBSchema - err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "txn", string(txnHash.Bytes())).Find(&records).Error + err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "txn", txnHash.String()).Find(&records).Error // find result in sql database if err == nil && len(records) > 0 { return DecodeSignedTxn([]byte(records[0].Value)) @@ -159,7 +159,7 @@ func (bb *TxDB) ExistTxn(txnHash Hash) bool { } if bb.enableUseSql { var records []TxnDBSchema - err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "txn", string(txnHash.Bytes())).Find(&records).Error + err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "txn", txnHash.String()).Find(&records).Error if err == nil && len(records) > 0 { return true } @@ -181,7 +181,7 @@ func (bb *TxDB) SetTxns(txns []*SignedTxn) (err error) { logrus.Errorf("TxDB.SetTxns set tx(%s) failed: %v", txn.TxnHash.String(), err) return err } - if err := bb.db.Db().Exec("insert into txndb (type,key,value) values (?,?,?)", "txn", string(txn.TxnHash.Bytes()), string(txbyt)).Error; err != nil { + if err := bb.db.Db().Exec("insert into txndb (type,key,value) values (?,?,?)", "txn", txn.TxnHash.String(), string(txbyt)).Error; err != nil { logrus.Errorf("Insert TxDB.SetTxns tx(%s) failed: %v", txn.TxnHash.String(), err) return err } @@ -215,7 +215,7 @@ func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) (err error) { if err != nil { return err } - if err := bb.db.Db().Exec("insert into txndb (type,key,value) values (?,?,?)", "receipt", string(txHash.Bytes()), string(byt)).Error; err != nil { + if err := bb.db.Db().Exec("insert into txndb (type,key,value) values (?,?,?)", "receipt", txHash.String(), string(byt)).Error; err != nil { return err } return nil @@ -229,7 +229,7 @@ func (bb *TxDB) GetReceipt(txHash Hash) (rec *Receipt, err error) { }() if bb.enableUseSql { var records []TxnDBSchema - err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "receipt", string(txHash.Bytes())).Find(&records).Error + err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "receipt", txHash.String()).Find(&records).Error if err == nil && len(records) > 0 { receipt := new(Receipt) err = receipt.Decode([]byte(records[0].Value))