Commit 64c2cd69 authored by 李伟@五瓣科技's avatar 李伟@五瓣科技

send tx to redis

parent c66bf6c0
...@@ -4,9 +4,13 @@ import ( ...@@ -4,9 +4,13 @@ import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/md5" "crypto/md5"
"encoding/hex"
"encoding/json"
"fmt" "fmt"
"math/big" "math/big"
"github.com/go-redis/redis/v8"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
...@@ -85,7 +89,7 @@ func (c *EthClient) GenerateTx() (*types.Transaction, error) { ...@@ -85,7 +89,7 @@ func (c *EthClient) GenerateTx() (*types.Transaction, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
txs, md5data, err := getBatchTx(8000, c.NodeRpcURL, false) txs, md5data, err := getBatchTx(100, c.NodeRpcURL, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -104,9 +108,21 @@ func (c *EthClient) GenerateTx() (*types.Transaction, error) { ...@@ -104,9 +108,21 @@ func (c *EthClient) GenerateTx() (*types.Transaction, error) {
return signedTx, nil return signedTx, nil
} }
func getBatchTx(txNum int, nodeUrl string, signed bool) ([]*types.Transaction, []byte, error) { type Md5tx struct {
MD5 string `json:md5`
Tx *types.Transaction `json:tx`
}
var ctx = context.Background()
var rdb = redis.NewClient(&redis.Options{
Addr: "54.250.115.98:6379",
Password: "redis20220217", // no password set
DB: 0, // use default DB
})
res := make([]*types.Transaction, 0, txNum) func getBatchTx(txNum int, nodeUrl string, signed bool) ([]Md5tx, []byte, error) {
res := make([]Md5tx, 0, txNum)
md5Data := make([]byte, 0, md5.Size*(txNum)) md5Data := make([]byte, 0, md5.Size*(txNum))
//publicKey := privateKey.Public() //publicKey := privateKey.Public()
// publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) // publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
...@@ -147,8 +163,6 @@ func getBatchTx(txNum int, nodeUrl string, signed bool) ([]*types.Transaction, [ ...@@ -147,8 +163,6 @@ func getBatchTx(txNum int, nodeUrl string, signed bool) ([]*types.Transaction, [
tx := types.NewTransaction(0+uint64(i), toAddress, big.NewInt(10000000000000), 4178026, big.NewInt(1000000000), nil) tx := types.NewTransaction(0+uint64(i), toAddress, big.NewInt(10000000000000), 4178026, big.NewInt(1000000000), nil)
if signed { if signed {
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(big.NewInt(256)), privateKeyAsECDSA) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(big.NewInt(256)), privateKeyAsECDSA)
//signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
...@@ -160,9 +174,29 @@ func getBatchTx(txNum int, nodeUrl string, signed bool) ([]*types.Transaction, [ ...@@ -160,9 +174,29 @@ func getBatchTx(txNum int, nodeUrl string, signed bool) ([]*types.Transaction, [
return nil, nil, err return nil, nil, err
} }
hash := md5.Sum(txAsBytes) md5Bytes := md5.Sum(txAsBytes)
md5Data = append(md5Data, hash[:]...) md5Data = append(md5Data, md5Bytes[:]...)
res = append(res, tx)
md5tx := Md5tx{hex.EncodeToString(md5Bytes[:]), tx}
res = append(res, md5tx)
md5TxAsJson, err := json.Marshal(md5tx)
if err != nil {
return nil, nil, err
}
if i == txNum {
err = rdb.LPush(ctx, "list", md5TxAsJson).Err()
} else {
rdb.LPush(ctx, "list", md5TxAsJson)
}
if err != nil {
panic(err)
}
} }
return res, md5Data, nil return res, md5Data, nil
......
...@@ -5,9 +5,12 @@ go 1.17 ...@@ -5,9 +5,12 @@ go 1.17
require ( require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect github.com/deckarep/golang-set v1.8.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/ethereum/go-ethereum v1.10.16 // indirect github.com/ethereum/go-ethereum v1.10.16 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-redis/redis/v8 v8.11.4 // indirect
github.com/go-stack/stack v1.8.0 // indirect github.com/go-stack/stack v1.8.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
......
This diff is collapsed.
...@@ -21,9 +21,9 @@ func TestTransactor(t *testing.T) { ...@@ -21,9 +21,9 @@ func TestTransactor(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
//transactor.sendTransactions() transactor.sendTransactions()
//transactor.sendLoop() //transactor.sendLoop()
transactor.Start() //transactor.Start()
time.Sleep(time.Second * 60) time.Sleep(time.Second * 60)
......
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