Skip to content

Commit

Permalink
feat: add wait for masternode-status "ready" (#659)
Browse files Browse the repository at this point in the history
* feat: add wait for masternode-status ready

* feat: use "READY" to the state field in masternode-status mocked response for e2e test

* refactor: some modifications according to PR feedback

* Update dash/core/client.go
  • Loading branch information
shotonoff authored Jul 21, 2023
1 parent ce8c936 commit e11684a
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 1 deletion.
28 changes: 28 additions & 0 deletions dash/core/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//go:generate ../../scripts/mockery_generate.sh Client

package core

import (
"fmt"
"time"

"github.com/dashpay/dashd-go/btcjson"
rpc "github.com/dashpay/dashd-go/rpcclient"
Expand All @@ -13,6 +16,8 @@ import (

const ModuleName = "rpcclient"

var sleep = time.Sleep

// QuorumVerifier represents subset of priv validator features that
// allows verification of threshold signatures.
type QuorumVerifier interface {
Expand Down Expand Up @@ -220,3 +225,26 @@ func (rpcClient *RPCClient) QuorumVerify(
return resp, err

}

// WaitForMNReady waits until the masternode is ready
func WaitForMNReady(client Client, retryTimeout time.Duration) error {
for {
result, err := client.MasternodeStatus()
if err != nil {
return fmt.Errorf("failed to get masternode status: %w", err)
}
switch result.State {
case btcjson.MNStatusStateReady:
return nil
case btcjson.MNStatusStateWaitingForProtx:
sleep(retryTimeout)
case btcjson.MNStatusStatePoseBanned,
btcjson.MNStatusStateRemoved,
btcjson.MNStatusStateOperatorKeyChanged,
btcjson.MNStatusStateProtxIpChanged,
btcjson.MNStatusStateError,
btcjson.MNStatusStateUnknown:
return fmt.Errorf("unexpected masternode state %s", result.State)
}
}
}
94 changes: 94 additions & 0 deletions dash/core/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package core

import (
"errors"
"fmt"
"testing"
"time"

"github.com/dashpay/dashd-go/btcjson"
"github.com/stretchr/testify/require"

"github.com/tendermint/tendermint/dash/core/mocks"
)

func TestWaitForMNReady(t *testing.T) {
retryTimeout := 1 * time.Millisecond
sleep = func(d time.Duration) {
require.Equal(t, d, retryTimeout)
}
defer func() { sleep = time.Sleep }()
testCases := []struct {
states []btcjson.MNStatusState
wantErr string
}{
{
states: []btcjson.MNStatusState{btcjson.MNStatusStateReady},
},
{
states: []btcjson.MNStatusState{
btcjson.MNStatusStateWaitingForProtx,
btcjson.MNStatusStateReady,
},
},
{
states: []btcjson.MNStatusState{
btcjson.MNStatusStateWaitingForProtx,
btcjson.MNStatusStateWaitingForProtx,
btcjson.MNStatusStateWaitingForProtx,
btcjson.MNStatusStateReady,
},
},
{
states: []btcjson.MNStatusState{btcjson.MNStatusStatePoseBanned},
wantErr: string(btcjson.MNStatusStatePoseBanned),
},
{
states: []btcjson.MNStatusState{btcjson.MNStatusStateRemoved},
wantErr: string(btcjson.MNStatusStateRemoved),
},
{
states: []btcjson.MNStatusState{btcjson.MNStatusStateOperatorKeyChanged},
wantErr: string(btcjson.MNStatusStateOperatorKeyChanged),
},
{
states: []btcjson.MNStatusState{btcjson.MNStatusStateProtxIpChanged},
wantErr: string(btcjson.MNStatusStateProtxIpChanged),
},
{
states: []btcjson.MNStatusState{btcjson.MNStatusStateError},
wantErr: string(btcjson.MNStatusStateError),
},
{
states: []btcjson.MNStatusState{btcjson.MNStatusStateUnknown},
wantErr: string(btcjson.MNStatusStateUnknown),
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
client := mocks.NewClient(t)
for _, state := range tc.states {
client.
On("MasternodeStatus").
Once().
Return(&btcjson.MasternodeStatusResult{State: state}, nil)
}
err := WaitForMNReady(client, 1*time.Millisecond)
if tc.wantErr != "" {
require.ErrorContains(t, err, tc.wantErr)
} else {
require.NoError(t, err)
}
})
}
}

func TestWaitForMNReadyError(t *testing.T) {
err := errors.New("some error")
client := mocks.NewClient(t)
client.
On("MasternodeStatus").
Once().
Return(nil, err)
require.ErrorContains(t, WaitForMNReady(client, 1), err.Error())
}
212 changes: 212 additions & 0 deletions dash/core/mocks/client.go

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

10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/containerd/continuity v0.3.0 // indirect
github.com/dashpay/bls-signatures/go-bindings v0.0.0-20230207105415-06df92693ac8
github.com/dashpay/dashd-go v0.24.0
github.com/dashpay/dashd-go v0.24.1
github.com/dashpay/dashd-go/btcec/v2 v2.1.0 // indirect
github.com/fortytw2/leaktest v1.3.0
github.com/fxamacker/cbor/v2 v2.4.0
Expand Down Expand Up @@ -57,17 +57,25 @@ require (
)

require (
github.com/aead/siphash v1.0.1 // indirect
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f // indirect
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
github.com/btcsuite/goleveldb v1.0.0 // indirect
github.com/btcsuite/snappy-go v1.0.0 // indirect
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
github.com/btcsuite/winsvc v1.0.0 // indirect
github.com/dashpay/dashd-go/btcutil v1.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/decred/dcrd/lru v1.0.0 // indirect
github.com/didip/tollbooth/v6 v6.0.1 // indirect
github.com/didip/tollbooth_chi v0.0.0-20200524181329-8b84cd7183d9 // indirect
github.com/go-chi/render v1.0.1 // indirect
github.com/go-pkgz/expirable-cache v0.0.3 // indirect
github.com/go-pkgz/rest v1.5.0 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/jrick/logrotate v1.0.0 // indirect
github.com/kkdai/bstream v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
Expand Down
Loading

0 comments on commit e11684a

Please sign in to comment.