frostfs-node/pkg/morph/client/waiter.go
Evgenii Stratonikov 69c63006da
[#1496] morph: Move tx waiter to morph package
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2024-11-15 16:36:07 +03:00

57 lines
1.5 KiB
Go

package client
import (
"context"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/waiter"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
)
// WaitParams represents transaction to wait for.
type WaitParams struct {
Hash util.Uint256
ValidUntilBlock uint32
}
type waiterClient struct {
c *Client
}
func (w *waiterClient) Context() context.Context {
return context.Background()
}
func (w *waiterClient) GetApplicationLog(hash util.Uint256, trig *trigger.Type) (*result.ApplicationLog, error) {
return w.c.GetApplicationLog(hash, trig)
}
func (w *waiterClient) GetBlockCount() (uint32, error) {
return w.c.BlockCount()
}
func (w *waiterClient) GetVersion() (*result.Version, error) {
return w.c.GetVersion()
}
// WaitTxHalt waits until transaction with the specified hash persists on the blockchain.
// It also checks execution result to finish in HALT state.
func (c *Client) WaitTxHalt(ctx context.Context, p WaitParams) error {
w, err := waiter.NewPollingBased(&waiterClient{c: c})
if err != nil {
return fmt.Errorf("create tx waiter: %w", err)
}
res, err := w.WaitAny(ctx, p.ValidUntilBlock, p.Hash)
if err != nil {
return fmt.Errorf("wait until tx persists: %w", err)
}
if res.VMState.HasFlag(vmstate.Halt) {
return nil
}
return wrapFrostFSError(&notHaltStateError{state: res.VMState.String(), exception: res.FaultException})
}