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

PlantUML diagrams #8712

Merged
merged 54 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
31a1f74
Overview of keepers in object capability model (OCM)
Feb 9, 2021
2c4ba34
Updates to the spec, making clarifications
Feb 9, 2021
a4998f3
Create a sequence diagram of a (fresh) delegation
Feb 9, 2021
73c21a8
Misc notes, not yet decided where to put them
Feb 9, 2021
8bcf73a
Description of the shares abstraction in validators
hjorthjort Feb 10, 2021
b476faf
Model all keeper dependencies and move the UML file to docs
hjorthjort Feb 11, 2021
151a4a9
Move and rename delegation sequence diagram
hjorthjort Feb 11, 2021
11e1946
Move shares description
hjorthjort Feb 11, 2021
89b5a8a
Remove TODO
hjorthjort Feb 11, 2021
d221b6f
Diagram touch-ups
hjorthjort Feb 12, 2021
d151fa1
Add how consensus power is calculated
hjorthjort Feb 12, 2021
d791e12
remove temp file
hjorthjort Feb 12, 2021
f6f8161
Diagram improvements
hjorthjort Feb 12, 2021
92050cd
Describe slashing in more detail
hjorthjort Feb 12, 2021
f3d5e8f
Describe redelegation
hjorthjort Feb 16, 2021
e717f07
Describe unbonding
hjorthjort Feb 16, 2021
6f9c7a7
Delegation updates
hjorthjort Feb 17, 2021
c878b23
Delegation updates
hjorthjort Feb 17, 2021
c42bf87
Make a diagram describing overall transaction flow
hjorthjort Feb 17, 2021
ed32e4d
Add delegation flows for the events of tokens being bonded/unbonding/…
hjorthjort Feb 17, 2021
30c3933
Grammar fix
hjorthjort Feb 18, 2021
02dbc23
Diagram updates: distinguish alts, remove numbering.
hjorthjort Feb 25, 2021
ca7e6d6
Use groups instead of "func:" participants
hjorthjort Feb 25, 2021
b731d3c
Merge pull request #22 from hjorthjort/hjorthjort/documentation
hjorthjort Feb 25, 2021
61377cd
Merge remote-tracking branch 'upstream/master'
hjorthjort Feb 26, 2021
cbbeb3b
Remove unused keepers from dependency diagram
hjorthjort Feb 26, 2021
9e4f11e
Add title to unbonding diagram
hjorthjort Feb 26, 2021
c00da7a
Move keeper dependencies
hjorthjort Feb 26, 2021
fb07c03
small doc updates
hjorthjort Feb 26, 2021
fd1382e
remove numbers on sequence diagram
hjorthjort Feb 26, 2021
48f4d5a
!!!WIP EndBlock
hjorthjort Feb 26, 2021
04f6b48
Explain "Last"-prefix in storage
hjorthjort Feb 26, 2021
5762dad
Remove `panic` step (they are supposed to never happen)
hjorthjort Feb 26, 2021
ea6c9d5
EndBlock sequence diagram (with TODOs)
hjorthjort Feb 26, 2021
25ca4c3
Add TODO
hjorthjort Feb 26, 2021
ed646c0
More visible TODOs
hjorthjort Feb 26, 2021
6abbe9d
Remove numbering
hjorthjort Feb 26, 2021
22fc57d
Complete EndBlock
hjorthjort Mar 3, 2021
5ca1ae8
Remove numbering
hjorthjort Mar 3, 2021
27720b0
Remove TODOs and update title
hjorthjort Mar 3, 2021
7a1929b
add title back
hjorthjort Mar 3, 2021
cc6aedf
Merge branch 'master' into hjorthjort/sequence-diagrams
hjorthjort Mar 3, 2021
6a0e944
remove endblock seq-diagram
hjorthjort Mar 9, 2021
7b4ab55
Make power index update conditional on not being jailed
hjorthjort Mar 10, 2021
4dda170
update title
hjorthjort Mar 10, 2021
b02943d
Move files to /docs
hjorthjort Apr 5, 2021
8fb7a40
Install PlantUML and compile images to png and txt
hjorthjort Apr 5, 2021
1a0d992
Use transaction flow in documentation
hjorthjort Apr 5, 2021
656a278
Use staking UML in staking docs
hjorthjort Apr 5, 2021
47e330d
Clarify uml with inline doc
hjorthjort Apr 5, 2021
13a39d0
Merge branch 'master' into hjorthjort/sequence-diagrams
hjorthjort Apr 5, 2021
aa6950b
Add keeper deps diagram to docs
hjorthjort Apr 5, 2021
6c5650c
Only produce SVG images
hjorthjort Apr 5, 2021
44dd0eb
Merge branch 'master' into hjorthjort/sequence-diagrams
tac0turtle Apr 6, 2021
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: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
FROM golang:alpine AS build-env

# Install minimum necessary dependencies,
ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3
ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3 plantuml
Copy link
Member

Choose a reason for hiding this comment

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

@hjorthjort could you add a way to generate the plantuml? This is incorrect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marbar3778 Can you elaborate on "a way to generate the plantuml"? We need a plantuml binary to generate diagrams. By far the least hairy way would be to install it as a package from the standard package manager. Is it not available?

The other options are building it from source (hairier) or committing the official .jar directly, which would not stay up to date. The .jar option might be best if we can't install the package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another option would be to do something like this: https://github.com/miy4/docker-plantuml/blob/master/Dockerfile

Copy link
Member

Choose a reason for hiding this comment

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

It seems you were trying to generate the uml files for useage in docs. This was failing because there is no uml binary. Is it okay we just dont generated the uml files into something that can be rendered?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's also possible to just generate the files locally (by anyone who has plantuml) and we simply commit the files. Not having them in the docs at all seems like a big step back, they are there to clarify things and there seemed to be a lot of positive feedback on having them.

But it's up to you what you think is the most hygenic approach.

Copy link
Member

Choose a reason for hiding this comment

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

we can generate them locally and push them. Could you do this? I dont see us changing these files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay. can you update your open PR to remove the plantuml from the Dockerfile and remove (instead of commenting out) the line for generating them.

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be solved by #9124 (comment)

RUN apk add --no-cache $PACKAGES

# Set working directory for the build
Expand Down
4 changes: 4 additions & 0 deletions docs/building-modules/msg-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ This method takes care of marshaling the `res` parameter to protobuf and attachi

+++ https://github.com/cosmos/cosmos-sdk/blob/d55c1a26657a0af937fa2273b38dcfa1bb3cff9f/proto/cosmos/base/abci/v1beta1/abci.proto#L81-L95

This diagram shows a typical structure of an `Msg` Service, and how the message propagates through the module.

![](../uml/transaction_flow.svg)

## Legacy Amino `Msg`s

### `handler` type
Expand Down
4 changes: 4 additions & 0 deletions docs/core/ocap.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ gaia app.

+++ https://github.com/cosmos/cosmos-sdk/blob/v0.41.4/simapp/app.go#L249-L273

The following diagram shows the current dependencies between keepers.

![](../uml/keeper_dependencies.svg)

## Next {hide}

Learn about the [`runTx` middleware](./runtx_middleware.md) {hide}
4 changes: 3 additions & 1 deletion docs/pre.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ for D in ../x/*; do
fi
done

cat ../x/README.md | sed 's/\.\/x/\/modules/g' | sed 's/spec\/README.md//g' | sed 's/\.\.\/docs\/building-modules\/README\.md/\/building-modules\/intro\.html/g' > ./modules/README.md
cat ../x/README.md | sed 's/\.\/x/\/modules/g' | sed 's/spec\/README.md//g' | sed 's/\.\.\/docs\/building-modules\/README\.md/\/building-modules\/intro\.html/g' > ./modules/README.md

plantuml -tsvg uml/*.puml
50 changes: 50 additions & 0 deletions docs/uml/begin_redelegation_sequence.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@startuml
'https://plantuml.com/sequence-diagram

title: Redelegation

msgServer -> keeper : BeginRedelegation(delAddr, valSrcAddr, valDstAddr, sharesAmount)
participant "keeper (staking)" as keeper
keeper -> keeper : get number of sharew
note left: If the delegator has more shares than the total shares in the validator\n(due to rounding errors), then just withdraw the max number of shares.
keeper -> keeper : check the redelegation uses correct denom

alt valSrcAddr == valDstAddr
keeper --> msgServer : error
end
alt transitive redelegation
keeper --> msgServer : error
end
alt already has max redelegations
keeper --> msgServer : error
note left : this is the number of redelegations for a specific (del, valSrc, valDst) triple\ndefault : 7
end


keeper -> keeper : Unbond(del, valSrc) returns returnAmount
...
note left : See unbonding diagram

alt returnAmount is zero
keeper -> msgServer : error
end

keeper -> keeper : Delegate(del, returnAmount, status := valSrc.status, valDst, subtractAccount := false)
note left : See delegation diagram
...

alt validator is unbonded
keeper -> msgServer : current time
end

alt unbonding not complete, or just started
database store
keeper -> store : create redelegation object
keeper -> store : insert redelegation in queue, to be processed at the appropriate time
end

msgServer <-- keeper : completion time of the redelegation
msgServer -> msgServer : emit event: delegator, valSrc, valSrc,\nsharesAmount, completionTime

@enduml

93 changes: 93 additions & 0 deletions docs/uml/delegation_sequence.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
@startuml
'https://plantuml.com/sequence-diagram

title: Delegating (currently undelegated funds delegator)

participant "msgServer (staking)"
participant "keeper (staking)" as keeper
participant validator
participant keeper.bankKeeper
participant vestingAccount
participant ctx.EventManager

database store

"msgServer (staking)" -> keeper : Delegate(Context, DelegatorAddress, Amount, Validator, tokenSrc := Unbonded)

alt exchange rate is invalid (tokens in validator is 0)
keeper --> "msgServer (staking)" : error
end

alt perform a new delegation
keeper -> keeper : delegation := create delegation object
keeper -> keeper : BeforeDelegationCreated hook
note left: Calls IncrementValidatorPeriod (Used to calculate distribution) in keeper/validator.go
else delegation exists, more tokens being added
keeper -> keeper : BeforeDelegationModified hook
note left: withdraw current delegation rewards (and increment period)
end

alt delegating from an account (subtractTokens == true)
keeper -> keeper.bankKeeper : DelegateCoinsFromAccountToModule
group DelegateCoinsFromAccountToModule function
keeper.bankKeeper -> keeper.bankKeeper : DelegateCoinsFromAccountToModule
keeper.bankKeeper -> keeper.bankKeeper : DelegateCoins
group DelegateCoins function
keeper.bankKeeper --> keeper.bankKeeper : Check the delegator has enough balances of all tokens delegated
keeper.bankKeeper --> keeper.bankKeeper : Track delegation (register that it exists to keep track of it)
alt validator is currently bonded
keeper.bankKeeper --> store : Transfer tokens from delegator to BondedTokensPool.
else validator is currently unbonded or unbonding
keeper.bankKeeper --> store : Transfer tokens from delegator to NotBondedTokensPool.
end
group trackDelegation function
keeper.bankKeeper -> keeper.bankKeeper : trackDelegation
alt delegator is a vesting account
keeper.bankKeeper -> vestingAccount : keep track of this delegation
end
end
end
end
keeper <-- keeper.bankKeeper : nil (success)
else moving tokens between pools (subtractTokens == false)
alt delegator tokens are not bonded but validator is bonded
keeper -> keeper.bankKeeper : SendCoinsFromModuleToModule(notBondedPool, bondedPool, coins)
else delegator tokens are bonded but validator is not bonded
keeper -> keeper.bankKeeper : SendCoinsFromModuleToModule(bondedPool, notBondedPool, coins)
end
group SendCoins function
keeper.bankKeeper -> keeper.bankKeeper : SendCoins
keeper.bankKeeper -> ctx.EventManager : Emit TransferEvent(to, from, amount)
alt amount of spendable (balance - locked) coins too low
keeper <-- keeper.bankKeeper : error
end
keeper.bankKeeper -> store : subtract balance from sender
keeper.bankKeeper -> store : add balance to recipient
end
end

keeper -> validator : AddTokensFromDel
validator -> validator : calculate number of shares to issue
note left: If there are no shares (validator being created) then 1 token = 1 share.\nIf there are already shares, then\nadded shares = (added tokens amount) * (current validator shares) / (current validator tokens)

validator -> validator : add delegated tokens to validator
keeper <-- validator : validator, addedShares
keeper -> store : update validator state
keeper -> keeper: calculate new validator's power
note left : Number of tokens divided by PowerReduction (default: 1,000,000,000,000,000,000 = 10^18)
alt validator is not jailed
keeper -> store : update validator's power in power index
note left : the power index has entries shaped as 35 || power || address.\nThis makes the validators sorted by power, high to low.
end

keeper -> keeper : AfterDelegationModified hook
note left: Calls initializeDelegation\nStore the previous period\nCalculate the number of tokens from shares\n(shares the delegator has) * (tokens in delegation object)/(total tokens delegated to the validator)\nStore delegation starting info.
"msgServer (staking)" <-- keeper : newShares (ignored by Delegate function)


"msgServer (staking)" -> "msgServer (staking)" : Emit event: Delegation(ValidatorAddress)
"msgServer (staking)" -> "msgServer (staking)" : Emit event: Message(DelegatorAddress)
"msgServer (staking)" -> "msgServer (staking)" : telemetry(Amount, Denom)

@enduml

36 changes: 36 additions & 0 deletions docs/uml/keeper_dependencies.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@startuml
'https://plantuml.com/class-diagram

title: The dependencies between Keepers (Feb 2021)

abstract class Staking
abstract class Distribution
abstract class Slashing
abstract class Evidence
abstract class Bank
abstract class "Auth/Account" as Auth
abstract class Gov
abstract class Mint

Staking <|-- Mint
Bank <|-- Mint

Staking <|-- Gov
Bank <|-- Gov
Auth <|-- Gov

Auth <|-- Bank

Bank <|-- Distribution
Auth <|-- Distribution
Staking <|-- Distribution

Staking <|-- Evidence
Slashing <|-- Evidence

Staking <|-- Slashing

Auth <|-- Staking
Bank <|-- Staking

@enduml
22 changes: 22 additions & 0 deletions docs/uml/transaction_flow.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
What happens after a transaction is unmarshalled and is processed by the SDK?

@startuml
'https://plantuml.com/sequence-diagram

actor User
User -> baseApp : Transaction Type<Tx>
baseApp -> router : Route(ctx, msgRoute)
router --> baseApp : handler
baseApp -> handler: Msg<Tx>(Context, Msg(...))
handler -> msgServer : <Tx>(Context, Msg)
alt addresses invalid, denominations wrong, etc.
msgServer --> handler : error
end
msgServer -> keeper : perform action, update context
keeper --> msgServer : results, error code
msgServer -> Context.EventManager : Emit relevant events
msgServer -> msgServer : maybe wrap results in more structure
msgServer --> handler : result, error code
baseApp <-- handler : results, error code

@enduml
51 changes: 51 additions & 0 deletions docs/uml/unbond_sequence.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@startuml
'https://plantuml.com/sequence-diagram

title: Undelegate

msgServer -> keeper : Undelegate(delAddr, valAddr, tokenAmount)

keeper -> keeper : calculate number of shares the tokenAmount represents

alt wrong denom
msgServer <-- keeper : error
end

group Unbond(delAddr, valAddr, shares)
keeper -> keeper: BeforeDelegationSharesModified hook
alt no such delegation
keeper --> msgServer : error
end
alt not enough shares
keeper --> msgServer : error
end
alt delegator is the operator of the validator\nand validator is not already jailed\nand unbonding would put self-delegation under min threshold
keeper -> keeper : jail the validator, but proceed with unbonding
note left : Default min delegation threshold : 1 share
end

database store

alt complete unbonding, all shares removed
keeper -> store : remove delegation object
else there are still shares delegated (not a complete undbonding)
keeper -> store : update delegation object
keeper -> keeper : AfterDelegationModified hook
end

keeper -> store : update validator power index
keeper -> store : update validator information (including token amount)

alt validator status is "unbonded" and it has no more tokens
keeper -> store : delete the validator
note right : otherwise, do this in EndBlock once validator is unbonded
end
end

alt validator is bonded
keeper -> bankKeeper : send tokens from bonded pool to not bonded pool
end

msgServer -> msgServer : emit event : EventTypeUnbond(delAddr, valAddr, tokenAmount, completion time)

@enduml
6 changes: 6 additions & 0 deletions x/staking/spec/03_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ tracked in validator object in the `Validators` index.
It is possible to delegate to a jailed validator, the only difference being it
will not be added to the power index until it is unjailed.

![](docs/uml/delegation_sequence.svg)

## Msg/Undelegate

The `Msg/Undelegate` service message allows delegators to undelegate their tokens from
Expand Down Expand Up @@ -112,6 +114,8 @@ When this service message is processed the following actions occur:
- if there are no more `Shares` in the delegation, then the delegation object is removed from the store
- under this situation if the delegation is the validator's self-delegation then also jail the validator.

![](docs/uml/unbond_sequence.svg)

## Msg/BeginRedelegate

The redelegation command allows delegators to instantly switch validators. Once
Expand Down Expand Up @@ -146,3 +150,5 @@ When this service message is processed the following actions occur:
- Delegate the token worth to the destination validator, possibly moving tokens back to the bonded state.
- if there are no more `Shares` in the source delegation, then the source delegation object is removed from the store
- under this situation if the delegation is the validator's self-delegation then also jail the validator.

![](docs/uml/begin_redelegation_sequence.svg)