Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Fee Estimation Functionality #1486

Closed
wants to merge 12 commits into from
Closed

Conversation

AndrewZvvv
Copy link

Fee estimation algorithm rewrited according to new bitcoin algorithm, added read/write estimation state to a file, FeeTest changed according to new algorithm

@bokobza
Copy link
Contributor

bokobza commented Jun 7, 2018

😍

@dangershony
Copy link
Contributor

Brilliant will try to review once I have time.

@Aprogiena
Copy link
Contributor

This seems to be humongous PR, putting critical label on it because of that.

Quick question – is this just support of bitcoin core code or is it something else? After quickly looking into the code, it seems that it is very much bitcoin oriented and I'm not sure if or how much it is compatible with Stratis. Can we have any information about that please?

@AndrewZvvv
Copy link
Author

AndrewZvvv commented Jun 7, 2018

It's just port of bitcoin new algorithm of fee estimation calculation. Changes were made mainly in BlockPolicyEstimator and TxConfirmStats classes. Also FeeTest was changed according to new algorithm (ported from bitcoin also). Nothing more. Calling of this algorithm from other parts of a system will be added today.

@Aprogiena
Copy link
Contributor

In that case I think we will need some changes to make it more generic. I'm unable to say how big those changes will have to be. However I can see many constants that looks like very much bitcoin only. This could be a problem as we are working on solutions that must support different coins and must not be tightly coupled with the specific configuration of single coin. Therefore I think we would need to identify those points in this code and make them more generic.

@AndrewZvvv
Copy link
Author

Understood. I'll think about it and present some suggestions. Task description did not contain these details, so I just applied bitcoin algorithm as is.

@dangershony
Copy link
Contributor

I am not sure that are that much differences with bitcoin when it comes to fee estimation.
however in the case of stratis we don't have full blocks (yet)
Smart Contracts are using the same mempool so perhaps for them we will need some adjustments indeed as SC use a different fee model (worth case it will be a to be plug able component) @fassadlr @codingupastorm @rowandh

@AndrewZvvv
Copy link
Author

AndrewZvvv commented Jun 8, 2018

After more detailed review of algorithm it seems to me that all constants, which stated it estimator, are connected rather to algorithm itself, not to specifics of blockchain. So I can move these constants to separate class, which can be accessed from NodeSettings. I guess, this class will be the same for bitcoin and stratis network. The only question is how to fill these parameters. From file?

@AndrewZvvv
Copy link
Author

According to smart contracts - as far as I understood, we shouldn't add sc fee to data for fee estimation (buckets). How can we distinguish them from casual txs on mempool level?

///<summary>Historical estimates that are older than this aren't valid</summary>
private const int OldestEstimateHistory = 6 * 1008;

///<summary>Decay of .962 is a half-life of 18 blocks or about 3 hours</summary>
Copy link
Contributor

Choose a reason for hiding this comment

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

All these comments suggest that this is bitcoin specific, at least the comments themselves are bitcoin specific it it is a question whether the same number of blocks should be used for other block chains.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I see, I thought that horizons are rather counted in blocks, not in time intervals, so, e.g. medium horizon should be 48 blocks, no matter what is block time in concrete blockchain. I can suggest to make pair of settings for every horizon - for blocks and for minutes (or hours). E.g. MedBlocks and MedTime. If MedBlocks is filled - algorithm just gets its amount as block amount. If MedTime filled - algorithm gets time and divides it by block time for certain blockchain. It gives some flexibility in algorithm setup. Because honestly, I don't know what will be more correct - keep horizon duration the same as bitcoin has in blocks or in amount of time.

Copy link
Author

Choose a reason for hiding this comment

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

About horizon decays - I can make them flexible by calculating them from block time (e.g. for bitcoin "Short history: 0.962 decay (half-life 18 blocks, 3 hours)", then for another blockchain it should be another number to suit this 3 hours)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not familiar with the algorithm yet, I just wanted to show you one example of what seems to be specific for bitcoin. You shouldn't then ask yourself if the same number should be used for blockchain which has different block size or different target spacing or different maximum number of transaction in a block or different recommended number of confirmations to wait for or different whatever.

If you realise that you really want to use the same number, then you just need to modify the comment appropriately so that it is not bitcoin specific. Otherwise you're looking for some kind of formula that will get you the correct number on each blockchain based on its parameters.

@codingupastorm
Copy link
Collaborator

Don't worry about smart contracts. We can work around whatever happens here. We will have to sort transactions in the mempool separately, and we do add funds to the fee to account for gas usage, so modularity wherever possible is handy however.

@dangershony
Copy link
Contributor

@AndrewZvvv I will review this tomorrow, please merge latest changes.

We will probably have to try to make this interface based and injectable to the mempool, then when we use bitcoin we can inject otherwise perhaps use a default empty estimators for stratis.

@AndrewZvvv
Copy link
Author

AndrewZvvv commented Jun 15, 2018

@dangershony Committed my last changes, but there are still no changes about:

  • more "generic" algorithm - I'm going to move all these constants into separate class with settings, which will be hosted in NodeSettings. File will have default (bitcoin) settings. Some constants will be changed to formulas
  • tests for the new algorithm - in bitcoin code they are written in Python, I need more time to port them
  • extracting interface for block estimator and working with it through mempool only

In this commit there are:

  • fix some bags
  • remove some redunant code
  • fix one test
    I'm planning to add appropriate code this weekend

@noescape00
Copy link
Contributor

This one is quite huge. Is anyone going to review it? @dangershony @bokobza

If not I'll try to dive into it, let me know.

@dangershony
Copy link
Contributor

This is a huge code change indeed but its isolated to fee policy and show not change much apart for ht existing fee policy code that did not work anyway (and was not activated)

I will look in to this once I have time but feel free to dig in as well @noescape00

@Aprogiena
Copy link
Contributor

Aprogiena commented Jul 7, 2018

As this is huge, let's try to identify separated tasks for reviewers. The following is just the first attempt, feel free to identify more separated task as you become familiar with the code. At any point, anyone can just take a task from this list, do it, and the report that it has been done. You can also take a task and try to identify separated subtasks.

Task one – code style – go through whole PR and strictly enforce code style.

Task two – comments – go through whole PR and read all comments, mention where it is needed to improve the grammar, style, content, et cetera.

Task three – logs – go through whole PR and make sure good logs are implemented according to our style.

Task four – good separation of protocol specific constants – go through the whole PR and think about all the constants that are used in the algorithm. If you see anything that is specific to the bitcoin, make sure it is properly separated and that for Stratis network a proper value is used instead.

Task five – compare to bitcoin core – as this is supposed to be a port of the logic and algorithm from bitcoin core, we should compare the C# code with the bitcoin core code as the developer might have make a mistake while porting. We want to make sure the logic is equivalent to the original.

Task six – understand the logic – try to understand the logic of this algorithm. Then try to question whether it fits only bitcoin or if it is usable for arbitrary network that we want to support, focus on Stratis of course. If any changes are needed in order to support other networks, mention that.

Task seven – tests – Check all tests, check whether the coverage is sufficient, if the tests are ported from bitcoin check whether they match.

/// <summary>Map of bucket upper-bound to index into all vectors by bucket.</summary>
private SortedDictionary<double, int> bucketMap;

private object lockObject;
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the contract of this object? What is it supposed to protect?

@Aprogiena
Copy link
Contributor

Quite confused here, what is Algorithm014? It is the old one or the new one? And which one is used?

Also, how does the used one work with reorganisations? Is everything recalculated when reorganisation happens?

@Aprogiena
Copy link
Contributor

Definitely a lot of work is needed to make it is to comply with current code style and logging style policies as well as the commenting syntax and scope.

@noescape00
Copy link
Contributor

noescape00 commented Jul 20, 2018

@AndrewZvvv tests are failing, action is needed on @Aprogiena 's review, there are merge conflicts

@AndrewZvvv
Copy link
Author

Unfortunatelly, I have no free time now, I will try to fix issues in a week or two. Thank you for your time and for review.

@noescape00
Copy link
Contributor

This PR is greatly outdated so far.

@AndrewZvvv are you going to work on it?

Also if anyone would like to pick it up- please write here.

Otherwise I think it makes sense to close the PR

@dangershony
Copy link
Contributor

We will push it at some point we just need to get other "things" out of the way so happy to keep it here (if we close it we should create an issue thats tracking the close PR and reopen it at some point)

@noescape00
Copy link
Contributor

Created an issue for this: #2295
Closing this PR to clean the list of PRs a bit.
Please reopen when needed

@zeptin
Copy link
Contributor

zeptin commented Oct 26, 2019

Continuing this in #4040

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants