Skip to content

Commit

Permalink
rebuild crud, consensus, config (#237)
Browse files Browse the repository at this point in the history
Co-authored-by: sbw <[email protected]>
  • Loading branch information
Subway2023 and subowei authored Oct 10, 2023
1 parent a76d20b commit 3dc0cb2
Show file tree
Hide file tree
Showing 33 changed files with 2,955 additions and 844 deletions.
30 changes: 10 additions & 20 deletions cmd/commandline/kvTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ For more information please refer:
value := args[2]
var valueFields = []string{}
valueFields = append(valueFields, value)
crudService, err := crud.NewCRUDService(RPC)
tableManagerService, err := crud.NewTableManagerService(RPC)
if err != nil {
fmt.Printf("set failed, crud.NewCRUDService err: %v\n", err)
fmt.Printf("set failed, crud.NewTableManagerService err: %v\n", err)
return
}
result, err := crudService.CreateTable(tableName, key, valueFields)
result, err := tableManagerService.CreateTable(tableName, key, valueFields)
if err != nil {
fmt.Printf("set failed, crudService.CreateTable err: %v\n", err)
fmt.Printf("set failed, tableManagerService.CreateTable err: %v\n", err)
return
}
if result != 0 {
Expand Down Expand Up @@ -76,19 +76,14 @@ For more information please refer:
Key: key,
Fields: valueFields,
}
crudService, err := crud.NewCRUDService(RPC)
tableManagerService, err := crud.NewTableManagerService(RPC)
if err != nil {
fmt.Printf("set failed, crud.NewCRUDService err: %v\n", err)
fmt.Printf("set failed, crud.NewTableManagerService err: %v\n", err)
return
}
_, err = crudService.OpenTable(tableName)
result, err := tableManagerService.Insert(tableName, entry)
if err != nil {
fmt.Printf("set failed,crudService.OpenTable err: %v\n", err)
return
}
result, err := crudService.Insert(&entry)
if err != nil {
fmt.Printf("set failed, crudService.Insert err: %v\n", err)
fmt.Printf("set failed, tableManagerService.Insert err: %v\n", err)
return
}
if result != 1 {
Expand Down Expand Up @@ -118,17 +113,12 @@ For more information please refer:
Run: func(cmd *cobra.Command, args []string) {
tableName := args[0]
key := args[1]
crudService, err := crud.NewCRUDService(RPC)
tableManagerService, err := crud.NewTableManagerService(RPC)
if err != nil {
fmt.Printf("get failed, consensus.NewConsensusService err: %v\n", err)
return
}
_, err = crudService.OpenTable(tableName)
if err != nil {
fmt.Printf("get failed,crudService.OpenTable err: %v\n", err)
return
}
result, err := crudService.Select(key)
result, err := tableManagerService.Select0(tableName, key)
if err != nil {
fmt.Printf("get failed, consensusService.AddObserver err: %v\n", err)
return
Expand Down
74 changes: 62 additions & 12 deletions precompiled/config/system_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions precompiled/config/system_config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"github.com/FISCO-BCOS/go-sdk/core/types"
"github.com/FISCO-BCOS/go-sdk/precompiled"
"github.com/ethereum/go-ethereum/common"
"github.com/FISCO-BCOS/go-sdk/abi/bind"
)

// SystemConfigService is a precompile contract service.
type SystemConfigService struct {
systemConfig *Config
client *client.Client
systemConfig *Config
systemConfigAuth *bind.TransactOpts
client *client.Client
}

// systemConfig precompiled contract error code
Expand Down Expand Up @@ -55,12 +57,13 @@ func NewSystemConfigService(client *client.Client) (*SystemConfigService, error)
if err != nil {
return nil, fmt.Errorf("construct SystemConfigService failed: %+v", err)
}
return &SystemConfigService{systemConfig: instance, client: client}, nil
auth := client.GetTransactOpts()
return &SystemConfigService{systemConfig: instance, systemConfigAuth:auth, client: client}, nil
}

// SetValueByKey returns nil if there is no error occurred.
func (s *SystemConfigService) SetValueByKey(key string, value string) (int64, error) {
_, receipt, err := s.systemConfig.SetValueByKey(s.client.GetTransactOpts(), key, value)
_, _, receipt, err := s.systemConfig.SetValueByKey(s.client.GetTransactOpts(), key, value)
if err != nil {
return types.PrecompiledError, fmt.Errorf("client.WaitMined failed, err: %v", err)
}
Expand All @@ -78,3 +81,13 @@ func (s *SystemConfigService) SetValueByKey(key string, value string) (int64, er
}
return errorCode, errorCodeToError(errorCode)
}

func (s *SystemConfigService) GetValueByKey(key string) (string, int64, error) {
opts := &bind.CallOpts{From: s.systemConfigAuth.From}
fmt.Println("opts",opts)
ret0, ret1, err := s.systemConfig.GetValueByKey(opts, key)
if err != nil {
return "", precompiled.DefaultErrorCode, fmt.Errorf("systemConfigService getValueByKey failed: %+v", err)
}
return ret0, ret1.Int64(), nil
}
73 changes: 43 additions & 30 deletions precompiled/config/system_config_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,65 @@ import (
"context"
"encoding/hex"
"testing"
"os"

"github.com/FISCO-BCOS/go-sdk/client"
)

func testSetValueByKey(t *testing.T, key string, value string) {
privateKey, _ := hex.DecodeString("b89d42f12290070f235fb8fb61dcf96e3b11516c5d4f6333f26e49bb955f8b62")
const (
standardOutput = 0
key = "tx_count_limit"
value = "30000000"
)

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("init client failed: %+v", err)
t.Fatalf("Dial to %s:%d failed of %v", config.Host, config.Port, err)
}
service, err := NewSystemConfigService(c)
return c
}

func getService(t *testing.T) {
c := getClient(t)
newService, err := NewSystemConfigService(c)
if err != nil {
t.Fatalf("init SystemConfigService failed: %+v", err)
t.Fatalf("init CnsService failed: %+v", err)
}
service = newService
}

var (
service *SystemConfigService
)

func TestMain(m *testing.M) {
getService(&testing.T{})
exitCode := m.Run()
os.Exit(exitCode)
}

num, err := service.SetValueByKey(key, value)
func TestSetValueByKey(t *testing.T) {
result, err := service.SetValueByKey(key, value)
if err != nil {
t.Fatalf("SystemConfigService SetValueByKey failed: %+v", err)
t.Fatalf("Service RegisterCns failed: %+v\n", err)
}
if num != 0 {
t.Fatalf("testSetValueByKey failed, the result %v is inconsistent with \"1\"", num)
if result != standardOutput {
t.Fatalf("TestRegisterCns failed, the result %v is inconsistent with \"%v\"", result, standardOutput)
}
t.Logf("TestRegisterCns result: %v", result)
}

result, err := c.GetSystemConfigByKey(context.Background(), key)
func TestGetValueByKey(t *testing.T) {
ret0, _, err := service.GetValueByKey(key)
if err != nil {
t.Fatalf("GetSystemConfigByKey failed: %v", err)
t.Fatalf("Service GetValueByKey failed: %+v\n", err)
}
t.Logf("set %s value: %s, GetSystemConfigByKey: %s", key, value, result.GetValue())
//t.Logf("set %s value: %s, GetSystemConfigByKey: %s", key, value, result[1:len(result)-1])
if value != result.GetValue() {
t.Fatalf("SetValueByKey failed!")
if ret0 != value {
t.Fatalf("TestGetValueByKey failed, the ret0 %v is inconsistent with \"%v\"", ret0, value)
}
}

func TestSetValueByKey(t *testing.T) {
// test tx_count_limit
testSetValueByKey(t, "tx_count_limit", "30000000")

// test tx_gas_limit
testSetValueByKey(t, "tx_gas_limit", "3000000000")

// test rpbft_epoch_sealer_num
//testSetValueByKey(t, "rpbft_epoch_sealer_num", "20")

// test rpbft_epoch_block_num
//testSetValueByKey(t, "rpbft_epoch_block_num", "100")
}
t.Logf("TestGetValueByKey ret0: %v", ret0)
}
Loading

0 comments on commit 3dc0cb2

Please sign in to comment.