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: chainReader shares and operatorSets functions #437

Merged
merged 31 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6c67852
Add code from PR#416
damiramirez Jan 14, 2025
7ca1403
Add test for GetSlashableShares function
damiramirez Jan 14, 2025
561f656
Add two fields to RegistrationRequest
damiramirez Jan 14, 2025
bdcc73b
Change logic RegisterForOperatorSets()
damiramirez Jan 14, 2025
58f8e49
Add more test for slashable functions and utils
damiramirez Jan 14, 2025
53e01ea
Change address to constant values
damiramirez Jan 14, 2025
5955c4b
Remove t.Log
damiramirez Jan 14, 2025
7f5d030
Run fmt
damiramirez Jan 14, 2025
6c4181e
Some refactor and add new test cases for getoperatorsets
damiramirez Jan 14, 2025
10f0a98
Run fmt
damiramirez Jan 14, 2025
192d165
Run make fmt
damiramirez Jan 14, 2025
9a19a97
Add blocknumber and run make fmt
damiramirez Jan 15, 2025
203e2ca
Remove t.Log
damiramirez Jan 15, 2025
407969d
Add more test cases
damiramirez Jan 15, 2025
095890b
Add new function that return the anvilC
damiramirez Jan 15, 2025
a965138
Remove unnecessary t.Logs
damiramirez Jan 15, 2025
b72c6a2
Finish with the test and refactor
damiramirez Jan 15, 2025
12121e7
Add test case for GetRegisteredSets
damiramirez Jan 15, 2025
e6a0e62
Add functions to create readers and writers
damiramirez Jan 16, 2025
1c97057
Check if allocationmanager address is not null
damiramirez Jan 16, 2025
f8eaa89
Add test for edge cases and wrong input
damiramirez Jan 16, 2025
778075b
Refactor and run make fmt
damiramirez Jan 16, 2025
c3a638a
Fix typos and remove address check in reader
damiramirez Jan 16, 2025
97500d0
Merge branch 'dev' into test/slashable-shares-read
damiramirez Jan 17, 2025
9e24725
Remove build client with anvil function
damiramirez Jan 17, 2025
53f13fe
Refactor and move some logic
damiramirez Jan 17, 2025
aab1903
Change test timeout to 5m
damiramirez Jan 17, 2025
9d32443
Change const to variable
damiramirez Jan 20, 2025
4fbf686
Replace literal to geth type
damiramirez Jan 20, 2025
1e9bf2d
Remove redundant checks
damiramirez Jan 20, 2025
ca5f8b8
Revert changes in writer.go
damiramirez Jan 20, 2025
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=4m
go test -race ./... -timeout=5m

.PHONY: tests-cover
tests-cover: ## run all tests with test coverge
Expand Down
220 changes: 219 additions & 1 deletion chainio/clients/elcontracts/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
allocationmanager "github.com/Layr-Labs/eigensdk-go/contracts/bindings/AllocationManager"
erc20 "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IERC20"
rewardscoordinator "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IRewardsCoordinator"
"github.com/Layr-Labs/eigensdk-go/crypto/bls"
"github.com/Layr-Labs/eigensdk-go/testutils"
"github.com/Layr-Labs/eigensdk-go/testutils/testclients"
"github.com/Layr-Labs/eigensdk-go/types"
Expand Down Expand Up @@ -640,7 +641,6 @@ func TestAdminFunctions(t *testing.T) {
listPendingAdmins, err := chainReader.ListPendingAdmins(context.Background(), operatorAddr)
assert.NoError(t, err)
assert.NotEmpty(t, listPendingAdmins)
assert.Len(t, listPendingAdmins, 1)
})

t.Run("non-existent admin", func(t *testing.T) {
Expand Down Expand Up @@ -734,3 +734,221 @@ func TestAppointeesFunctions(t *testing.T) {
assert.NotEmpty(t, appointeesPermission)
})
}

func TestOperatorSetsAndSlashableShares(t *testing.T) {
testConfig := testutils.GetDefaultTestConfig()
anvilC, err := testutils.StartAnvilContainer(testConfig.AnvilStateFileName)
require.NoError(t, err)

anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http")
require.NoError(t, err)
contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint)

config := elcontracts.Config{
DelegationManagerAddress: contractAddrs.DelegationManager,
}
chainReader, err := testclients.NewTestChainReaderFromConfig(anvilHttpEndpoint, config)
require.NoError(t, err)

operatorAddr := common.HexToAddress(testutils.ANVIL_SECOND_ADDRESS)
operatorPrivateKeyHex := testutils.ANVIL_SECOND_PRIVATE_KEY
chainWriter, err := testclients.NewTestChainWriterFromConfig(anvilHttpEndpoint, operatorPrivateKeyHex, config)
require.NoError(t, err)

avsAdrr := common.HexToAddress(testutils.ANVIL_FIRST_ADDRESS)
avsPrivateKeyHex := testutils.ANVIL_FIRST_PRIVATE_KEY
operatorSetId := uint32(1)
operatorSet := allocationmanager.OperatorSet{
Avs: avsAdrr,
Id: operatorSetId,
}

strategyAddr := contractAddrs.Erc20MockStrategy
strategies := []common.Address{strategyAddr}

err = createOperatorSet(anvilHttpEndpoint, avsPrivateKeyHex, avsAdrr, operatorSetId, strategyAddr)
require.NoError(t, err)

keypair, err := bls.NewKeyPairFromString("0x01")
require.NoError(t, err)

request := elcontracts.RegistrationRequest{
OperatorAddress: operatorAddr,
AVSAddress: avsAdrr,
OperatorSetIds: []uint32{operatorSetId},
WaitForReceipt: true,
Socket: "socket",
BlsKeyPair: keypair,
}

registryCoordinatorAddress := contractAddrs.RegistryCoordinator
receipt, err := chainWriter.RegisterForOperatorSets(
context.Background(),
registryCoordinatorAddress,
request,
)
require.NoError(t, err)
require.Equal(t, uint64(1), receipt.Status)

allocationDelay := 1
allocationMagnitude := 100
allocationConfigurationDelay := 1200

receipt, err = chainWriter.SetAllocationDelay(
context.Background(),
operatorAddr,
uint32(allocationDelay),
true,
)
require.NoError(t, err)
require.Equal(t, gethtypes.ReceiptStatusSuccessful, receipt.Status)

testutils.AdvanceChainByNBlocksExecInContainer(
context.Background(),
allocationConfigurationDelay+1,
anvilC,
)

allocationParams := []allocationmanager.IAllocationManagerTypesAllocateParams{
{
OperatorSet: operatorSet,
Strategies: strategies,
NewMagnitudes: []uint64{uint64(allocationMagnitude)},
},
}

receipt, err = chainWriter.ModifyAllocations(
context.Background(),
operatorAddr,
allocationParams,
true,
)
require.NoError(t, err)
require.Equal(t, gethtypes.ReceiptStatusSuccessful, receipt.Status)

t.Run("get operators and operator sets", func(t *testing.T) {
t.Run("validate strategies for operatorSet", func(t *testing.T) {
strats, err := chainReader.GetStrategiesForOperatorSet(context.Background(), operatorSet)
require.NoError(t, err)
require.Len(t, strats, 1)
require.Equal(t, strats[0].Hex(), strategyAddr.Hex())
})

t.Run("get registered sets", func(t *testing.T) {
registeredSets, err := chainReader.GetRegisteredSets(context.Background(), operatorAddr)
require.NoError(t, err)
require.NotEmpty(t, registeredSets)
})

t.Run("get operator sets for operator", func(t *testing.T) {
opSets, err := chainReader.GetOperatorSetsForOperator(context.Background(), operatorAddr)
require.NoError(t, err)
require.NotEmpty(t, opSets)
})

t.Run("get amount operatorSets for operator", func(t *testing.T) {
opSetsCount, err := chainReader.GetNumOperatorSetsForOperator(
context.Background(),
operatorAddr,
)
require.NoError(t, err)
require.NotZero(t, opSetsCount)
})

t.Run("get operator for operatorsets", func(t *testing.T) {
operators, err := chainReader.GetOperatorsForOperatorSet(context.Background(), operatorSet)
require.NoError(t, err)
require.NotEmpty(t, operators)
})

t.Run("get amount of operators for operatorsets", func(t *testing.T) {
operatorsCount, err := chainReader.GetNumOperatorsForOperatorSet(context.Background(), operatorSet)
require.NoError(t, err)
require.NotZero(t, operatorsCount)
})
})

t.Run("slashable shares tests", func(t *testing.T) {
t.Run("get slashable shares for single operator", func(t *testing.T) {
shares, err := chainReader.GetSlashableShares(
context.Background(),
operatorAddr,
operatorSet,
strategies,
)
require.NoError(t, err)
require.NotEmpty(t, shares)
})

t.Run("get slashable shares for multiple operatorSets", func(t *testing.T) {
shares, err := chainReader.GetSlashableSharesForOperatorSets(
context.Background(),
[]allocationmanager.OperatorSet{operatorSet},
)
require.NoError(t, err)
require.NotEmpty(t, shares)
})

t.Run("get slashable shares before specific block", func(t *testing.T) {
shares, err := chainReader.GetSlashableSharesForOperatorSetsBefore(
context.Background(),
[]allocationmanager.OperatorSet{operatorSet},
2,
)
require.NoError(t, err)
require.NotEmpty(t, shares)
})
})
}

func TestOperatorSetsWithWrongInput(t *testing.T) {
_, anvilHttpEndpoint := testclients.BuildTestClients(t)
ctx := context.Background()

contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint)
operatorAddr := common.HexToAddress(testutils.ANVIL_FIRST_ADDRESS)

config := elcontracts.Config{}
operatorSet := allocationmanager.OperatorSet{
Avs: common.HexToAddress(testutils.ANVIL_SECOND_ADDRESS),
Id: 0,
}

chainReader, err := testclients.NewTestChainReaderFromConfig(anvilHttpEndpoint, config)
require.NoError(t, err)

t.Run("test operator set with invalid id", func(t *testing.T) {
_, err := chainReader.GetOperatorsForOperatorSet(ctx, operatorSet)
require.Error(t, err)

_, err = chainReader.GetNumOperatorsForOperatorSet(ctx, operatorSet)
require.Error(t, err)

_, err = chainReader.GetStrategiesForOperatorSet(ctx, operatorSet)
require.Error(t, err)

strategies := []common.Address{contractAddrs.Erc20MockStrategy}

_, err = chainReader.GetSlashableShares(
ctx,
operatorAddr,
operatorSet,
strategies,
)
require.Error(t, err)
})

t.Run("get slashable shares with invalid operatorSet", func(t *testing.T) {
config := elcontracts.Config{
DelegationManagerAddress: contractAddrs.DelegationManager,
}

chainReader, err = testclients.NewTestChainReaderFromConfig(anvilHttpEndpoint, config)
require.NoError(t, err)

operatorSets := []allocationmanager.OperatorSet{operatorSet}

_, err = chainReader.GetSlashableSharesForOperatorSetsBefore(context.Background(), operatorSets, 10)
require.Error(t, err)
})
}
Loading