Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bxq2011hust committed Dec 20, 2023
1 parent 51b3604 commit ba35d2f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
4 changes: 2 additions & 2 deletions client/go_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
61 changes: 59 additions & 2 deletions client/go_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetClient(t *testing.T) *Client {
return c
}

// Get contractAddresstransactionHashblockHash by this test
// Get contractAddress, transactionHash, blockHash by this test
func TestBlockHashByNumber(t *testing.T) {
deployedAddress, txHash := deployHelloWorld(t)
c := GetClient(t)
Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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++ {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit ba35d2f

Please sign in to comment.