Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CT-1050] DeliverTx state change reset for subaccount updates #2063

Merged
merged 51 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
bda8ec4
first
dydxwill Jul 30, 2024
af1eb90
test
dydxwill Jul 30, 2024
2c10c1d
test
dydxwill Aug 2, 2024
20e627f
revert
dydxwill Aug 2, 2024
0dd93fd
merge
dydxwill Aug 2, 2024
cd6572a
update proto
dydxwill Aug 2, 2024
dc2bc8e
lint
dydxwill Aug 2, 2024
3c7a63b
rm global cache
dydxwill Aug 5, 2024
af79f20
dev5
dydxwill Aug 5, 2024
96cb022
add branch
dydxwill Aug 5, 2024
57e54fa
dev2
dydxwill Aug 6, 2024
ce15619
lint
dydxwill Aug 6, 2024
e6fcbf5
revert ghwf
dydxwill Aug 6, 2024
1357239
fix
dydxwill Aug 6, 2024
2816d5f
merge
dydxwill Aug 6, 2024
7d08ac2
add indexer proto
dydxwill Aug 6, 2024
8d333b8
address cmts
dydxwill Aug 6, 2024
390b118
add cmts
dydxwill Aug 6, 2024
fd7abb0
Merge branch 'wl/sa_updates' of https://github.com/dydxprotocol/v4-ch…
dydxwill Aug 6, 2024
318e9ac
t
dydxwill Aug 6, 2024
71c0f08
latest
dydxwill Aug 6, 2024
f03735d
merge
dydxwill Aug 6, 2024
1ab0669
add su update
dydxwill Aug 6, 2024
079ffd5
add snapshotting logic
dydxwill Aug 6, 2024
3d5db4e
updates
dydxwill Aug 6, 2024
ee5e8f7
set Snapshot to true
dydxwill Aug 6, 2024
71cbe99
merge
dydxwill Aug 7, 2024
0a6bd3a
add back todo
dydxwill Aug 7, 2024
cd7472c
add gh wf
dydxwill Aug 7, 2024
ef9a0ec
add logs
dydxwill Aug 7, 2024
e4f254b
latest
dydxwill Aug 7, 2024
3c024b0
add log
dydxwill Aug 7, 2024
10e4512
fix
dydxwill Aug 7, 2024
d555b9d
rm logs
dydxwill Aug 7, 2024
49a8dcc
fix cmt
dydxwill Aug 7, 2024
8d97bda
fix event
dydxwill Aug 7, 2024
e6a5be0
Merge branch 'main' of https://github.com/dydxprotocol/v4-chain into …
dydxwill Aug 8, 2024
898074b
gh wf
dydxwill Aug 8, 2024
9ca9cd7
process operaitons
dydxwill Aug 8, 2024
e6666f0
merge
dydxwill Aug 8, 2024
d340113
fix merge conflict and metric emissions
jonfung-dydx Aug 8, 2024
0e94679
Revert "Bump grpc stream flag default values (#2051)"
jonfung-dydx Aug 9, 2024
c6a3974
properly clear out array
jonfung-dydx Aug 9, 2024
794ff71
tmp merge
dydxwill Aug 9, 2024
102a652
merge
dydxwill Aug 9, 2024
afff020
fix
dydxwill Aug 9, 2024
7da102e
Merge branch 'main' of https://github.com/dydxprotocol/v4-chain into …
dydxwill Aug 9, 2024
4ba411b
merge?
dydxwill Aug 12, 2024
d63bec6
fix
dydxwill Aug 12, 2024
518220f
Update protocol/x/clob/keeper/process_operations.go
dydxwill Aug 12, 2024
895bb98
address cmts
dydxwill Aug 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 1 addition & 180 deletions .github/workflows/protocol-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,12 @@ name: Protocol Build & Push Image to AWS ECR
on: # yamllint disable-line rule:truthy
push:
branches:
- 'wl/sa3'
- main
- 'release/protocol/v[0-9]+.[0-9]+.x' # e.g. release/protocol/v0.1.x
- 'release/protocol/v[0-9]+.x' # e.g. release/protocol/v1.x

jobs:
build-and-push-dev:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./protocol
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: '0' # without this, ignite fails.

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_VALIDATOR_DEV }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_VALIDATOR_DEV }}
aws-region: us-east-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, Tag, and Push the Image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: dev-validator
run: |
make localnet-build-amd64
commit_hash=$(git rev-parse --short=7 HEAD)
docker build \
--platform amd64 \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$commit_hash \
-f testing/testnet-dev/Dockerfile .
docker push $ECR_REGISTRY/$ECR_REPOSITORY --all-tags

build-and-push-dev2:
runs-on: ubuntu-latest
defaults:
Expand Down Expand Up @@ -79,147 +44,3 @@ jobs:
-t $ECR_REGISTRY/$ECR_REPOSITORY:$commit_hash \
-f testing/testnet-dev/Dockerfile .
docker push $ECR_REGISTRY/$ECR_REPOSITORY --all-tags

build-and-push-dev3:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./protocol
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: '0' # without this, ignite fails.

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_VALIDATOR_DEV3 }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_VALIDATOR_DEV3 }}
aws-region: us-east-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, Tag, and Push the Image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: dev3-validator
run: |
make localnet-build-amd64
commit_hash=$(git rev-parse --short=7 HEAD)
docker build \
--platform amd64 \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$commit_hash \
-f testing/testnet-dev/Dockerfile .
docker push $ECR_REGISTRY/$ECR_REPOSITORY --all-tags

build-and-push-dev4:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./protocol
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: '0' # without this, ignite fails.

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_VALIDATOR_DEV4 }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_VALIDATOR_DEV4 }}
aws-region: us-east-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, Tag, and Push the Image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: dev4-validator
run: |
make localnet-build-amd64
commit_hash=$(git rev-parse --short=7 HEAD)
docker build \
--platform amd64 \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$commit_hash \
-f testing/testnet-dev/Dockerfile .
docker push $ECR_REGISTRY/$ECR_REPOSITORY --all-tags

build-and-push-dev5:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./protocol
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: '0' # without this, ignite fails.

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_VALIDATOR_DEV5 }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_VALIDATOR_DEV5 }}
aws-region: us-east-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, Tag, and Push the Image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: dev5-validator
run: |
make localnet-build-amd64
commit_hash=$(git rev-parse --short=7 HEAD)
docker build \
--platform amd64 \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$commit_hash \
-f testing/testnet-dev/Dockerfile .
docker push $ECR_REGISTRY/$ECR_REPOSITORY --all-tags

build-and-push-staging:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./protocol
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: '0' # without this, ignite fails.

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_VALIDATOR_STAGING }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_VALIDATOR_STAGING }}
aws-region: us-east-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, Tag, and Push the Image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: staging-validator
run: |
make localnet-build-amd64
commit_hash=$(git rev-parse --short=7 HEAD)
docker build \
--platform amd64 \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$commit_hash \
-f testing/testnet-staging/Dockerfile .
docker push $ECR_REGISTRY/$ECR_REPOSITORY --all-tags
24 changes: 2 additions & 22 deletions protocol/x/clob/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,31 +264,11 @@ func (k Keeper) InitializeNewStreams(ctx sdk.Context) {
)
},
func(subaccountId satypes.SubaccountId) *satypes.StreamSubaccountUpdate {
subaccount := k.subaccountsKeeper.GetSubaccount(
subaccountUpdate := k.subaccountsKeeper.GetStreamSubaccountUpdate(
ctx,
subaccountId,
)
assetPositions := make([]*satypes.SubaccountAssetPosition, len(subaccount.AssetPositions))
for i, ap := range subaccount.AssetPositions {
assetPositions[i] = &satypes.SubaccountAssetPosition{
AssetId: ap.AssetId,
Quantums: ap.Quantums.BigInt().Uint64(),
}
}
perpetualPositions := make([]*satypes.SubaccountPerpetualPosition, len(subaccount.PerpetualPositions))
for i, pp := range subaccount.PerpetualPositions {
perpetualPositions[i] = &satypes.SubaccountPerpetualPosition{
PerpetualId: pp.PerpetualId,
Quantums: pp.Quantums.BigInt().Uint64(),
}
}

return &satypes.StreamSubaccountUpdate{
SubaccountId: &subaccountId,
UpdatedAssetPositions: assetPositions,
UpdatedPerpetualPositions: perpetualPositions,
Snapshot: true,
}
return &subaccountUpdate
},
lib.MustConvertIntegerToUint32(ctx.BlockHeight()),
ctx.ExecMode(),
Expand Down
34 changes: 34 additions & 0 deletions protocol/x/clob/keeper/process_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ func fetchOrdersInvolvedInOpQueue(
return orderIdSet
}

// fetchSubaccountIdsInvolvedInOpQueue fetches all SubaccountIds involved in an operations
// queue's matches and returns them as a set.
func fetchSubaccountIdsInvolvedInOpQueue(
operations []types.InternalOperation,
) (subaccountIdSet map[satypes.SubaccountId]struct{}) {
subaccountIdSet = make(map[satypes.SubaccountId]struct{})
for _, operation := range operations {
if clobMatch := operation.GetMatch(); clobMatch != nil {
subaccountIdSetForClobMatch := clobMatch.GetAllSubaccountIds()
subaccountIdSet = lib.MergeMaps(subaccountIdSet, subaccountIdSetForClobMatch)
}
}
return subaccountIdSet
}

// ProcessProposerOperations updates on-chain state given an []OperationRaw operations queue
// representing matches that occurred in the previous block. It performs validation on an operations
// queue. If all validation passes, the operations queue is written to state.
Expand All @@ -58,6 +73,10 @@ func (k Keeper) ProcessProposerOperations(
}

// If grpc streams are on, send absolute fill amounts from local + proposed opqueue to the grpc stream.
// Also send subaccount snapshots for impacted subaccounts.
dydxwill marked this conversation as resolved.
Show resolved Hide resolved
// An impacted subaccount is defined as:
// - A subaccount that was involved in any match in the local opqueue.
// Only matches generate subaccount updates.
// This must be sent out to account for checkState being discarded and deliverState being used.
if streamingManager := k.GetFullNodeStreamingManager(); streamingManager.Enabled() {
localValidatorOperationsQueue, _ := k.MemClob.GetOperationsToReplay(ctx)
Expand All @@ -75,6 +94,21 @@ func (k Keeper) ProcessProposerOperations(
allUpdates.Append(orderbookUpdate)
}
k.SendOrderbookUpdates(ctx, allUpdates)

// send subaccount snapshots
subaccountIdsFromProposed := fetchSubaccountIdsInvolvedInOpQueue(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super meganit, up to you: combine the two loops where we iterate through the opqueue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only sending local for now

operations,
)
subaccountIdsFromLocal := fetchSubaccountIdsInvolvedInOpQueue(
localValidatorOperationsQueue,
)
subaccountIdsToUpdate := lib.MergeMaps(subaccountIdsFromLocal, subaccountIdsFromProposed)
allSubaccountUpdates := make([]satypes.StreamSubaccountUpdate, 0)
for subaccountId := range subaccountIdsToUpdate {
subaccountUpdate := k.subaccountsKeeper.GetStreamSubaccountUpdate(ctx, subaccountId)
allSubaccountUpdates = append(allSubaccountUpdates, subaccountUpdate)
}
k.subaccountsKeeper.SendSubaccountUpdates(ctx, allSubaccountUpdates)
}

log.DebugLog(ctx, "Processing operations queue",
Expand Down
10 changes: 10 additions & 0 deletions protocol/x/clob/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type SubaccountsKeeper interface {
) (
val satypes.Subaccount,
)
GetStreamSubaccountUpdate(
ctx sdk.Context,
id satypes.SubaccountId,
) (
val satypes.StreamSubaccountUpdate,
)
GetAllSubaccount(
ctx sdk.Context,
) (
Expand Down Expand Up @@ -78,6 +84,10 @@ type SubaccountsKeeper interface {
quantums *big.Int,
perpetualId uint32,
) error
SendSubaccountUpdates(
ctx sdk.Context,
subaccountUpdates []satypes.StreamSubaccountUpdate,
)
}

type AssetsKeeper interface {
Expand Down
26 changes: 26 additions & 0 deletions protocol/x/clob/types/message_clob_match.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"

// NewClobMatchFromMatchOrders creates a `ClobMatch` from the provided `MatchOrders`.
func NewClobMatchFromMatchOrders(
msgMatchOrders *MatchOrders,
Expand Down Expand Up @@ -40,3 +42,27 @@ func (clobMatch *ClobMatch) GetAllOrderIds() (orderIds map[OrderId]struct{}) {
}
return orderIds
}

// GetAllSubaccountIds returns a set of subaccountIds involved in a ClobMatch.
func (clobMatch *ClobMatch) GetAllSubaccountIds() (subaccountIds map[satypes.SubaccountId]struct{}) {
subaccountIds = make(map[satypes.SubaccountId]struct{})
if matchOrders := clobMatch.GetMatchOrders(); matchOrders != nil {
subaccountIds[matchOrders.GetTakerOrderId().SubaccountId] = struct{}{}
for _, makerFill := range matchOrders.GetFills() {
subaccountIds[makerFill.GetMakerOrderId().SubaccountId] = struct{}{}
}
}
if matchOrders := clobMatch.GetMatchPerpetualLiquidation(); matchOrders != nil {
subaccountIds[matchOrders.GetLiquidated()] = struct{}{}
for _, makerFill := range matchOrders.GetFills() {
subaccountIds[makerFill.GetMakerOrderId().SubaccountId] = struct{}{}
}
}
if matchOrders := clobMatch.GetMatchPerpetualDeleveraging(); matchOrders != nil {
subaccountIds[matchOrders.GetLiquidated()] = struct{}{}
for _, makerFill := range matchOrders.GetFills() {
subaccountIds[makerFill.GetOffsettingSubaccountId()] = struct{}{}
}
}
return subaccountIds
}
28 changes: 28 additions & 0 deletions protocol/x/subaccounts/keeper/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,34 @@ func (k Keeper) GetSubaccount(
return val
}

func (k Keeper) GetStreamSubaccountUpdate(
ctx sdk.Context,
id types.SubaccountId,
) (val types.StreamSubaccountUpdate) {
subaccount := k.GetSubaccount(ctx, id)
assetPositions := make([]*types.SubaccountAssetPosition, len(subaccount.AssetPositions))
for i, ap := range subaccount.AssetPositions {
assetPositions[i] = &types.SubaccountAssetPosition{
AssetId: ap.AssetId,
Quantums: ap.Quantums.BigInt().Uint64(),
}
}
perpetualPositions := make([]*types.SubaccountPerpetualPosition, len(subaccount.PerpetualPositions))
for i, pp := range subaccount.PerpetualPositions {
perpetualPositions[i] = &types.SubaccountPerpetualPosition{
PerpetualId: pp.PerpetualId,
Quantums: pp.Quantums.BigInt().Uint64(),
}
}

return types.StreamSubaccountUpdate{
SubaccountId: &id,
UpdatedAssetPositions: assetPositions,
UpdatedPerpetualPositions: perpetualPositions,
Snapshot: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a snapshot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't it? We are getting the subaccount from state which should have all asset/perp positions?

}
}

// GetAllSubaccount returns all subaccount.
// For more performant searching and iteration, use `ForEachSubaccount`.
func (k Keeper) GetAllSubaccount(ctx sdk.Context) (list []types.Subaccount) {
Expand Down
4 changes: 4 additions & 0 deletions protocol/x/subaccounts/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type SubaccountsKeeper interface {
ctx sdk.Context,
id SubaccountId,
) (val Subaccount)
GetStreamSubaccountUpdate(
ctx sdk.Context,
id SubaccountId,
) (val StreamSubaccountUpdate)
LegacyGetNegativeTncSubaccountSeenAtBlock(ctx sdk.Context) (uint32, bool)
GetNegativeTncSubaccountSeenAtBlock(
ctx sdk.Context,
Expand Down
Loading