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

update process api

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