Skip to content

Commit

Permalink
CHANGE:增加 环境变量
Browse files Browse the repository at this point in the history
  • Loading branch information
dao authored and dao committed Nov 5, 2018
1 parent 8c50f6f commit 9b622f3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 51 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ REDIS_MAXIDLE=20
REDIS_MAXACTIVE=0
TX_FEE=10
FORK_ID=6f937c2f5944f5da2a118cebb067cd2c9c92c75955ce05aa05158a1af28e1607
LWS_ID=123
15 changes: 14 additions & 1 deletion cmd/gateway/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package main

import (
"flag"
"github.com/FissionAndFusion/lws/internal/gateway"
"log"
"os"
)

func main() {
server := gateway.Server{}
var lwsId string

flag.StringVar(&lwsId, "id", "", "lwsId")
flag.Parse()
if lwsId == "" {
lwsId = os.Getenv("LWS_ID")
}
log.Printf(lwsId)
server := gateway.Server{
Id: lwsId,
}
defer server.Start()
}
7 changes: 0 additions & 7 deletions internal/gateway/mqtt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ func Interrupt() {
msgChan <- os.Interrupt
}

// var connHandle mqtt.OnConnectHandler = func(client mqtt.Client) {
// client.Subscribe("LWS/lws/ServiceReq", byte(0), serviceReqHandler)
// client.Subscribe("LWS/lws/SyncReq", byte(1), syncReqHandler)
// client.Subscribe("LWS/lws/UTXOAbort", byte(1), uTXOAbortReqHandler)
// client.Subscribe("LWS/lws/SendTxReq", byte(1), sendTxReqReqHandler)
// }

// start client
func (p *Program) Start() error {
if token := p.Client.Connect(); token.Wait() && token.Error() != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/gateway/mqtt/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ func TxDataToStruct(tx []byte, txData *TxData) (err error) {
log.Printf("TxDataToStruct get params:tx--%+v\n", tx)
var leftIndex uint64 = 0
var sizeLen uint64 = 0
totalLength := uint64(len(tx))
for i := 0; i < resultValue.NumField(); i++ {
// leng := resultType.Field(i).Tag.Get("len")
leng, err := strconv.Atoi(resultType.Field(i).Tag.Get("len"))
len64 := uint64(leng)
if err != nil {
return err
}
if (leftIndex + len64) > totalLength {
return errors.New("slice bounds out of range")
}
// log.Printf("txData : %+v\n", txData)
if resultValue.Field(i).CanSet() {
switch resultValue.Field(i).Type().Kind() {
Expand Down
5 changes: 2 additions & 3 deletions internal/gateway/mqtt/serviceReq.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,10 @@ func RemoveRedis(conn *redis.Conn, cliMap *CliMap) (err error) {

func VerifyAddress(s *ServicePayload, payload []byte) bool {
messageLen := uint16(len(payload)) - (s.SignBytes + 2)
log.Printf("messageLen: %d", messageLen)
log.Printf("pub Key: %+v", s.Address[1:])
log.Printf("pub Key: %+v", hex.EncodeToString(s.Address[1:]))
log.Printf("payload : %+v", payload[:messageLen])
if messageLen > uint16(len(payload)) {
log.Printf("VerifyAddress failed with err:slice bounds out of range")
log.Printf("VerifyAddress failed with err: slice bounds out of range")
return false
}
if uint8(s.Address[0]) == 1 {
Expand Down
17 changes: 0 additions & 17 deletions internal/gateway/mqtt/serviceReq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"encoding/hex"
"log"
"os"
"strconv"
"strings"
"testing"
"time"

Expand All @@ -19,20 +17,6 @@ import (
edwards25519 "golang.org/x/crypto/ed25519"
)

func TestAPISS(t *testing.T) {

code1 := "178 10 234 215 209 22 250 63 27 69 115 27 46 132 80 66 174 68 88 173 67 216 182 213 94 54 6 184 54 223 31 21 1 1 112 89 84 86 194 230 223 96 49 238 244 249 254 205 74 76 41 115 115 161 21 67 165 27 3 181 77 78 31 203 17 34 0 112 89 84 86 194 230 223 96 49 238 244 249 254 205 74 76 41 115 115 161 21 67 165 27 3 181 77 78 31 203 17 34"
codeArr1 := strings.Split(code1, " ")
log.Printf("len : %+v", len(codeArr1))
codeAdd1 := make([]byte, 99)
log.Printf("len : %+v", codeArr1)
for index := 0; index < len(codeArr1); index++ {
value, _ := strconv.Atoi(codeArr1[index])
codeAdd1[index] = byte(value)
}
log.Printf("codeAdd1 : %+v", hex.EncodeToString(codeAdd1))
}

func TestServiceReq(t *testing.T) {
cli := &Program{
Id: "clilassss",
Expand All @@ -42,7 +26,6 @@ func TestServiceReq(t *testing.T) {
if err := cli.Start(); err != nil {
t.Errorf("client start failed")
}

// cli.Subscribe("wqweqwasasqw/fnfn/ServiceReply", 0, servicReplyHandler)
addr, _, signKey := crypto.GenerateKeyPair(nil)
// addr, _ := hex.DecodeString("6f937c2f5944f5da2a118cebb067cd2c9c92c75955ce05aa05158a1af28e1607")
Expand Down
38 changes: 17 additions & 21 deletions internal/gateway/mqtt/syncReq.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var syncReqHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Messa
"INNER JOIN tx "+
"ON utxo.tx_hash = tx.hash "+
"AND utxo.destination = ? "+
"ORDER BY utxo.tx_hash ASC, utxo.out ASC ", cliMap.Address).Find(&UTXOs).Error
"ORDER BY REVERSE(utxo.tx_hash) ASC, utxo.out ASC ", cliMap.Address).Find(&UTXOs).Error
if err != nil {
ReplySyncReq(&client, &s, &UTXOs, &cliMap, 16, 0)
return
Expand All @@ -100,20 +100,10 @@ var syncReqHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Messa
}
// 计算utxo数量
// 如果utxo 数量超过replyUtxo长度,分多次发送list
// 如果replyUtxo = 0 , 计算长度是否超过256
if cliMap.ReplyUTXON == 0 {
maxLen := 256 * 1024
totalLen := len(UTXOs) + 42
if maxLen < totalLen {
// TODO:分包 发送
} else {
// 一次发送
ReplySyncReq(&client, &s, &UTXOs, &cliMap, 1, 0)
}
} else if cliMap.ReplyUTXON < uint16(len(UTXOs)) {
if cliMap.ReplyUTXON < uint16(len(UTXOs)) && cliMap.ReplyUTXON != 0 {
// 多次发送
// 发送次数
c := make(chan int, 1)
// 发送次数
times := int(math.Ceil(float64(uint16(len(UTXOs)) / cliMap.ReplyUTXON)))
for index := 0; index < times; index++ {
log.Printf("total:%+v, send utxo in data pack %d\n", times, index)
Expand All @@ -138,7 +128,10 @@ var syncReqHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Messa
ReplySyncReq(&client, &s, &UTXOs, &cliMap, 1, 0)
}
// save nonce
updateRedis(&redisConn, &cliMap, "Nonce", s.Nonce)
err = updateRedis(&redisConn, &cliMap, "Nonce", s.Nonce)
if err != nil {
log.Printf("save nonce err: %+v\n", err)
}
}

// reply sync req
Expand All @@ -149,17 +142,21 @@ func ReplySyncReq(client *mqtt.Client, s *SyncPayload, u *[]UTXO, cliMap *CliMap
reply.Error = uint8(err)
if err == 0 {
tailBlock := block.GetTailBlock()
reply.BlockHash = tailBlock.Hash
reply.BlockHeight = tailBlock.Height
reply.BlockTime = tailBlock.Tstamp
if tailBlock != nil {
reply.BlockHash = tailBlock.Hash
reply.BlockHeight = tailBlock.Height
reply.BlockTime = tailBlock.Tstamp
}
reply.UTXONum = uint16(0)
reply.Continue = uint8(end)
}
if err == 1 {
tailBlock := block.GetTailBlock()
reply.BlockHash = tailBlock.Hash
reply.BlockHeight = tailBlock.Height
reply.BlockTime = tailBlock.Tstamp
if tailBlock != nil {
reply.BlockHash = tailBlock.Hash
reply.BlockHeight = tailBlock.Height
reply.BlockTime = tailBlock.Tstamp
}
reply.UTXONum = uint16(len(*u))
byteList, _ := UTXOListToByte(u)
reply.UTXOList = byteList
Expand All @@ -170,7 +167,6 @@ func ReplySyncReq(client *mqtt.Client, s *SyncPayload, u *[]UTXO, cliMap *CliMap
log.Printf("err: %+v\n", err)
}
t := cliMap.TopicPrefix + "/fnfn/SyncReply"
// TODO
token := (*client).Publish(t, 1, false, result)
if token.Wait() {
log.Printf("publish err: %+v", token.Error())
Expand Down
6 changes: 5 additions & 1 deletion internal/gateway/mqtt/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ func GetProgram() *Program {
mu.Lock()
defer mu.Unlock()
if cliProgram == nil {
cliProgram = &Program{Id: "update001", IsLws: false}
id := os.Getenv("LWS_ID")
if id == "" {
id = "lws-001"
}
cliProgram = &Program{Id: "update" + id, IsLws: false}
cliProgram.Init()
if err := cliProgram.Start(); err != nil {
log.Printf("client start failed")
Expand Down
4 changes: 3 additions & 1 deletion internal/gateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (

type Server struct {
Status int
Id string
}

func (s *Server) Start() {
s.Status = 1
p := &mqtt.Program{Id: "LWS000010", Topic: "LWS01", IsLws: true}

p := &mqtt.Program{Id: s.Id, Topic: s.Id, IsLws: true}
mqtt.Run(p)
fmt.Printf("gateway server started (status: %d)", 3)
}

0 comments on commit 9b622f3

Please sign in to comment.