fix dc4bc_d

This commit is contained in:
programmer10110 2020-10-20 13:38:39 +03:00
parent 834eba6e06
commit 90e82f6eb1
4 changed files with 87 additions and 0 deletions

View File

@ -81,6 +81,7 @@ func (c *BaseClient) StartHTTPServer(listenAddr string) error {
mux.HandleFunc("/proposeSignMessage", c.proposeSignDataHandler)
mux.HandleFunc("/getFSMDump", c.getFSMDumpHandler)
mux.HandleFunc("/getFSMList", c.getFSMList)
c.Logger.Log("Starting HTTP server on address: %s", listenAddr)
return http.ListenAndServe(listenAddr, mux)
@ -99,6 +100,28 @@ func (c *BaseClient) getFSMDumpHandler(w http.ResponseWriter, r *http.Request) {
successResponse(w, dump)
}
func (c *BaseClient) getFSMList(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
return
}
fsmInstances, err := c.state.GetAllFSM()
if err != nil {
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to get all FSM instances: %v", err))
return
}
fsmInstancesStates := make(map[string]string, len(fsmInstances))
for k, v := range fsmInstances {
state, err := v.State()
if err != nil {
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to get FSM state: %v", err))
return
}
fsmInstancesStates[k] = state.String()
}
successResponse(w, fsmInstancesStates)
}
func (c *BaseClient) getUsernameHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")

View File

@ -31,6 +31,7 @@ type State interface {
SaveFSM(dkgRoundID string, dump []byte) error
LoadFSM(dkgRoundID string) (*state_machines.FSMInstance, bool, error)
GetAllFSM() (map[string]*state_machines.FSMInstance, error)
PutOperation(operation *types.Operation) error
DeleteOperation(operationID string) error
@ -146,6 +147,27 @@ func (s *LevelDBState) SaveFSM(dkgRoundID string, dump []byte) error {
return nil
}
func (s *LevelDBState) GetAllFSM() (map[string]*state_machines.FSMInstance, error) {
bz, err := s.stateDb.Get([]byte(fsmStateKey), nil)
if err != nil {
return nil, fmt.Errorf("failed to get FSM instances: %w", err)
}
var fsmInstancesBz = map[string][]byte{}
if len(bz) > 0 {
if err := json.Unmarshal(bz, &fsmInstancesBz); err != nil {
return nil, fmt.Errorf("failed to unmarshal FSM instances: %w", err)
}
}
fsmInstances := make(map[string]*state_machines.FSMInstance, len(fsmInstancesBz))
for k, v := range fsmInstancesBz {
fsmInstances[k], err = state_machines.FromDump(v)
if err != nil {
return nil, fmt.Errorf("failed to restore FSM instance from dump: %w", err)
}
}
return fsmInstances, nil
}
func (s *LevelDBState) LoadFSM(dkgRoundID string) (*state_machines.FSMInstance, bool, error) {
bz, err := s.stateDb.Get([]byte(fsmStateKey), nil)
if err != nil {

View File

@ -59,6 +59,7 @@ func main() {
getSignaturesCommand(),
getSignatureCommand(),
getFSMStatusCommand(),
getFSMListCommand(),
)
if err := rootCmd.Execute(); err != nil {
log.Fatalf("Failed to execute root command: %v", err)
@ -609,3 +610,29 @@ func getFSMStatusCommand() *cobra.Command {
},
}
}
func getFSMListCommand() *cobra.Command {
return &cobra.Command{
Use: "get_fsm_list",
Short: "returns a list of all FSMs served by the client",
RunE: func(cmd *cobra.Command, args []string) error {
listenAddr, err := cmd.Flags().GetString(flagListenAddr)
if err != nil {
return fmt.Errorf("failed to read configuration: %v", err)
}
resp, err := rawGetRequest(fmt.Sprintf("http://%s/getFSMList", listenAddr))
if err != nil {
return fmt.Errorf("failed to make HTTP request to get FSM list: %w", err)
}
if resp.ErrorMessage != "" {
return fmt.Errorf("failed to make HTTP request to get FSM list: %v", resp.ErrorMessage)
}
fsms := resp.Result.(map[string]interface{})
for dkgID, state := range fsms {
fmt.Printf("DKG ID: %s - FSM state: %s\n", dkgID, state.(string))
}
return nil
},
}
}

View File

@ -93,6 +93,21 @@ func (mr *MockStateMockRecorder) LoadFSM(dkgRoundID interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LoadFSM", reflect.TypeOf((*MockState)(nil).LoadFSM), dkgRoundID)
}
// GetAllFSM mocks base method
func (m *MockState) GetAllFSM() (map[string]*state_machines.FSMInstance, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAllFSM")
ret0, _ := ret[0].(map[string]*state_machines.FSMInstance)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetAllFSM indicates an expected call of GetAllFSM
func (mr *MockStateMockRecorder) GetAllFSM() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllFSM", reflect.TypeOf((*MockState)(nil).GetAllFSM))
}
// PutOperation mocks base method
func (m *MockState) PutOperation(operation *types.Operation) error {
m.ctrl.T.Helper()