From 4deebaedf43535e42e05be6ba89c84aa73c334ce Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Sun, 29 Sep 2019 02:10:09 +0530 Subject: [PATCH 1/5] repeat log for test --- core/state_processor.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/state_processor.go b/core/state_processor.go index bed6a073063d..0e51d400e5ed 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -125,5 +125,12 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo receipt.BlockNumber = header.Number receipt.TransactionIndex = uint(statedb.TxIndex()) + if len(receipt.Logs) > 0 { + var l types.Log + l = *receipt.Logs[0] + statedb.AddLog(&l) + receipt.Logs = append(receipt.Logs, &l) + } + return receipt, gas, err } From b3473b729ecf961c2ddadb6d4f51fe6f83a087ca Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Thu, 3 Oct 2019 21:05:24 +0530 Subject: [PATCH 2/5] Revert "repeat log for test" This reverts commit 4deebaedf43535e42e05be6ba89c84aa73c334ce. --- core/state_processor.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core/state_processor.go b/core/state_processor.go index 0e51d400e5ed..bed6a073063d 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -125,12 +125,5 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo receipt.BlockNumber = header.Number receipt.TransactionIndex = uint(statedb.TxIndex()) - if len(receipt.Logs) > 0 { - var l types.Log - l = *receipt.Logs[0] - statedb.AddLog(&l) - receipt.Logs = append(receipt.Logs, &l) - } - return receipt, gas, err } From e92e0a3959d22e0f7bbad589feb6370bae8a1f8c Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Fri, 4 Oct 2019 00:38:25 +0530 Subject: [PATCH 3/5] add tranfer log --- core/evm.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/core/evm.go b/core/evm.go index b654bbd4796f..fc68b4557940 100644 --- a/core/evm.go +++ b/core/evm.go @@ -92,6 +92,48 @@ 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 tranfer + db.AddLog(&types.Log{ + Address: common.HexToAddress("0x0000000000000000000000000000000000001010"), + Topics: []common.Hash{ + common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), + sender.Hash(), + recipient.Hash(), + }, + Data: common.BytesToHash(amount.Bytes()).Bytes(), + }) + + 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 + db.AddLog(&types.Log{ + Address: common.HexToAddress("0x0000000000000000000000000000000000001010"), + Topics: []common.Hash{ + common.HexToHash("0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4"), + sender.Hash(), + recipient.Hash(), + }, + Data: data, + }) } From 4433db35c29b3e067643f9c03354457806e037a9 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Fri, 4 Oct 2019 13:50:36 +0530 Subject: [PATCH 4/5] fees log --- core/evm.go | 33 +----------------- core/state_transition.go | 72 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 32 deletions(-) diff --git a/core/evm.go b/core/evm.go index fc68b4557940..979003bda791 100644 --- a/core/evm.go +++ b/core/evm.go @@ -103,37 +103,6 @@ func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) output1 := db.GetBalance(sender) output2 := db.GetBalance(recipient) - // add tranfer - db.AddLog(&types.Log{ - Address: common.HexToAddress("0x0000000000000000000000000000000000001010"), - Topics: []common.Hash{ - common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), - sender.Hash(), - recipient.Hash(), - }, - Data: common.BytesToHash(amount.Bytes()).Bytes(), - }) - - 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 - db.AddLog(&types.Log{ - Address: common.HexToAddress("0x0000000000000000000000000000000000001010"), - Topics: []common.Hash{ - common.HexToHash("0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4"), - sender.Hash(), - recipient.Hash(), - }, - Data: data, - }) + AddTransferLog(db, sender, recipient, amount, input1, input2, output1, output2) } diff --git a/core/state_transition.go b/core/state_transition.go index fda081b7d113..0245e99ce436 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -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" @@ -181,6 +182,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 } @@ -224,6 +228,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).SetUint64(input1.Uint64()) + output2 := new(big.Int).SetUint64(input2.Uint64()) + + // add transfer log + AddTransferLog( + 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 } @@ -248,3 +270,53 @@ 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, +) { + + // // add tranfer + // db.AddLog(&types.Log{ + // Address: common.HexToAddress("0x0000000000000000000000000000000000001010"), + // Topics: []common.Hash{ + // common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), + // sender.Hash(), + // recipient.Hash(), + // }, + // Data: common.BytesToHash(amount.Bytes()).Bytes(), + // }) + + 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: common.HexToAddress("0x0000000000000000000000000000000000001010"), + Topics: []common.Hash{ + common.HexToHash("0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4"), + sender.Hash(), + recipient.Hash(), + }, + Data: data, + }) +} From 66b51325044eb4f63775797845a0bca4b5873123 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Sat, 5 Oct 2019 13:07:35 +0530 Subject: [PATCH 5/5] add transfer and fee log --- core/state_transition.go | 80 ++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/core/state_transition.go b/core/state_transition.go index 0245e99ce436..6272619a0ebf 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -32,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 @@ -229,11 +233,11 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo 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).SetUint64(input1.Uint64()) - output2 := new(big.Int).SetUint64(input2.Uint64()) + output1 := new(big.Int).SetBytes(input1.Bytes()) + output2 := new(big.Int).SetBytes(input2.Bytes()) // add transfer log - AddTransferLog( + AddFeeTransferLog( st.state, msg.From(), @@ -284,18 +288,63 @@ func AddTransferLog( output1, output2 *big.Int, ) { + addTransferLog( + state, + transferLogSig, + + sender, + recipient, - // // add tranfer - // db.AddLog(&types.Log{ - // Address: common.HexToAddress("0x0000000000000000000000000000000000001010"), - // Topics: []common.Hash{ - // common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), - // sender.Hash(), - // recipient.Hash(), - // }, - // Data: common.BytesToHash(amount.Bytes()).Bytes(), - // }) + 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, @@ -311,9 +360,10 @@ func AddTransferLog( // add transfer log state.AddLog(&types.Log{ - Address: common.HexToAddress("0x0000000000000000000000000000000000001010"), + Address: feeAddress, Topics: []common.Hash{ - common.HexToHash("0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4"), + eventSig, + feeAddress.Hash(), sender.Hash(), recipient.Hash(), },