Commit 67c4ca2e authored by 李伟@五瓣科技's avatar 李伟@五瓣科技

return tree

parent 7b04a666
...@@ -69,7 +69,7 @@ var rootCmd = &cobra.Command{ ...@@ -69,7 +69,7 @@ var rootCmd = &cobra.Command{
// }() // }()
//multisend.Start(redisAddr, redisPasswd) multisend.Start(redisAddr, redisPasswd)
}, },
} }
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet" hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
"github.com/rs/cors" "github.com/rs/cors"
"github.com/shopspring/decimal"
) )
var mnemonic = "matter someone fee garlic final police during vapor stool cargo snake dove" var mnemonic = "matter someone fee garlic final police during vapor stool cargo snake dove"
...@@ -47,12 +48,18 @@ type SendRecord struct { ...@@ -47,12 +48,18 @@ type SendRecord struct {
SendRecord []BatchSend `json:"send_record"` SendRecord []BatchSend `json:"send_record"`
} }
type ConsTxWithBatchHash struct {
ConsTxHash []byte `json:"cons_tx_hash"`
BatchTxsHash []byte `json:"batch_txs_hash"`
}
type BatchSend struct { type BatchSend struct {
SendToRedisBeginTime int64 `json:"send_to_redis_begin_time"` SendToRedisBeginTime int64 `json:"send_to_redis_begin_time"`
SendTxsEndTime int64 `json:"send_txs_end_time"` SendTxsEndTime int64 `json:"send_txs_end_time"`
TxNum int `json:"cons_tx_num"` TxNum int `json:"cons_tx_num"`
BeginOriginalTx common.Hash `json:"begin_original_tx"` BeginOriginalTx common.Hash `json:"begin_original_tx"`
EndOriginalTx common.Hash `json:"end_original_tx"` EndOriginalTx common.Hash `json:"end_original_tx"`
ConsTxWithBatchHash []ConsTxWithBatchHash `json:"con_tx_with_batch_hash""`
} }
type WebServicer struct { type WebServicer struct {
...@@ -113,12 +120,53 @@ func (web *WebServicer) ParamHandler(w http.ResponseWriter, r *http.Request) { ...@@ -113,12 +120,53 @@ func (web *WebServicer) ParamHandler(w http.ResponseWriter, r *http.Request) {
w.Write(respAsJson) w.Write(respAsJson)
} }
func (web *WebServicer) GetTreeHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
uuidStr := vars["uuid"]
id, err := uuid.Parse(uuidStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if record, ok := GetSendRecord(id); ok {
consTxWithBatchHash := []ConsTxWithBatchHash{}
for _, v := range record.SendRecord {
consTxWithBatchHash = append(consTxWithBatchHash, v.ConsTxWithBatchHash...)
}
recordAsJon, err := json.Marshal(consTxWithBatchHash)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(recordAsJon)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("can not find the uuid: %s ", id.String()), http.StatusInternalServerError)
return
}
}
func (web *WebServicer) WebService(config Config) error { func (web *WebServicer) WebService(config Config) error {
r := mux.NewRouter() r := mux.NewRouter()
// Routes consist of a path and a handler function. // Routes consist of a path and a handler function.
r.HandleFunc("/param", web.ParamHandler) r.HandleFunc("/param", web.ParamHandler)
//r.HandleFunc("/faucet/{addr}", web.FaucetHandler) //r.HandleFunc("/faucet/{addr}", web.FaucetHandler)
r.HandleFunc("/process/{uuid}", web.ProcessHandler) r.HandleFunc("/process/{uuid}", web.ProcessHandler)
r.HandleFunc("/tree/{uuid}", web.GetTreeHandler)
r.HandleFunc("/txs", web.TxsHandler).Methods("POST") r.HandleFunc("/txs", web.TxsHandler).Methods("POST")
clientFactory, exists := clientFactories["ethclient"] clientFactory, exists := clientFactories["ethclient"]
...@@ -157,11 +205,6 @@ func (web *WebServicer) ProcessHandler(w http.ResponseWriter, r *http.Request) { ...@@ -157,11 +205,6 @@ func (web *WebServicer) ProcessHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
uuidStr := vars["uuid"] uuidStr := vars["uuid"]
fmt.Printf("request addr: %s \n", uuidStr)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
id, err := uuid.Parse(uuidStr) id, err := uuid.Parse(uuidStr)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
...@@ -176,10 +219,10 @@ func (web *WebServicer) ProcessHandler(w http.ResponseWriter, r *http.Request) { ...@@ -176,10 +219,10 @@ func (web *WebServicer) ProcessHandler(w http.ResponseWriter, r *http.Request) {
sent += v.TxNum sent += v.TxNum
} }
fmt.Printf("record.TotalConTx: %d sent: %d\n", record.TotalConsTx, sent) quantity := decimal.NewFromInt(int64(sent)).Div(decimal.NewFromInt(record.TotalConsTx))
_ = quantity
record.Percent = FloatRound(float64(sent)/float64(record.TotalConsTx), 2) record.Percent = FloatRound(float64(sent)/float64(record.TotalConsTx), 2)
recordAsJon, err := json.Marshal(record) recordAsJon, err := json.Marshal(record)
if err != nil { if err != nil {
...@@ -246,13 +289,16 @@ func (web *WebServicer) TxsHandler(w http.ResponseWriter, r *http.Request) { ...@@ -246,13 +289,16 @@ func (web *WebServicer) TxsHandler(w http.ResponseWriter, r *http.Request) {
} }
res, err := web.ProduceTxs(params.From, params.ToAddrs, int(params.TxCount), params.EveryTxAmount) id := uuid.New()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) go func() {
return if err := web.ProduceTxs(params.From, params.ToAddrs, int(params.TxCount), params.EveryTxAmount, id); err != nil {
} http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}()
resAsJson, err := json.Marshal(res) resAsJson, err := json.Marshal(id)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
...@@ -271,23 +317,22 @@ type BatchTx struct { ...@@ -271,23 +317,22 @@ type BatchTx struct {
OriginalTxHash []string `json:"original_tx_hash"` OriginalTxHash []string `json:"original_tx_hash"`
} }
type ConsTxHash struct { type ConsTxHashs struct {
TxHash string `json:"tx_hash"` TxHash []byte `json:"tx_hash"`
Batches []BatchTx `json:"batches"` Batches []BatchTx `json:"batches"`
} }
type WebResp struct { type ConsTxHash struct {
ProcessId uuid.UUID `json:"process_id"` ConsTxHash []byte
AllTxs []ConsTxHash `json:"all_txs"` BatchTxsHash [][]byte
} }
func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount int, amount int64) (*WebResp, error) { type WebResp struct {
ProcessId uuid.UUID `json:"process_id"`
consTxHashs := []ConsTxHash{} AllTxs []ConsTxHashs `json:"all_txs"`
batches := []BatchTx{} }
id := uuid.New()
func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount int, amount int64, id uuid.UUID) error {
consTxNum := 0 consTxNum := 0
for { for {
...@@ -305,7 +350,7 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in ...@@ -305,7 +350,7 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
for i := 0; i < batchTxSize; i++ { for i := 0; i < batchTxSize; i++ {
tx, err := buildOriginalTx(originalTxParam.Nonce, toAddress, big.NewInt(256), nil) tx, err := buildOriginalTx(originalTxParam.Nonce, toAddress, big.NewInt(256), nil)
if err != nil { if err != nil {
return nil, err return err
} }
if j == i && i == 0 { if j == i && i == 0 {
...@@ -318,14 +363,14 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in ...@@ -318,14 +363,14 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
txAsBytes, err := tx.MarshalBinary() txAsBytes, err := tx.MarshalBinary()
if err != nil { if err != nil {
return nil, err return err
} }
txsBytes = append(txsBytes, txAsBytes...) txsBytes = append(txsBytes, txAsBytes...)
txs = append(txs, TxWithFrom{ txs = append(txs, TxWithFrom{
originalTxParam.FromAddr[:], originalTxParam.FromAddr[:],
txAsBytes}) tx})
txshash = append(txshash, tx.Hash().String()) txshash = append(txshash, tx.Hash().String())
...@@ -338,19 +383,12 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in ...@@ -338,19 +383,12 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
h := sha256.New() h := sha256.New()
if _, err := h.Write(txsBytes); err != nil { if _, err := h.Write(txsBytes); err != nil {
return nil, err return err
} }
hashBytes := h.Sum(nil) hashBytes := h.Sum(nil)
hashesBytes = append(hashesBytes, hashBytes...) hashesBytes = append(hashesBytes, hashBytes...)
batch := BatchTx{
BatchHash: fmt.Sprintf("%x", hashBytes),
OriginalTxHash: txshash,
}
batches = append(batches, batch)
batchTxs := OriginalBatchTxs{Hash: hashBytes, Txs: txs} batchTxs := OriginalBatchTxs{Hash: hashBytes, Txs: txs}
batchTxsForRedis <- &batchTxs batchTxsForRedis <- &batchTxs
...@@ -363,10 +401,10 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in ...@@ -363,10 +401,10 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
} }
} }
originalTxsHashQueue <- &hashesBytes originalTxsHashQueue <- &hashesBytes //和下一行改为同步模式
tx, err := web.cli.GenerateTx() tx, err := web.cli.GenerateTx()
if err != nil { if err != nil {
return nil, err return err
} }
conTxsQueue <- ConTxsWithId{ conTxsQueue <- ConTxsWithId{
...@@ -374,16 +412,11 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in ...@@ -374,16 +412,11 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
BeginOriginalTx: beginOriginalTx, BeginOriginalTx: beginOriginalTx,
EndOriginalTx: endOriginalTx, EndOriginalTx: endOriginalTx,
Id: id, Id: id,
Tx: tx} Tx: tx,
BatchTxHash: hashesBytes}
consTxNum++ consTxNum++
consTxHash := ConsTxHash{
TxHash: tx.Hash().Hex(),
Batches: batches,
}
consTxHashs = append(consTxHashs, consTxHash)
if txCount == 0 { if txCount == 0 {
break break
} }
...@@ -391,7 +424,5 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in ...@@ -391,7 +424,5 @@ func (web *WebServicer) ProduceTxs(fromAddr string, toAddrs []string, txCount in
SetSendRecord(id, SendRecord{TotalConsTx: int64(consTxNum)}) SetSendRecord(id, SendRecord{TotalConsTx: int64(consTxNum)})
return &WebResp{ return nil
ProcessId: id,
AllTxs: consTxHashs}, nil
} }
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/google/uuid" "github.com/google/uuid"
//"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
...@@ -31,6 +32,7 @@ type ConTxsWithId struct { ...@@ -31,6 +32,7 @@ type ConTxsWithId struct {
EndOriginalTx common.Hash EndOriginalTx common.Hash
Id uuid.UUID Id uuid.UUID
Tx *types.Transaction Tx *types.Transaction
BatchTxHash []byte
} }
type OriginalBatchTxs struct { type OriginalBatchTxs struct {
...@@ -39,8 +41,8 @@ type OriginalBatchTxs struct { ...@@ -39,8 +41,8 @@ type OriginalBatchTxs struct {
} }
type TxWithFrom struct { type TxWithFrom struct {
From []byte From []byte
TxBytes []byte Tx *types.Transaction
} }
func init() { func init() {
...@@ -91,7 +93,7 @@ func ProduceOriginalTx() error { ...@@ -91,7 +93,7 @@ func ProduceOriginalTx() error {
txs = append(txs, TxWithFrom{ txs = append(txs, TxWithFrom{
originalTxParam.FromAddr[:], originalTxParam.FromAddr[:],
txAsBytes}) tx})
} }
h := sha256.New() h := sha256.New()
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,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/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"golang.org/x/time/rate" "golang.org/x/time/rate"
) )
...@@ -46,7 +47,28 @@ func Start(redisAddr, passwd string) { ...@@ -46,7 +47,28 @@ func Start(redisAddr, passwd string) {
select { select {
case batchTxs := <-batchTxsForRedis: case batchTxs := <-batchTxsForRedis:
batchTxsAsBytes, err := json.Marshal(batchTxs) hashs := []common.Hash{}
txBytes := []TxBytes{}
for _, v := range batchTxs.Txs {
hashs = append(hashs, v.Tx.Hash())
txAsBytes, err := v.Tx.MarshalBinary()
if err != nil {
panic(err)
}
txBytes = append(txBytes, TxBytes{
From: v.From,
Tx: txAsBytes,
})
}
batchTxsAsBytes, err := json.Marshal(RedisBatchTxs{
Hash: batchTxs.Hash,
Txs: txBytes,
})
if err != nil { if err != nil {
panic(err) panic(err)
...@@ -55,6 +77,11 @@ func Start(redisAddr, passwd string) { ...@@ -55,6 +77,11 @@ func Start(redisAddr, passwd string) {
if err := client.LPush(context.Background(), "list", batchTxsAsBytes).Err(); err != nil { if err := client.LPush(context.Background(), "list", batchTxsAsBytes).Err(); err != nil {
panic(err) panic(err)
} }
if err := client.LPush(context.Background(), fmt.Sprintf("%x", batchTxs.Hash), hashs); err != nil {
panic(err)
}
count += 1 count += 1
case <-logTicker.C: case <-logTicker.C:
...@@ -63,3 +90,13 @@ func Start(redisAddr, passwd string) { ...@@ -63,3 +90,13 @@ func Start(redisAddr, passwd string) {
} }
} }
} }
type TxBytes struct {
From []byte
Tx []byte
}
type RedisBatchTxs struct {
Txs []TxBytes
Hash []byte
}
...@@ -260,11 +260,16 @@ func (t *Transactor) sendTransactions() error { ...@@ -260,11 +260,16 @@ func (t *Transactor) sendTransactions() error {
var beginTx common.Hash var beginTx common.Hash
var endTx common.Hash var endTx common.Hash
id := uuid.UUID{} id := uuid.UUID{}
consTxWithBatchs := []ConsTxWithBatchHash{}
for ; sent < toSend; sent++ { for ; sent < toSend; sent++ {
select { select {
case txWithId := <-conTxsQueue: case txWithId := <-conTxsQueue:
id = txWithId.Id id = txWithId.Id
consTxWithBatchs = append(consTxWithBatchs, ConsTxWithBatchHash{ConsTxHash: txWithId.Tx.Hash().Bytes(),
BatchTxsHash: txWithId.BatchTxHash})
sendToRedisStartTime = txWithId.SendRedisTime sendToRedisStartTime = txWithId.SendRedisTime
if sent == 0 { if sent == 0 {
...@@ -312,6 +317,7 @@ func (t *Transactor) sendTransactions() error { ...@@ -312,6 +317,7 @@ func (t *Transactor) sendTransactions() error {
SendToRedisBeginTime: sendToRedisStartTime.Unix(), SendToRedisBeginTime: sendToRedisStartTime.Unix(),
SendTxsEndTime: time.Now().Unix(), SendTxsEndTime: time.Now().Unix(),
TxNum: sent, TxNum: sent,
ConsTxWithBatchHash: consTxWithBatchs,
} }
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