[#905] Make morph client non-panic-prone
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 1m8s
DCO action / DCO (pull_request) Successful in 2m8s
Build / Build Components (1.21) (pull_request) Successful in 4m2s
Build / Build Components (1.20) (pull_request) Successful in 4m21s
Tests and linters / Staticcheck (pull_request) Successful in 5m7s
Tests and linters / Tests (1.21) (pull_request) Successful in 5m20s
Tests and linters / Tests (1.20) (pull_request) Successful in 5m27s
Tests and linters / Lint (pull_request) Successful in 5m36s
Tests and linters / Tests with -race (pull_request) Successful in 8m3s

Morph client returns errors instead.

Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
This commit is contained in:
Ekaterina Lebedeva 2024-02-06 03:20:35 +03:00
parent b5622ebead
commit a9b564a27e
3 changed files with 14 additions and 16 deletions

View file

@ -74,8 +74,6 @@ func defaultConfig() *cfg {
// Notary support should be enabled with EnableNotarySupport client
// method separately.
//
// If private key is nil, it panics.
//
// Other values are set according to provided options, or by default:
// - client context: Background;
// - dial timeout: 5s;
@ -92,7 +90,7 @@ func defaultConfig() *cfg {
// If there are no healthy endpoint - returns ErrNoHealthyEndpoint.
func New(ctx context.Context, key *keys.PrivateKey, opts ...Option) (*Client, error) {
if key == nil {
panic("empty private key")
return nil, errors.New("empty private key")
}
acc := wallet.NewAccountFromPrivateKey(key)

View file

@ -62,7 +62,7 @@ const (
setDesignateMethod = "designateAsRole"
notaryBalanceErrMsg = "can't fetch notary balance"
notaryNotEnabledPanicMsg = "notary support was not enabled on this client"
notaryNotEnabledErrMsg = "notary support was not enabled on this client"
)
var errUnexpectedItems = errors.New("invalid number of NEO VM arguments on stack")
@ -190,7 +190,7 @@ func (c *Client) ProbeNotary() (res bool) {
// be called periodically. Notary support should be enabled in client to
// use this function.
//
// This function must be invoked with notary enabled otherwise it throws panic.
// This function must be invoked with notary enabled.
func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) (res util.Uint256, err error) {
c.switchLock.RLock()
defer c.switchLock.RUnlock()
@ -200,7 +200,7 @@ func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) (res util.Uin
}
if c.notary == nil {
panic(notaryNotEnabledPanicMsg)
return util.Uint256{}, fmt.Errorf(notaryNotEnabledErrMsg)
}
bc, err := c.rpcActor.GetBlockCount()
@ -225,7 +225,7 @@ func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) (res util.Uin
// this method sets notary deposit till parameter to a maximum possible value.
// This allows to avoid ValidAfterDeposit failures.
//
// This function must be invoked with notary enabled otherwise it throws panic.
// This function must be invoked with notary enabled.
func (c *Client) DepositEndlessNotary(amount fixedn.Fixed8) (res util.Uint256, err error) {
c.switchLock.RLock()
defer c.switchLock.RUnlock()
@ -235,7 +235,7 @@ func (c *Client) DepositEndlessNotary(amount fixedn.Fixed8) (res util.Uint256, e
}
if c.notary == nil {
panic(notaryNotEnabledPanicMsg)
return util.Uint256{}, errors.New(notaryNotEnabledErrMsg)
}
// till value refers to a block height and it is uint32 value in neo-go
@ -275,7 +275,7 @@ func (c *Client) depositNotary(amount fixedn.Fixed8, till int64) (res util.Uint2
// GetNotaryDeposit returns deposit of client's account in notary contract.
// Notary support should be enabled in client to use this function.
//
// This function must be invoked with notary enabled otherwise it throws panic.
// This function must be invoked with notary enabled.
func (c *Client) GetNotaryDeposit() (res int64, err error) {
c.switchLock.RLock()
defer c.switchLock.RUnlock()
@ -285,7 +285,7 @@ func (c *Client) GetNotaryDeposit() (res int64, err error) {
}
if c.notary == nil {
panic(notaryNotEnabledPanicMsg)
return 0, errors.New(notaryNotEnabledErrMsg)
}
sh := c.acc.PrivateKey().PublicKey().GetScriptHash()
@ -327,7 +327,7 @@ func (u *UpdateNotaryListPrm) SetHash(hash util.Uint256) {
// UpdateNotaryList updates list of notary nodes in designate contract. Requires
// committee multi signature.
//
// This function must be invoked with notary enabled otherwise it throws panic.
// This function must be invoked with notary enabled.
func (c *Client) UpdateNotaryList(prm UpdateNotaryListPrm) error {
c.switchLock.RLock()
defer c.switchLock.RUnlock()
@ -337,7 +337,7 @@ func (c *Client) UpdateNotaryList(prm UpdateNotaryListPrm) error {
}
if c.notary == nil {
panic(notaryNotEnabledPanicMsg)
return errors.New(notaryNotEnabledErrMsg)
}
nonce, vub, err := c.CalculateNonceAndVUB(&prm.hash)
@ -375,7 +375,7 @@ func (u *UpdateAlphabetListPrm) SetHash(hash util.Uint256) {
// As for sidechain list should contain all inner ring nodes.
// Requires committee multi signature.
//
// This function must be invoked with notary enabled otherwise it throws panic.
// This function must be invoked with notary enabled.
func (c *Client) UpdateNeoFSAlphabetList(prm UpdateAlphabetListPrm) error {
c.switchLock.RLock()
defer c.switchLock.RUnlock()
@ -385,7 +385,7 @@ func (c *Client) UpdateNeoFSAlphabetList(prm UpdateAlphabetListPrm) error {
}
if c.notary == nil {
panic(notaryNotEnabledPanicMsg)
return errors.New(notaryNotEnabledErrMsg)
}
nonce, vub, err := c.CalculateNonceAndVUB(&prm.hash)

View file

@ -63,7 +63,7 @@ func (c *Client) ReceiveBlocks(ch chan<- *block.Block) (string, error) {
// connection to any of passed RPC endpoints.
func (c *Client) ReceiveNotaryRequests(txSigner util.Uint160, ch chan<- *result.NotaryRequestEvent) (string, error) {
if c.notary == nil {
panic(notaryNotEnabledPanicMsg)
panic(notaryNotEnabledErrMsg)
}
c.switchLock.Lock()