OSDN Git Service

Dwth testdb
authorHAOYUatHZ <haoyu@protonmail.com>
Sun, 23 Jun 2019 00:04:44 +0000 (08:04 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Sun, 23 Jun 2019 00:04:44 +0000 (08:04 +0800)
federation/database/orm/cross_transaction.go
federation/service/node.go
federation/util/hash.go [new file with mode: 0644]
main.go [new file with mode: 0644]
main2.go [new file with mode: 0644]

index 7a21211..e24f44c 100644 (file)
@@ -19,7 +19,7 @@ type CrossTransaction struct {
        DestBlockHash        sql.NullString
        DestTxIndex          sql.NullInt64
        DestTxHash           sql.NullString
-       Status               uint8
+       Status               uint8 `gorm:"not null"`
        CreatedAt            types.Timestamp
        UpdatedAt            types.Timestamp
 
index 861f766..8403c90 100644 (file)
@@ -2,6 +2,7 @@ package service
 
 import (
        "encoding/json"
+       "log"
 
        btmTypes "github.com/bytom/protocol/bc/types"
 
@@ -84,12 +85,14 @@ func (n *Node) SubmitTx(tx interface{}) (string, error) {
                if err != nil {
                        return "", errors.Wrap(err, "json marshal")
                }
+               log.Println("btmTypes")
 
        case *vaporTypes.Tx:
                payload, err = json.Marshal(submitSidechainTxReq{Tx: tx})
                if err != nil {
                        return "", errors.Wrap(err, "json marshal")
                }
+               log.Println("vaporTypes")
 
        default:
                return "", errors.New("unknown tx type")
diff --git a/federation/util/hash.go b/federation/util/hash.go
new file mode 100644 (file)
index 0000000..e077602
--- /dev/null
@@ -0,0 +1,27 @@
+package util
+
+import (
+       // "encoding/hex"
+
+       // "github.com/vapor/errors"
+       "github.com/vapor/protocol/bc"
+)
+
+func StringToAssetID(s string) (*bc.Hash, error) {
+       // h, err := hex.DecodeString(s)
+       // if err != nil {
+       //      return nil, errors.Wrap(err, "decode asset string")
+       // }
+
+       // panic(len(h))
+
+       // var b [32]byte
+       // copy(b[:], h)
+       // assetID := bc.NewAssetID(b)
+       assetID := &bc.Hash{}
+       if err := assetID.UnmarshalText([]byte(s)); err != nil {
+               panic(err)
+       }
+
+       return assetID, nil
+}
diff --git a/main.go b/main.go
new file mode 100644 (file)
index 0000000..6831e5d
--- /dev/null
+++ b/main.go
@@ -0,0 +1,78 @@
+package main
+
+import (
+       // "sync"
+       // "database/sql"
+       "fmt"
+
+       btmTypes "github.com/bytom/protocol/bc/types"
+       _ "github.com/go-sql-driver/mysql"
+       "github.com/jinzhu/gorm"
+       log "github.com/sirupsen/logrus"
+
+       // "github.com/vapor/errors"
+       // "github.com/vapor/federation/config"
+       // "github.com/vapor/federation/database"
+       "github.com/vapor/federation/common"
+       "github.com/vapor/federation/database/orm"
+       "github.com/vapor/federation/service"
+       "github.com/vapor/federation/util"
+       vaporTypes "github.com/vapor/protocol/bc/types"
+       // "github.com/vapor/federation/synchron"
+)
+
+func main() {
+       str := "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
+       asset, _ := util.StringToAssetID(str)
+       fmt.Println(asset)
+
+       dsnTemplate := "%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true&loc=Local"
+       dsn := fmt.Sprintf(dsnTemplate, "root", "toor", "127.0.0.1", 3306, "federation")
+       db, err := gorm.Open("mysql", dsn)
+       if err != nil {
+               log.Errorln(err, "open db cluster")
+       }
+       db.LogMode(true)
+
+       // reqs := []*orm.CrossTransactionReq{}
+       // db.Where(&orm.CrossTransactionReq{CrossTransactionID: 1}).Find(&reqs)
+       // log.Info(reqs)
+
+       // xs :=
+       if err := db.Where(&orm.CrossTransactionSign{CrossTransactionID: 1}).First(&orm.CrossTransactionSign{}).Error; err == nil {
+               log.Warnln("err == nil")
+       } else {
+               log.Warnln("err != nil")
+       }
+
+       txs := []*orm.CrossTransaction{}
+       if err := db.Preload("Chain").Preload("Reqs").Model(&orm.CrossTransaction{}).Where("status = ?", common.CrossTxPendingStatus).Find(&txs).Error; err == gorm.ErrRecordNotFound {
+               log.Warnln("ErrRecordNotFound")
+       } else if err != nil {
+               log.Warnln("collectUnsubmittedTx", err)
+       }
+
+       node := service.NewNode("http://127.0.0.1")
+       tx1 := &btmTypes.Tx{}
+       tx2 := &vaporTypes.Tx{}
+       node.SubmitTx(tx1)
+       node.SubmitTx(tx2)
+
+       // ormTx := &orm.CrossTransaction{
+       //      ChainID:              1,
+       //      SourceBlockHeight:    2,
+       //      SourceBlockHash:      "blockHash.String()",
+       //      SourceTxIndex:        3,
+       //      SourceMuxID:          "muxID.String()",
+       //      SourceTxHash:         "tx.ID.String()",
+       //      SourceRawTransaction: "string(rawTx)",
+       //      DestBlockHeight:      sql.NullInt64{Valid: false},
+       //      DestBlockHash:        sql.NullString{Valid: false},
+       //      DestTxIndex:          sql.NullInt64{Valid: false},
+       //      DestTxHash:           sql.NullString{Valid: false},
+       //      Status:               common.CrossTxPendingStatus,
+       // }
+       // if err := db.Create(ormTx).Error; err != nil {
+       //      log.Warnln(err)
+       // }
+}
diff --git a/main2.go b/main2.go
new file mode 100644 (file)
index 0000000..2103f3b
--- /dev/null
+++ b/main2.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+       "database/sql"
+       "fmt"
+
+       _ "github.com/go-sql-driver/mysql"
+       "github.com/jinzhu/gorm"
+
+       "github.com/vapor/federation/common"
+       // "github.com/vapor/federation/config"
+       "github.com/vapor/federation/database/orm"
+)
+
+func main2() {
+       dsnTemplate := "%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true&loc=Local"
+       dsn := fmt.Sprintf(dsnTemplate, "root", "toor", "127.0.0.1", 3306, "federation")
+       db, err := gorm.Open("mysql", dsn)
+       if err != nil {
+               panic(err)
+       }
+
+       // db.LogMode(true)
+
+       // stmt :=
+       db.Debug().Model(&orm.CrossTransaction{}).Where("chain_id != ?", 1).
+               Where(&orm.CrossTransaction{
+                       DestTxHash: sql.NullString{"tx.ID.String()", true},
+                       Status:     common.CrossTxSubmittedStatus,
+               }).UpdateColumn(&orm.CrossTransaction{
+               DestBlockHeight: sql.NullInt64{int64(2), true},
+               DestBlockHash:   sql.NullString{"blockHash.String()", true},
+               DestTxIndex:     sql.NullInt64{int64(3), true},
+               Status:          common.CrossTxCompletedStatus,
+       })
+
+}