From 19868481b60e6a60fbeef476541b9713586f2264 Mon Sep 17 00:00:00 2001 From: bxq2011hust Date: Thu, 25 Jan 2024 10:13:36 +0800 Subject: [PATCH] update bcos-c-sdk --- v3/abi/bind/util.go | 76 ---- v3/client/go_client.go | 20 +- v3/go.mod | 6 +- v3/go.sum | 4 +- v3/ios/contract_proxy.go | 9 +- .../config/system_config_service.go | 2 +- .../sharding/sharding_service_test.go | 325 +++++++++--------- 7 files changed, 175 insertions(+), 267 deletions(-) delete mode 100644 v3/abi/bind/util.go diff --git a/v3/abi/bind/util.go b/v3/abi/bind/util.go deleted file mode 100644 index c4c90100..00000000 --- a/v3/abi/bind/util.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2016 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package bind - -import ( - "context" - "fmt" - "time" - - "github.com/FISCO-BCOS/go-sdk/v3/types" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/log" -) - -// WaitMined waits for tx to be mined on the blockchain. -// It stops waiting when the context is canceled. -func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) { - queryTicker := time.NewTicker(time.Second) - defer queryTicker.Stop() - - logger := log.New("hash", tx.Hash()) - for { - receipt, err := b.TransactionReceipt(ctx, tx.Hash()) - if receipt != nil { - return receipt, nil - } - if err != nil { - logger.Trace("Receipt retrieval failed", "err", err) - } else { - logger.Trace("Transaction not yet mined") - } - // Wait for the next round. - select { - case <-ctx.Done(): - return nil, ctx.Err() - case <-queryTicker.C: - } - } -} - -// WaitDeployed waits for a contract deployment transaction and returns the on-chain -// contract address when it is mined. It stops waiting when ctx is canceled. -func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) { - if tx.To() != nil { - return common.Address{}, fmt.Errorf("tx is not contract creation") - } - receipt, err := WaitMined(ctx, b, tx) - if err != nil { - return common.Address{}, err - } - if receipt.ContractAddress == "" { - return common.Address{}, fmt.Errorf("zero address") - } - // Check that code has indeed been deployed at the address. - // This matters on pre-Homestead chains: OOG in the constructor - // could leave an empty account behind. - code, err := b.CodeAt(ctx, common.HexToAddress(receipt.ContractAddress)) - if err == nil && len(code) == 0 { - err = ErrNoCodeAfterDeploy - } - return common.HexToAddress(receipt.ContractAddress), err -} diff --git a/v3/client/go_client.go b/v3/client/go_client.go index c8eacb45..8595dc22 100644 --- a/v3/client/go_client.go +++ b/v3/client/go_client.go @@ -16,6 +16,7 @@ package client import ( "context" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -27,7 +28,6 @@ import ( "github.com/FISCO-BCOS/go-sdk/v3/types" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" ) @@ -106,17 +106,17 @@ func toCallArg(msg ethereum.CallMsg) interface{} { "to": strings.ToLower(msg.To.String()[2:]), } if len(msg.Data) > 0 { - arg["data"] = hexutil.Bytes(msg.Data).String() + arg["data"] = hex.EncodeToString(msg.Data) } if msg.Value != nil { - arg["value"] = (*hexutil.Big)(msg.Value).String() + arg["value"] = fmt.Sprintf("%#x", msg.Value) } if msg.Gas != 0 { - arg["gas"] = hexutil.Uint64(msg.Gas) + arg["gas"] = fmt.Sprintf("%#x", msg.Gas) } if msg.GasPrice != nil { - arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice) + arg["gasPrice"] = fmt.Sprintf("%#x", msg.GasPrice) } return arg @@ -148,11 +148,6 @@ func (c *Client) GetCallOpts() *bind.CallOpts { return c.callOpts } -// WaitMined is wrapper of bind.WaitMined -func (c *Client) WaitMined(tx *types.Transaction) (*types.Receipt, error) { - return bind.WaitMined(context.Background(), c, tx) -} - // SMCrypto returns true if use sm crypto func (c *Client) SMCrypto() bool { return c.conn.GetCSDK().SMCrypto() @@ -199,7 +194,7 @@ func (c *Client) PendingCodeAt(ctx context.Context, address common.Address) ([]b // CallContract invoke the call method of rpc api func (c *Client) CallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) { - var hexBytes hexutil.Bytes + var hexBytes []byte var cr *callResult err := c.conn.CallContext(ctx, &cr, "call", toCallArg(msg)) if err != nil { @@ -386,7 +381,6 @@ func (c *Client) GetBlockNumber(ctx context.Context) (int64, error) { } // GetPBFTView returns the latest PBFT view(hex format) of the specific group and it will returns a wrong sentence -// if the consensus algorithm is not the PBFT. func (c *Client) GetPBFTView(ctx context.Context) ([]byte, error) { var raw interface{} err := c.conn.CallContext(ctx, &raw, "getPbftView") @@ -395,8 +389,6 @@ func (c *Client) GetPBFTView(ctx context.Context) ([]byte, error) { } js, err := json.MarshalIndent(raw, "", indent) return js, err - // TODO - // Raft consensus } type ConsensusNodeInfo struct { diff --git a/v3/go.mod b/v3/go.mod index d248f5f5..fe0fbfae 100644 --- a/v3/go.mod +++ b/v3/go.mod @@ -2,14 +2,15 @@ module github.com/FISCO-BCOS/go-sdk/v3 go 1.21.5 -//replace github.com/FISCO-BCOS/bcos-c-sdk => ../../bcos-c-sdk +// replace github.com/FISCO-BCOS/bcos-c-sdk => ../../bcos-c-sdk require ( - github.com/FISCO-BCOS/bcos-c-sdk v0.0.0-20240122125257-ba87131d4c17 + github.com/FISCO-BCOS/bcos-c-sdk v0.0.0-20240125020548-dbdab210fe92 github.com/FISCO-BCOS/crypto v0.0.0-20200202032121-bd8ab0b5d4f1 github.com/TarsCloud/TarsGo v1.4.5 github.com/deckarep/golang-set/v2 v2.6.0 github.com/ethereum/go-ethereum v1.13.10 + github.com/patrickmn/go-cache v2.1.0+incompatible github.com/schollz/progressbar/v3 v3.14.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.5.0 @@ -80,7 +81,6 @@ require ( github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/opentracing/opentracing-go v1.1.0 // indirect - github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.12.0 // indirect diff --git a/v3/go.sum b/v3/go.sum index 4d9df715..0e7768ba 100644 --- a/v3/go.sum +++ b/v3/go.sum @@ -40,8 +40,8 @@ github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EF github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w= github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ= github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= -github.com/FISCO-BCOS/bcos-c-sdk v0.0.0-20240122125257-ba87131d4c17 h1:Sl9134y03PD98vO3fSMqABhUT6JFQJZiEOXHvLRuejE= -github.com/FISCO-BCOS/bcos-c-sdk v0.0.0-20240122125257-ba87131d4c17/go.mod h1:n2KxbYa73MW3xdLVu2vpPpoblZMms+CwPmvFkubO9xM= +github.com/FISCO-BCOS/bcos-c-sdk v0.0.0-20240125020548-dbdab210fe92 h1:w9THq2E9Al5IEbpxXgRFkZLK7jbWN5QRPFpfO6MIdNc= +github.com/FISCO-BCOS/bcos-c-sdk v0.0.0-20240125020548-dbdab210fe92/go.mod h1:n2KxbYa73MW3xdLVu2vpPpoblZMms+CwPmvFkubO9xM= github.com/FISCO-BCOS/crypto v0.0.0-20200202032121-bd8ab0b5d4f1 h1:ThPht4qK10+cMZC5COIjHPq0INm5HAMVYqrez5zEgFI= github.com/FISCO-BCOS/crypto v0.0.0-20200202032121-bd8ab0b5d4f1/go.mod h1:UrLdwsFrjiaCsvdcPLcH6B7s/FUmym3qfM93u2ziR+4= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= diff --git a/v3/ios/contract_proxy.go b/v3/ios/contract_proxy.go index 061c125e..3ca58b74 100644 --- a/v3/ios/contract_proxy.go +++ b/v3/ios/contract_proxy.go @@ -15,7 +15,6 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/rlp" ) const ( @@ -50,14 +49,10 @@ func (c *ContractProxy) PendingCodeAt(ctx context.Context, account common.Addres // SendTransaction injects the transaction into the pending pool for execution. // todo ios 怎么处理? func (c *ContractProxy) SendTransaction(ctx context.Context, tx *types.Transaction) (*types.Receipt, error) { - data, err := rlp.EncodeToBytes(tx) - if err != nil { - fmt.Printf("rlp encode tx error!") - return nil, err - } + data := tx.Bytes() msg, err := c.newMessage("sendTransaction", c.groupID, hexutil.Encode(data)) if err != nil { - fmt.Printf("rlp encode tx error!") + fmt.Printf("encode tx error!") return nil, err } respString := c.callback.SendRequest(msg.String()) diff --git a/v3/precompiled/config/system_config_service.go b/v3/precompiled/config/system_config_service.go index 5e90f92d..87271332 100644 --- a/v3/precompiled/config/system_config_service.go +++ b/v3/precompiled/config/system_config_service.go @@ -65,7 +65,7 @@ func NewSystemConfigService(client *client.Client) (*SystemConfigService, error) func (s *SystemConfigService) SetValueByKey(key string, value string) (int64, error) { _, _, receipt, err := s.systemConfig.SetValueByKey(s.client.GetTransactOpts(), key, value) if err != nil { - return types.PrecompiledError, fmt.Errorf("client.WaitMined failed, err: %v", err) + return types.PrecompiledError, fmt.Errorf("SetValueByKey failed, err: %v", err) } errorMessage := receipt.GetErrorMessage() if errorMessage != "" { diff --git a/v3/precompiled/sharding/sharding_service_test.go b/v3/precompiled/sharding/sharding_service_test.go index 898fa229..55fe518d 100644 --- a/v3/precompiled/sharding/sharding_service_test.go +++ b/v3/precompiled/sharding/sharding_service_test.go @@ -1,164 +1,161 @@ -package sharding - -import ( - "context" - "encoding/hex" - "os" - "testing" - - // "fmt" - "math/big" - "time" - - "github.com/FISCO-BCOS/go-sdk/v3/client" - "github.com/FISCO-BCOS/go-sdk/v3/precompiled" - "github.com/FISCO-BCOS/go-sdk/v3/types" - // "github.com/ethereum/go-ethereum/common" -) - -const ( - standardOutput = 0 - timeout = 1 * time.Second - name = "hello_v11" - version = "11.0" - address = "0xc92ad282ba7868b032341a3921b3635b0c45de74" - addressAsync = "0x272d69cfdb321d147f63fdfa9126c4ad1969265a" - shardName = "shardName" - shardNameAsync = "shardName_async" -) - -func getClient(t *testing.T) *client.Client { - privateKey, _ := hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58") - config := &client.Config{IsSMCrypto: false, GroupID: "group0", - PrivateKey: privateKey, Host: "127.0.0.1", Port: 20200, TLSCaFile: "./ca.crt", TLSKeyFile: "./sdk.key", TLSCertFile: "./sdk.crt"} - c, err := client.DialContext(context.Background(), config) - if err != nil { - t.Fatalf("Dial to %s:%d failed of %v", config.Host, config.Port, err) - } - return c -} - -func getService(t *testing.T) { - c := getClient(t) - newService, err := NewShardingService(c) - if err != nil { - t.Fatalf("init CnsService failed: %+v", err) - } - service = newService -} - -var ( - service *Service - channel = make(chan int) -) - -func TestMain(m *testing.M) { - getService(&testing.T{}) - exitCode := m.Run() - os.Exit(exitCode) -} - -func TestMakeShard(t *testing.T) { - ret0, _, _, err := service.MakeShard(shardName) - if err != nil { - t.Fatalf("Service MakeShard failed: %+v\n", err) - } - - if ret0 != standardOutput { - t.Fatalf("TestMakeShard failed, the ret0 %v is inconsistent with \"%v\"", ret0, standardOutput) - } - - t.Logf("TestMakeShard ret0:%v", ret0) -} - -func TestAsyncMakeShard(t *testing.T) { - handler := func(receipt *types.Receipt, err error) { - if err != nil { - t.Fatalf("receive receipt failed, %v\n", err) - } - var bigNum *big.Int - bigNum, err = precompiled.ParseBigIntFromOutput(receipt) - if err != nil { - t.Fatalf("parseReturnValue failed, err: %v\n", err) - } - result, err := precompiled.BigIntToInt64(bigNum) - if err != nil { - t.Fatalf("%v\n", err) - } - if result != 0 { - t.Fatalf("TestAsyncMakeShard failed, the result \"%v\" is inconsistent with \"0\"", result) - } - t.Logf("result: %d\n", result) - channel <- 0 - } - - _, err := service.AsyncMakeShard(handler, shardNameAsync) - if err != nil { - t.Fatalf("remove data failed: %v", err) - } - select { - case <-channel: - return - case <-time.After(timeout): - t.Fatal("timeout") - } -} - -func TestLinkShard(t *testing.T) { - ret0, _, _, err := service.LinkShard(shardName, address) - if err != nil { - t.Fatalf("Service LinkShard failed: %+v\n", err) - } - - if ret0 != standardOutput { - t.Fatalf("TestLinkShard failed, the ret0 %v is inconsistent with \"%v\"", ret0, standardOutput) - } - - t.Logf("TestMakeShard ret0:%v", ret0) -} - -func TestAsyncLinkShard(t *testing.T) { - handler := func(receipt *types.Receipt, err error) { - if err != nil { - t.Fatalf("receive receipt failed, %v\n", err) - } - var bigNum *big.Int - bigNum, err = precompiled.ParseBigIntFromOutput(receipt) - if err != nil { - t.Fatalf("parseReturnValue failed, err: %v\n", err) - } - result, err := precompiled.BigIntToInt64(bigNum) - if err != nil { - t.Fatalf("%v\n", err) - } - if result != 0 { - t.Fatalf("TestAsyncLinkShard failed, the result \"%v\" is inconsistent with \"0\"", result) - } - t.Logf("result: %d\n", result) - channel <- 0 - } - - _, err := service.AsyncLinkShard(handler, shardNameAsync, addressAsync) - if err != nil { - t.Fatalf("remove data failed: %v", err) - } - select { - case <-channel: - return - case <-time.After(timeout): - t.Fatal("timeout") - } -} - -func TestGetContractShard(t *testing.T) { - ret0, ret1, err := service.GetContractShard(address) - if err != nil { - t.Fatalf("Service GetContractShard failed: %+v\n", err) - } - - if ret0 != standardOutput { - t.Fatalf("TestGetContractShard failed, the ret0 %v is inconsistent with \"%v\"", ret0, standardOutput) - } - - t.Logf("TestGetContractShard ret0:%v, ret0:%v", ret0, ret1) -} +package sharding + +import ( + "context" + "encoding/hex" + "math/big" + "os" + "testing" + "time" + + "github.com/FISCO-BCOS/go-sdk/v3/client" + "github.com/FISCO-BCOS/go-sdk/v3/precompiled" + "github.com/FISCO-BCOS/go-sdk/v3/types" +) + +const ( + standardOutput = 0 + timeout = 1 * time.Second + name = "hello_v11" + version = "11.0" + address = "0xc92ad282ba7868b032341a3921b3635b0c45de74" + addressAsync = "0x272d69cfdb321d147f63fdfa9126c4ad1969265a" + shardName = "shardName" + shardNameAsync = "shardName_async" +) + +func getClient(t *testing.T) *client.Client { + privateKey, _ := hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58") + config := &client.Config{IsSMCrypto: false, GroupID: "group0", + PrivateKey: privateKey, Host: "127.0.0.1", Port: 20200, TLSCaFile: "./ca.crt", TLSKeyFile: "./sdk.key", TLSCertFile: "./sdk.crt"} + c, err := client.DialContext(context.Background(), config) + if err != nil { + t.Fatalf("Dial to %s:%d failed of %v", config.Host, config.Port, err) + } + return c +} + +func getService(t *testing.T) { + c := getClient(t) + newService, err := NewShardingService(c) + if err != nil { + t.Fatalf("init CnsService failed: %+v", err) + } + service = newService +} + +var ( + service *Service + channel = make(chan int) +) + +func TestMain(m *testing.M) { + getService(&testing.T{}) + exitCode := m.Run() + os.Exit(exitCode) +} + +func TestMakeShard(t *testing.T) { + ret0, _, _, err := service.MakeShard(shardName) + if err != nil { + t.Fatalf("Service MakeShard failed: %+v\n", err) + } + + if ret0 != standardOutput { + t.Fatalf("TestMakeShard failed, the ret0 %v is inconsistent with \"%v\"", ret0, standardOutput) + } + + t.Logf("TestMakeShard ret0:%v", ret0) +} + +func TestAsyncMakeShard(t *testing.T) { + handler := func(receipt *types.Receipt, err error) { + if err != nil { + t.Fatalf("receive receipt failed, %v\n", err) + } + var bigNum *big.Int + bigNum, err = precompiled.ParseBigIntFromOutput(receipt) + if err != nil { + t.Fatalf("parseReturnValue failed, err: %v\n", err) + } + result, err := precompiled.BigIntToInt64(bigNum) + if err != nil { + t.Fatalf("%v\n", err) + } + if result != 0 { + t.Fatalf("TestAsyncMakeShard failed, the result \"%v\" is inconsistent with \"0\"", result) + } + t.Logf("result: %d\n", result) + channel <- 0 + } + + _, err := service.AsyncMakeShard(handler, shardNameAsync) + if err != nil { + t.Fatalf("remove data failed: %v", err) + } + select { + case <-channel: + return + case <-time.After(timeout): + t.Fatal("timeout") + } +} + +func TestLinkShard(t *testing.T) { + ret0, _, _, err := service.LinkShard(shardName, address) + if err != nil { + t.Fatalf("Service LinkShard failed: %+v\n", err) + } + + if ret0 != standardOutput { + t.Fatalf("TestLinkShard failed, the ret0 %v is inconsistent with \"%v\"", ret0, standardOutput) + } + + t.Logf("TestMakeShard ret0:%v", ret0) +} + +func TestAsyncLinkShard(t *testing.T) { + handler := func(receipt *types.Receipt, err error) { + if err != nil { + t.Fatalf("receive receipt failed, %v\n", err) + } + var bigNum *big.Int + bigNum, err = precompiled.ParseBigIntFromOutput(receipt) + if err != nil { + t.Fatalf("parseReturnValue failed, err: %v\n", err) + } + result, err := precompiled.BigIntToInt64(bigNum) + if err != nil { + t.Fatalf("%v\n", err) + } + if result != 0 { + t.Fatalf("TestAsyncLinkShard failed, the result \"%v\" is inconsistent with \"0\"", result) + } + t.Logf("result: %d\n", result) + channel <- 0 + } + + _, err := service.AsyncLinkShard(handler, shardNameAsync, addressAsync) + if err != nil { + t.Fatalf("remove data failed: %v", err) + } + select { + case <-channel: + return + case <-time.After(timeout): + t.Fatal("timeout") + } +} + +func TestGetContractShard(t *testing.T) { + ret0, ret1, err := service.GetContractShard(address) + if err != nil { + t.Fatalf("Service GetContractShard failed: %+v\n", err) + } + + if ret0 != standardOutput { + t.Fatalf("TestGetContractShard failed, the ret0 %v is inconsistent with \"%v\"", ret0, standardOutput) + } + + t.Logf("TestGetContractShard ret0:%v, ret0:%v", ret0, ret1) +}