remove duplicate query in receipts (#273)

* logs tests

* updates

* String()

* remove extra query, add comments

* test fixes

* lint

* lint
This commit is contained in:
Federico Kunze
2020-05-02 21:56:18 -04:00
committed by GitHub
parent 86cd39defb
commit 26d4e968e0
7 changed files with 234 additions and 59 deletions
+27 -4
View File
@@ -20,7 +20,10 @@ import (
const addrHex = "0x756F45E3FA69347A9A973A725E3C98bC4db0b4c1"
var address = ethcmn.HexToAddress(addrHex)
var (
address = ethcmn.HexToAddress(addrHex)
hash = ethcmn.FromHex("0x0d87a3a5f73140f46aac1bf419263e4e94e87c292f25007700ab7f2060e2af68")
)
type KeeperTestSuite struct {
suite.Suite
@@ -42,6 +45,26 @@ func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
func (suite *KeeperTestSuite) TestTransactionLogs() {
log := &ethtypes.Log{
Address: address,
Data: []byte("log"),
BlockNumber: 10,
}
expLogs := []*ethtypes.Log{log}
err := suite.app.EvmKeeper.SetTransactionLogs(suite.ctx, expLogs, hash)
suite.Require().NoError(err)
suite.app.EvmKeeper.AddLog(suite.ctx, expLogs[0])
logs, err := suite.app.EvmKeeper.GetTransactionLogs(suite.ctx, hash)
suite.Require().NoError(err)
suite.Require().Equal(expLogs, logs)
logs = suite.app.EvmKeeper.AllLogs(suite.ctx)
suite.Require().Equal(expLogs, logs)
}
func (suite *KeeperTestSuite) TestDBStorage() {
// Perform state transitions
suite.app.EvmKeeper.CreateAccount(suite.ctx, address)
@@ -51,8 +74,8 @@ func (suite *KeeperTestSuite) TestDBStorage() {
suite.app.EvmKeeper.SetCode(suite.ctx, address, []byte{0x1})
// Test block hash mapping functionality
suite.app.EvmKeeper.SetBlockHashMapping(suite.ctx, ethcmn.FromHex("0x0d87a3a5f73140f46aac1bf419263e4e94e87c292f25007700ab7f2060e2af68"), 7)
height, err := suite.app.EvmKeeper.GetBlockHashMapping(suite.ctx, ethcmn.FromHex("0x0d87a3a5f73140f46aac1bf419263e4e94e87c292f25007700ab7f2060e2af68"))
suite.app.EvmKeeper.SetBlockHashMapping(suite.ctx, hash, 7)
height, err := suite.app.EvmKeeper.GetBlockHashMapping(suite.ctx, hash)
suite.Require().NoError(err)
suite.Require().Equal(int64(7), height)
@@ -68,7 +91,7 @@ func (suite *KeeperTestSuite) TestDBStorage() {
suite.Require().Equal(suite.app.EvmKeeper.GetState(suite.ctx, address, ethcmn.HexToHash("0x2")), ethcmn.HexToHash("0x3"))
suite.Require().Equal(suite.app.EvmKeeper.GetCode(suite.ctx, address), []byte{0x1})
height, err = suite.app.EvmKeeper.GetBlockHashMapping(suite.ctx, ethcmn.FromHex("0x0d87a3a5f73140f46aac1bf419263e4e94e87c292f25007700ab7f2060e2af68"))
height, err = suite.app.EvmKeeper.GetBlockHashMapping(suite.ctx, hash)
suite.Require().NoError(err)
suite.Require().Equal(height, int64(7))
height, err = suite.app.EvmKeeper.GetBlockHashMapping(suite.ctx, []byte{0x43, 0x32})
+18 -15
View File
@@ -110,11 +110,11 @@ func (st StateTransition) TransitionDb(ctx sdk.Context) (*ExecutionResult, error
evm := st.newEVM(ctx, csdb, gasLimit, gasPrice.BigInt())
var (
ret []byte
leftOverGas uint64
addr common.Address
recipientLog string
senderRef = vm.AccountRef(st.Sender)
ret []byte
leftOverGas uint64
contractAddress common.Address
recipientLog string
senderRef = vm.AccountRef(st.Sender)
)
// Get nonce of account outside of the EVM
@@ -125,13 +125,13 @@ func (st StateTransition) TransitionDb(ctx sdk.Context) (*ExecutionResult, error
// create contract or execute call
switch contractCreation {
case true:
ret, addr, leftOverGas, err = evm.Create(senderRef, st.Payload, gasLimit, st.Amount)
recipientLog = fmt.Sprintf("contract address %s", addr)
ret, contractAddress, leftOverGas, err = evm.Create(senderRef, st.Payload, gasLimit, st.Amount)
recipientLog = fmt.Sprintf("contract address %s", contractAddress.String())
default:
// Increment the nonce for the next transaction (just for evm state transition)
csdb.SetNonce(st.Sender, csdb.GetNonce(st.Sender)+1)
ret, leftOverGas, err = evm.Call(senderRef, *st.Recipient, st.Payload, gasLimit, st.Amount)
recipientLog = fmt.Sprintf("recipient address %s", st.Recipient)
recipientLog = fmt.Sprintf("recipient address %s", st.Recipient.String())
}
gasConsumed := gasLimit - leftOverGas
@@ -172,12 +172,15 @@ func (st StateTransition) TransitionDb(ctx sdk.Context) (*ExecutionResult, error
}
// Encode all necessary data into slice of bytes to return in sdk result
resultData := &ResultData{
Address: addr,
Bloom: bloomFilter,
Logs: logs,
Ret: ret,
TxHash: *st.TxHash,
resultData := ResultData{
Bloom: bloomFilter,
Logs: logs,
Ret: ret,
TxHash: *st.TxHash,
}
if contractCreation {
resultData.ContractAddress = contractAddress
}
resBz, err := EncodeResultData(resultData)
@@ -186,7 +189,7 @@ func (st StateTransition) TransitionDb(ctx sdk.Context) (*ExecutionResult, error
}
resultLog := fmt.Sprintf(
"executed EVM state transition; sender address %s; %s", st.Sender, recipientLog,
"executed EVM state transition; sender address %s; %s", st.Sender.String(), recipientLog,
)
executionResult := &ExecutionResult{
+21 -9
View File
@@ -3,6 +3,7 @@ package types
import (
"fmt"
"math/big"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -53,27 +54,38 @@ func rlpHash(x interface{}) (hash ethcmn.Hash) {
// ResultData represents the data returned in an sdk.Result
type ResultData struct {
Address ethcmn.Address `json:"address"`
Bloom ethtypes.Bloom `json:"bloom"`
Logs []*ethtypes.Log `json:"logs"`
Ret []byte `json:"ret"`
TxHash ethcmn.Hash `json:"tx_hash"`
ContractAddress ethcmn.Address `json:"contract_address"`
Bloom ethtypes.Bloom `json:"bloom"`
Logs []*ethtypes.Log `json:"logs"`
Ret []byte `json:"ret"`
TxHash ethcmn.Hash `json:"tx_hash"`
}
// String implements fmt.Stringer interface.
func (rd ResultData) String() string {
return strings.TrimSpace(fmt.Sprintf(`ResultData:
ContractAddress: %s
Bloom: %s
Logs: %v
Ret: %v
TxHash: %s
`, rd.ContractAddress.String(), rd.Bloom.Big().String(), rd.Logs, rd.Ret, rd.TxHash.String()))
}
// EncodeResultData takes all of the necessary data from the EVM execution
// and returns the data as a byte slice encoded with amino
func EncodeResultData(data *ResultData) ([]byte, error) {
func EncodeResultData(data ResultData) ([]byte, error) {
return ModuleCdc.MarshalBinaryLengthPrefixed(data)
}
// DecodeResultData decodes an amino-encoded byte slice into ResultData
func DecodeResultData(in []byte) (ResultData, error) {
data := new(ResultData)
err := ModuleCdc.UnmarshalBinaryLengthPrefixed(in, data)
var data ResultData
err := ModuleCdc.UnmarshalBinaryLengthPrefixed(in, &data)
if err != nil {
return ResultData{}, err
}
return *data, nil
return data, nil
}
// EncodeLogs encodes an array of logs using amino
+5 -5
View File
@@ -9,13 +9,13 @@ import (
)
func TestEvmDataEncoding(t *testing.T) {
addr := ethcmn.HexToAddress("0x12345")
addr := ethcmn.HexToAddress("0x5dE8a020088a2D6d0a23c204FFbeD02790466B49")
bloom := ethtypes.BytesToBloom([]byte{0x1, 0x3})
ret := []byte{0x5, 0x8}
data := &ResultData{
Address: addr,
Bloom: bloom,
data := ResultData{
ContractAddress: addr,
Bloom: bloom,
Logs: []*ethtypes.Log{{
Data: []byte{1, 2, 3, 4},
BlockNumber: 17,
@@ -28,7 +28,7 @@ func TestEvmDataEncoding(t *testing.T) {
res, err := DecodeResultData(enc)
require.NoError(t, err)
require.Equal(t, addr, res.Address)
require.Equal(t, addr, res.ContractAddress)
require.Equal(t, bloom, res.Bloom)
require.Equal(t, data.Logs, res.Logs)
require.Equal(t, ret, res.Ret)