作者 yangfu

1. 数据上链修改

... ... @@ -7,6 +7,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/blockchain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
"time"
)
... ... @@ -41,20 +42,28 @@ func (blockChainService *BlockChainService) UpChain(upChainCommand *command.UpCh
// 1. 查重
upChainRepository, _, _ := factory.FastPgUpChain(transactionContext, 0)
// 可溯源数据,可重复上传,可以追溯历史修改记录
if len(upChain.IssueId) == 0 {
if item, err := upChainRepository.FindOne(map[string]interface{}{"source": upChain.Source, "primaryId": upChain.PrimaryId}); err == nil && item != nil {
return nil, fmt.Errorf("duplicate message %v %v", upChain.Source, upChain.PrimaryId)
}
}
// 2.上链
bc := &blockchain.BSNBlockChain{
PublicPem: []byte(""),
Host: "",
PublicKey: "",
PublicPem: []byte(blockchain.PubPem),
Host: blockchain.Host,
PublicKey: blockchain.PubKey,
PrivatePem: blockchain.PriK,
EnableDebugLog: true,
}
options := blockchain.NewUpToChainOptions(upChain.Source, upChain.PrimaryId, upChain.Data).WithInnerPrimaryIssueId(upChain.IssueId)
upToChainResponse, err := bc.UpToChain(options)
if err != nil || upToChainResponse == nil {
upToChainResponse, e := bc.UpToChain(options)
if e != nil || upToChainResponse == nil {
upChain.UpFail()
if e != nil {
log.Logger.Error("up chain err:" + e.Error())
}
} else {
upChain.UpSuccess(string(*upToChainResponse))
}
... ...
... ... @@ -4,11 +4,11 @@ import "os"
var (
// kafka 地址
KAFKA_HOST = "192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092" //"106.75.231.90:9092"
KAFKA_HOST = "106.75.231.90:9092" //"192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092"
// kafka topic log stash
TOPIC_LOG_STASH = "go_stash_dev" //"pushMessage"
// kafka topic up_block_chain
TOPIC_UP_BLOCK_CHAIN = "pushMessage"
TOPIC_UP_BLOCK_CHAIN = "up_block_chain"
// 是否启用日志收集 (本地不启用)
ENABLE_KAFKA_LOG = false
)
... ...
package blockchain
var PriK = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA2H6x0D1mg5QbXfU7
MZKltypRj+eZktPKIApyEqRsyLqe3sRSd1Eh+VqKlQ9QFI8dae3t0USWlVmyfIDM
0ly85QIDAQABAkAPnKNJ9wOLfYSzs9l+66pTmROkovjqI6exw88SFRVbLCgM8maa
GOWEP/nhZDlQYBKHUqG0/KsLkeyLGkE8N7JBAiEA8lM3npA3q+Kmhy+lmQbfHFPQ
31OSkA+RaW/LPn0lP50CIQDktlF3iDk5kxnzgT/3lvvKhHInUh+pH5F19C6MymMD
6QIgLxDct655MahnAdDOUCeWhBD/e7DmwZZUfu8Ywb1a070CIArsUjO9Q85mIiUp
FR8EDP59GN6b43s2UMIraVW8DMKRAiEAnnMPbDsD2HsQbgmNNEqETUxYGVyO+p7w
OZZReuOyvCM=
-----END RSA PRIVATE KEY-----`)
var PubPem = `-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANh+sdA9ZoOUG131OzGSpbcqUY/nmZLT
yiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ==
-----END PUBLIC KEY-----`
var PubKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANh+sdA9ZoOUG131OzGSpbcqUY/nmZLTyiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ=="
var Host = "http://101.34.29.149:9092/test"
... ...
... ... @@ -35,6 +35,7 @@ func init() {
(*models.User)(nil),
(*models.UserBase)(nil),
(*models.AccountDestroyRecord)(nil),
(*models.UpChain)(nil),
} {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: false,
... ...
... ... @@ -3,7 +3,7 @@ package models
import "time"
type UpChain struct {
tableName string `comment:"上链数据" pg:"up_chain,alias:up_chain"`
tableName string `comment:"上链数据" pg:"business.up_chain"`
// 上链数据唯一标识
UpChainId int64 `comment:"上链数据唯一标识" pg:"pk:up_chain_id"`
// 数据来源 例如:app.model
... ...
... ... @@ -35,10 +35,10 @@ func (repository *UpChainRepository) Save(upChain *domain.UpChain) (*domain.UpCh
"up_chain_status",
"created_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "up_chain_id"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "up_chain_id"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "upChain_id")
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "up_chain_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if upChain.Identify() == nil {
... ... @@ -59,8 +59,8 @@ func (repository *UpChainRepository) Save(upChain *domain.UpChain) (*domain.UpCh
&upChain.UpChainStatus,
&upChain.CreatedAt,
),
fmt.Sprintf("INSERT INTO up_chains (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
upChain.UpChainId,
fmt.Sprintf("INSERT INTO business.up_chain (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
//upChain.UpChainId,
upChain.Source,
upChain.PrimaryId,
upChain.IssueId,
... ... @@ -83,7 +83,7 @@ func (repository *UpChainRepository) Save(upChain *domain.UpChain) (*domain.UpCh
&upChain.UpChainStatus,
&upChain.CreatedAt,
),
fmt.Sprintf("UPDATE up_chains SET %s WHERE up_chain_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
fmt.Sprintf("UPDATE business.up_chain SET %s WHERE up_chain_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
upChain.Source,
upChain.PrimaryId,
upChain.IssueId,
... ...
package goqueue
import (
"fmt"
"github.com/linmadan/egglib-go/utils/json"
"github.com/tal-tech/go-queue/kq"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"strings"
"testing"
"time"
)
func Test_UpChain(t *testing.T) {
pusher := kq.NewPusher(strings.Split(constant.KAFKA_HOST, ","), constant.TOPIC_UP_BLOCK_CHAIN)
err := pusher.Push(json.MarshalToString(map[string]interface{}{
"source": "allied-creation.cooperation",
"primaryId": fmt.Sprintf("%v", time.Now().Unix()),
"issueId": "key12345",
"data": "{}",
}))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second * 5)
}
... ...