Skip to content

Commit e973ddf

Browse files
committed
fix: tests
1 parent fe1f9f9 commit e973ddf

File tree

5 files changed

+42
-41
lines changed

5 files changed

+42
-41
lines changed

core/meterer/meterer_test.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/Layr-Labs/eigenda/core/mock"
1818
"github.com/Layr-Labs/eigenda/inabox/deploy"
1919
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
20+
gethcommon "github.com/ethereum/go-ethereum/common"
2021
"github.com/ethereum/go-ethereum/crypto"
2122
"github.com/ory/dockertest/v3"
2223
"github.com/stretchr/testify/assert"
@@ -30,12 +31,12 @@ var (
3031
dockertestResource *dockertest.Resource
3132
dynamoClient commondynamodb.Client
3233
clientConfig commonaws.ClientConfig
33-
accountID1 string
34-
account1Reservations core.ActiveReservation
35-
account1OnDemandPayments core.OnDemandPayment
36-
accountID2 string
37-
account2Reservations core.ActiveReservation
38-
account2OnDemandPayments core.OnDemandPayment
34+
accountID1 gethcommon.Address
35+
account1Reservations *core.ActiveReservation
36+
account1OnDemandPayments *core.OnDemandPayment
37+
accountID2 gethcommon.Address
38+
account2Reservations *core.ActiveReservation
39+
account2OnDemandPayments *core.OnDemandPayment
3940
mt *meterer.Meterer
4041

4142
deployLocalStack bool
@@ -123,12 +124,12 @@ func setup(_ *testing.M) {
123124
}
124125

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

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

179-
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.MatchedBy(func(account string) bool {
180+
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.MatchedBy(func(account gethcommon.Address) bool {
180181
return account == accountID1
181182
})).Return(account1Reservations, nil)
182-
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.MatchedBy(func(account string) bool {
183+
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.MatchedBy(func(account gethcommon.Address) bool {
183184
return account == accountID2
184185
})).Return(account2Reservations, nil)
185-
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.Anything).Return(core.ActiveReservation{}, fmt.Errorf("reservation not found"))
186+
paymentChainState.On("GetActiveReservationByAccount", testifymock.Anything, testifymock.Anything).Return(&core.ActiveReservation{}, fmt.Errorf("reservation not found"))
186187

187188
// test invalid quorom ID
188189
header := createPaymentHeader(1, 0, accountID1)
@@ -203,7 +204,7 @@ func TestMetererReservations(t *testing.T) {
203204
if err != nil {
204205
t.Fatalf("Failed to generate key: %v", err)
205206
}
206-
header = createPaymentHeader(1, 0, crypto.PubkeyToAddress(unregisteredUser.PublicKey).Hex())
207+
header = createPaymentHeader(1, 0, crypto.PubkeyToAddress(unregisteredUser.PublicKey))
207208
assert.NoError(t, err)
208209
err = mt.MeterRequest(ctx, *header, 1000, []uint8{0, 1, 2})
209210
assert.ErrorContains(t, err, "failed to get active reservation by account: reservation not found")
@@ -221,7 +222,7 @@ func TestMetererReservations(t *testing.T) {
221222
err = mt.MeterRequest(ctx, *header, symbolLength, quoromNumbers)
222223
assert.NoError(t, err)
223224
item, err := dynamoClient.GetItem(ctx, reservationTableName, commondynamodb.Key{
224-
"AccountID": &types.AttributeValueMemberS{Value: accountID2},
225+
"AccountID": &types.AttributeValueMemberS{Value: accountID2.Hex()},
225226
"BinIndex": &types.AttributeValueMemberN{Value: strconv.Itoa(int(binIndex))},
226227
})
227228
assert.NoError(t, err)
@@ -237,11 +238,11 @@ func TestMetererReservations(t *testing.T) {
237238
assert.NoError(t, err)
238239
overflowedBinIndex := binIndex + 2
239240
item, err := dynamoClient.GetItem(ctx, reservationTableName, commondynamodb.Key{
240-
"AccountID": &types.AttributeValueMemberS{Value: accountID2},
241+
"AccountID": &types.AttributeValueMemberS{Value: accountID2.Hex()},
241242
"BinIndex": &types.AttributeValueMemberN{Value: strconv.Itoa(int(overflowedBinIndex))},
242243
})
243244
assert.NoError(t, err)
244-
assert.Equal(t, accountID2, item["AccountID"].(*types.AttributeValueMemberS).Value)
245+
assert.Equal(t, accountID2.Hex(), item["AccountID"].(*types.AttributeValueMemberS).Value)
245246
assert.Equal(t, strconv.Itoa(int(overflowedBinIndex)), item["BinIndex"].(*types.AttributeValueMemberN).Value)
246247
// 25 rounded up to the nearest multiple of minNumSymbols - (200-21*9) = 16
247248
assert.Equal(t, strconv.Itoa(int(16)), item["BinUsage"].(*types.AttributeValueMemberN).Value)
@@ -260,21 +261,21 @@ func TestMetererOnDemand(t *testing.T) {
260261
paymentChainState.On("GetMinNumSymbols", testifymock.Anything).Return(uint32(3), nil)
261262
binIndex := uint32(0) // this field doesn't matter for on-demand payments wrt global rate limit
262263

263-
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.MatchedBy(func(account string) bool {
264+
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.MatchedBy(func(account gethcommon.Address) bool {
264265
return account == accountID1
265266
})).Return(account1OnDemandPayments, nil)
266-
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.MatchedBy(func(account string) bool {
267+
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.MatchedBy(func(account gethcommon.Address) bool {
267268
return account == accountID2
268269
})).Return(account2OnDemandPayments, nil)
269-
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.Anything).Return(core.OnDemandPayment{}, fmt.Errorf("payment not found"))
270+
paymentChainState.On("GetOnDemandPaymentByAccount", testifymock.Anything, testifymock.Anything).Return(&core.OnDemandPayment{}, fmt.Errorf("payment not found"))
270271
paymentChainState.On("GetOnDemandQuorumNumbers", testifymock.Anything).Return(quorumNumbers, nil)
271272

272273
// test unregistered account
273274
unregisteredUser, err := crypto.GenerateKey()
274275
if err != nil {
275276
t.Fatalf("Failed to generate key: %v", err)
276277
}
277-
header := createPaymentHeader(binIndex, 2, crypto.PubkeyToAddress(unregisteredUser.PublicKey).Hex())
278+
header := createPaymentHeader(binIndex, 2, crypto.PubkeyToAddress(unregisteredUser.PublicKey))
278279
assert.NoError(t, err)
279280
err = mt.MeterRequest(ctx, *header, 1000, quorumNumbers)
280281
assert.ErrorContains(t, err, "failed to get on-demand payment by account: payment not found")
@@ -291,7 +292,7 @@ func TestMetererOnDemand(t *testing.T) {
291292
// No rollback after meter request
292293
result, err := dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
293294
":account": &types.AttributeValueMemberS{
294-
Value: accountID1,
295+
Value: accountID1.Hex(),
295296
}})
296297
assert.NoError(t, err)
297298
assert.Equal(t, 1, len(result))
@@ -336,7 +337,7 @@ func TestMetererOnDemand(t *testing.T) {
336337
numPrevRecords := 12
337338
result, err = dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
338339
":account": &types.AttributeValueMemberS{
339-
Value: accountID2,
340+
Value: accountID2.Hex(),
340341
}})
341342
assert.NoError(t, err)
342343
assert.Equal(t, numPrevRecords, len(result))
@@ -347,7 +348,7 @@ func TestMetererOnDemand(t *testing.T) {
347348
// Correct rollback
348349
result, err = dynamoClient.Query(ctx, ondemandTableName, "AccountID = :account", commondynamodb.ExpressionValues{
349350
":account": &types.AttributeValueMemberS{
350-
Value: accountID2,
351+
Value: accountID2.Hex(),
351352
}})
352353
assert.NoError(t, err)
353354
assert.Equal(t, numPrevRecords, len(result))
@@ -464,9 +465,9 @@ func TestMeterer_symbolsCharged(t *testing.T) {
464465
}
465466
}
466467

467-
func createPaymentHeader(binIndex uint32, cumulativePayment uint64, accountID string) *core.PaymentMetadata {
468+
func createPaymentHeader(binIndex uint32, cumulativePayment uint64, accountID gethcommon.Address) *core.PaymentMetadata {
468469
return &core.PaymentMetadata{
469-
AccountID: accountID,
470+
AccountID: accountID.Hex(),
470471
BinIndex: binIndex,
471472
CumulativePayment: big.NewInt(int64(cumulativePayment)),
472473
}

core/meterer/onchain_state_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
)
1515

1616
var (
17-
dummyActiveReservation = core.ActiveReservation{
17+
dummyActiveReservation = &core.ActiveReservation{
1818
SymbolsPerSecond: 100,
1919
StartTimestamp: 1000,
2020
EndTimestamp: 2000,
2121
QuorumSplits: []byte{50, 50},
2222
}
23-
dummyOnDemandPayment = core.OnDemandPayment{
23+
dummyOnDemandPayment = &core.OnDemandPayment{
2424
CumulativePayment: big.NewInt(1000),
2525
}
2626
)

disperser/apiserver/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,10 +758,10 @@ func newTestServer(transactor core.Writer, testName string) *apiserver.Dispersal
758758
mockState.On("GetRequiredQuorumNumbers").Return([]uint8{0, 1}, nil)
759759
mockState.On("GetOnDemandQuorumNumbers").Return([]uint8{0, 1}, nil)
760760
mockState.On("GetReservationWindow").Return(uint32(1), nil)
761-
mockState.On("GetOnDemandPaymentByAccount", tmock.Anything, tmock.Anything).Return(core.OnDemandPayment{
761+
mockState.On("GetOnDemandPaymentByAccount", tmock.Anything, tmock.Anything).Return(&core.OnDemandPayment{
762762
CumulativePayment: big.NewInt(3000),
763763
}, nil)
764-
mockState.On("GetActiveReservationByAccount", tmock.Anything, tmock.Anything).Return(core.ActiveReservation{
764+
mockState.On("GetActiveReservationByAccount", tmock.Anything, tmock.Anything).Return(&core.ActiveReservation{
765765
SymbolsPerSecond: 2048,
766766
StartTimestamp: 0,
767767
EndTimestamp: math.MaxUint32,

disperser/apiserver/server_v2_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ func newTestServerV2(t *testing.T) *testComponents {
441441
mockState.On("GetMinNumSymbols", tmock.Anything).Return(uint32(3), nil)
442442

443443
now := uint64(time.Now().Unix())
444-
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)
445-
mockState.On("GetOnDemandPaymentByAccount", tmock.Anything, tmock.Anything).Return(core.OnDemandPayment{CumulativePayment: big.NewInt(3864)}, nil)
444+
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)
445+
mockState.On("GetOnDemandPaymentByAccount", tmock.Anything, tmock.Anything).Return(&core.OnDemandPayment{CumulativePayment: big.NewInt(3864)}, nil)
446446
mockState.On("GetOnDemandQuorumNumbers", tmock.Anything).Return([]uint8{0, 1}, nil)
447447

448448
if err := mockState.RefreshOnchainPaymentState(context.Background(), nil); err != nil {

test/integration_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,20 @@ func mustMakeDisperser(t *testing.T, cst core.IndexedChainState, store disperser
216216
if err != nil {
217217
panic("failed to convert hex to ECDSA")
218218
}
219-
publicKey := crypto.PubkeyToAddress(privateKey.PublicKey).Hex()
219+
publicKey := crypto.PubkeyToAddress(privateKey.PublicKey)
220220

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

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

0 commit comments

Comments
 (0)