diff --git a/.ci/check-commit.sh b/.ci/check-commit.sh index 371a58eb..e693a45d 100755 --- a/.ci/check-commit.sh +++ b/.ci/check-commit.sh @@ -8,7 +8,7 @@ SHELL_FOLDER=$( ) # check_script=gofmt -e -s -w -check_script="goimports -w" +check_script="goimports -d" commit_limit=2 file_limit=35 insert_limit=800 @@ -39,17 +39,18 @@ execute_cmd() { } function check_codeFormat() { - # Redirect output to stderr. - exec 1>&2 go get golang.org/x/tools/cmd/goimports sum=0 - for file in $(git diff-index --name-status HEAD^ -- | grep -v D | grep -E '\.go' | awk '{print $2}'); do - execute_cmd "$check_script $file" - sum=$(expr ${sum} + $?) + for file in $(git diff-index --name-status HEAD^ | grep -v D | grep -E '\.go' | awk '{print $2}'); do + checkResult=$(eval "${check_script} ${file}") + if [ -n "${checkResult}" ]; then + LOG_ERROR "file ${file} does not meet the format requirements" + echo "${checkResult}" + sum=1 + fi done - if [ ${sum} -ne 0 ]; then - echo "######### ERROR: Format check failed, please adjust them before commit" + LOG_ERROR "######### ERROR: Format check failed, please adjust them before commit" exit 1 fi } @@ -100,6 +101,5 @@ function check_PR_limit() { LOG_INFO "modify ${files} files, insert ${insertions} lines, valid insertion ${valid_insertions}, delete ${deletions} lines. Total ${commits} commits." } -go get golang.org/x/tools/cmd/goimports check_codeFormat check_PR_limit diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 7c8dbb16..8ec1237c 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -25,7 +25,7 @@ jobs: steps: - uses: actions/checkout@v2 with: - fetch-depth: 1 + fetch-depth: 5 # - uses: actions/cache@v1 # id: cache # with: diff --git a/client/goclient_test.go b/client/goclient_test.go index d8161e8c..a7358780 100644 --- a/client/goclient_test.go +++ b/client/goclient_test.go @@ -3,16 +3,16 @@ package client import ( "context" "fmt" - helloworld "github.com/FISCO-BCOS/go-sdk/.ci/hello" "strings" "testing" + helloworld "github.com/FISCO-BCOS/go-sdk/.ci/hello" "github.com/FISCO-BCOS/go-sdk/conf" ) var ( contractAddress = "" - blockHash = "" // get blockHash by TestBlockHashByNumber test case + blockHash = "" // get blockHash by TestBlockHashByNumber test case transactionHash = "" ) @@ -54,7 +54,7 @@ func TestBlockHashByNumber(t *testing.T) { } t.Logf("block hash by number:\n%s", raw) - blockHash = strings.Trim(string(raw),"\"") + blockHash = strings.Trim(string(raw), "\"") } func TestClientVersion(t *testing.T) { @@ -91,7 +91,7 @@ func TestPBFTView(t *testing.T) { } func TestBlockLimit(t *testing.T) { - c := GetClient(t) + c := GetClient(t) // cannot use big.NewInt to construct json request // TODO: analysis the ethereum's big.NewInt bl, err := c.GetBlockLimit(context.Background()) @@ -103,7 +103,7 @@ func TestBlockLimit(t *testing.T) { } func TestGroupID(t *testing.T) { - c := GetClient(t) + c := GetClient(t) // cannot use big.NewInt to construct json request // TODO: analysis the ethereum's big.NewInt groupid := c.GetGroupID() @@ -111,7 +111,7 @@ func TestGroupID(t *testing.T) { } func TestChainID(t *testing.T) { - c := GetClient(t) + c := GetClient(t) // cannot use big.NewInt to construct json request // TODO: analysis the ethereum's big.NewInt chainid, err := c.GetChainID(context.Background()) diff --git a/precompiled/cns/cns_service_test.go b/precompiled/cns/cns_service_test.go index 6b70bde8..fed0822d 100644 --- a/precompiled/cns/cns_service_test.go +++ b/precompiled/cns/cns_service_test.go @@ -3,9 +3,10 @@ package cns import ( "context" "crypto/ecdsa" - "github.com/FISCO-BCOS/go-sdk/abi/bind" "testing" + "github.com/FISCO-BCOS/go-sdk/abi/bind" + "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" "github.com/ethereum/go-ethereum/crypto" @@ -42,7 +43,7 @@ func GetService(t *testing.T) *Service { } const ( - name = "store" + name = "store" version = "5.0" address = "0x0626918C51A1F36c7ad4354BB1197460A533a2B9" testABI = `[ @@ -128,7 +129,7 @@ const ( ]` ) -func TestRegisterCns(t *testing.T) { +func TestRegisterCns(t *testing.T) { c := GetClient(t) service := GetService(t) // test RegisterCns @@ -144,7 +145,7 @@ func TestRegisterCns(t *testing.T) { t.Logf("transaction hash: %s\n", receipt.GetTransactionHash()) } -func TestGetAddressByContractNameAndVersion(t *testing.T) { +func TestGetAddressByContractNameAndVersion(t *testing.T) { service := GetService(t) // test GetAddressByContractNameAndVersion @@ -155,7 +156,7 @@ func TestGetAddressByContractNameAndVersion(t *testing.T) { t.Logf("address: %s", addr) } -func TestQueryCnsByNameAndVersion(t *testing.T) { +func TestQueryCnsByNameAndVersion(t *testing.T) { service := GetService(t) // test QueryCnsByNameAndVersion diff --git a/precompiled/config/system_config_service_test.go b/precompiled/config/system_config_service_test.go index 1e5e19e3..668077e5 100644 --- a/precompiled/config/system_config_service_test.go +++ b/precompiled/config/system_config_service_test.go @@ -2,10 +2,11 @@ package config import ( "context" + "testing" + "github.com/FISCO-BCOS/go-sdk/abi/bind" "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" - "testing" ) func TestSetValueByKey(t *testing.T) { diff --git a/precompiled/consensus/consensus_service.go b/precompiled/consensus/consensus_service.go index f017eec3..2ed16b77 100644 --- a/precompiled/consensus/consensus_service.go +++ b/precompiled/consensus/consensus_service.go @@ -1,23 +1,23 @@ package consensus import ( - "fmt" - "crypto/ecdsa" - "math/big" "context" + "crypto/ecdsa" "encoding/json" + "fmt" + "math/big" - "github.com/FISCO-BCOS/go-sdk/client" - "github.com/ethereum/go-ethereum/common" "github.com/FISCO-BCOS/go-sdk/abi/bind" + "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/core/types" + "github.com/ethereum/go-ethereum/common" ) // ConsensusService is a precompile contract service. type ConsensusService struct { - consensus *Consensus + consensus *Consensus consensusAuth *bind.TransactOpts - client *client.Client + client *client.Client } // contract address @@ -31,7 +31,7 @@ func NewConsensusService(client *client.Client, privateKey *ecdsa.PrivateKey) (* } auth := bind.NewKeyedTransactor(privateKey) auth.GasLimit = big.NewInt(30000000) - return &ConsensusService{consensus:instance, consensusAuth:auth, client: client}, nil + return &ConsensusService{consensus: instance, consensusAuth: auth, client: client}, nil } // AddObserver add a new observe node according to the node ID @@ -45,7 +45,7 @@ func (service *ConsensusService) AddObserver(nodeID string) (*types.Transaction, observerRaw, err := service.client.GetObserverList(context.Background()) if err != nil { - return nil, fmt.Errorf("get the observer list failed: %v", err) + return nil, fmt.Errorf("get the observer list failed: %v", err) } var nodeIDs []string @@ -60,8 +60,8 @@ func (service *ConsensusService) AddObserver(nodeID string) (*types.Transaction, } } tx, err := service.consensus.AddObserver(service.consensusAuth, nodeID) - if err != nil { - return nil, fmt.Errorf("ConsensusService addObserver failed: %+v", err) + if err != nil { + return nil, fmt.Errorf("ConsensusService addObserver failed: %+v", err) } return tx, nil } @@ -77,7 +77,7 @@ func (service *ConsensusService) AddSealer(nodeID string) (*types.Transaction, e sealerRaw, err := service.client.GetSealerList(context.Background()) if err != nil { - return nil, fmt.Errorf("get the sealer list failed: %v", err) + return nil, fmt.Errorf("get the sealer list failed: %v", err) } var nodeIDs []string @@ -93,8 +93,8 @@ func (service *ConsensusService) AddSealer(nodeID string) (*types.Transaction, e } tx, err := service.consensus.AddSealer(service.consensusAuth, nodeID) - if err != nil { - return nil, fmt.Errorf("ConsensusService addSealer failed: %+v", err) + if err != nil { + return nil, fmt.Errorf("ConsensusService addSealer failed: %+v", err) } return tx, nil @@ -104,7 +104,7 @@ func (service *ConsensusService) AddSealer(nodeID string) (*types.Transaction, e func (service *ConsensusService) RemoveNode(nodeID string) (*types.Transaction, error) { peersRaw, err := service.client.GetGroupPeers(context.Background()) if err != nil { - return nil, fmt.Errorf("get the group peers failed: %v", err) + return nil, fmt.Errorf("get the group peers failed: %v", err) } var nodeIDs []string @@ -125,10 +125,10 @@ func (service *ConsensusService) RemoveNode(nodeID string) (*types.Transaction, } tx, err := service.consensus.Remove(service.consensusAuth, nodeID) - // maybe will occur something wrong + // maybe will occur something wrong // when request the receipt from the SDK since the connected node of SDK is removed - if err != nil { - return nil, fmt.Errorf("ConsensusService Remove failed: %+v", err) + if err != nil { + return nil, fmt.Errorf("ConsensusService Remove failed: %+v", err) } return tx, nil } @@ -138,7 +138,7 @@ func (service *ConsensusService) isValidNodeID(nodeID string) (bool, error) { var flag = false nodeIDRaw, err := service.client.GetNodeIDList(context.Background()) if err != nil { - return flag, fmt.Errorf("get the valid Node IDs failed: %v", err) + return flag, fmt.Errorf("get the valid Node IDs failed: %v", err) } var nodeIDs []string err = json.Unmarshal(nodeIDRaw, &nodeIDs) @@ -151,4 +151,4 @@ func (service *ConsensusService) isValidNodeID(nodeID string) (bool, error) { } } return flag, nil -} \ No newline at end of file +} diff --git a/precompiled/consensus/consensus_service_test.go b/precompiled/consensus/consensus_service_test.go index c8c4c0fc..ad4be0a2 100644 --- a/precompiled/consensus/consensus_service_test.go +++ b/precompiled/consensus/consensus_service_test.go @@ -3,10 +3,11 @@ package consensus import ( "context" "crypto/ecdsa" - "github.com/FISCO-BCOS/go-sdk/abi/bind" "regexp" "testing" + "github.com/FISCO-BCOS/go-sdk/abi/bind" + "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" "github.com/ethereum/go-ethereum/crypto" @@ -56,7 +57,7 @@ func TestGetNodeID(t *testing.T) { } reg := regexp.MustCompile(`[\w]+`) nodeList := reg.FindAllString(string(sealerList), -1) - if len(nodeList) < 4 { // pbft consensus needs 2f+1 + if len(nodeList) < 4 { // pbft consensus needs 2f+1 t.Fatalf("the number of nodes does not exceed 4") } nodeID = nodeList[1] diff --git a/precompiled/crud/condition.go b/precompiled/crud/condition.go index b240bb2f..9c35bb5c 100644 --- a/precompiled/crud/condition.go +++ b/precompiled/crud/condition.go @@ -64,4 +64,4 @@ func (c *Condition) GetConditions() map[string]map[EnumOP]string { func (c *Condition) SetConditions(newMap map[string]map[EnumOP]string) { c.conditions = newMap -} \ No newline at end of file +} diff --git a/precompiled/crud/entry.go b/precompiled/crud/entry.go index eda6d294..4467a73a 100644 --- a/precompiled/crud/entry.go +++ b/precompiled/crud/entry.go @@ -4,7 +4,6 @@ type Entry struct { fields map[string]string } - func (e *Entry) GetFields() map[string]string { return e.fields } @@ -19,4 +18,4 @@ func (e *Entry) Put(key string, value string) { func (e *Entry) Get(key string) string { return e.fields[key] -} \ No newline at end of file +} diff --git a/precompiled/crud/enumop.go b/precompiled/crud/enumop.go index bf96b9c2..a5662193 100644 --- a/precompiled/crud/enumop.go +++ b/precompiled/crud/enumop.go @@ -3,11 +3,11 @@ package crud type EnumOP string const ( - EQ EnumOP = "eq" - NE EnumOP = "ne" - GT EnumOP = "gt" - GE EnumOP = "ge" - LT EnumOP = "lt" - LE EnumOP = "le" + EQ EnumOP = "eq" + NE EnumOP = "ne" + GT EnumOP = "gt" + GE EnumOP = "ge" + LT EnumOP = "lt" + LE EnumOP = "le" Limit EnumOP = "limit" -) \ No newline at end of file +) diff --git a/precompiled/crud/table.go b/precompiled/crud/table.go index f512f525..53210ac8 100644 --- a/precompiled/crud/table.go +++ b/precompiled/crud/table.go @@ -1,10 +1,10 @@ package crud type Table struct { - TableName string - Key string - ValueFields string - Optional string + TableName string + Key string + ValueFields string + Optional string } func (t *Table) GetTableName() string { @@ -40,9 +40,9 @@ func (t *Table) SetOptional(optional string) { } func (t *Table) GetEntry() *Entry { - return &Entry{fields:make(map[string]string)} + return &Entry{fields: make(map[string]string)} } func (t *Table) GetCondition() *Condition { - return &Condition{conditions:make(map[string]map[EnumOP]string)} -} \ No newline at end of file + return &Condition{conditions: make(map[string]map[EnumOP]string)} +} diff --git a/precompiled/permission/permission_info.go b/precompiled/permission/permission_info.go index cebcd388..783ae5b9 100644 --- a/precompiled/permission/permission_info.go +++ b/precompiled/permission/permission_info.go @@ -1,9 +1,9 @@ package permission type PermissionInfo struct { - Address string `json:"address"` - EnableNum string `json:"enable_num"` - TableName string `json:"table_name"` + Address string `json:"address"` + EnableNum string `json:"enable_num"` + TableName string `json:"table_name"` } func (p *PermissionInfo) GetTableName() string { diff --git a/precompiled/permission/permission_service.go b/precompiled/permission/permission_service.go index d3d98138..d1568bbc 100644 --- a/precompiled/permission/permission_service.go +++ b/precompiled/permission/permission_service.go @@ -9,9 +9,9 @@ import ( "github.com/FISCO-BCOS/go-sdk/abi/bind" "github.com/FISCO-BCOS/go-sdk/client" - "github.com/ethereum/go-ethereum/common" "github.com/FISCO-BCOS/go-sdk/core/types" "github.com/FISCO-BCOS/go-sdk/precompiled/crud" + "github.com/ethereum/go-ethereum/common" ) const (