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

ADR 001: coin cross-chain transfer source tracing #6662

Merged
merged 68 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
0833c55
adr: coin cross-chain transfer source tracing
fedekunze Jul 9, 2020
6bca8df
update pros and cons
fedekunze Jul 9, 2020
13cf126
update spec README
fedekunze Jul 9, 2020
ca1de7e
Merge branch 'master' into adr-coin-source-tracing
fedekunze Jul 15, 2020
3a9dc3e
Update docs/architecture/adr-001-coin-source-tracing.md
fedekunze Jul 21, 2020
5532428
Apply suggestions from code review
fedekunze Jul 21, 2020
82a30db
Update docs/architecture/adr-001-coin-source-tracing.md
fedekunze Jul 21, 2020
5abb1ed
Merge branch 'master' of github.com:cosmos/cosmos-sdk into adr-coin-s…
fedekunze Jul 22, 2020
a4fdab9
Merge branch 'master' of github.com:cosmos/cosmos-sdk into adr-coin-s…
fedekunze Jul 22, 2020
5ba6b58
address comments from review
fedekunze Jul 22, 2020
2f69f4d
update ADR with Send/Recv logic
fedekunze Jul 22, 2020
0dab556
Merge branch 'master' into adr-coin-source-tracing
fedekunze Jul 22, 2020
7ebf21b
final touches
fedekunze Jul 22, 2020
56d2b11
Merge branch 'adr-coin-source-tracing' of github.com:cosmos/cosmos-sd…
fedekunze Jul 22, 2020
3ff4bb9
Merge branch 'master' of github.com:cosmos/cosmos-sdk into adr-coin-s…
fedekunze Jul 22, 2020
fbaf655
Apply suggestions from code review
fedekunze Jul 22, 2020
1954510
address comments from review
fedekunze Jul 22, 2020
95199d5
address @aaronc review comments
fedekunze Jul 22, 2020
38d99eb
Merge branch 'master' into adr-coin-source-tracing
cwgoes Jul 23, 2020
d63f0c9
Apply suggestions from code review
fedekunze Jul 23, 2020
628ec72
use SplitN
fedekunze Jul 23, 2020
33ba62e
custom denom validation reference
fedekunze Jul 23, 2020
80d76b0
Merge branch 'master' into adr-coin-source-tracing
alexanderbez Jul 23, 2020
8591fc3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into adr-coin-s…
fedekunze Jul 28, 2020
1e8f02f
address some comments from review
fedekunze Jul 28, 2020
ae34c2c
more updates based on Colin's review
fedekunze Jul 28, 2020
4da7e85
final draft with changes to relay.go
fedekunze Jul 28, 2020
18bc6ec
Merge branch 'master' into adr-coin-source-tracing
fedekunze Jul 28, 2020
1560d6d
undo proto changes
fedekunze Jul 28, 2020
ed677ec
Merge branch 'adr-coin-source-tracing' of github.com:cosmos/cosmos-sd…
fedekunze Jul 28, 2020
8e5c044
Merge branch 'master' into adr-coin-source-tracing
fedekunze Jul 28, 2020
6d3b25f
address @aaronc review comments
fedekunze Jul 28, 2020
152b2e1
Merge branch 'adr-coin-source-tracing' of github.com:cosmos/cosmos-sd…
fedekunze Jul 28, 2020
e9ccb3c
why do I keep updating the proto files?
fedekunze Jul 28, 2020
295d93c
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 29, 2020
f35a843
address @AdityaSripal comments
fedekunze Jul 29, 2020
54ee703
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 29, 2020
e66e0e3
address more comments
fedekunze Jul 30, 2020
dd564e8
Merge branch 'adr-coin-source-tracing' of github.com:cosmos/cosmos-sd…
fedekunze Jul 30, 2020
a7f99d0
typos
fedekunze Jul 30, 2020
52b621b
Merge branch 'master' into adr-coin-source-tracing
fedekunze Jul 30, 2020
ae3b4b3
final ammendments
fedekunze Jul 30, 2020
b73908d
Merge branch 'adr-coin-source-tracing' of github.com:cosmos/cosmos-sd…
fedekunze Jul 30, 2020
6fd5c19
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 30, 2020
810d517
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 30, 2020
a18409e
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 30, 2020
cdc1d66
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 30, 2020
88994bb
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 30, 2020
d3de242
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 30, 2020
f886641
minor fix
fedekunze Jul 30, 2020
e8d754f
Merge branch 'adr-coin-source-tracing' of github.com:cosmos/cosmos-sd…
fedekunze Jul 30, 2020
c205a8d
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 30, 2020
877d67f
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 30, 2020
cadeacf
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 31, 2020
3a69288
Merge branch 'master' of github.com:cosmos/cosmos-sdk into adr-coin-s…
fedekunze Jul 31, 2020
a71be5c
Merge branch 'adr-coin-source-tracing' of github.com:cosmos/cosmos-sd…
fedekunze Jul 31, 2020
7b882c4
address more comments
fedekunze Jul 31, 2020
226eb92
update example
fedekunze Jul 31, 2020
c0cab23
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 31, 2020
ea3655c
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Jul 31, 2020
9a9b144
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Aug 1, 2020
75a3eff
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Aug 2, 2020
59363c1
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Aug 2, 2020
18e86c1
Update docs/architecture/adr-001-coin-source-tracing.md
fedekunze Aug 3, 2020
2dcfff2
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Aug 3, 2020
07b9ec0
Merge branch 'master' into adr-coin-source-tracing
mergify[bot] Aug 3, 2020
cb4907f
address more comments
fedekunze Aug 3, 2020
c01d0da
update prefix example
fedekunze Aug 3, 2020
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 docs/architecture/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Please add a entry below in your Pull Request for an ADR.

## ADR Table of Contents

- [ADR 001: Coin Source Tracing](./adr-001-coin-source-tracing.md)
- [ADR 002: SDK Documentation Structure](./adr-002-docs-structure.md)
- [ADR 003: Dynamic Capability Store](./adr-003-dynamic-capability-store.md)
- [ADR 004: Split Denomination Keys](./adr-004-split-denomination-keys.md)
Expand All @@ -50,3 +51,4 @@ Please add a entry below in your Pull Request for an ADR.
- [ADR 022: Custom baseapp panic handling](./adr-022-custom-panic-handling.md)
- [ADR 023: Protocol Buffer Naming and Versioning](./adr-023-protobuf-naming.md)
- [ADR 024: Coin Metadata](./adr-024-coin-metadata.md)
- [ADR 025: IBC Passive Channels](./adr-025-ibc-passive-channels.md)
247 changes: 247 additions & 0 deletions docs/architecture/adr-001-coin-source-tracing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
# ADR 001: Coin Source Tracing

## Changelog

- 2020-07-09: Initial Draft

## Status

Proposed

## Context

The specification for IBC cross-chain fungible token transfers
([ICS20](https://github.com/cosmos/ics/tree/master/spec/ics-020-fungible-token-transfer)), needs to
be aware of the origin any token denomination in order to relay a `Packet` which contains the sender
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
and recipient addressed in the
[`FungibleTokenPacketData`](https://github.com/cosmos/ics/tree/master/spec/ics-020-fungible-token-transfer#data-structures).

The Packet relay works as follows (per
[specification](https://github.com/cosmos/ics/tree/master/spec/ics-020-fungible-token-transfer#packet-relay)):

> - When acting as the **source** zone, the bridge module escrows an existing local asset
> denomination on the sending chain and mints vouchers on the receiving chain.
> - When acting as the **sink** zone, the bridge module burns local vouchers on the sending chains
> and unescrows the local asset denomination on the receiving chain.

In this context, when the sender of a cross-chain transfer is **not** the source where the tokens
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
were originated, the protocol prefixes the denomination with the port and channel identifiers in the
following format:

```typescript
prefix + denom = {destPortN}/{destChannelN}/.../{destPort0}/{destChannel0}/denom
```

Example: transfering `100 uatom` from port `HubPort` and channel `HubChannel` on the Hub to
Ethermint's port `EthermintPort` and channel `EthermintChannel` results in `100
EthermintPort/EthermintChannel/uatom`, where `EthermintPort/EthermintChannel/uatom` is the new
denomination on the receiving chain.

In the case those tokens are transfered back to the Hub (i.e the **source** chain), the prefix is
trimmed and the token denomination updated to the original one.

### Problem

The problem of adding additional information to the coin denomination is twofold:

1. The ever increasing length if tokens are transfered to zones other than the source:

If a token is transferred `n` times via IBC to a sink chain, the token denom will contain `n` pairs
of prefixes, as shown on the format example above. This poses a problem because, while port
and channel identifiers have a maximum length of 64 each, the SDK `Coin` type only accepts
denoms up to 64 characters. Thus, a single cross-chain token, which again, is composed by the port and channels identifiers plus the base denomination, can exceed the length validation for the SDK `Coins`.

This can result in undesired behaviours such as tokens not being able to be transferred to multiple
sink chains if the denomination exceeds the length or unexpected `panics` due to denomination
validation failing on the receiving chain.

2. The existence of special characters and uppercase letters on the denomination:

In the SDK every time a `Coin` is initialized through the constructor function `NewCoin`, a validation
of a coin's denom is performed according to a
[Regex](https://github.com/cosmos/cosmos-sdk/blob/a940214a4923a3bf9a9161cd14bd3072299cd0c9/types/coin.go#L583),
where only lowercase alphanumeric characters are accepted. While this is desirable for native denoms
to keep a clean UX, it presents a challenge for IBC as ports and channels might be randomly
generated with special carracters and uppercases as per the [ICS 024 - Host
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
Requirements](https://github.com/cosmos/ics/tree/master/spec/ics-024-host-requirements#paths-identifiers-separators)
specification.

## Decision

Instead of adding the identifiers on the coin denomination directly, the proposed solution hashes the denomination prefix in order to get a consistent lenght for all the cross-chain fungible tokens. The new format will be the following:
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

```golang
ibcDenom = "ibc/" + SHA256 hash of the trace identifiers prefix + "/" + base coin denomination
```

fedekunze marked this conversation as resolved.
Show resolved Hide resolved
### `x/ibc-transfer` Changes

In order to retreive the trace information from an IBC denomination, a lookup table needs to be added to the `ibc-transfer` module. These values need to also be persisted between upgrades, meaning that a new `[]Trace` `GenesisState` field state needs to be added to the module:

```golang
// GetDenom retreives the full identifiers trace from the store.
func (k Keeper) GetTrace(ctx Context, traceHash []byte) string {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyTrace(traceHash))
if len(bz) == 0 {
return ""
}
return string(bz)
}

// HasTrace checks if a the key with the given trace hash exists on the store.
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
func (k Keeper) HasTrace(ctx Context, traceHash []byte) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.KeyTrace(traceHash))
}

// SetTrace sets a new {trace hash -> trace} pair to the store.
func (k Keeper) SetTrace(ctx Context, traceHash []byte, trace string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyTrace(traceHash), []byte(trace))
}
```

When a fungible token is send to a sink chain, the trace information needs to be updated with the new port and channel identifiers:

```golang
// PrefixDenom adds the given port and channel identifiers prefix to the denomination and sets the
// new {trace hash -> trace} pair to the store.
func (k Keeper) PrefixDenom(ctx Context, portID, channelID, denom string) string {
// Get each component of the denom. The resulting slice will be:
//
// - [ "ibc", traceHash, baseDenom], if the denom is dirty (contains trace metadata).
// - [ baseDenom ], if the denom has never been sent from the origin chain.
denomSplit := strings.Split(denom, "/")

var (
baseDenom string
trace string
traceHash tmbytes.HexBytes
)

// check if the denomination is clean or if it contains the trace info
if denomSplit[0] == denom {
Copy link
Member

Choose a reason for hiding this comment

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

This should also check that len(denomSplit) == 1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it will always be according to the Split godoc. Do you want me to add it as a safety check?

Copy link
Member

Choose a reason for hiding this comment

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

No because I want to ensure that it is only one element and not more than that.

Otherwise a denomSplit atom/notatom will enter this conditional if the passed-in denom is atom

baseDenom = denom
trace = portID + "/" + channelID +"/"
} else {
Copy link
Member

Choose a reason for hiding this comment

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

check length is expected here as well

// concatenate if base denom already contains a separator
baseDenom = strings.Join(denomSplit[2], "/")
traceHash = tmbytes.HexBytes(denomSplit[1])
// Get the value from the map trace hash -> denom identifiers prefix
trace = k.GetTrace(ctx, traceHash)
Copy link
Contributor

Choose a reason for hiding this comment

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

how do we know the trace is in the lookup table, what if i transfer for the first time from zoneA -> zoneB -> zoneC -> Hub. How would the Hub know the trace if it simply receives ibc/tracehash/denom from zoneC? Would it be extracted from the coin metadata?

Copy link
Contributor

Choose a reason for hiding this comment

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

is it being set in OnRecvPacket?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now that I think of it you are right. The app won't be able to retrieve the trace from the hash if it's received the first time. I'd propose adding an optional extra field to theFungibleTokenPacketData that contains the Trace info for each coin in the Amount field.

Thoughts @cwgoes @AdityaSripal?

Copy link
Contributor

Choose a reason for hiding this comment

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

That seems alright. Alternatively, we could require that the Trace is always provided and always hashed (instead of looking it up).

Copy link
Member

Choose a reason for hiding this comment

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

I prefer Chris' approach personally. It would create a simpler implementation.

Otherwise the sending chain would have to "know" which traces the receiving chain has stored, so that it can sent the trace info along if the receiving chain doesn't have it.

We could do some sort of "resend with trace" logic based on whether the ack tells us if the receiving chain doesn't have the trace, but that seems unnecessarily complicated. Seems better to just always send unhashed trace in the packet data.

Copy link
Contributor

Choose a reason for hiding this comment

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

Has this been resolved?

Copy link
Member

Choose a reason for hiding this comment

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

Yea bump @fedekunze , I still think passing in full trace in packet is the way to go. Could you confirm a decision and put it in this PR, thanks!

// prefix the identifiers to create the new trace
trace = portID + "/" + channelID +"/" + trace + "/"
}

traceHash = tmbytes.HexBytes(tmhash.Sum(trace))

// set the value to the lookup table if not stored already
if !k.HasTrace(ctx, traceHash) {
k.SetTrace(ctx, traceHash)
}

denom = "ibc/"+ traceHash.String() "/" + baseDenom
return denom
}
```

The denomination also needs to be updated when token is received on the source chain:

```golang
// UnprefixDenom removes the first portID/channelID pair from a given denomination trace info and returns the
// denomination with the updated trace hash and the new trace info.
// An error is returned if the trace cannot be found on the store from the denom's trace hash.
func (k Keeper) UnprefixDenom(ctx Context, denom string) (denom, trace string, err error) {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
denomSplit := strings.Split(denom, "/")
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
if denomSplit[0] == denom {
return denom, fmt.Errorf("denomination %s doesn't contain a prefix", denom)
}

// concatenate if base denom already contains a separator
baseDenom = strings.Join(denomSplit[2], "/")
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
traceHash := tmbytes.HexBytes(denomSplit[1])
// Get the value from the map trace hash -> denom identifiers prefix
trace = k.GetTrace(ctx, traceHash)
if trace == "" {
return "", Wrapf(ErrTraceNotFound, "denom: %s", denom)
}

traceSplit := strings.Split(trace, "/")
if len(traceSplit) == 3 {
// the trace has only one portID/channelID pair
return baseDenom
}

// remove a single identifiers pair to create the new trace
trace = strings.Join(trace[2:], "/")
traceHash = tmbytes.HexBytes(tmhash.Sum(trace))

// set the value to the lookup table if not stored already
if !k.HasTrace(ctx, traceHash) {
k.SetTrace(ctx, traceHash)
}

denom = "ibc/"+ traceHash.String() + "/" + baseDenom
return denom
}
```

```golang
// GetTraceFromDenom returns the token source tracing info from the trace hash of the given denomination
func (k Keeper) GetTraceFromDenom(ctx Context, denom string) (string, error) {
denomSplit := strings.Split(denom, "/")
if denomSplit[0] == denom {
return "", nil
}

traceHash := tmbytes.HexBytes(denomSplit[1])
// Get the value from the map trace hash -> denom identifiers prefix
trace := k.GetTrace(ctx, traceHash)
if trace == "" {
return "", Wrapf(ErrTraceNotFound, "denom: %s", denom)
}

return trace
}

```

Additionally, the `SendTransfer`'s `createOutgoingPacket` call and the `OnRecvPacket` need to be updated to be retreive the trace info (using `GetTraceFromDenom`) prior to checking the correctness of the prefix.

### Coin Changes

The coin denomination validation will need to be updated to reflect these changes. In particular, the denomination validation
function will now accept slash separators (`"/"`) and will bump the maximum character length to 64.
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved

For the specific case of cross-chain fungible token transfers, an IBC denomination will contain at most:

```golang
maxLen = 4 + 32 + 1 + 16 // "ibc/" + SHA256 hash + "/" + base denom
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
maxLen = 53 // valid length as 53 < 64
```

### Positive

- Clearer separation of the source tracing behaviour of the token (transfer prefix) from the original
`Coin` denomination
- Consistent validation of `Coin` fields (i.e no special characters, fixed max length)
- Cleaner `Coin` and standard denominations for IBC
- No additional fields to SDK `Coin`

### Negative

- Store each set of tracing denomination identifiers on the `ibc-transfer` module store.

### Neutral

- Slight difference with the ICS20 spec
- Additional validation logic for IBC coins
- Additional genesis fields.
- Slightly increases the gas usage on cross-chain transfers due to access to the store. This should
be inter-block cached if transfers are frequent

## References

- [ICS 20 - Fungible token
transfer](https://github.com/cosmos/ics/tree/master/spec/ics-020-fungible-token-transfer)