From 087c44d4bc22a48ef1304889aba79fef9945cbae Mon Sep 17 00:00:00 2001 From: Jaynti Kanani <jdkanani@gmail.com> Date: Fri, 13 Nov 2020 20:24:38 +0530 Subject: [PATCH] chg: add log changes for fee --- core/bor_fee_log.go | 115 +++++++++++++++++++++++++++++++++++++++ core/evm.go | 11 ++++ core/state_transition.go | 21 +++++++ 3 files changed, 147 insertions(+) create mode 100644 core/bor_fee_log.go diff --git a/core/bor_fee_log.go b/core/bor_fee_log.go new file mode 100644 index 000000000..c2851750d --- /dev/null +++ b/core/bor_fee_log.go @@ -0,0 +1,115 @@ +package core + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" +) + +var transferLogSig = common.HexToHash("0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4") +var transferFeeLogSig = common.HexToHash("0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63") +var feeAddress = common.HexToAddress("0x0000000000000000000000000000000000001010") +var bigZero = big.NewInt(0) + +// AddTransferLog adds transfer log into state +func AddTransferLog( + state vm.StateDB, + + sender, + recipient common.Address, + + amount, + input1, + input2, + output1, + output2 *big.Int, +) { + addTransferLog( + state, + transferLogSig, + + sender, + recipient, + + amount, + input1, + input2, + output1, + output2, + ) +} + +// AddFeeTransferLog adds transfer log into state +func AddFeeTransferLog( + state vm.StateDB, + + sender, + recipient common.Address, + + amount, + input1, + input2, + output1, + output2 *big.Int, +) { + addTransferLog( + state, + transferFeeLogSig, + + sender, + recipient, + + amount, + input1, + input2, + output1, + output2, + ) +} + +// addTransferLog adds transfer log into state +func addTransferLog( + state vm.StateDB, + eventSig common.Hash, + + sender, + recipient common.Address, + + amount, + input1, + input2, + output1, + output2 *big.Int, +) { + // ignore if amount is 0 + if amount.Cmp(bigZero) <= 0 { + return + } + + dataInputs := []*big.Int{ + amount, + input1, + input2, + output1, + output2, + } + + var data []byte + for _, v := range dataInputs { + data = append(data, common.LeftPadBytes(v.Bytes(), 32)...) + } + + // add transfer log + state.AddLog(&types.Log{ + Address: feeAddress, + Topics: []common.Hash{ + eventSig, + feeAddress.Hash(), + sender.Hash(), + recipient.Hash(), + }, + Data: data, + }) +} diff --git a/core/evm.go b/core/evm.go index 8abe5a047..1648d95d4 100644 --- a/core/evm.go +++ b/core/evm.go @@ -100,6 +100,17 @@ func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { // Transfer subtracts amount from sender and adds amount to recipient using the given Db func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { + // get inputs before + input1 := db.GetBalance(sender) + input2 := db.GetBalance(recipient) + db.SubBalance(sender, amount) db.AddBalance(recipient, amount) + + // get outputs after + output1 := db.GetBalance(sender) + output2 := db.GetBalance(recipient) + + // add transfer log + AddTransferLog(db, sender, recipient, amount, input1, input2, output1, output2) } diff --git a/core/state_transition.go b/core/state_transition.go index 9a9bf475e..5f683f27e 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -214,6 +214,9 @@ func (st *StateTransition) preCheck() error { // However if any consensus issue encountered, return the error directly with // nil evm execution result. func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { + input1 := st.state.GetBalance(st.msg.From()) + input2 := st.state.GetBalance(st.evm.Coinbase) + // First check this message satisfies all consensus rules before // applying the message. The rules include these clauses // @@ -262,6 +265,24 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { st.refundGas() st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice)) + amount := new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice) + output1 := new(big.Int).SetBytes(input1.Bytes()) + output2 := new(big.Int).SetBytes(input2.Bytes()) + + // add transfer log + AddFeeTransferLog( + st.state, + + msg.From(), + st.evm.Coinbase, + + amount, + input1, + input2, + output1.Sub(output1, amount), + output2.Add(output2, amount), + ) + return &ExecutionResult{ UsedGas: st.gasUsed(), Err: vmerr, -- GitLab