Commit 2db7e343 authored by 李伟@五瓣科技's avatar 李伟@五瓣科技

update process api

parent 608a54d4
......@@ -9,9 +9,11 @@ import (
"net/http"
"strconv"
"sync/atomic"
"time"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"github.com/gorilla/mux"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
......@@ -46,9 +48,11 @@ type SendRecord struct {
}
type BatchSend struct {
BeginTime int64 `json:"begin_time"`
EndTime int64 `json:"end_time"`
TxNum int `json:"tx_num"`
SendToRedisBeginTime int64 `json:"send_to_redis_begin_time"`
SendTxsEndTime int64 `json:"send_txs_end_time"`
TxNum int `json:"cons_tx_num"`
BeginOriginalTx common.Hash `json:"begin_original_tx"`
EndOriginalTx common.Hash `json:"end_original_tx"`
}
type WebServicer struct {
......@@ -223,8 +227,8 @@ func (web *WebServicer) TxsHandler(w http.ResponseWriter, r *http.Request) {
return
}
if params.TxCount > 1000*1000 { //百万
http.Error(w, fmt.Sprintf("max tx count 1000*1000 "), http.StatusBadRequest)
if params.TxCount > MaxTxCount {
http.Error(w, fmt.Sprintf("max tx count %d ", MaxTxCount), http.StatusBadRequest)
return
}
......@@ -235,24 +239,19 @@ func (web *WebServicer) TxsHandler(w http.ResponseWriter, r *http.Request) {
atomic.StoreInt32(&Running, 1)
// 有余额的话,金额不好判断,由前端校验;
if params.EveryTxAmount*params.TxCount < params.RequestAmount && params.RequestAmount > 5*1000*1000 {
if params.EveryTxAmount*params.TxCount < params.RequestAmount && params.RequestAmount > MaxRequestAmount {
http.Error(w, fmt.Sprintf("params.EveryTxAmount*params.TxCount < params.RequestAmount && params.RequestAmount > 5*1000*1000"), http.StatusBadRequest)
http.Error(w, fmt.Sprintf("params.EveryTxAmount*params.TxCount < params.RequestAmount && params.RequestAmount > %d", MaxRequestAmount), http.StatusBadRequest)
return
}
//startTime := time.Now()
res, err := web.ProduceTxs(params.From, params.ToAddrs, int(params.TxCount), params.EveryTxAmount)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//fmt.Printf("takes time: %s \n", time.Since(startTime).String())
resAsJson, err := json.Marshal(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
......@@ -294,6 +293,9 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
for {
var hashesBytes []byte = make([]byte, 0, 32*batchTxHashSize)
var beginOriginalTx common.Hash
var endOriginalTx common.Hash
var sendRedisBeginTime time.Time
for j := 0; j < batchTxHashSize; j++ {
var txsBytes []byte
......@@ -306,6 +308,12 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
return nil, err
}
if j == i && i == 0 {
beginOriginalTx = tx.Hash()
}
endOriginalTx = tx.Hash()
originalTxParam.Nonce += 1
txAsBytes, err := tx.MarshalBinary()
......@@ -346,6 +354,9 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
batchTxs := OriginalBatchTxs{Hash: hashBytes, Txs: txs}
batchTxsForRedis <- &batchTxs
if j == 0 {
sendRedisBeginTime = time.Now()
}
if txCount == 0 {
break
......@@ -359,8 +370,11 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
}
conTxsQueue <- ConTxsWithId{
Id: id,
Tx: tx}
SendRedisTime: sendRedisBeginTime,
BeginOriginalTx: beginOriginalTx,
EndOriginalTx: endOriginalTx,
Id: id,
Tx: tx}
consTxNum++
......
......@@ -26,10 +26,11 @@ const batchTxHashSize = 3
const batchTxHashQueueSize = 10
type ConTxsWithId struct {
SendRedisTime time.Time
OriginalTx common.Hash
Id uuid.UUID
Tx *types.Transaction
SendRedisTime time.Time
BeginOriginalTx common.Hash
EndOriginalTx common.Hash
Id uuid.UUID
Tx *types.Transaction
}
type OriginalBatchTxs struct {
......
......@@ -11,6 +11,7 @@ import (
"time"
"code.wuban.net.cn/multisend/internal/logging"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/google/uuid"
"github.com/gorilla/websocket"
......@@ -255,12 +256,22 @@ func (t *Transactor) sendTransactions() error {
}()
t.logger.Info("Sending batch of transactions", "now", time.Now().Format("15:04:05"), "toSend", toSend)
batchStartTime := time.Now()
var sendToRedisStartTime time.Time
var beginTx common.Hash
var endTx common.Hash
id := uuid.UUID{}
for ; sent < toSend; sent++ {
select {
case txWithId := <-conTxsQueue:
id = txWithId.Id
sendToRedisStartTime = txWithId.SendRedisTime
if sent == 0 {
beginTx = txWithId.BeginOriginalTx
}
endTx = txWithId.EndOriginalTx
data, err := txWithId.Tx.MarshalBinary()
if err != nil {
return err
......@@ -296,9 +307,11 @@ func (t *Transactor) sendTransactions() error {
if record, ok := GetSendRecord(id); ok {
b := BatchSend{
BeginTime: batchStartTime.Unix(),
EndTime: time.Now().Unix(),
TxNum: sent,
BeginOriginalTx: beginTx,
EndOriginalTx: endTx,
SendToRedisBeginTime: sendToRedisStartTime.Unix(),
SendTxsEndTime: time.Now().Unix(),
TxNum: sent,
}
record.SendRecord = append(record.SendRecord, b)
......
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