This repository was archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Refactor block reward application and tracing #8490
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,17 @@ | |
| // You should have received a copy of the GNU General Public License | ||
| // along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! A module with types for declaring block rewards and a client interface for interacting with a | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure |
||
| //! block reward contract. | ||
|
|
||
| use ethabi; | ||
| use ethabi::ParamType; | ||
| use ethereum_types::{H160, Address, U256}; | ||
|
|
||
| use block::ExecutedBlock; | ||
| use error::Error; | ||
| use machine::EthereumMachine; | ||
| use machine::WithRewards; | ||
| use parity_machine::{Machine, WithBalances}; | ||
| use trace; | ||
| use super::SystemCall; | ||
|
|
||
| use_contract!(block_reward_contract, "BlockReward", "res/contracts/block_reward.json"); | ||
|
|
@@ -37,6 +41,8 @@ pub enum RewardKind { | |
| Uncle = 1, | ||
| /// Reward attributed to the author(s) of empty step(s) included in the block (AuthorityRound engine). | ||
| EmptyStep = 2, | ||
| /// Reward attributed by an external protocol (e.g. block reward contract). | ||
| External = 3, | ||
| } | ||
|
|
||
| impl From<RewardKind> for u16 { | ||
|
|
@@ -45,6 +51,17 @@ impl From<RewardKind> for u16 { | |
| } | ||
| } | ||
|
|
||
| impl Into<trace::RewardType> for RewardKind { | ||
| fn into(self) -> trace::RewardType { | ||
| match self { | ||
| RewardKind::Author => trace::RewardType::Block, | ||
| RewardKind::Uncle => trace::RewardType::Uncle, | ||
| RewardKind::EmptyStep => trace::RewardType::EmptyStep, | ||
| RewardKind::External => trace::RewardType::External, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A client for the block reward contract. | ||
| pub struct BlockRewardContract { | ||
| /// Address of the contract. | ||
|
|
@@ -112,14 +129,17 @@ impl BlockRewardContract { | |
|
|
||
| /// Applies the given block rewards, i.e. adds the given balance to each benefactors' address. | ||
| /// If tracing is enabled the operations are recorded. | ||
| pub fn apply_block_rewards(rewards: &[(Address, U256)], block: &mut ExecutedBlock, machine: &EthereumMachine) -> Result<(), Error> { | ||
| use parity_machine::WithBalances; | ||
|
|
||
| for &(ref author, ref block_reward) in rewards { | ||
| pub fn apply_block_rewards<M: Machine + WithBalances + WithRewards>( | ||
| rewards: &[(Address, RewardKind, U256)], | ||
| block: &mut M::LiveBlock, | ||
| machine: &M, | ||
| ) -> Result<(), M::Error> { | ||
| for &(ref author, _, ref block_reward) in rewards { | ||
| machine.add_balance(block, author, block_reward)?; | ||
| } | ||
|
|
||
| machine.note_rewards(block, &rewards, &[]) | ||
| let rewards: Vec<_> = rewards.into_iter().map(|&(a, k, r)| (a, k.into(), r)).collect(); | ||
| machine.note_rewards(block, &rewards) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,13 +141,19 @@ pub enum RewardType { | |
| Block, | ||
| /// Uncle | ||
| Uncle, | ||
| /// Empty step (AuthorityRound) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove this type from tracing (
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds ok for me. |
||
| EmptyStep, | ||
| /// A reward directly attributed by an external protocol (e.g. block reward contract) | ||
| External, | ||
| } | ||
|
|
||
| impl Encodable for RewardType { | ||
| fn rlp_append(&self, s: &mut RlpStream) { | ||
| let v = match *self { | ||
| RewardType::Block => 0u32, | ||
| RewardType::Uncle => 1, | ||
| RewardType::EmptyStep => 2, | ||
| RewardType::External => 3, | ||
| }; | ||
| Encodable::rlp_append(&v, s); | ||
| } | ||
|
|
@@ -158,6 +164,8 @@ impl Decodable for RewardType { | |
| rlp.as_val().and_then(|v| Ok(match v { | ||
| 0u32 => RewardType::Block, | ||
| 1 => RewardType::Uncle, | ||
| 2 => RewardType::EmptyStep, | ||
| 3 => RewardType::External, | ||
| _ => return Err(DecoderError::Custom("Invalid value of RewardType item")), | ||
| })) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately when using the block reward contract all rewards must be tagged as
External, there's no way to properly map to the benefactors list that was passed to the contract.