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

validate staking ops #415

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
rosetta-cli
bin/
/.vscode
/cli-data
22 changes: 21 additions & 1 deletion pkg/processor/broadcast_storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func (h *BroadcastStorageHandler) TransactionConfirmed(
}

if err := h.parser.ExpectedOperations(intent, observed, false, true); err != nil {
return fmt.Errorf("confirmed transaction did not match intent: %w", err)
errMsg := fmt.Errorf("confirmed transaction did not match intent: %w", err)
if !isValidStakingOperation(intent, intentMetadata) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what if confirmed transaction did not match intent but staking operation is valid, then we won't catch the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code is used for check:construction only, we never full tested staking txns in check:construction.
Staking operation looks like this:

  "network_identifier": {
    "blockchain": "abc",
    "network": "testnet"
  },
  "operations": [
    {
      "operation_identifier": {
        "index": 0
      },
      "type": "stake",
      "status": "",
      "account": {
        "address": "dydx12c0xh69avh8xd0z76as9as4mcy0cfncukmmd7z",
        "metadata": {}
      },
      "amount": {
        "value": "5000000",
        "currency": {
          "symbol": "COSMOSDYDX",
          "decimals": 18
        }
      }
    }
  ],
  "suggested_fee_multiplier": 2,
  "metadata": {
    "gas_adjustment": 0.5,
    "validator_src_address": "dydxvaloper10lzv79d96l7jh07z76ry6cnn6ftnnl8fgketzu",
    "validator_dst_address": ""
  }
}

The amount in the preprocess API is a positive number, but our block API interprets staking as two operations; the first one is negative. Hence we get an intent mismatch.

return errMsg
}
}

// Validate destination memo if it's needed
Expand Down Expand Up @@ -114,6 +117,23 @@ func (h *BroadcastStorageHandler) TransactionConfirmed(
return nil
}

func isValidStakingOperation(intent []*types.Operation, metadata map[string]interface{}) bool {
stakingOpsTypes := map[string]bool{
"stake": true,
"unstake": true,
"withdraw": true,
"restake": true,
}

if _, found := metadata["validator_src_address"]; found {
if len(intent) == 1 {
_, found := stakingOpsTypes[intent[0].Type]
return found
}
}
return false
}

// TransactionStale is called when a transaction has not yet been
// seen on-chain and is considered stale. This occurs when
// current block height - last broadcast > staleDepth.
Expand Down
Loading