Commit 5fe6e3b0 authored by szh's avatar szh

up

parent 4b2443a3
package main
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"chain33-pai/pkg/logging"
"chain33-pai/pkg/setting"
"chain33-pai/routers"
"chain33-pai/models"
"chain33-pai/pkg/app"
"chain33-pai/pkg/setting"
"chain33-pai/pkg/util"
"net"
"chain33-pai/routers"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
"chain33-pai/service/pai_service"
"chain33-pai/pkg/logging"
)
func init() {
......@@ -21,10 +18,10 @@ func init() {
models.Setup()
logging.Setup()
//gredis.Setup()
//chain33.Setup()
util.Setup()
}
// @title Golang Gin API
// @version 1.0
// @description An example of gin
......@@ -32,14 +29,14 @@ func init() {
// @license.name MIT
// @license.url https://chain33-pai/blob/master/LICENSE
func main() {
go app.CornClean(time.NewTicker(time.Hour*24))
go app.CornData(time.NewTicker(time.Minute*5))
gin.SetMode(setting.ServerSetting.RunMode)
//go broadcast()
routersInit := routers.InitRouter()
readTimeout := setting.ServerSetting.ReadTimeout
writeTimeout := setting.ServerSetting.WriteTimeout
endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
maxHeaderBytes := 1 << 20
server := &http.Server{
Addr: endPoint,
Handler: routersInit,
......@@ -47,13 +44,8 @@ func main() {
WriteTimeout: writeTimeout,
MaxHeaderBytes: maxHeaderBytes,
}
log.Printf("[info] start http server listening %s", endPoint)
server.ListenAndServe()
//select {}
select {}
// If you want Graceful Restart, you need a Unix system and download github.com/fvbock/endless
//endless.DefaultReadTimeOut = readTimeout
//endless.DefaultWriteTimeOut = writeTimeout
......@@ -69,41 +61,3 @@ func main() {
//}
}
func broadcast() {
ip,err := util.GetLocalIP()
if err != nil {
log.Fatal(err)
}
// 这里设置发送者的IP地址,自己查看一下自己的IP自行设定
laddr := net.UDPAddr{
IP: ip.IP,
Port: 8804,
}
// 这里设置接收者的IP地址为广播地址
raddr := net.UDPAddr{
IP: net.IPv4(255, 255, 255, 255),
Port: 8804,
}
conn, err := net.DialUDP("udp", &laddr, &raddr)
if err != nil {
log.Println(err.Error())
return
}
var pai pai_service.Pai
send := "hello world"
ok := pai.GetConfig()
if ok {
send = pai.Serial
}
for {
_, err := conn.Write([]byte(send))
if err != nil{
fmt.Println(err)
}
log.Println(send)
time.Sleep(time.Second)
}
}
\ No newline at end of file
......@@ -161,6 +161,116 @@ func SyncBlock() {
}
func SyncBlockAccount() {
logging.Info("init prevheight",prev)
for {
var t time.Duration
prev.lock.Lock()
prev.SetMainHeight()
lastHeight := prev.Height
startHeight := prev.LocalHeight
maxDownload := prev.DownloadHeight
if lastHeight > startHeight {
if lastHeight - startHeight < prev.DownloadHeight {
maxDownload = 1
}
//速度模式
for processHeight := startHeight + 1; processHeight <= lastHeight; processHeight += maxDownload {
statMap := make(map[string]*models.RaspMinerStat,0)
txsMap := make(map[int64]*models.RaspMinerTxs,0)
//statList := make([]*models.RaspMinerStat,0)
txsList := make([]*models.RaspMinerTxs,0)
openlist := make([]*models.RaspOpen,0)
checkList := make(map[int64]int64,0)
var wg sync.WaitGroup
var mutx sync.Mutex
logging.Info("start startHeight:",processHeight," endHeight:",processHeight+maxDownload-1)
for height := processHeight ;height < processHeight + maxDownload;height ++ {
wg.Add(1)
go func(height int64) {
//defer wg.Done()
stat,txs,open,err := DealBlock(height)
if err != nil {
logging.Error("DealBlock err",err)
panic(err)
}
mutx.Lock()
logging.Info("openlist",len(open))
openlist = append(openlist,open...)
for _, vv:= range stat {
if _,ok := statMap[vv.Addr];!ok {
statMap[vv.Addr] = vv
} else {
statMap[vv.Addr].MinedTicketCount += vv.MinedTicketCount
statMap[vv.Addr].MiningTicketCount += vv.MiningTicketCount
statMap[vv.Addr].MinedAmount += vv.MinedAmount
if statMap[vv.Addr].Height < vv.Height {
statMap[vv.Addr].Height = vv.Height
statMap[vv.Addr].Time = vv.Time
}
}
}
txsList = append(txsList,txs)
mutx.Unlock()
logging.Info("DealBlock",height)
checkList[height] = height
wg.Done()
}(height)
}
//db.SetMainNetHeight(int(processHeight + types.MaxProcessBlocks + 1))
wg.Wait()
for _,v := range txsList {
if v != nil && v.Height >0 {
if _, ok := txsMap[v.Height];!ok {
txsMap[v.Height] = v
}
}
}
if len(txsList) != int(maxDownload) {
logging.Error("txsList",len(txsList),"txsMap",len(txsMap),"statMap",len(statMap),"checkList",len(checkList))
for _,v:= range checkList {
var t bool
for _, vv:= range txsList {
if v == vv.Height {
t = true
break
}
}
if !t {
logging.Error("++++++++",v)
}
}
for _, vv:= range txsList {
logging.Info("txslist:",*vv)
}
for _, vv:= range statMap {
logging.Info("statMap:",*vv)
}
panic("exception txs not eq expect")
}
err := models.UpdateBlocks(txsMap,statMap,openlist,processHeight+maxDownload-1)
if err != nil {
panic(err)
}
prev.LocalHeight += maxDownload
logging.Info("end startHeight:",processHeight," endHeight:",processHeight+maxDownload-1)
}
t = time.Millisecond * 50
} else {
prev.SetMainHeight()
t = time.Second * 5
}
prev.lock.Unlock()
logging.Info("prevHeight",prev)
time.Sleep(t)
}
}
func (prev *prevHeight) SetMainHeight() error {
//header,err := client.GetLastHeader()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment