Skip to content

Commit

Permalink
feat: added additional info for the autodeposit (#228)
Browse files Browse the repository at this point in the history
* feat: added additional info for the autodeposit

* fix: fixed data race

* fix: renamed start block to start window
  • Loading branch information
Mikelle authored Jul 11, 2024
1 parent b1c7986 commit b0a20a3
Show file tree
Hide file tree
Showing 9 changed files with 632 additions and 545 deletions.
926 changes: 482 additions & 444 deletions p2p/gen/go/bidderapi/v1/bidderapi.pb.go

Large diffs are not rendered by default.

54 changes: 12 additions & 42 deletions p2p/gen/go/bidderapi/v1/bidderapi.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 67 additions & 26 deletions p2p/gen/openapi/bidderapi/v1/bidderapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,11 @@ paths:
in: path
required: true
type: string
- name: windowNumber
description: Optional window number for querying deposit. If not specified, the current block number is used.
in: query
required: false
type: string
format: uint64
- name: blockNumber
description: Optional block number for querying deposit. If specified, calculate window based on this block number.
in: query
required: false
type: string
format: uint64
- name: body
in: body
required: true
schema:
$ref: '#/definitions/v1BidderAutoDepositBody'
/v1/bidder/auto_deposit_status:
get:
summary: AutoDepositStatus
Expand Down Expand Up @@ -98,10 +91,12 @@ paths:
schema:
$ref: '#/definitions/googlerpcStatus'
parameters:
- name: withdraw
in: query
required: false
type: boolean
- name: body
description: Request to cancel AutoDeposit.
in: body
required: true
schema:
$ref: '#/definitions/v1CancelAutoDepositRequest'
/v1/bidder/deposit/{amount}:
post:
summary: Deposit
Expand Down Expand Up @@ -191,15 +186,12 @@ paths:
schema:
$ref: '#/definitions/googlerpcStatus'
parameters:
- name: windowNumbers
description: Window numbers for withdrawing deposits.
in: query
- name: body
description: Withdraw deposit from the bidder registry.
in: body
required: true
type: array
items:
type: string
format: uint64
collectionFormat: multi
schema:
$ref: '#/definitions/v1WithdrawFromWindowsRequest'
definitions:
bidderapiv1AutoDeposit:
type: object
Expand All @@ -209,6 +201,14 @@ definitions:
windowNumber:
type: string
format: uint64
isCurrent:
type: boolean
startBlockNumber:
type: string
format: uint64
endBlockNumber:
type: string
format: uint64
bidderapiv1Bid:
type: object
example:
Expand Down Expand Up @@ -274,9 +274,9 @@ definitions:
type: object
example:
amount_per_window: "1000000000000000000"
start_block_number: "1"
start_window_number: "1"
properties:
startBlockNumber:
startWindowNumber:
type: string
format: uint64
amountPerWindow:
Expand Down Expand Up @@ -304,6 +304,29 @@ definitions:
type: boolean
description: AutoDeposit status from the bidder registry.
title: AutoDeposit status response
v1BidderAutoDepositBody:
type: object
example:
amount: "1000000000000000000"
window_number: 1
properties:
windowNumber:
type: string
format: uint64
description: Optional window number for querying deposit. If not specified, the current block number is used.
blockNumber:
type: string
format: uint64
description: Optional block number for querying deposit. If specified, calculate window based on this block number.
description: Deposit for bids to be issued by the bidder in wei.
title: Deposit request
v1CancelAutoDepositRequest:
type: object
properties:
withdraw:
type: boolean
description: Request to cancel AutoDeposit.
title: CancelAutoDeposit request
v1CancelAutoDepositResponse:
type: object
example:
Expand Down Expand Up @@ -380,6 +403,24 @@ definitions:
format: uint64
description: Get deposit for bidder in the bidder registry.
title: Deposit response
v1WithdrawFromWindowsRequest:
type: object
example:
window_numbers:
- 1
- 2
- 3
properties:
windowNumbers:
type: array
items:
type: string
format: uint64
description: Window numbers for withdrawing deposits.
description: Withdraw deposit from the bidder registry.
title: Withdraw from multiple windows request
required:
- windowNumbers
v1WithdrawFromWindowsResponse:
type: object
example:
Expand Down
2 changes: 1 addition & 1 deletion p2p/integrationtest/real-bidder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func main() {
logger.Error("failed to auto deposit", "err", err)
return
}
logger.Info("auto deposit", "amount", resp.AmountPerWindow, "window", resp.StartBlockNumber)
logger.Info("auto deposit", "amount", resp.AmountPerWindow, "window", resp.StartWindowNumber)
}

type blockWithTxns struct {
Expand Down
33 changes: 21 additions & 12 deletions p2p/pkg/autodepositor/autodepositor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"slices"
"sync"
"sync/atomic"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -27,16 +28,17 @@ type BlockTrackerContract interface {
}

type AutoDepositTracker struct {
startMu sync.Mutex
isWorking bool
eventMgr events.EventManager
deposits sync.Map
windowChan chan *blocktracker.BlocktrackerNewWindow
brContract BidderRegistryContract
btContract BlockTrackerContract
optsGetter OptsGetter
logger *slog.Logger
cancelFunc context.CancelFunc
startMu sync.Mutex
isWorking bool
eventMgr events.EventManager
deposits sync.Map
windowChan chan *blocktracker.BlocktrackerNewWindow
brContract BidderRegistryContract
btContract BlockTrackerContract
optsGetter OptsGetter
currentOracleWindow atomic.Value
logger *slog.Logger
cancelFunc context.CancelFunc
}

func New(
Expand Down Expand Up @@ -167,6 +169,7 @@ func (adt *AutoDepositTracker) startAutodeposit(egCtx context.Context, eg *errgr
case err := <-sub.Err():
return fmt.Errorf("error in autodeposit event subscription: %w", err)
case window := <-adt.windowChan:
adt.currentOracleWindow.Store(window.Window)
withdrawWindows := make([]*big.Int, 0)
adt.deposits.Range(func(key, value interface{}) bool {
if key.(uint64) < window.Window.Uint64() {
Expand Down Expand Up @@ -255,7 +258,7 @@ func (adt *AutoDepositTracker) IsWorking() bool {
return adt.isWorking
}

func (adt *AutoDepositTracker) GetStatus() (map[uint64]bool, bool) {
func (adt *AutoDepositTracker) GetStatus() (map[uint64]bool, bool, *big.Int) {
adt.startMu.Lock()
isWorking := adt.isWorking
adt.startMu.Unlock()
Expand All @@ -265,5 +268,11 @@ func (adt *AutoDepositTracker) GetStatus() (map[uint64]bool, bool) {
deposits[key.(uint64)] = value.(bool)
return true
})
return deposits, isWorking

var currentOracleWindow *big.Int
if val := adt.currentOracleWindow.Load(); val != nil {
currentOracleWindow = val.(*big.Int)
}

return deposits, isWorking, currentOracleWindow
}
2 changes: 1 addition & 1 deletion p2p/pkg/autodepositor/autodepositor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestAutoDepositTracker_Start(t *testing.T) {
t.Helper()

for {
depositsMap, status := adt.GetStatus()
depositsMap, status, _ := adt.GetStatus()
if status != working {
t.Fatalf("expected status to be %v, got %v", working, status)
}
Expand Down
Loading

0 comments on commit b0a20a3

Please sign in to comment.