Skip to content

Commit

Permalink
allow fund address on the same time window if different auth methods …
Browse files Browse the repository at this point in the history
…used
  • Loading branch information
p4u committed Oct 26, 2023
1 parent 4d1ea7c commit 30227db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 6 additions & 6 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ func (f *faucet) authOpenHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext
if err != nil {
return err
}
if funded, t := f.storage.checkIsFundedAddress(addr); funded {
if funded, t := f.storage.checkIsFundedAddress(addr, "open"); funded {
errReason := fmt.Sprintf("address %s already funded, wait until %s", addr.Hex(), t)
return ctx.Send(new(HandlerResponse).SetError(errReason).MustMarshall(), CodeErrFlood)
}
data, err := f.prepareFaucetPackage(addr, "open")
if err != nil {
return err
}
if err := f.storage.addFundedAddress(addr); err != nil {
if err := f.storage.addFundedAddress(addr, "open"); err != nil {
return err
}
return ctx.Send(new(HandlerResponse).Set(data).MustMarshall(), apirest.HTTPstatusOK)
Expand All @@ -109,7 +109,7 @@ func (f *faucet) authOAuthHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContex
if err != nil {
return err
}
if funded, t := f.storage.checkIsFundedAddress(addr); funded {
if funded, t := f.storage.checkIsFundedAddress(addr, "oauth"); funded {
errReason := fmt.Sprintf("address %s already funded, wait until %s", addr.Hex(), t)
return ctx.Send(new(HandlerResponse).SetError(errReason).MustMarshall(), CodeErrFlood)
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func (f *faucet) authOAuthHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContex
if err != nil {
return err
}
if err := f.storage.addFundedAddress(addr); err != nil {
if err := f.storage.addFundedAddress(addr, "oauth"); err != nil {
return err
}

Expand Down Expand Up @@ -199,7 +199,7 @@ func (f *faucet) authAragonDaoHandler(msg *apirest.APIdata, ctx *httprouter.HTTP
}

// Check if the address is already funded
if funded, t := f.storage.checkIsFundedAddress(addr); funded {
if funded, t := f.storage.checkIsFundedAddress(addr, "aragon"); funded {
errReason := fmt.Sprintf("address %s already funded, wait until %s", addr.Hex(), t)
return ctx.Send(new(HandlerResponse).SetError(errReason).MustMarshall(), CodeErrFlood)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (f *faucet) authAragonDaoHandler(msg *apirest.APIdata, ctx *httprouter.HTTP
return ctx.Send(new(HandlerResponse).SetError(err.Error()).MustMarshall(), CodeErrInternalError)
}

if err := f.storage.addFundedAddress(addr); err != nil {
if err := f.storage.addFundedAddress(addr, "aragon"); err != nil {
return ctx.Send(new(HandlerResponse).SetError(err.Error()).MustMarshall(), CodeErrInternalError)
}

Expand Down
10 changes: 6 additions & 4 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ func newStorage(dbType string, dataDir string, waitPeriod time.Duration) (*stora

// addFundedAddress adds the given address to the funded addresses list, with the current time
// as the wait period end time.
func (st *storage) addFundedAddress(addr common.Address) error {
func (st *storage) addFundedAddress(addr common.Address, authType string) error {
tx := st.kv.WriteTx()
defer tx.Discard()
key := append([]byte(fundedAddressPrefix), append(addr.Bytes(), []byte(authType)...)...)
wp := uint64(time.Now().Unix()) + st.waitPeriodSeconds
wpBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(wpBytes, wp)
if err := tx.Set(append([]byte(fundedAddressPrefix), addr.Bytes()...), wpBytes); err != nil {
if err := tx.Set(key, wpBytes); err != nil {
log.Error(err)
}
return tx.Commit()
Expand All @@ -56,8 +57,9 @@ func (st *storage) addFundedAddress(addr common.Address) error {
// checkIsFundedAddress checks if the given address is funded and returns true if it is, within
// the wait period time window. Otherwise, it returns false.
// The second return value is the wait period end time, if the address is funded.
func (st *storage) checkIsFundedAddress(addr common.Address) (bool, time.Time) {
wpBytes, err := st.kv.Get(append([]byte(fundedAddressPrefix), addr.Bytes()...))
func (st *storage) checkIsFundedAddress(addr common.Address, authType string) (bool, time.Time) {
key := append([]byte(fundedAddressPrefix), append(addr.Bytes(), []byte(authType)...)...)
wpBytes, err := st.kv.Get(key)
if err != nil {
return false, time.Time{}
}
Expand Down

0 comments on commit 30227db

Please sign in to comment.