Commit 76c4f4de authored by Ubuntu's avatar Ubuntu

encode with RLP

parent d839b8d3
...@@ -17,12 +17,22 @@ var originalTxParam EthClient ...@@ -17,12 +17,22 @@ var originalTxParam EthClient
var originTxPrivateKey string = "9e0944f587e1043d6e303644738b0c7c77ed15b176ca574ed0be40c0b9bbdc3a" var originTxPrivateKey string = "9e0944f587e1043d6e303644738b0c7c77ed15b176ca574ed0be40c0b9bbdc3a"
var originalTxsHashQueue chan *[]byte = make(chan *[]byte, 1000) var originalTxsHashQueue chan *[]byte = make(chan *[]byte, 1000)
var batchTxsForRedis chan *BatchTx = make(chan *BatchTx, batchTxHashSize*batchTxHashQueueSize) var batchTxsForRedis chan *OriginalBatchTxs = make(chan *OriginalBatchTxs, batchTxHashSize*batchTxHashQueueSize)
const batchTxSize = 10000 const batchTxSize = 10000
const batchTxHashSize = 100 const batchTxHashSize = 100
const batchTxHashQueueSize = 10 const batchTxHashQueueSize = 10
type OriginalBatchTxs struct {
Txs []TxWithFrom
Hash []byte
}
type TxWithFrom struct {
From []byte
TxBytes []byte
}
func init() { func init() {
originalTxPrivatekeyAsECDSA, err := crypto.HexToECDSA(originTxPrivateKey) originalTxPrivatekeyAsECDSA, err := crypto.HexToECDSA(originTxPrivateKey)
...@@ -55,19 +65,13 @@ func ProduceOriginalTx() error { ...@@ -55,19 +65,13 @@ func ProduceOriginalTx() error {
for j := 0; j < batchTxHashSize; j++ { for j := 0; j < batchTxHashSize; j++ {
var txsBytes []byte var txsBytes []byte
var txsWithFrom []*Tx = make([]*Tx, 0, batchTxSize) var txs []TxWithFrom = make([]TxWithFrom, 0, batchTxSize)
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 err return err
} }
txsWithFrom = append(txsWithFrom, &Tx{
From: originalTxParam.FromAddr.Bytes(),
To: tx.To().Bytes(),
Amount: tx.Value().Int64(),
})
originalTxParam.Nonce += 1 originalTxParam.Nonce += 1
txAsBytes, err := tx.MarshalBinary() txAsBytes, err := tx.MarshalBinary()
if err != nil { if err != nil {
...@@ -75,6 +79,10 @@ func ProduceOriginalTx() error { ...@@ -75,6 +79,10 @@ func ProduceOriginalTx() error {
} }
txsBytes = append(txsBytes, txAsBytes...) txsBytes = append(txsBytes, txAsBytes...)
txs = append(txs, TxWithFrom{
originalTxParam.FromAddr[:],
txAsBytes})
} }
h := sha256.New() h := sha256.New()
...@@ -85,8 +93,8 @@ func ProduceOriginalTx() error { ...@@ -85,8 +93,8 @@ func ProduceOriginalTx() error {
hashBytes := h.Sum(nil) hashBytes := h.Sum(nil)
hashesBytes = append(hashesBytes, hashBytes...) hashesBytes = append(hashesBytes, hashBytes...)
txs := BatchTx{Hash: hashBytes, Txs: txsWithFrom} batchTxs := OriginalBatchTxs{Hash: hashBytes, Txs: txs}
batchTxsForRedis <- &txs batchTxsForRedis <- &batchTxs
} }
originalTxsHashQueue <- &hashesBytes originalTxsHashQueue <- &hashesBytes
} else { } else {
......
...@@ -2,15 +2,13 @@ package multisend ...@@ -2,15 +2,13 @@ package multisend
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"runtime" "runtime"
"time" "time"
"github.com/golang/protobuf/proto"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"golang.org/x/time/rate" "golang.org/x/time/rate"
// "github.com/go-redis/redis_rate/v9"
) )
var ( var (
...@@ -18,12 +16,11 @@ var ( ...@@ -18,12 +16,11 @@ var (
) )
type Job struct { type Job struct {
Client *redis.Client Client *redis.Client
//Limiter *redis_rate.Limiter Id int
Id int
} }
func initClient(poolSize int) (*redis.Client) { func initClient(poolSize int) *redis.Client {
client := redis.NewClient(&redis.Options{ client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379", Addr: "127.0.0.1:6379",
DialTimeout: time.Second, DialTimeout: time.Second,
...@@ -43,9 +40,6 @@ func initClient(poolSize int) (*redis.Client) { ...@@ -43,9 +40,6 @@ func initClient(poolSize int) (*redis.Client) {
func Start() { func Start() {
//任务channel 定义缓冲器为job数量
//jobs := make(chan Job, jobnum)
client := initClient(10) client := initClient(10)
count := 0 count := 0
limiter := rate.NewLimiter(rate.Every(time.Millisecond*100), 1) limiter := rate.NewLimiter(rate.Every(time.Millisecond*100), 1)
...@@ -53,20 +47,26 @@ func Start() { ...@@ -53,20 +47,26 @@ func Start() {
for { for {
limiter.Wait(cxt) limiter.Wait(cxt)
select { select {
case txs := <-batchTxsForRedis: case batchTxs := <-batchTxsForRedis:
startTime := time.Now() startTime := time.Now()
data, err := proto.Marshal(txs) // data, err := proto.Marshal(txs)
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
batchTxsAsBytes, err := json.Marshal(batchTxs)
if err := client.LPush(context.Background(), "list", data).Err(); err != nil { if err != nil {
panic(err) panic(err)
} }
count += 1 if err := client.LPush(context.Background(), "list", batchTxsAsBytes).Err(); err != nil {
fmt.Printf("count %d txs size: %d takes %v time: %s \n", count, len(data), time.Since(startTime),time.Now()) panic(err)
}
count += 1
fmt.Printf("count %d txs size: %d takes %v time: %s \n", count, len(batchTxsAsBytes), time.Since(startTime), time.Now())
} }
} }
} }
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