Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 5, 2024
1 parent fe1f9f9 commit e973ddf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 41 deletions.
57 changes: 29 additions & 28 deletions core/meterer/meterer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/Layr-Labs/eigenda/core/mock"
"github.com/Layr-Labs/eigenda/inabox/deploy"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/assert"
Expand All @@ -30,12 +31,12 @@ var (
dockertestResource *dockertest.Resource
dynamoClient commondynamodb.Client
clientConfig commonaws.ClientConfig
accountID1 string
account1Reservations core.ActiveReservation
account1OnDemandPayments core.OnDemandPayment
accountID2 string
account2Reservations core.ActiveReservation
account2OnDemandPayments core.OnDemandPayment
accountID1 gethcommon.Address
account1Reservations *core.ActiveReservation
account1OnDemandPayments *core.OnDemandPayment
accountID2 gethcommon.Address
account2Reservations *core.ActiveReservation
account2OnDemandPayments *core.OnDemandPayment
mt *meterer.Meterer

deployLocalStack bool
Expand Down Expand Up @@ -123,12 +124,12 @@ func setup(_ *testing.M) {
}

now := uint64(time.Now().Unix())
accountID1 = crypto.PubkeyToAddress(privateKey1.PublicKey).Hex()
accountID2 = crypto.PubkeyToAddress(privateKey2.PublicKey).Hex()
account1Reservations = core.ActiveReservation{SymbolsPerSecond: 100, StartTimestamp: now + 1200, EndTimestamp: now + 1800, QuorumSplits: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}}
account2Reservations = core.ActiveReservation{SymbolsPerSecond: 200, StartTimestamp: now - 120, EndTimestamp: now + 180, QuorumSplits: []byte{30, 70}, QuorumNumbers: []uint8{0, 1}}
account1OnDemandPayments = core.OnDemandPayment{CumulativePayment: big.NewInt(3864)}
account2OnDemandPayments = core.OnDemandPayment{CumulativePayment: big.NewInt(2000)}
accountID1 = crypto.PubkeyToAddress(privateKey1.PublicKey)
accountID2 = crypto.PubkeyToAddress(privateKey2.PublicKey)
account1Reservations = &core.ActiveReservation{SymbolsPerSecond: 100, StartTimestamp: now + 1200, EndTimestamp: now + 1800, QuorumSplits: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}}
account2Reservations = &core.ActiveReservation{SymbolsPerSecond: 200, StartTimestamp: now - 120, EndTimestamp: now + 180, QuorumSplits: []byte{30, 70}, QuorumNumbers: []uint8{0, 1}}
account1OnDemandPayments = &core.OnDemandPayment{CumulativePayment: big.NewInt(3864)}
account2OnDemandPayments = &core.OnDemandPayment{CumulativePayment: big.NewInt(2000)}

store, err := meterer.NewOffchainStore(
clientConfig,
Expand Down Expand Up @@ -176,13 +177,13 @@ func TestMetererReservations(t *testing.T) {
binIndex := meterer.GetBinIndex(uint64(time.Now().Unix()), mt.ChainPaymentState.GetReservationWindow())
quoromNumbers := []uint8{0, 1}

paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.MatchedBy(func(account string) bool {
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.MatchedBy(func(account gethcommon.Address) bool {
return account == accountID1
})).Return(account1Reservations, nil)
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.MatchedBy(func(account string) bool {
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.MatchedBy(func(account gethcommon.Address) bool {
return account == accountID2
})).Return(account2Reservations, nil)
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.Anything).Return(core.ActiveReservation{}, fmt.Errorf("reservation not found"))
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.Anything).Return(&core.ActiveReservation{}, fmt.Errorf("reservation not found"))

// test invalid quorom ID
header := createPaymentHeader(1, 0, accountID1)
Expand All @@ -203,7 +204,7 @@ func TestMetererReservations(t *testing.T) {
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}
header = createPaymentHeader(1, 0, crypto.PubkeyToAddress(unregisteredUser.PublicKey).Hex())
header = createPaymentHeader(1, 0, crypto.PubkeyToAddress(unregisteredUser.PublicKey))
assert.NoError(t, err)
err = mt.MeterRequest(ctx, *header, 1000, []uint8{0, 1, 2})
assert.ErrorContains(t, err, "failed to get active reservation by account: reservation not found")
Expand All @@ -221,7 +222,7 @@ func TestMetererReservations(t *testing.T) {
err = mt.MeterRequest(ctx, *header, symbolLength, quoromNumbers)
assert.NoError(t, err)
item, err := dynamoClient.GetItem(ctx, reservationTableName, commondynamodb.Key{
"AccountID": &types.AttributeValueMemberS{Value: accountID2},
"AccountID": &types.AttributeValueMemberS{Value: accountID2.Hex()},
"BinIndex": &types.AttributeValueMemberN{Value: strconv.Itoa(int(binIndex))},
})
assert.NoError(t, err)
Expand All @@ -237,11 +238,11 @@ func TestMetererReservations(t *testing.T) {
assert.NoError(t, err)
overflowedBinIndex := binIndex + 2
item, err := dynamoClient.GetItem(ctx, reservationTableName, commondynamodb.Key{
"AccountID": &types.AttributeValueMemberS{Value: accountID2},
"AccountID": &types.AttributeValueMemberS{Value: accountID2.Hex()},
"BinIndex": &types.AttributeValueMemberN{Value: strconv.Itoa(int(overflowedBinIndex))},
})
assert.NoError(t, err)
assert.Equal(t, accountID2, item["AccountID"].(*types.AttributeValueMemberS).Value)
assert.Equal(t, accountID2.Hex(), item["AccountID"].(*types.AttributeValueMemberS).Value)
assert.Equal(t, strconv.Itoa(int(overflowedBinIndex)), item["BinIndex"].(*types.AttributeValueMemberN).Value)
// 25 rounded up to the nearest multiple of minNumSymbols - (200-21*9) = 16
assert.Equal(t, strconv.Itoa(int(16)), item["BinUsage"].(*types.AttributeValueMemberN).Value)
Expand All @@ -260,21 +261,21 @@ func TestMetererOnDemand(t *testing.T) {
paymentChainState.On("GetMinNumSymbols", testifymock.Anything).Return(uint32(3), nil)
binIndex := uint32(0) // this field doesn't matter for on-demand payments wrt global rate limit

paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.MatchedBy(func(account string) bool {
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.MatchedBy(func(account gethcommon.Address) bool {
return account == accountID1
})).Return(account1OnDemandPayments, nil)
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.MatchedBy(func(account string) bool {
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.MatchedBy(func(account gethcommon.Address) bool {
return account == accountID2
})).Return(account2OnDemandPayments, nil)
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.Anything).Return(core.OnDemandPayment{}, fmt.Errorf("payment not found"))
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.Anything).Return(&core.OnDemandPayment{}, fmt.Errorf("payment not found"))
paymentChainState.On("GetOnDemandQuorumNumbers", testifymock.Anything).Return(quorumNumbers, nil)

// test unregistered account
unregisteredUser, err := crypto.GenerateKey()
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}
header := createPaymentHeader(binIndex, 2, crypto.PubkeyToAddress(unregisteredUser.PublicKey).Hex())
header := createPaymentHeader(binIndex, 2, crypto.PubkeyToAddress(unregisteredUser.PublicKey))
assert.NoError(t, err)
err = mt.MeterRequest(ctx, *header, 1000, quorumNumbers)
assert.ErrorContains(t, err, "failed to get on-demand payment by account: payment not found")
Expand All @@ -291,7 +292,7 @@ func TestMetererOnDemand(t *testing.T) {
// No rollback after meter request
result, err := dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
":account": &types.AttributeValueMemberS{
Value: accountID1,
Value: accountID1.Hex(),
}})
assert.NoError(t, err)
assert.Equal(t, 1, len(result))
Expand Down Expand Up @@ -336,7 +337,7 @@ func TestMetererOnDemand(t *testing.T) {
numPrevRecords := 12
result, err = dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
":account": &types.AttributeValueMemberS{
Value: accountID2,
Value: accountID2.Hex(),
}})
assert.NoError(t, err)
assert.Equal(t, numPrevRecords, len(result))
Expand All @@ -347,7 +348,7 @@ func TestMetererOnDemand(t *testing.T) {
// Correct rollback
result, err = dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
":account": &types.AttributeValueMemberS{
Value: accountID2,
Value: accountID2.Hex(),
}})
assert.NoError(t, err)
assert.Equal(t, numPrevRecords, len(result))
Expand Down Expand Up @@ -464,9 +465,9 @@ func TestMeterer_symbolsCharged(t *testing.T) {
}
}

func createPaymentHeader(binIndex uint32, cumulativePayment uint64, accountID string) *core.PaymentMetadata {
func createPaymentHeader(binIndex uint32, cumulativePayment uint64, accountID gethcommon.Address) *core.PaymentMetadata {
return &core.PaymentMetadata{
AccountID: accountID,
AccountID: accountID.Hex(),
BinIndex: binIndex,
CumulativePayment: big.NewInt(int64(cumulativePayment)),
}
Expand Down
4 changes: 2 additions & 2 deletions core/meterer/onchain_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
)

var (
dummyActiveReservation = core.ActiveReservation{
dummyActiveReservation = &core.ActiveReservation{
SymbolsPerSecond: 100,
StartTimestamp: 1000,
EndTimestamp: 2000,
QuorumSplits: []byte{50, 50},
}
dummyOnDemandPayment = core.OnDemandPayment{
dummyOnDemandPayment = &core.OnDemandPayment{
CumulativePayment: big.NewInt(1000),
}
)
Expand Down
4 changes: 2 additions & 2 deletions disperser/apiserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,10 @@ func newTestServer(transactor core.Writer, testName string) *apiserver.Dispersal
mockState.On("GetRequiredQuorumNumbers").Return([]uint8{0, 1}, nil)
mockState.On("GetOnDemandQuorumNumbers").Return([]uint8{0, 1}, nil)
mockState.On("GetReservationWindow").Return(uint32(1), nil)
mockState.On("GetOnDemandPaymentByAccount", tmock.Anything, tmock.Anything).Return(core.OnDemandPayment{
mockState.On("GetOnDemandPaymentByAccount", tmock.Anything, tmock.Anything).Return(&core.OnDemandPayment{
CumulativePayment: big.NewInt(3000),
}, nil)
mockState.On("GetActiveReservationByAccount", tmock.Anything, tmock.Anything).Return(core.ActiveReservation{
mockState.On("GetActiveReservationByAccount", tmock.Anything, tmock.Anything).Return(&core.ActiveReservation{
SymbolsPerSecond: 2048,
StartTimestamp: 0,
EndTimestamp: math.MaxUint32,
Expand Down
4 changes: 2 additions & 2 deletions disperser/apiserver/server_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ func newTestServerV2(t *testing.T) *testComponents {
mockState.On("GetMinNumSymbols", tmock.Anything).Return(uint32(3), nil)

now := uint64(time.Now().Unix())
mockState.On("GetActiveReservationByAccount", tmock.Anything, tmock.Anything).Return(core.ActiveReservation{SymbolsPerSecond: 100, StartTimestamp: now + 1200, EndTimestamp: now + 1800, QuorumSplits: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}}, nil)
mockState.On("GetOnDemandPaymentByAccount", tmock.Anything, tmock.Anything).Return(core.OnDemandPayment{CumulativePayment: big.NewInt(3864)}, nil)
mockState.On("GetActiveReservationByAccount", tmock.Anything, tmock.Anything).Return(&core.ActiveReservation{SymbolsPerSecond: 100, StartTimestamp: now + 1200, EndTimestamp: now + 1800, QuorumSplits: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}}, nil)
mockState.On("GetOnDemandPaymentByAccount", tmock.Anything, tmock.Anything).Return(&core.OnDemandPayment{CumulativePayment: big.NewInt(3864)}, nil)
mockState.On("GetOnDemandQuorumNumbers", tmock.Anything).Return([]uint8{0, 1}, nil)

if err := mockState.RefreshOnchainPaymentState(context.Background(), nil); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,20 @@ func mustMakeDisperser(t *testing.T, cst core.IndexedChainState, store disperser
if err != nil {
panic("failed to convert hex to ECDSA")
}
publicKey := crypto.PubkeyToAddress(privateKey.PublicKey).Hex()
publicKey := crypto.PubkeyToAddress(privateKey.PublicKey)

mockState := &coremock.MockOnchainPaymentState{}
reservationLimit := uint64(1024)
paymentLimit := big.NewInt(512)
mockState.On("GetActiveReservationByAccount", mock.Anything, mock.MatchedBy(func(account string) bool {
mockState.On("GetActiveReservationByAccount", mock.Anything, mock.MatchedBy(func(account gethcommon.Address) bool {
return account == publicKey
})).Return(core.ActiveReservation{SymbolsPerSecond: reservationLimit, StartTimestamp: 0, EndTimestamp: math.MaxUint32, QuorumSplits: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}}, nil)
mockState.On("GetActiveReservationByAccount", mock.Anything, mock.Anything).Return(core.ActiveReservation{}, errors.New("reservation not found"))
})).Return(&core.ActiveReservation{SymbolsPerSecond: reservationLimit, StartTimestamp: 0, EndTimestamp: math.MaxUint32, QuorumSplits: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}}, nil)
mockState.On("GetActiveReservationByAccount", mock.Anything, mock.Anything).Return(&core.ActiveReservation{}, errors.New("reservation not found"))

mockState.On("GetOnDemandPaymentByAccount", mock.Anything, mock.MatchedBy(func(account string) bool {
mockState.On("GetOnDemandPaymentByAccount", mock.Anything, mock.MatchedBy(func(account gethcommon.Address) bool {
return account == publicKey
})).Return(core.OnDemandPayment{CumulativePayment: paymentLimit}, nil)
mockState.On("GetOnDemandPaymentByAccount", mock.Anything, mock.Anything).Return(core.OnDemandPayment{}, errors.New("payment not found"))
})).Return(&core.OnDemandPayment{CumulativePayment: paymentLimit}, nil)
mockState.On("GetOnDemandPaymentByAccount", mock.Anything, mock.Anything).Return(&core.OnDemandPayment{}, errors.New("payment not found"))
mockState.On("GetOnDemandQuorumNumbers", mock.Anything).Return([]uint8{0, 1}, nil)
mockState.On("GetGlobalSymbolsPerSecond", mock.Anything).Return(uint64(1024), nil)
mockState.On("GetPricePerSymbol", mock.Anything).Return(uint32(1), nil)
Expand Down

0 comments on commit e973ddf

Please sign in to comment.