以太坊中交易费处理逻辑

简介

分析以太坊代码中交易费的处理逻辑。

  • 代码:github.com:ethereum/go-ethereum
  • 版本:1.14.6

概述

交易费采用先扣除,再返还剩余gas的方式:

  1. 交易执行前,先按照gasLimit,扣除用户的最大交易费。
  2. 交易执行过程中,统计gasRemaing以及gasUsed,此时并不扣除。
  3. 交易执行过程中,有些指令涉及退费,统计相关的refund,此时并不扣除。
  4. 交易执行后,返还用户剩余gas(gasRemaining+refund)。
  5. 支付矿工tip费用。

代码分析

核心逻辑位于交易执行的主流程state_transition.go文件的TransitionDb方法中。

1. 扣除最大gas费(gasLimit)

交易执行前,首先会按照用户的gasLimit,以及真实的gasPrice,扣除用户的最大费用

func (st *StateTransition) buyGas() error {
    //按照gasLimit计算最大费用
    mgval := new(big.Int).SetUint64(st.msg.GasLimit)
    mgval.Mul(mgval, st.msg.GasPrice)
    ...
    st.gasRemaining = st.msg.GasLimit    
    st.initialGas = st.msg.GasLimit    
    //从用户账户扣除最大费用
    mgvalU256, _ := uint256.FromBig(mgval)
    st.state.SubBalance(st.msg.From, mgvalU256, tracing.BalanceDecreaseGasBuy)
    return nil
 }

调用堆栈为:

func (st *StateTransition) buyGas()//state_transition.go
func (st *StateTransition) preCheck()//state_transition.go
func (st *StateTransition) TransitionDb()//state_transition.go
func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool)//state_transition.go
func ApplyTransactionWithEVM //state_processor.go
func ApplyTransaction //state_processor.go

2. 返回gasRemaining以及Refund

在交易执行完成后,计算剩下的gasRemaining,以及退费refund,返还用户。

其中gasRemaing和refund由具体的opcode决定,会在具体指令执行过程中记录。

func (st *StateTransition) refundGas(refundQuotient uint64) uint64 {
    // Apply refund counter, capped to a refund quotient   
    // 计算退费Refund 
    refund := st.gasUsed() / refundQuotient   
    if refund > st.state.GetRefund() {
       refund = st.state.GetRefund()
    }
    ...
    //把退费加在原本的gas剩余gasRemaining上
    st.gasRemaining += refund    // Return ETH for remaining gas, exchanged at the original rate.    remaining := uint256.NewInt(st.gasRemaining)
    remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice))
    //返还用户
    st.state.AddBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn)
}

调用堆栈为:

func (st *StateTransition) refundGas()//state_transition.go
func (st *StateTransition) TransitionDb()//state_transition.go
func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool)//state_transition.go
func ApplyTransactionWithEVM //state_processor.go
func ApplyTransaction //state_processor.go

3. 矿工coinbase账户增加交易费

支付矿工tip费用。

func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
    ...
    //统计本笔交易使用的gas, gasUsed
    fee := new(uint256.Int).SetUint64(st.gasUsed())
    fee.Mul(fee, effectiveTipU256)
    //给Coinbase账户增加交易费
    st.state.AddBalance(st.evm.Context.Coinbase, fee, tracing.BalanceIncreaseRewardTransactionFee)
}

调用堆栈为:

func (st *StateTransition) refundGas()//state_transition.go
func (st *StateTransition) TransitionDb()//state_transition.go
func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool)//state_transition.go
func ApplyTransactionWithEVM //state_processor.go
func ApplyTransaction //state_processor.go


《 “以太坊中交易费处理逻辑” 》 有 61 条评论

  1. Hey there! Someone in my Facebook group shared this website with us so I came to check it out.
    I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers!
    Terrific blog and brilliant design.

  2. I do agree with all of the concepts you’ve introduced for your
    post. They are really convincing and will definitely work.
    Nonetheless, the posts are too short for starters.
    May you please extend them a bit from next time? Thanks for the
    post.

  3. fantastic post, very informative. I’m wondering why the other specialists of this sector don’t notice this.
    You must continue your writing. I’m confident, you have a huge readers’ base already!

  4. For the reason that the admin of this web page is working, no doubt very rapidly it will be renowned, due to its
    feature contents.

  5. Hi there, I read your blog regularly. Your humoristic style is awesome, keep up
    the good work!

  6. Hello, i believe that i saw you visited my site so i got here to go back the choose?.I’m attempting to find things to enhance my website!I suppose its ok to make use of a few of your concepts!!

  7. Hi there! I know this is kinda off topic however , I’d figured I’d ask.
    Would you be interested in exchanging links
    or maybe guest writing a blog post or vice-versa? My site goes over a lot of the same topics as yours and I think we could greatly benefit
    from each other. If you happen to be interested feel free to send me
    an e-mail. I look forward to hearing from you! Excellent blog
    by the way!

  8. Wonderful goods from you, man. I’ve understand your stuff previous to
    and you’re just too great. I really like what you’ve acquired here, certainly like what
    you’re saying and the way in which you say it.

    You make it enjoyable and you still care for to
    keep it smart. I cant wait to read much more from you.
    This is really a wonderful website.

  9. Wow! Finally I got a webpage from where I be able to really take
    useful data regarding my study and knowledge.

  10. Hi, I do think this is a great web site. I stumbledupon it 😉 I will
    return once again since i have bookmarked it. Money and freedom is the best way to change,
    may you be rich and continue to guide others.

  11. Hi there, its nice post on the topic of media
    print, we all be aware of media is a wonderful source of information.

  12. I’m not that much of a internet reader to be honest but your blogs really nice, keep
    it up! I’ll go ahead and bookmark your site to come back down the road.
    Many thanks

  13. Fantastic goods from you, man. I have understand your stuff previous to and you’re just extremely wonderful.
    I actually like what you have acquired here, really like what you’re saying and the way in which you say it.
    You make it enjoyable and you still take care of to keep it wise.
    I can not wait to read much more from you.

    This is really a tremendous site.

  14. Thanks for the auspicious writeup. It actually was a entertainment account it.
    Glance advanced to more brought agreeable from you!

    By the way, how can we keep in touch?

  15. Highly energetic article, I liked that a lot. Will there be
    a part 2?

  16. What’s up friends, how is everything, and what you want
    to say concerning this paragraph, in my view its actually amazing in support of me.

  17. Hi, I want to subscribe for this website to get most recent updates, thus
    where can i do it please assist.

  18. Does your site have a contact page? I’m having a tough
    time locating it but, I’d like to send you an email.
    I’ve got some ideas for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing it develop over time.

  19. Nice blog here! Also your web site loads up very fast!
    What host are you using? Can I get your affiliate link
    to your host? I wish my site loaded up as quickly as yours lol

  20. What’s up i am kavin, its my first occasion to commenting anyplace, when i read this article i thought i could also create
    comment due to this sensible piece of writing.

  21. Every weekend i used to go to see this web site, as i wish for enjoyment, as this
    this web site conations really fastidious funny data
    too.

  22. I really like reading a post that will make men and women think.

    Also, thank you for allowing for me to comment!

  23. Great article! This is the type of info that should be shared across the web.
    Disgrace on Google for no longer positioning this put up upper!
    Come on over and consult with my site . Thanks
    =)

  24. Hi, i read your blog occasionally and i own a similar one and i was just
    wondering if you get a lot of spam comments? If so how do you reduce it, any plugin or anything you can advise?
    I get so much lately it’s driving me mad so any help is very much appreciated.

  25. Your mode of telling everything in this article is really pleasant,
    all can simply be aware of it, Thanks a lot.

  26. Thanks for every other great post. The place else may anybody get that kind
    of information in such an ideal manner of writing?
    I’ve a presentation next week, and I am at the search for such info.

  27. You can definitely see your enthusiasm in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always go after your heart.

  28. What you wrote made a great deal of sense. But, what about this?

    suppose you composed a catchier post title? I am not suggesting your information is not good., however what if you added a headline to maybe grab people’s attention? I mean 以太坊中交易费处理逻辑 –
    李无知 is a little vanilla. You could look at Yahoo’s home page and see
    how they create post titles to get people to click. You might try adding a
    video or a picture or two to grab readers interested about what you’ve got to say.
    In my opinion, it could bring your blog a little bit more interesting.

  29. Everything posted was actually very reasonable.
    However, consider this, suppose you were to write a killer post title?

    I mean, I don’t want to tell you how to run your blog, however
    what if you added a headline that makes people want more?
    I mean 以太坊中交易费处理逻辑 – 李无知 is kinda boring.
    You ought to look at Yahoo’s home page and watch how they create
    article titles to get people interested. You might add
    a related video or a related pic or two to grab readers excited about what you’ve written. In my opinion, it could make your posts a little livelier.

  30. Hi there, just became alert to your blog through Google,
    and found that it is really informative. I am gonna watch out for brussels.
    I’ll be grateful if you continue this in future. Many people will be benefited from your writing.
    Cheers!

  31. We are a group of volunteers and starting a brand new scheme in our community.
    Your web site offered us with valuable info to work on. You’ve done an impressive task and our entire community will be grateful to you.

  32. Heya just wanted to give you a quick heads up and let you know
    a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different internet browsers and both show
    the same results.

  33. You actually make it appear so easy together with your presentation however I in finding this matter to be really one thing that I think I might
    never understand. It sort of feels too complex and extremely broad for me.

    I’m taking a look ahead for your next post, I will attempt to get the
    grasp of it!

  34. You really make it seem so easy with your presentation but I find
    this matter to be really something which I think I would never understand.
    It seems too complicated and extremely broad for me.
    I’m looking forward for your next post, I’ll try to get the hang of it!

  35. Hello mates, pleasant paragraph and fastidious urging commented here, I am in fact
    enjoying by these.

  36. It’s an awesome article for all the online users; they will take benefit
    from it I am sure.

  37. Excellent article. Keep writing such kind of info on your site.
    Im really impressed by your blog.
    Hello there, You’ve performed a fantastic job. I will definitely digg
    it and for my part recommend to my friends.

    I am sure they will be benefited from this web site.

  38. Thanks for some other informative web site.
    Where else could I am getting that type of information written in such a perfect way?
    I have a project that I’m simply now working on, and I have been on the glance out for
    such info.

  39. I love what you guys tend to be up too. This kind of clever work and reporting!
    Keep up the very good works guys I’ve incorporated you guys to our blogroll.

  40. I’m gone to say to my little brother, that he should also pay a visit this blog
    on regular basis to get updated from newest gossip.

  41. Wow, that’s what I was seeking for, what a material!
    present here at this web site, thanks admin of this website.

  42. Hello there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My website looks weird when viewing from my apple iphone. I’m trying to find a theme or plugin that might be able to fix this problem. If you have any suggestions, please share. Appreciate it!

  43. I reaⅼly love уour blog.. Pleasant colors & theme. Ɗid you make this
    web site yoᥙrself? Pleaѕe reply ƅack as І’m lookіng to cгeate my own personal blog and wоuld love to find outt wһere yоu got
    thiѕ from or whaqt tһe theme іѕ caⅼled. Appreciate it!

    Review my webpage; omegle alternative

  44. Excellent weblog right һere! Additionally yߋur website loads ᥙp fast!
    Wһat host aгe yoᥙ the usage of? Can I am getting your affiliate hyperlink for your host?

    I wish my webite loaded up aѕ fast as yօurs lol

    Here iss mу site; https://www.letmejerk.com

  45. Ԝhat’ѕ up, еverything iѕ going fіne heгe аnd ofcourse evеry onne is sharing data,
    that’s truly excellent, keerp ᥙp writing.

    Also visit my web page https://www.fapjunk.com

  46. Hello, i think that i saw you visited my weblog thus i came to “return the favor”.I’m
    attempting to find things to improve my web site!I suppose its ok to use a few of your ideas!!

  47. Informative article, totally what I wanted to find.

  48. Frenchies are seriously the best! Their personalities are so unique and
    funny. Definitely a breed worth considering.

  49. Simply desire to say your article is as surprising. The clearness for your publish is just spectacular and i can think you are knowledgeable in this subject. Fine together with your permission let me to snatch your feed to stay up to date with imminent post. Thank you one million and please carry on the enjoyable work.

  50. Wow! I’ve been searching for Pitbull puppies for sale and this site has so many options!
    Totally worth checking out.

  51. Recently visited Isla Mujeres and it was breathtaking!
    Highly recommend snorkeling . Already planning my return trip!

  52. Just got back from Isla Mujeres and it was unreal!
    Highly recommend exploring Playa Norte. Can’t wait to go back!

  53. Recently visited Isla Mujeres and it was incredible!
    Highly recommend snorkeling . Already planning my return trip!

  54. This makes finding French Bulldogs for sale so much easier

  55. Adopting a French Bulldog is truly life-changing – thank you for this info!.

  56. You have observed very interesting points! ps nice website .

  57. Doxycost Hyclate Delayed Release Tablets, 200 mg are white, oval, scored tablets containing yellow pellets and debossed with D D on one face and plain on the other where to get cytotec without insurance injectable solutions and for certain liquid preparations for, e

  58. Inflammatory aging refers to a chronic and low- degree proinflammatory status with increasing age and is closely associated with multiple diseases, as POF 4 [url=https://fastpriligy.top/]buy priligy tablets[/url] He wasn t concerned about feeding Dylan kibble either and as long as we soaked it in water beforehand

  59. Cancer can be so cruel, and it sounds like your pup is really struggling amazon priligy

  60. pqf64cd8e32f5ac7553c150bd05d6f2252bb73f68dpq 的头像
    pqf64cd8e32f5ac7553c150bd05d6f2252bb73f68dpq

    pq118a9989815489c24b81b160782015890ed2085epq

回复 bandar qris 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

About Me

一位程序员,会弹吉他,喜欢读诗。
有一颗感恩的心,一位美丽的妻子,两个可爱的女儿
mail: geraldlee0825@gmail.com
github: https://github.com/lisuxiaoqi
medium: https://medium.com/@geraldlee0825