This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emint tx type for eth_call and logs setup (#118)
* Implement new tx message type for eth_call and module txs and abstracted state transition, prepared db for logs * Added transaction indexing to evm keeper * Alternative count type
- Loading branch information
1 parent
6ba38d6
commit 8bb8b40
Showing
8 changed files
with
337 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/ethermint/types" | ||
ethcmn "github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
var ( | ||
_ sdk.Msg = EmintMsg{} | ||
) | ||
|
||
const ( | ||
// TypeEmintMsg defines the type string of Emint message | ||
TypeEmintMsg = "emint_tx" | ||
) | ||
|
||
// EmintMsg implements a cosmos equivalent structure for Ethereum transactions | ||
type EmintMsg struct { | ||
AccountNonce uint64 `json:"nonce"` | ||
Price sdk.Int `json:"gasPrice"` | ||
GasLimit uint64 `json:"gas"` | ||
Recipient *sdk.AccAddress `json:"to" rlp:"nil"` // nil means contract creation | ||
Amount sdk.Int `json:"value"` | ||
Payload []byte `json:"input"` | ||
|
||
// From address (formerly derived from signature) | ||
From sdk.AccAddress `json:"from"` | ||
} | ||
|
||
// NewEmintMsg returns a reference to a new Ethermint transaction | ||
func NewEmintMsg( | ||
nonce uint64, to *sdk.AccAddress, amount sdk.Int, | ||
gasLimit uint64, gasPrice sdk.Int, payload []byte, from sdk.AccAddress, | ||
) EmintMsg { | ||
return EmintMsg{ | ||
AccountNonce: nonce, | ||
Price: gasPrice, | ||
GasLimit: gasLimit, | ||
Recipient: to, | ||
Amount: amount, | ||
Payload: payload, | ||
From: from, | ||
} | ||
} | ||
|
||
// Route should return the name of the module | ||
func (msg EmintMsg) Route() string { return RouterKey } | ||
|
||
// Type returns the action of the message | ||
func (msg EmintMsg) Type() string { return TypeEmintMsg } | ||
|
||
// GetSignBytes encodes the message for signing | ||
func (msg EmintMsg) GetSignBytes() []byte { | ||
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg)) | ||
} | ||
|
||
// ValidateBasic runs stateless checks on the message | ||
func (msg EmintMsg) ValidateBasic() sdk.Error { | ||
if msg.Price.Sign() != 1 { | ||
return types.ErrInvalidValue(fmt.Sprintf("Price must be positive: %x", msg.Price)) | ||
} | ||
|
||
// Amount can be 0 | ||
if msg.Amount.Sign() == -1 { | ||
return types.ErrInvalidValue(fmt.Sprintf("amount cannot be negative: %x", msg.Amount)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// GetSigners defines whose signature is required | ||
func (msg EmintMsg) GetSigners() []sdk.AccAddress { | ||
return []sdk.AccAddress{msg.From} | ||
} | ||
|
||
// To returns the recipient address of the transaction. It returns nil if the | ||
// transaction is a contract creation. | ||
func (msg EmintMsg) To() *ethcmn.Address { | ||
if msg.Recipient == nil { | ||
return nil | ||
} | ||
|
||
addr := ethcmn.BytesToAddress(msg.Recipient.Bytes()) | ||
return &addr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/tendermint/crypto/secp256k1" | ||
) | ||
|
||
func TestEmintMsg(t *testing.T) { | ||
addr := newSdkAddress() | ||
fromAddr := newSdkAddress() | ||
|
||
msg := NewEmintMsg(0, &addr, sdk.NewInt(1), 100000, sdk.NewInt(2), []byte("test"), fromAddr) | ||
require.NotNil(t, msg) | ||
require.Equal(t, msg.Recipient, &addr) | ||
|
||
require.Equal(t, msg.Route(), RouterKey) | ||
require.Equal(t, msg.Type(), TypeEmintMsg) | ||
} | ||
|
||
func TestEmintMsgValidation(t *testing.T) { | ||
testCases := []struct { | ||
nonce uint64 | ||
to *sdk.AccAddress | ||
amount sdk.Int | ||
gasLimit uint64 | ||
gasPrice sdk.Int | ||
payload []byte | ||
expectPass bool | ||
from sdk.AccAddress | ||
}{ | ||
{amount: sdk.NewInt(100), gasPrice: sdk.NewInt(100000), expectPass: true}, | ||
{amount: sdk.NewInt(0), gasPrice: sdk.NewInt(100000), expectPass: true}, | ||
{amount: sdk.NewInt(-1), gasPrice: sdk.NewInt(100000), expectPass: false}, | ||
{amount: sdk.NewInt(100), gasPrice: sdk.NewInt(-1), expectPass: false}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
msg := NewEmintMsg(tc.nonce, tc.to, tc.amount, tc.gasLimit, tc.gasPrice, tc.payload, tc.from) | ||
|
||
if tc.expectPass { | ||
require.Nil(t, msg.ValidateBasic(), "test: %v", i) | ||
} else { | ||
require.NotNil(t, msg.ValidateBasic(), "test: %v", i) | ||
} | ||
} | ||
} | ||
|
||
func TestEmintEncodingAndDecoding(t *testing.T) { | ||
addr := newSdkAddress() | ||
fromAddr := newSdkAddress() | ||
|
||
msg := NewEmintMsg(0, &addr, sdk.NewInt(1), 100000, sdk.NewInt(2), []byte("test"), fromAddr) | ||
|
||
raw, err := cdc.MarshalBinaryBare(msg) | ||
require.NoError(t, err) | ||
|
||
var msg2 EmintMsg | ||
err = cdc.UnmarshalBinaryBare(raw, &msg2) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, msg.AccountNonce, msg2.AccountNonce) | ||
require.Equal(t, msg.Recipient, msg2.Recipient) | ||
require.Equal(t, msg.Amount, msg2.Amount) | ||
require.Equal(t, msg.GasLimit, msg2.GasLimit) | ||
require.Equal(t, msg.Price, msg2.Price) | ||
require.Equal(t, msg.Payload, msg2.Payload) | ||
require.Equal(t, msg.From, msg2.From) | ||
} | ||
|
||
func newSdkAddress() sdk.AccAddress { | ||
tmpKey := secp256k1.GenPrivKey().PubKey() | ||
return sdk.AccAddress(tmpKey.Address().Bytes()) | ||
} |
Oops, something went wrong.