以太坊中交易费处理逻辑

简介

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

  • 代码: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


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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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!

  6. 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.

  7. 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.

  8. 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!

  9. 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!

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

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

  12. 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.

  13. 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.

  14. 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.

  15. 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.

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

  17. 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!

  18. 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

  19. 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

  20. Ԝ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

  21. 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!!

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

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

  24. 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.

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

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

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

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

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

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

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

  32. 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

  33. 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

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

  35. pqf64cd8e32f5ac7553c150bd05d6f2252bb73f68dpq 的头像
    pqf64cd8e32f5ac7553c150bd05d6f2252bb73f68dpq

    pq118a9989815489c24b81b160782015890ed2085epq

回复 https://www.letmejerk.com 取消回复

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

About Me

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