Skip to content

Commit

Permalink
add parallelok manual
Browse files Browse the repository at this point in the history
  • Loading branch information
bxq2011hust committed Jan 10, 2024
1 parent 7d061dc commit 47e8621
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 38 deletions.
12 changes: 9 additions & 3 deletions v3/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package bind
import (
"context"
"errors"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -100,10 +101,15 @@ func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller
func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, abiStr string, backend ContractBackend, params ...interface{}) (common.Address, *types.Receipt, *BoundContract, error) {
_, receipt, c, err := deploy(opts, abi, bytecode, abiStr, backend, params...)
addr := common.Address{}
if receipt != nil {
addr = common.HexToAddress(receipt.ContractAddress)
if receipt != nil && err == nil {
if receipt.Status == types.Success {
addr = common.HexToAddress(receipt.ContractAddress)
return addr, receipt, c, nil
} else {
return addr, receipt, c, fmt.Errorf("deploy failed, receipt status: %d, message: %s", receipt.Status, receipt.GetErrorMessage())
}
}
return addr, receipt, c, err
return addr, nil, nil, err
}

func DeployContractGetReceipt(opts *TransactOpts, abi abi.ABI, bytecode []byte, abiStr string, backend ContractBackend, params ...interface{}) (*types.Transaction, *types.Receipt, *BoundContract, error) {
Expand Down
10 changes: 9 additions & 1 deletion v3/client/go_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ const (

// Dial connects a client to the given URL and groupID.
func Dial(configFile, groupID string, privateKey []byte) (*Client, error) {

if len(privateKey) == 0 {
return nil, errors.New("private key is empty")
}
c, err := NewConnectionByFile(configFile, groupID, privateKey)
if err != nil {
return nil, err
Expand All @@ -58,6 +60,12 @@ func Dial(configFile, groupID string, privateKey []byte) (*Client, error) {

// DialContext pass the context to the rpc client
func DialContext(ctx context.Context, config *Config) (*Client, error) {
if config == nil {
return nil, errors.New("config is nil")
}
if len(config.PrivateKey) == 0 {
return nil, errors.New("private key is empty")
}
c, err := NewConnection(config)
if err != nil {
return nil, err
Expand Down
15 changes: 13 additions & 2 deletions v3/client/go_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/FISCO-BCOS/go-sdk/v3/abi"
"github.com/FISCO-BCOS/go-sdk/v3/abi/bind"
"github.com/FISCO-BCOS/go-sdk/v3/types"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
)

Expand All @@ -27,8 +28,8 @@ func GetClient(t *testing.T) *Client {
if err != nil {
t.Fatalf("decode hex failed of %v", err)
}
config := &Config{IsSMCrypto: false, GroupID: "group0",
PrivateKey: privateKey, Host: "127.0.0.1", Port: 20200, TLSCaFile: "./ca.crt", TLSKeyFile: "./sdk.key", TLSCertFile: "./sdk.crt", DisableSsl: false}
config := &Config{IsSMCrypto: false, GroupID: "group0", DisableSsl: true,
PrivateKey: privateKey, Host: "127.0.0.1", Port: 20200, TLSCaFile: "./ca.crt", TLSKeyFile: "./sdk.key", TLSCertFile: "./sdk.crt"}
c, err := DialContext(context.Background(), config)
if err != nil {
t.Fatalf("Dial to %s:%d failed of %v", config.Host, config.Port, err)
Expand Down Expand Up @@ -347,6 +348,16 @@ func TestSystemConfigByKey(t *testing.T) {
t.Logf("the value got by the key:\n%s", raw.GetValue())
}

func TestCallEmptyAddress(t *testing.T) {
c := GetClient(t)
address := common.HexToAddress("0x0")
msg := ethereum.CallMsg{To: &address, Data: []byte{0, 0, 0, 0}}
_, err := c.CallContract(context.Background(), msg)
if err == nil {
t.Fatalf("call empty address, the err is nil")
}
}

func TestCreateEncodedTransactionAndSend(t *testing.T) {
c := GetClient(t)
// deploy helloworld contract
Expand Down
3 changes: 2 additions & 1 deletion v3/cmd/commandline/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func initConfig() {
}
privateKey = key
}
} else {
}
if len(privateKey) == 0 {
address := "0xFbb18d54e9Ee57529cda8c7c52242EFE879f064F"
privateKey, _ = hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58")
if smCrypto {
Expand Down
262 changes: 262 additions & 0 deletions v3/examples/parallelok/manual/main.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,73 @@ import (
"math/big"
"os"
"strconv"
"strings"
"sync"

"github.com/FISCO-BCOS/go-sdk/v3/client"
"github.com/FISCO-BCOS/go-sdk/v3/smcrypto"
"github.com/FISCO-BCOS/go-sdk/v3/types"
"github.com/schollz/progressbar/v3"
flag "github.com/spf13/pflag"
)

func main() {
if len(os.Args) < 5 {
fmt.Printf("Usage: ./%s groupID userCount total qps", os.Args[0])
return
}
groupID := os.Args[1]
userCount, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("parse userCount error", err)
return
pemFileName := flag.StringP("pem", "p", "", "pem file path")
groupID := flag.StringP("group", "g", "group0", "groupID")
disableSsl := flag.BoolP("disableSsl", "d", false, "disable ssl")
isSmCrypto := flag.BoolP("smCrypto", "s", false, "use sm crypto")
endpoint := flag.StringP("endpoint", "e", "127.0.0.1:20200", "node endpoint")
certPath := flag.StringP("cert", "c", "./conf/", "cert path")
userCount := flag.IntP("userCount", "u", 1000, "user count")
totalTx := flag.IntP("totalTxTx", "t", 10000, "totalTx tx")
qps := flag.IntP("qps", "q", 1000, "qps")
flag.Parse()
fmt.Printf("pem: %s, groupID: %s, disableSsl: %v, isSmCrypto: %v, endpoint: %s, certPath: %s, userCount: %d, totalTx: %d, qps: %d\n", *pemFileName, *groupID, *disableSsl, *isSmCrypto, *endpoint, *certPath, *userCount, *totalTx, *qps)

var privateKey []byte
if len(*pemFileName) != 0 {
_, err := os.Stat(*pemFileName)
if err != nil && os.IsNotExist(err) {
fmt.Println("private key file set but not exist, use default private key")
} else if err != nil {
fmt.Printf("check private key file failed, err: %v\n", err)
return
} else {
key, curve, err := client.LoadECPrivateKeyFromPEM(*pemFileName)
if err != nil {
fmt.Printf("parse private key failed, err: %v\n", err)
return
}
if *isSmCrypto && curve != client.Sm2p256v1 {
fmt.Printf("smCrypto should use sm2p256v1 private key, but found %s\n", curve)
return
}
if !*isSmCrypto && curve != client.Secp256k1 {
fmt.Printf("should use secp256k1 private key, but found %s\n", curve)
return
}
privateKey = key
}
}
total, err := strconv.Atoi(os.Args[3])
if err != nil {
fmt.Println("parse total error", err)
return
if len(privateKey) == 0 {
address := "0xFbb18d54e9Ee57529cda8c7c52242EFE879f064F"
privateKey, _ = hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58")
if *isSmCrypto {
address = smcrypto.SM2KeyToAddress(privateKey).Hex()
}
fmt.Println("use default private key, address: ", address)
}
qps, err := strconv.Atoi(os.Args[4])
if err != nil {
fmt.Println("parse qps error", err)
return
ret := strings.Split(*endpoint, ":")
host := ret[0]
port, _ := strconv.Atoi(ret[1])
var config *client.Config
if !*isSmCrypto {
config = &client.Config{IsSMCrypto: *isSmCrypto, GroupID: *groupID, DisableSsl: *disableSsl,
PrivateKey: privateKey, Host: host, Port: port, TLSCaFile: *certPath + "/ca.crt", TLSKeyFile: *certPath + "/sdk.key", TLSCertFile: *certPath + "/sdk.crt"}
} else {
config = &client.Config{IsSMCrypto: *isSmCrypto, GroupID: *groupID, DisableSsl: *disableSsl,
PrivateKey: privateKey, Host: host, Port: port, TLSCaFile: *certPath + "/sm_ca.crt", TLSKeyFile: *certPath + "/sm_sdk.key", TLSCertFile: *certPath + "/sm_sdk.crt", TLSSmEnKeyFile: *certPath + "/sm_ensdk.key", TLSSmEnCertFile: *certPath + "/sm_ensdk.crt"}
}
fmt.Println("start perf groupID:", groupID, "userCount:", userCount, "total:", total, "qps:", qps)

privateKey, _ := hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58")
config := &client.Config{IsSMCrypto: false, GroupID: groupID, DisableSsl: false,
PrivateKey: privateKey, Host: "127.0.0.1", Port: 20200, TLSCaFile: "./conf/ca.crt", TLSKeyFile: "./conf/sdk.key", TLSCertFile: "./conf/sdk.crt"}
client, err := client.DialContext(context.Background(), config)
// client, err := client.Dial("./config.ini", groupID, privateKey)
if err != nil {
Expand Down Expand Up @@ -68,7 +102,7 @@ func main() {
balance := sync.Map{}
initValue := int64(1000000000)
failedCount := 0
for i := 0; i < userCount; i++ {
for i := 0; i < *userCount; i++ {
_, err = transfer.AsyncSet(func(receipt *types.Receipt, err error) {
if err != nil {
fmt.Println("add user error", err)
Expand All @@ -89,8 +123,8 @@ func main() {
wg.Wait()
fmt.Println("start transfer")
var wg2 sync.WaitGroup
sendBar := progressbar.Default(int64(total), "send")
receiveBar := progressbar.Default(int64(total), "receive")
sendBar := progressbar.Default(int64(*totalTx), "send")
receiveBar := progressbar.Default(int64(*totalTx), "receive")
// routineCount := (qps + 4000) / 4000
// sended := int64(0)
// for i := 0; i < routineCount; i++ {
Expand Down Expand Up @@ -145,9 +179,9 @@ func main() {
// time.Sleep(time.Second * 5)
// wg2.Wait()

for i := 0; i < total; i++ {
from := i % userCount
to := (i + userCount/2) % userCount
for i := 0; i < *totalTx; i++ {
from := i % *userCount
to := (i + *userCount/2) % *userCount
amount := int64(1)
_, err = transfer.AsyncTransfer(func(receipt *types.Receipt, err error) {
receiveBar.Add(1)
Expand Down Expand Up @@ -187,7 +221,7 @@ func main() {
// check balance
fmt.Println("check balance...")
var wg3 sync.WaitGroup
for i := 0; i < userCount; i++ {
for i := 0; i < *userCount; i++ {
wg3.Add(1)
go func(i int) {
b, err := transfer.BalanceOf(strconv.Itoa(i))
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions v3/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ require (
github.com/schollz/progressbar/v3 v3.14.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/urfave/cli/v2 v2.10.2
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
)

require (
Expand Down Expand Up @@ -71,7 +73,6 @@ require (
github.com/rs/cors v1.7.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
Expand All @@ -83,7 +84,6 @@ require (
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down

0 comments on commit 47e8621

Please sign in to comment.