Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add cancelled context tests for chainReader #455

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mocks: ## generates mocks

.PHONY: tests
tests: ## runs all tests
go test -race ./... -timeout=5m
go test -race ./... -timeout=6m

.PHONY: tests-cover
tests-cover: ## run all tests with test coverge
Expand Down
140 changes: 140 additions & 0 deletions chainio/clients/elcontracts/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,146 @@ func TestOperatorSetsWithWrongInput(t *testing.T) {
})
}

// The idea is to cover some cases where network can fail by passing a
// cancelled context, so the binding returns an error
func TestFailingNetwork(t *testing.T) {
read_clients, anvilHttpEndpoint := testclients.BuildTestReadClients(t)
ctx := context.Background()

subCtx, cancelFn := context.WithCancel(ctx)
cancelFn()

contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint)
operator := types.Operator{
Address: testutils.ANVIL_FIRST_ADDRESS,
}

t.Run("is operator registered", func(t *testing.T) {
isOperator, err := read_clients.ElChainReader.IsOperatorRegistered(subCtx, operator)
assert.Error(t, err)
assert.False(t, isOperator)
})

t.Run("get operator details", func(t *testing.T) {
operatorDetails, err := read_clients.ElChainReader.GetOperatorDetails(subCtx, operator)
assert.Error(t, err)
assert.Equal(t, types.Operator{}, operatorDetails)
})

t.Run("get strategy and underlying token", func(t *testing.T) {
strategyAddr := contractAddrs.Erc20MockStrategy
strategy, underlyingTokenAddr, err := read_clients.ElChainReader.GetStrategyAndUnderlyingToken(
subCtx,
strategyAddr,
)
assert.Error(t, err)
assert.Nil(t, strategy)
assert.Equal(t, common.Address{}, underlyingTokenAddr)
})

t.Run("get strategy and underlying ERC20 token", func(t *testing.T) {
strategyAddr := contractAddrs.Erc20MockStrategy
strategy, contractUnderlyingToken, underlyingTokenAddr, err := read_clients.ElChainReader.GetStrategyAndUnderlyingERC20Token(
subCtx,
strategyAddr,
)
assert.Error(t, err)
assert.Nil(t, strategy)
assert.Equal(t, common.Address{}, underlyingTokenAddr)
assert.Nil(t, contractUnderlyingToken)
})

t.Run("get operator shares in strategy", func(t *testing.T) {
shares, err := read_clients.ElChainReader.GetOperatorSharesInStrategy(
subCtx,
common.HexToAddress(operator.Address),
contractAddrs.Erc20MockStrategy,
)
assert.Error(t, err)
assert.Zero(t, shares)
})

t.Run("calculate delegation approval digest hash", func(t *testing.T) {
staker := common.Address{0x0}
delegationApprover := common.Address{0x0}
approverSalt := [32]byte{}
expiry := big.NewInt(0)
digest, err := read_clients.ElChainReader.CalculateDelegationApprovalDigestHash(
subCtx,
staker,
common.HexToAddress(operator.Address),
delegationApprover,
approverSalt,
expiry,
)
assert.Error(t, err)
assert.Empty(t, digest)
})

t.Run("calculate operator AVS registration digest hash", func(t *testing.T) {
avs := common.Address{0x0}
salt := [32]byte{}
expiry := big.NewInt(0)
digest, err := read_clients.ElChainReader.CalculateOperatorAVSRegistrationDigestHash(
subCtx,
common.HexToAddress(operator.Address),
avs,
salt,
expiry,
)
assert.Error(t, err)
assert.Empty(t, digest)
})

t.Run("get staker shares", func(t *testing.T) {
strategies, shares, err := read_clients.ElChainReader.GetStakerShares(
subCtx,
common.HexToAddress(operator.Address),
)
assert.Empty(t, strategies)
assert.Empty(t, shares)
assert.Error(t, err)
})

t.Run("get delegated operator", func(t *testing.T) {
blockNumber := big.NewInt(0)
address, err := read_clients.ElChainReader.GetDelegatedOperator(
subCtx,
common.HexToAddress(operator.Address),
blockNumber,
)

assert.Error(t, err)
assert.Equal(t, address, common.Address{})
})

t.Run("GetOperatorShares", func(t *testing.T) {
strategyAddr := contractAddrs.Erc20MockStrategy
strategies := []common.Address{strategyAddr}
shares, err := read_clients.ElChainReader.GetOperatorShares(
subCtx,
common.HexToAddress(operator.Address),
strategies,
)
assert.Error(t, err)
assert.Nil(t, shares)
})

t.Run("GetOperatorsShares", func(t *testing.T) {
operatorAddr := common.HexToAddress(operator.Address)
operators := []common.Address{operatorAddr}
strategyAddr := contractAddrs.Erc20MockStrategy
strategies := []common.Address{strategyAddr}
shares, err := read_clients.ElChainReader.GetOperatorsShares(
subCtx,
operators,
strategies,
)
assert.Error(t, err)
assert.Nil(t, shares)
})
}

func TestCreateRederFromConfig(t *testing.T) {
_, anvilHttpEndpoint := testclients.BuildTestClients(t)
testConfig := testutils.GetDefaultTestConfig()
Expand Down