Skip to content

Commit

Permalink
Merge branch 'dev' into test_elcontracts_slashing_functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand authored Jan 17, 2025
2 parents 942b87b + ad30996 commit 10f1912
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 21 deletions.
6 changes: 3 additions & 3 deletions chainio/clients/avsregistry/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

chainioutils "github.com/Layr-Labs/eigensdk-go/chainio/utils"
"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"
gethcommon "github.com/ethereum/go-ethereum/common"
Expand All @@ -20,9 +21,8 @@ func TestWriterMethods(t *testing.T) {
keypair, err := bls.NewKeyPairFromString("0x01")
require.NoError(t, err)

addr := gethcommon.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
ecdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
ecdsaPrivateKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex)
addr := gethcommon.HexToAddress(testutils.ANVIL_FIRST_ADDRESS)
ecdsaPrivateKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
require.NoError(t, err)

quorumNumbers := types.QuorumNums{0}
Expand Down
4 changes: 2 additions & 2 deletions chainio/clients/elcontracts/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,13 @@ func (r *ChainReader) GetOperatorShares(

func (r *ChainReader) GetOperatorsShares(
ctx context.Context,
operatorAddress []gethcommon.Address,
operatorAddresses []gethcommon.Address,
strategyAddresses []gethcommon.Address,
) ([][]*big.Int, error) {
if r.delegationManager == nil {
return nil, errors.New("DelegationManager contract not provided")
}
return r.delegationManager.GetOperatorsShares(&bind.CallOpts{Context: ctx}, operatorAddress, strategyAddresses)
return r.delegationManager.GetOperatorsShares(&bind.CallOpts{Context: ctx}, operatorAddresses, strategyAddresses)
}

// GetNumOperatorSetsForOperator returns the number of operator sets that an operator is part of
Expand Down
154 changes: 153 additions & 1 deletion chainio/clients/elcontracts/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts"
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/testutils"
Expand All @@ -25,7 +26,7 @@ func TestChainReader(t *testing.T) {

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

t.Run("is operator registered", func(t *testing.T) {
Expand Down Expand Up @@ -141,6 +142,75 @@ func TestChainReader(t *testing.T) {
assert.Equal(t, address.String(), operator.Address)
})

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

// with n strategies, response's list length is n
strategies = []common.Address{strategyAddr, strategyAddr, strategyAddr}
shares, err = read_clients.ElChainReader.GetOperatorShares(
ctx,
common.HexToAddress(operator.Address),
strategies,
)
assert.NoError(t, err)
assert.Len(t, shares, 3)

// We could test modify the shares and verify the diff is the expected
})

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(
ctx,
operators,
strategies,
)
assert.NoError(t, err)
assert.Len(t, shares, 1)

// with n strategies, response's list length is [1][n]
mult_strategies := []common.Address{strategyAddr, strategyAddr, strategyAddr}
shares, err = read_clients.ElChainReader.GetOperatorsShares(
ctx,
operators,
mult_strategies,
)
assert.NoError(t, err)
assert.Len(t, shares, 1)
assert.Len(t, shares[0], 3)

// with n strategies, response's list length is [n][1]
mult_operators := []common.Address{operatorAddr, operatorAddr, operatorAddr}
shares, err = read_clients.ElChainReader.GetOperatorsShares(
ctx,
mult_operators,
strategies,
)
assert.NoError(t, err)
assert.Len(t, shares, 3)
assert.Len(t, shares[0], 1)

// with n strategies and n operators, response's list length is [n][n]
shares, err = read_clients.ElChainReader.GetOperatorsShares(
ctx,
mult_operators,
mult_strategies,
)
assert.NoError(t, err)
assert.Len(t, shares, 3)
assert.Len(t, shares[2], 3)
})
}

func TestGetCurrentClaimableDistributionRoot(t *testing.T) {
Expand Down Expand Up @@ -428,6 +498,88 @@ func TestCheckClaim(t *testing.T) {
assert.True(t, checked)
}

func TestGetAllocatableMagnitudeAndGetMaxMagnitudes(t *testing.T) {
// Without changes, Allocable magnitude is max magnitude

// Test setup
ctx := context.Background()

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)

operatorAddr := common.HexToAddress(testutils.ANVIL_FIRST_ADDRESS)
config := elcontracts.Config{
DelegationManagerAddress: contractAddrs.DelegationManager,
}

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

strategyAddr := contractAddrs.Erc20MockStrategy
testAddr := common.HexToAddress(testutils.ANVIL_FIRST_ADDRESS)
operatorSetId := uint32(1)

strategies := []common.Address{strategyAddr}
maxMagnitudes, err := chainReader.GetMaxMagnitudes(ctx, testAddr, strategies)
assert.NoError(t, err)

// Assert that at the beginning, Allocatable Magnitude is Max allocatable magnitude
allocable, err := chainReader.GetAllocatableMagnitude(ctx, testAddr, strategyAddr)
assert.NoError(t, err)

assert.Equal(t, maxMagnitudes[0], allocable)

// Reduce allocatable magnitude for testAddr
privateKeyHex := testutils.ANVIL_FIRST_PRIVATE_KEY

chainWriter, err := testclients.NewTestChainWriterFromConfig(anvilHttpEndpoint, privateKeyHex, config)
require.NoError(t, err)

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

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

// Check that Allocation delay has been applied
_, err = chainReader.GetAllocationDelay(context.Background(), operatorAddr)
require.NoError(t, err)

err = createOperatorSet(anvilHttpEndpoint, privateKeyHex, testAddr, operatorSetId, strategyAddr)
require.NoError(t, err)

operatorSet := allocationmanager.OperatorSet{
Avs: testAddr,
Id: operatorSetId,
}
allocatable_reduction := uint64(100)
allocateParams := []allocationmanager.IAllocationManagerTypesAllocateParams{
{
OperatorSet: operatorSet,
Strategies: []common.Address{strategyAddr},
NewMagnitudes: []uint64{allocatable_reduction},
},
}

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

// Assert that after stake reduction, Allocatable Magnitude + reduction ammount equals Max allocatable magnitude
allocable, err = chainReader.GetAllocatableMagnitude(ctx, testAddr, strategyAddr)
assert.NoError(t, err)

assert.Equal(t, maxMagnitudes[0], allocable+allocatable_reduction)
}

func TestAdminFunctions(t *testing.T) {
testConfig := testutils.GetDefaultTestConfig()
anvilC, err := testutils.StartAnvilContainer(testConfig.AnvilStateFileName)
Expand Down
3 changes: 1 addition & 2 deletions chainio/clients/elcontracts/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,7 @@ func TestSetClaimerFor(t *testing.T) {
require.NoError(t, err)

waitForReceipt := true
claimer := common.HexToAddress("0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6")

claimer := contractAddrs.RewardsCoordinator
// call SetClaimerFor
receipt, err := chainWriter.SetClaimerFor(context.Background(), claimer, waitForReceipt)
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions chainio/clients/eth/instrumented_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,7 @@ func TestTransactionMethods(t *testing.T) {
Gas: 21000,
})

ecdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
ecdsaPrivKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex)
ecdsaPrivKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
assert.NoError(t, err)
signer := types.LatestSignerForChainID(big.NewInt(31337))
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), ecdsaPrivKey)
Expand Down
3 changes: 1 addition & 2 deletions chainio/clients/wallet/privatekey_wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func TestPrivateKeyWallet(t *testing.T) {
ethClient, err := ethclient.Dial(anvilHttpEndpoint)
require.NoError(t, err)

ecdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
ecdsaPrivKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex)
ecdsaPrivKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
require.NoError(t, err)
signerV2, signerAddr, err := signerv2.SignerFromConfig(signerv2.Config{PrivateKey: ecdsaPrivKey}, chainId)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion chainio/txmgr/geometric/geometric_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ExampleGeometricTxManager() {
panic(err)
}

ecdsaPrivateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
ecdsaPrivateKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion chainio/txmgr/geometric/geometric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func newIntegrationTestHarness(t *testing.T) *integrationTestHarness {
require.NoError(t, err)

ecdsaSk, ecdsaAddr, err := ecdsa.KeyAndAddressFromHexKey(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
testutils.ANVIL_FIRST_PRIVATE_KEY,
)
require.NoError(t, err)

Expand Down
3 changes: 1 addition & 2 deletions services/bls_aggregation/blsagg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1644,8 +1644,7 @@ func TestIntegrationBlsAgg(t *testing.T) {
testData := testutils.NewTestData(defaultInput)

// define operator ecdsa and bls private keys
ecdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
ecdsaPrivKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex)
ecdsaPrivKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
require.NoError(t, err)
blsPrivKeyHex := testData.Input.BlsPrivKey
blsKeyPair := newBlsKeyPairPanics(blsPrivKeyHex)
Expand Down
5 changes: 2 additions & 3 deletions signerv2/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

func TestPrivateKeySignerFn(t *testing.T) {
privateKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
privateKey, err := crypto.HexToECDSA(privateKeyHex)
privateKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
require.NoError(t, err)
chainID := big.NewInt(1)

Expand Down Expand Up @@ -74,7 +73,7 @@ func TestWeb3SignerFn(t *testing.T) {
signer, err := signerv2.Web3SignerFn(anvilHttpEndpoint)
require.NoError(t, err)

privateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
privateKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
require.NoError(t, err)
anvilChainID := big.NewInt(31337)
address := crypto.PubkeyToAddress(privateKey.PublicKey)
Expand Down
2 changes: 1 addition & 1 deletion testutils/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

const (
ANVIL_FIRST_ADDRESS = "f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
ANVIL_FIRST_ADDRESS = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
ANVIL_FIRST_PRIVATE_KEY = "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
ANVIL_SECOND_ADDRESS = "70997970C51812dc3A010C7d01b50e0d17dc79C8"
ANVIL_SECOND_PRIVATE_KEY = "59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
Expand Down
2 changes: 1 addition & 1 deletion testutils/testclients/testclients.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func BuildTestClients(t *testing.T) (*clients.Clients, string) {
require.NoError(t, err)
logger := logging.NewTextSLogger(os.Stdout, &logging.SLoggerOptions{Level: testConfig.LogLevel})

privateKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
privateKeyHex := testutils.ANVIL_FIRST_PRIVATE_KEY
ecdsaPrivateKey, err := crypto.HexToECDSA(privateKeyHex)
require.NoError(t, err)

Expand Down

0 comments on commit 10f1912

Please sign in to comment.