From ba35d2f07f1bb414a399c608799868c4bae4b296 Mon Sep 17 00:00:00 2001 From: bxq2011hust Date: Wed, 20 Dec 2023 17:26:14 +0800 Subject: [PATCH] update --- client/go_client.go | 4 +-- client/go_client_test.go | 61 ++++++++++++++++++++++++++++++++++++++-- go.mod | 1 + 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/client/go_client.go b/client/go_client.go index 6d380c52..1e484ea4 100644 --- a/client/go_client.go +++ b/client/go_client.go @@ -316,8 +316,8 @@ func (c *Client) AsyncSendEncodedTransaction(ctx context.Context, encodedTransac return nil } -func (c *Client) SetPrivateKey(privateKey []byte) { - c.conn.GetCSDK().SetPrivateKey(privateKey) +func (c *Client) SetPrivateKey(privateKey []byte) error { + return c.conn.GetCSDK().SetPrivateKey(privateKey) } // TransactionReceipt returns the receipt of a transaction by transaction hash. diff --git a/client/go_client_test.go b/client/go_client_test.go index bb72acfb..8b31bef3 100644 --- a/client/go_client_test.go +++ b/client/go_client_test.go @@ -35,7 +35,7 @@ func GetClient(t *testing.T) *Client { return c } -// Get contractAddress、transactionHash、blockHash by this test +// Get contractAddress, transactionHash, blockHash by this test func TestBlockHashByNumber(t *testing.T) { deployedAddress, txHash := deployHelloWorld(t) c := GetClient(t) @@ -450,6 +450,9 @@ func TestAsnycHelloWorldSet(t *testing.T) { t.Fatalf("receipt status error: %v", receipt.Status) } input, err := parsed.Pack("set", "hello, world") + if err != nil { + t.Fatalf("parsed.Pack error: %v", err) + } var wg sync.WaitGroup count := 1000 for i := 0; i < count; i++ { @@ -481,6 +484,9 @@ func TestHelloWorldSet(t *testing.T) { t.Fatalf("receipt status error: %v", receipt.Status) } input, err := parsed.Pack("set", "hello, world") + if err != nil { + t.Fatalf("parsed.Pack error: %v", err) + } count := 1000 for i := 0; i < count; i++ { receipt, err = c.SendTransaction(context.Background(), nil, &address, input) @@ -494,7 +500,52 @@ func TestHelloWorldSet(t *testing.T) { } func TestSetPrivateKey(t *testing.T) { - + c := GetClient(t) + parsed, _ := abi.JSON(strings.NewReader(HelloWorldABI)) + address, receipt, _, err := bind.DeployContract(c.GetTransactOpts(), parsed, common.FromHex(HelloWorldBin), c) + if err != nil { + t.Fatalf("DeployHelloWorld failed: %v", err) + } + if receipt.Status != 0 { + t.Fatalf("receipt status error: %v", receipt.Status) + } + tx, err := c.GetTransactionByHash(context.Background(), common.HexToHash(receipt.TransactionHash), true) + if err != nil { + t.Fatalf("GetTransactionByHash failed: %v", err) + } + t.Logf("tx: %v", tx) + from := receipt.From + input, err := parsed.Pack("set", "hello, world") + if err != nil { + t.Fatalf("parsed.Pack error: %v", err) + } + receipt, err = c.SendTransaction(context.Background(), nil, &address, input) + if err != nil { + t.Fatalf("SendTransaction error: %v", err) + } + from2 := receipt.From + if from != from2 { + t.Fatalf("from not equal") + } + privateKey, err := hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b57") + if err != nil { + t.Fatalf("decode hex failed of %v", err) + } + err = c.SetPrivateKey(privateKey) + if err != nil { + t.Fatalf("SetPrivateKey failed of %v", err) + } + receipt, err = c.SendTransaction(context.Background(), nil, &address, input) + if err != nil { + t.Fatalf("SendTransaction error: %v", err) + } + if receipt.Status != 0 { + t.Fatalf("receipt status error: %v", receipt.Status) + } + from3 := receipt.From + if from == from3 { + t.Fatalf("from not change") + } } func GetClientForBench(b *testing.B) *Client { @@ -522,6 +573,9 @@ func BenchmarkHelloWorldSet(b *testing.B) { b.Fatalf("receipt status error: %v", receipt.Status) } input, err := parsed.Pack("set", "hello, world") + if err != nil { + b.Fatalf("parsed.Pack error: %v", err) + } b.ResetTimer() for i := 0; i < b.N; i++ { receipt, err = c.SendTransaction(context.Background(), nil, &address, input) @@ -545,6 +599,9 @@ func BenchmarkAsyncHelloWorldSet(b *testing.B) { b.Fatalf("receipt status error: %v", receipt.Status) } input, err := parsed.Pack("set", "hello, world") + if err != nil { + b.Fatalf("parsed.Pack error: %v", err) + } b.ResetTimer() var wg sync.WaitGroup for i := 0; i < b.N; i++ { diff --git a/go.mod b/go.mod index 6bbe5ddb..c3670418 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/FISCO-BCOS/go-sdk go 1.21.5 replace ( + github.com/FISCO-BCOS/bcos-c-sdk/bindings/go/csdk => /Users/baixingqiang/Workspace/fisco/bcos-c-sdk/bindings/go/csdk golang.org/x/net => github.com/golang/net v0.0.0-20190415214537-1da14a5a36f2 golang.org/x/sync => github.com/golang/sync v0.0.0-20190412183630-56d357773e84 )