mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
chg: add log changes for fee
This commit is contained in:
parent
e5058f94fd
commit
087c44d4bc
3 changed files with 147 additions and 0 deletions
115
core/bor_fee_log.go
Normal file
115
core/bor_fee_log.go
Normal file
|
|
@ -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,
|
||||||
|
})
|
||||||
|
}
|
||||||
11
core/evm.go
11
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
|
// 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) {
|
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.SubBalance(sender, amount)
|
||||||
db.AddBalance(recipient, 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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -214,6 +214,9 @@ func (st *StateTransition) preCheck() error {
|
||||||
// However if any consensus issue encountered, return the error directly with
|
// However if any consensus issue encountered, return the error directly with
|
||||||
// nil evm execution result.
|
// nil evm execution result.
|
||||||
func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
|
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
|
// First check this message satisfies all consensus rules before
|
||||||
// applying the message. The rules include these clauses
|
// applying the message. The rules include these clauses
|
||||||
//
|
//
|
||||||
|
|
@ -262,6 +265,24 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
|
||||||
st.refundGas()
|
st.refundGas()
|
||||||
st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice))
|
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{
|
return &ExecutionResult{
|
||||||
UsedGas: st.gasUsed(),
|
UsedGas: st.gasUsed(),
|
||||||
Err: vmerr,
|
Err: vmerr,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue