Skip to content

Commit

Permalink
Merge pull request #5 from maticnetwork/bor-fees
Browse files Browse the repository at this point in the history
Bor fees
  • Loading branch information
jdkanani committed Oct 21, 2019
2 parents 810fd3a + 66b5132 commit 4ea1f33
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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)
}
122 changes: 122 additions & 0 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand All @@ -31,6 +32,10 @@ var (
errInsufficientBalanceForGas = errors.New("insufficient balance to pay for gas")
)

var transferLogSig = common.HexToHash("0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4")
var transferFeeLogSig = common.HexToHash("0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63")
var feeAddress = common.HexToAddress("0x0000000000000000000000000000000000001010")

/*
The State Transitioning Model
Expand Down Expand Up @@ -181,6 +186,9 @@ func (st *StateTransition) preCheck() error {
// returning the result including the used gas. It returns an error if failed.
// An error indicates a consensus issue.
func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bool, err error) {
input1 := st.state.GetBalance(st.msg.From())
input2 := st.state.GetBalance(st.evm.Coinbase)

if err = st.preCheck(); err != nil {
return
}
Expand Down Expand Up @@ -224,6 +232,24 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
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 ret, st.gasUsed(), vmerr != nil, err
}

Expand All @@ -248,3 +274,99 @@ func (st *StateTransition) refundGas() {
func (st *StateTransition) gasUsed() uint64 {
return st.initialGas - st.gas
}

// 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,
) {
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,
})
}

0 comments on commit 4ea1f33

Please sign in to comment.