Skip to content

Commit

Permalink
refactor: test mocks and size/price calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Oct 11, 2024
1 parent f140830 commit d402849
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 71 deletions.
4 changes: 2 additions & 2 deletions core/auth/payment_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type EIP712Signer struct {
}

// NewEIP712Signer creates a new EIP712Signer instance
func NewEIP712Signer(chainID *big.Int, verifyingContract common.Address) *EIP712Signer {
return &EIP712Signer{
func NewEIP712Signer(chainID *big.Int, verifyingContract common.Address) EIP712Signer {
return EIP712Signer{
domain: apitypes.TypedDataDomain{
Name: "EigenDA",
Version: "1",
Expand Down
8 changes: 4 additions & 4 deletions core/auth/payment_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestConstructPaymentMetadata(t *testing.T) {
require.NoError(t, err)

header, err := auth.ConstructPaymentMetadata(
signer,
&signer,
0, // binIndex
1000, // cumulativePayment
1024, // dataLength
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestEIP712SignerWithDifferentKeys(t *testing.T) {
require.NoError(t, err)

header, err := auth.ConstructPaymentMetadata(
signer,
&signer,
0,
1000,
1024,
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestEIP712SignerWithModifiedHeader(t *testing.T) {
require.NoError(t, err)

header, err := auth.ConstructPaymentMetadata(
signer,
&signer,
0,
1000,
1024,
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestEIP712SignerWithDifferentChainID(t *testing.T) {
require.NoError(t, err)

header, err := auth.ConstructPaymentMetadata(
signer1,
&signer1,
0,
1000,
1024,
Expand Down
23 changes: 16 additions & 7 deletions core/meterer/meterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type Config struct {

// ChainReadTimeout is the timeout for reading payment state from chain
ChainReadTimeout time.Duration
// ChainID indicate the network in which meterer(payment) is handled
ChainID *big.Int
// VerifyingContract is the address of the PaymentVault contract that verifies signatures
VerifyingContract common.Address
}

// Meterer handles payment accounting across different accounts. Disperser API server receives requests from clients and each request contains a blob header
Expand All @@ -39,6 +43,7 @@ type Meterer struct {
// OffchainStore uses DynamoDB to track metering and used to validate requests
OffchainStore OffchainStore

signer auth.EIP712Signer
logger logging.Logger
}

Expand All @@ -49,12 +54,14 @@ func NewMeterer(
logger logging.Logger,
) (*Meterer, error) {
// TODO: create a separate thread to pull from the chain and update chain state

return &Meterer{
Config: config,

ChainState: paymentChainState,
OffchainStore: offchainStore,

signer: auth.NewEIP712Signer(config.ChainID, config.VerifyingContract),
logger: logger.With("component", "Meterer"),
}, nil
}
Expand All @@ -69,6 +76,7 @@ func (m *Meterer) MeterRequest(ctx context.Context, header core.PaymentMetadata)

// Validate against the payment method
if header.CumulativePayment == 0 {
fmt.Println("reservation: ", header.AccountID)
reservation, err := m.ChainState.GetActiveReservationByAccount(ctx, header.AccountID)
if err != nil {
return fmt.Errorf("failed to get active reservation by account: %w", err)
Expand All @@ -93,11 +101,7 @@ func (m *Meterer) MeterRequest(ctx context.Context, header core.PaymentMetadata)
// ValidateSignature checks if the signature is valid against all other fields in the header
// Assuming the signature is an eip712 signature
func (m *Meterer) ValidateSignature(ctx context.Context, header core.PaymentMetadata) error {
// Create the EIP712Signer
//TODO: update the chainID and verifyingContract
signer := auth.NewEIP712Signer(big.NewInt(17000), common.HexToAddress("0x1234000000000000000000000000000000000000"))

recoveredAddress, err := signer.RecoverSender(&header)
recoveredAddress, err := m.signer.RecoverSender(&header)
if err != nil {
return fmt.Errorf("failed to recover sender: %w", err)
}
Expand Down Expand Up @@ -251,9 +255,14 @@ func (m *Meterer) PaymentCharged(dataLength uint32) uint64 {
return uint64(m.SymbolsCharged(dataLength)) * uint64(m.PricePerSymbol)
}

// SymbolsCharged returns the chargeable data length for a given data length
// SymbolsCharged returns the number of symbols charged for a given data length
// being at least MinNumSymbols or the nearest rounded-up multiple of MinNumSymbols.
func (m *Meterer) SymbolsCharged(dataLength uint32) uint32 {
return uint32(max(dataLength, m.MinNumSymbols))
if dataLength <= m.MinNumSymbols {
return m.MinNumSymbols
}
// Round up to the nearest multiple of MinNumSymbols
return uint32(core.RoundUpDivide(uint(dataLength), uint(m.MinNumSymbols))) * m.MinNumSymbols
}

// ValidateBinIndex checks if the provided bin index is valid
Expand Down
Loading

0 comments on commit d402849

Please sign in to comment.