Skip to content

Commit

Permalink
validate staking ops
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaying-peng committed Apr 24, 2024
1 parent ae92cd6 commit 4865fd3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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
24 changes: 23 additions & 1 deletion pkg/processor/broadcast_storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (

var _ modules.BroadcastStorageHandler = (*BroadcastStorageHandler)(nil)

var stakingOpsTypes = []string{"stake", "unstake", "withdraw", "restake"}

Check failure on line 34 in pkg/processor/broadcast_storage_handler.go

View workflow job for this annotation

GitHub Actions / Lint

var `stakingOpsTypes` is unused (unused)

// BroadcastStorageHandler is invoked whenever a block is added
// or removed from block storage so that balance changes
// can be sent to other functions (ex: reconciler).
Expand Down Expand Up @@ -81,7 +83,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) {
return errMsg
}
}

// Validate destination memo if it's needed
Expand Down Expand Up @@ -114,6 +119,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

0 comments on commit 4865fd3

Please sign in to comment.