Open akolotov opened 6 years ago
Actually it is an epic for changes in both parts: solidity and rust. Since we agreed that reverse logic of contract is not the priority just now, I would leave this issue as is till the time we have enough resources to address it.
Changes on Solidity (and contract deployment) side already made: https://github.com/poanetwork/poa-bridge-contracts/tree/v2.
Background
The basis of the bridge operations is events EVM sends as part of contract execution. Every bridge instance is looking for new events and as soon as an event appears after transaction applying from a new block, the bridge performs an action.
For example, when an account sends ether to the brigde contract in the Home side of the bridge (the left side), Deposit events is produced. The bridge instance catches this event and creates transaction on the Foreign (right) side to invoke deposit() method of the bridge contract. This transaction is considered as a bridge signature under the fact that some user transfers asset to the Foreign network.
<a href="https://www.websequencediagrams.com/cgi-bin/cdraw?lz=dGl0bGUgYnJpZGdlIGRlcG9zaXQgKHNpbXBsZSBmbG93KQoKcGFydGljaXBhbnQgIkhvbWUgQWNjb3VudCIgYXMgSEEADhNzaWRlXG5Db250cmFjACEHQwA7DkIAbwVcbkluc3RhbmNlAEgFQjEAYA5Gb3JlaWduADwURkMKCkhBLT4rSEM6IGV0aGVyCm5vdGUgbGVmdCBvZiBIQwogICAgYXNzb3NpYXRlZAAKBXdpdGggdHggaWQKZW5kIG5vdGUKSEMtPi1CMTogRACBegYoc2VuZGVyLCB2YWx1ZSkAUQZvdmVyIEIxAEwIdW1lZCB0aGF0AGAFACsGID09IHJlY2lwaWVudABVCkIxLT5GQzoAglYIKAAaCQBaBywAgQoGKQo&s=rose" target="_blank"> <img src="https://www.websequencediagrams.com/cgi-bin/cdraw?lz=dGl0bGUgYnJpZGdlIGRlcG9zaXQgKHNpbXBsZSBmbG93KQoKcGFydGljaXBhbnQgIkhvbWUgQWNjb3VudCIgYXMgSEEADhNzaWRlXG5Db250cmFjACEHQwA7DkIAbwVcbkluc3RhbmNlAEgFQjEAYA5Gb3JlaWduADwURkMKCkhBLT4rSEM6IGV0aGVyCm5vdGUgbGVmdCBvZiBIQwogICAgYXNzb3NpYXRlZAAKBXdpdGggdHggaWQKZW5kIG5vdGUKSEMtPi1CMTogRACBegYoc2VuZGVyLCB2YWx1ZSkAUQZvdmVyIEIxAEwIdW1lZCB0aGF0AGAFACsGID09IHJlY2lwaWVudABVCkIxLT5GQzoAglYIKAAaCQBaBywAgQoGKQo&s=rose" alt="bridge deposit (flow with 3 bridges in action)" width="550" />
In order to achieve availability and reliability, there are more than one bridge instances (N). During these bridges configuration it is settled that just subset (M, M <= N) of them is enough to transit the state between two networks. So, if M bridges confirmed the same state transition operation (deposit), this state committed on the opposite side of the bridge:
<a href="https://www.websequencediagrams.com/cgi-bin/cdraw?lz=dGl0bGUgYnJpZGdlIGRlcG9zaXQgKGZsb3cgIHdpcmggMwAWB3MgaW4gYWN0aW9uKQoKcGFydGljaXBhbnQgIkhvbWUgQWNjb3VudCIgYXMgSEEADhNzaWRlXG5Db250cmFjACEHQwA7DkIAgQIFXG5JbnN0YW5jZSAjMQBLBUIxAAggMgAiBjIAMCAzAEoGMwCBMw5Gb3JlaWduAIEPFEZDCgpIQS0-K0hDOiBldGhlcgpub3RlIGxlZnQgb2YgSEMKICAgIGFzc29zaWF0ZWQACgV3aXRoIHR4IGlkCmVuZCBub3RlCkhDLT5CMTogRACCXwYoc2VuZGVyLCB2YWx1ZSkAGQYyAAIdLUIzACYZAIEQBW92ZXIgQjEsIEIyLCBCMwCBEgh1bWVkIHRoYXQAgSYFAHIGID09IHJlY2lwaWVudACBGwpCMS0-K0ZDOgCEAwgoABsJAIEiBywAgVEGKQpGQy0-LUZDOiAxc3Qgc2lnbmF0dXJlIHJlY2VpdmVkCkIyABkxMm5kADUVMwBoLEZDOiAzcgA0FQCBKgkic3RhdGVcbmNvbW1pdGVkIgo&s=rose" target="_blank"> <img src="https://www.websequencediagrams.com/cgi-bin/cdraw?lz=dGl0bGUgYnJpZGdlIGRlcG9zaXQgKGZsb3cgIHdpcmggMwAWB3MgaW4gYWN0aW9uKQoKcGFydGljaXBhbnQgIkhvbWUgQWNjb3VudCIgYXMgSEEADhNzaWRlXG5Db250cmFjACEHQwA7DkIAgQIFXG5JbnN0YW5jZSAjMQBLBUIxAAggMgAiBjIAMCAzAEoGMwCBMw5Gb3JlaWduAIEPFEZDCgpIQS0-K0hDOiBldGhlcgpub3RlIGxlZnQgb2YgSEMKICAgIGFzc29zaWF0ZWQACgV3aXRoIHR4IGlkCmVuZCBub3RlCkhDLT5CMTogRACCXwYoc2VuZGVyLCB2YWx1ZSkAGQYyAAIdLUIzACYZAIEQBW92ZXIgQjEsIEIyLCBCMwCBEgh1bWVkIHRoYXQAgSYFAHIGID09IHJlY2lwaWVudACBGwpCMS0-K0ZDOgCEAwgoABsJAIEiBywAgVEGKQpGQy0-LUZDOiAxc3Qgc2lnbmF0dXJlIHJlY2VpdmVkCkIyABkxMm5kADUVMwBoLEZDOiAzcgA0FQCBKgkic3RhdGVcbmNvbW1pdGVkIgo&s=rose" alt="bridge deposit (simple flow)" width="750" />
This is a basic scenario which is enough to transite state between two EVM-based networks. The maximum (for the third bridge) gas consumption of deposit operation is about 90000.
The use case considered by parity-bridge (https://github.com/paritytech/parity-bridge, commit ceaf22f) is to connect Ethereum Foundation (Home side) and a private PoA networks (Foreign side). So, it is assumed that we need to keep as cheap as possible transactions on the Home side whereas consumption of the gas on the Foreign side does not make much sense. This is due to the reason that most probably bridge instances are managed/supported by owners of the PoA network so almost infinite resources could be spent to maintain transactions from bridges within the PoA network.
That is why the original development of parity-bridge is focused on the reduce number of transactions and gas consumption for operations made on the Home side of the brige. In order to transit the state from the Home to the Foreign network three bridges will send three transactions. And only one bridge is being chosen to perform state transition from the Foreign side to Home.
It becomes posisble owing to the architecture when confirmations of bridges to transfer the state are collected on the Foreign side (remember that it costs nothing) and as soon as needed number of confirmation gathered the bridge which sent the latest confirmation is responsible for forwarding all signatures (which are part of the confirmations) to the Home side.
<a href="https://www.websequencediagrams.com/cgi-bin/cdraw?lz=dGl0bGUgcGFyaXR5LWJyaWRnZSB3aXRoZHJhdyBmbG93IChvcmlnaW5hbCkKCnBhcnRpY2lwYW50ICJIb21lIEFjY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EKCm5vdGUgcmlnaHQgb2YgRkEKICAgIGJhbACBXQV0cmFuc2ZlcgAQBXNpbWlsYXIgdG8gRVJDLTIwIHRva2VuCmVuZCBub3RlCkZBLT4rRkM6ICJleHRlcm5hbAA4CVxuKHJlY2lwaWVudCwgdmFsdWUpIgBsEEMAdwVhc3Nvc2lhdGVkAIEGBXdpdGggdHggaWQAXgtDLT5CMzogVwCDewcAShIAHQYxAAIhLUIyACodQjEAgUwHc3VibWl0U2lnbmF0dXJlKHNpZywgAIFDECwAgSMGAFQHRkM6IDFzdCBzaWcgcmVjZWl2ZWQKQjMAEz4ybmQAQg8yAGk5RkM6IDNyAEEPAIQLBWxlZgCDBwxzZW5kZXIgPT0gQkkgYWRkcmVzcwCEIwVoYXNoID09AAMFAINQEQCCBQkAgyoOMjogQ29sbGVjdGVkAIJHCXMoAF4GLABOBQCDNQoABCYtQjMAMSRCMgCBbgYibWVzc2FnZSgAZwVcbnMAg00JaGFzaCwgMXN0AAMTMm5kABkTM3JkKSIAgVAJbGlzAIZCBQBJCXMAhAwZAIMzBkhDOiAiAIlBCCgALBNcbgCETRgiCkhDLT5IQTogZXRoZXIK&s=rose" alt="bridge withdraw (flow, 3 bridges)" width="850" />
The procedure to confirm the state transition (submitSignature) is the most expensive one. It consumes about 270000 gas (if it was sent to provide a final signature) since requires lots of mathematical operations and use the contract storage extensively.
Here is the list gas consumption for all operations performed by bridge:
Relatively huge gas consumption is the only the reason why patity-bridge is not recommended to be used with Ethereum Foundation network in the right side: the meaningless from the state transaction confirmation operation will cost ~5 USD (exchange rate 1000 USD per 1 ether). But cases when a PoA network is on the Home side and the Ethereum Foundation network is on the Foreign side make sense since they allow to split transactions traffic between two networks which could have positive effect on network performance and transactions fees.
Proposal for costs optimization
From the analysis above it is evident that most costly transactions happens on the right side of the bridge. So, if Ethereum Foundation network is there so it will not be cost effective to send several transaction to confirm the deposit and several transaction to confirm the withdraw.
It means that all operations for confirmation should happens on the left side of the bridge and it equals of mirroring handling of confirmation for the current implementation:
deposit_confirm
.deposit_relay
.Withdraw
event is handled and every bridge instance sends the confirmation directly to the left side of the bridge. It iswithdraw_relay
.<a href="https://www.websequencediagrams.com/cgi-bin/cdraw?lz=dGl0bGUgUG9BIENUVCBicmlkZ2UgZGVwb3NpdCAoMwALB3MpCgpwYXJ0aWNpcGFudCAiSG9tZSBBY2NvdW50IiBhcyBIQQAOE3NpZGVcbkNvbnRyYWMAIQdDADsOQgBtBVxuSW5zdGFuY2UgIzFcbihCSSAjMSkAVAVCMQARIDIAKgcyACoHMgBCIDMAWwczAFsHMwCBTg5Gb3JlaWduAIEqFEYAgS8PACMIAIF-DEZBCgpIQS0-K0hDOiAiY29pbnMiCm5vdGUgcmlnaHQgb2YgSEMKICAgIGFzc29zaWF0ZWQACgV3aXRoIHR4IGlkCmVuZCBub3RlCkhDLT5CMzogRACDDAYoc2VuZGVyLCB2YWx1ZSkAGQYxAAIdLUIyADgHAC0RAIEQBW92ZXIgQjEsIEIyLCBCMwCBEQh1bWVkIHRoYXQAgSUFAHEGID09IHJlY2lwaWVudACBGgpCMQCBZgdzdWJtaXRTaWduYXR1cmUoc2lnLAAnCgCBLgcsAIFdBgCBFgdIQzogMXN0IHNpZyByZWNlaXZlZApCMwATPjJuZABCDzIAaTlIQzogM3IAQQ8AgzkVAIIaCkJJIGFkZHJlc3MAg2IFaGFzaCA9PQADBSgAgXYZAINlDjI6IENvbGxlY3RlZACCSAlzAIN2CWhhc2gAg3QKAAQmLUIzADEkQjIAgW8GIm1lc3NhZ2UoAGcFXG5zAINOCWhhc2gsIDFzdAADEzJuZAAZEzNyZCkiAIFQCWxpcwCGAQUASQlzAIQNGQCDNAZGQzogIgCJDQcoACsTXG4AhE0YIgpGQy0-LUZBOiB0b2tlbnMK&s=rose" target="_blank"> <img src="https://www.websequencediagrams.com/cgi-bin/cdraw?lz=dGl0bGUgUG9BIENUVCBicmlkZ2UgZGVwb3NpdCAoMwALB3MpCgpwYXJ0aWNpcGFudCAiSG9tZSBBY2NvdW50IiBhcyBIQQAOE3NpZGVcbkNvbnRyYWMAIQdDADsOQgBtBVxuSW5zdGFuY2UgIzFcbihCSSAjMSkAVAVCMQARIDIAKgcyACoHMgBCIDMAWwczAFsHMwCBTg5Gb3JlaWduAIEqFEYAgS8PACMIAIF-DEZBCgpIQS0-K0hDOiAiY29pbnMiCm5vdGUgcmlnaHQgb2YgSEMKICAgIGFzc29zaWF0ZWQACgV3aXRoIHR4IGlkCmVuZCBub3RlCkhDLT5CMzogRACDDAYoc2VuZGVyLCB2YWx1ZSkAGQYxAAIdLUIyADgHAC0RAIEQBW92ZXIgQjEsIEIyLCBCMwCBEQh1bWVkIHRoYXQAgSUFAHEGID09IHJlY2lwaWVudACBGgpCMQCBZgdzdWJtaXRTaWduYXR1cmUoc2lnLAAnCgCBLgcsAIFdBgCBFgdIQzogMXN0IHNpZyByZWNlaXZlZApCMwATPjJuZABCDzIAaTlIQzogM3IAQQ8AgzkVAIIaCkJJIGFkZHJlc3MAg2IFaGFzaCA9PQADBSgAgXYZAINlDjI6IENvbGxlY3RlZACCSAlzAIN2CWhhc2gAg3QKAAQmLUIzADEkQjIAgW8GIm1lc3NhZ2UoAGcFXG5zAINOCWhhc2gsIDFzdAADEzJuZAAZEzNyZCkiAIFQCWxpcwCGAQUASQlzAIQNGQCDNAZGQzogIgCJDQcoACsTXG4AhE0YIgpGQy0-LUZBOiB0b2tlbnMK&s=rose" alt="PoA CTT bridge deposit (3 bridges)" width="850" />
<a href="https://www.websequencediagrams.com/cgi-bin/cdraw?lz=dGl0bGUgUG9BIENUVCBicmlkZ2Ugd2l0aGRyYXcgKDMADAdzKQoKcGFydGljaXBhbnQgIkhvbWUgQWNjb3VudCIgYXMgSEEADhNzaWRlXG5Db250cmFjACEHQwA7DkIAbgVcbkluc3RhbmNlICMxAEsFQjEACCAyACIGMgAwIDMASgYzAIEzDkZvcmVpZ24AgQ8URgCBFA8AIwgAgWMMRkEKCm5vdGUgbGVmdCBvZiBGQQogICAgYmF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B0E6IGNvaW5zCg&s=rose" alt="PoA CTT bridge withdraw (3 bridges)" width="850" />
Changes required:
submitSignature()
,signature()
andmessage()
;deposit()
to work with list of signatures similarly towithdraw()
of the original HomeBridge contract;isMessageValueSufficientToCoverRelay()
,getWithdrawRelayCost()
andestimatedGasCostOfWithdraw
)isMessageValueSufficientToCoverRelay()
,getWithdrawRelayCost()
andestimatedGasCostOfWithdraw
must be removed.submitSignature()
,signature()
andmessage()
and corresponding maps to keep information about signatures similarly to the original ForeignBridge contract.bridge/src/bridge/withdraw_relay.rs
to sendwithdraw()
to the HomeBridge contract as soon asWithdraw
event appeared on the Foreign side. Every bridge instance should invoke this method simiarlydeposit()
of the original HomeBridge contract.bridge/src/bridge/deposit_relay.rs
to wait forCollectedSignatures
and forward deposit with the list of signatures to the ForeignBridge contract.bridge/src/bridge/withdraw_confirm.rs
tobridge/src/bridge/deposit_confirm.rs
and modify it as soDeposit
event is being waited from Home side andsubmitSignature()
is invoked by every bridge instance. The corresponding changes must be made inbridge/src/bridge/mod.rs
in order to run the confirmation module.bridge/src/database.rs
andbridge/src/bridge/mod.rs
to not trackchecked_withdraw_confirm
in the database and updatechecked_deposit_confirm
instead.bridge/src/config.rs
totransaction.withdraw_confirm.gas
,transaction.withdraw_confirm.gas_price
.transaction.deposit_confirm.gas
,transaction.withdraw_deposit.gas_price
.