Skip to content

Commit

Permalink
chore: 拼写错误修改,添加readme
Browse files Browse the repository at this point in the history
  • Loading branch information
胡腾 committed Sep 28, 2023
1 parent df29791 commit c4b700b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 20 deletions.
2 changes: 1 addition & 1 deletion chainManger/chainCall.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c Chain) InvokeCC() (*channel.Response, error) {
return &resp, nil
}

func (c *Chain) StartHTCL(tx dao.HTCLTx) (*channel.Response, error) {
func (c *Chain) StartHTCL(tx dao.HTLCTx) (*channel.Response, error) {
type info struct {
LtokenId string
Lto string
Expand Down
4 changes: 2 additions & 2 deletions chainManger/chainManger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewChainManger() *ChainManager {
}

// 判断chain是否在chainList中
func (c ChainManager) IsUsableChain(chainID string) bool {
func (c ChainManager) IsUseableChain(chainID string) bool {
for _, it := range c.ChainList {
if chainID == it.name {
return true
Expand All @@ -26,7 +26,7 @@ func (c ChainManager) IsUsableChain(chainID string) bool {
}

func (c *ChainManager) GetChainByName(chainID string) *Chain {
if c.IsUsableChain(chainID) {
if c.IsUseableChain(chainID) {
return c.ChainList[chainID]
}
return nil
Expand Down
10 changes: 5 additions & 5 deletions dao/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func AddChainsName(names []string) ([]string, error) {
return nil, err
}

func CreateHTCLTx(tx HTCLTx) (int64, error) {
func CreateHTLCTx(tx HTLCTx) (int64, error) {
chainNames := []string{tx.ChainAName, tx.ChainBName}
eixetChain := CheckNameExist(chainNames)
if len(eixetChain) != 2 {
Expand Down Expand Up @@ -133,19 +133,19 @@ func CreateHTCLTx(tx HTCLTx) (int64, error) {
return res.LastInsertId()
}

func GetHTCLTx(id int64) (HTCLTx, error) {
func GetHTLCTx(id int64) (HTLCTx, error) {
sqlStr := `SELECT * FROM htcl WHERE id=?`
var tx HTCLTx
var tx HTLCTx
var i int64
err := DB.QueryRow(sqlStr, id).Scan(&i, &tx.ChainAName, &tx.TradeNFTID, &tx.NFTRecipientAddr, &tx.ChainBName, &tx.CoinNUM, &tx.CoinRecipientAddr, &tx.Hs, &tx.TimeStart, &tx.TimeEnd, &tx.AproveID)
if err != nil {
log.Printf("无法查找id为%d的htcl交易", id)
return HTCLTx{}, err
return HTLCTx{}, err
}
return tx, nil
}

type HTCLTx struct {
type HTLCTx struct {
ChainAName string
TradeNFTID string //交易NFTID
NFTRecipientAddr string // NFT接受者地址
Expand Down
6 changes: 3 additions & 3 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type P2PServer struct {
}

var sendmsg = make(chan []byte, 1024)
var handmsg = make(chan []byte, 1024)
var handlemsg = make(chan []byte, 1024)
var stop chan interface{}

// Start 启动服务,并保持运行
Expand Down Expand Up @@ -64,7 +64,7 @@ func (p *P2PServer) Start(chain *chainmanger.ChainManager) {

func (p *P2PServer) handler() {
for {
data := <-handmsg
data := <-handlemsg
var msg Msg
err := json.Unmarshal(data, &msg)
if err != nil {
Expand Down Expand Up @@ -198,7 +198,7 @@ func readData(rw *bufio.ReadWriter) {
return
}
if len(data) != 1 {
handmsg <- data
handlemsg <- data
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
### chaincross
> 该项目是up本科毕设的作品,实现上较为粗糙,大有图片仅供参考的味道。整体代码大概只有2000行。另外按照原来的预想,将以太坊的的客户端与fabric的写在一起,但是当时实在是技术不到家,写不出来,所以这里面也只有fabric一边的适配,对以太坊的调用请求在另外一个项目文件里😂(我没传上去)
#### 入门说明

> 可以理解为我入门的说明
**一堆乱七八糟的技术**

- mysql
- p2p
- htlc
- nft
- fabric/以太坊SDK
- 好像没有了

> 说实话大部分技术都是用来凑数的,本人不是软件工程专业出生,大部分在初始设计上就存在一定的不合理
##### 一. 数据库说明

数据库的全部内容都在 dao/mysql.go文件里面

```go
DB
init()
CheckNameExist()
AddChainsName()
CreateHTCLTx()
GetHTCLTx()
HTCLTx
```

`DB`: dao模块下的全局数据库连接,我现在才知道这样写只用引入dao这个包就可以拿到这个全局的DB,实际上用小写的比较好

`init`:go语言的init函数,会在初始化的时候自动自行,用来初始化我这个mysql,具体怎么连接的可以参考网上任意教程

`CheckNameExist`:校验一下数据库里面是不是有重名的链的记录。这里先讲一下,按照最初的设计,多个网关是可以接同一个数据库的,所以要判断一下现在有那些链ID(链的唯一标识),在添加链的时候就会先查一下数据库是不是已经有链已经注册了。

`AddChainsName`: 这个函数只在网关启动的时候调用,理论上是可以一个网关接多个链,我应该是做了相关的实现。

`CreateHTCLTx`:创建一个HTLC请求,实际上只是将HTLC请求的数据保存在数据库里面

`GetHTCLTx`: 用来获取数据库里面的HTLC请求,后面对HTLC的操作的第一步都是先拿到这个请求

`HTCLTx`: HTCLTx结构

数据库的表结构就没什么好说的了,当时我就没设计这表结构,这个表最终是在修修改改中确认的。不过算是能用。

应该只有两张表,一个存有那些链,一个存htlc数据。

##### 二. p2p连接部分


> 今天先写道这儿,该干正事了

18 changes: 9 additions & 9 deletions rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type RpcHandler struct {
}

func (h *RpcHandler) CreateHTCLTxByPreInfo(PreInfo *PreHTCL, resout *int64) error {
Tx := dao.HTCLTx{
Tx := dao.HTLCTx{
ChainAName: PreInfo.ChainAName,
ChainBName: PreInfo.CHainBName,
TradeNFTID: PreInfo.TradeNFTID,
Expand All @@ -29,19 +29,19 @@ func (h *RpcHandler) CreateHTCLTxByPreInfo(PreInfo *PreHTCL, resout *int64) erro
TimeEnd: time.Now().Unix() + int64(PreInfo.TimeInterval*60*1000),
}

res, err := dao.CreateHTCLTx(Tx)
res, err := dao.CreateHTLCTx(Tx)
*resout = res
return err
}

func (h *RpcHandler) GetHTCLTxByID(id int64, resout *dao.HTCLTx) error {
res, err := dao.GetHTCLTx(id)
func (h *RpcHandler) GetHTCLTxByID(id int64, resout *dao.HTLCTx) error {
res, err := dao.GetHTLCTx(id)
*resout = res
return err
}

func (h *RpcHandler) StartHTLC(id int64, resout *string) error {
tx, err := dao.GetHTCLTx(id)
tx, err := dao.GetHTLCTx(id)
if err != nil {
log.Println(err)
*resout = err.Error()
Expand Down Expand Up @@ -73,7 +73,7 @@ func (h *RpcHandler) UnlockHTCL(UA *UnlockArgs, resout *string) error {
}

func (h *RpcHandler) Test(PreInfo *PreHTCL, resout *string) error {
Tx := dao.HTCLTx{
Tx := dao.HTLCTx{
TradeNFTID: PreInfo.TradeNFTID,
NFTRecipientAddr: PreInfo.NFTRecipientAddr,
CoinNUM: PreInfo.CoinNUM,
Expand All @@ -87,8 +87,8 @@ func (h *RpcHandler) Test(PreInfo *PreHTCL, resout *string) error {
return err
}

func (r *RpcHandler) handlerHTCL(tx dao.HTCLTx) error {
if r.chainManger.IsUsableChain(tx.ChainAName) {
func (r *RpcHandler) handlerHTCL(tx dao.HTLCTx) error {
if r.chainManger.IsUseableChain(tx.ChainAName) {
chain := r.chainManger.GetChainByName(tx.ChainAName)
if chain == nil {
return fmt.Errorf("没有找到名为%s的链", tx.ChainAName)
Expand All @@ -101,7 +101,7 @@ func (r *RpcHandler) handlerHTCL(tx dao.HTCLTx) error {
}

func (r *RpcHandler) handlerHTCLUnlock(unlock UnlockHTCL) error {
HTLCtx, err := dao.GetHTCLTx(unlock.TransaID)
HTLCtx, err := dao.GetHTLCTx(unlock.TransaID)
if err != nil {
log.Println(err)
}
Expand Down

0 comments on commit c4b700b

Please sign in to comment.