forked from cerc-io/laconicd-deprecated
Fixed sig issue, wrote embedded tests
This commit is contained in:
+1
-11
@@ -47,15 +47,8 @@ func AnteHandler(am auth.AccountMapper) sdk.AnteHandler {
|
||||
return sdkCtx, sdk.ErrInternal(fmt.Sprintf("invalid transaction: %T", tx)).Result(), true
|
||||
}
|
||||
|
||||
/*
|
||||
fmt.Println("handler begin")
|
||||
fmt.Println(am.GetAccount(sdkCtx, tx.))
|
||||
fmt.Println("handler end")*/
|
||||
|
||||
newCtx = sdkCtx.WithGasMeter(sdk.NewGasMeter(gasLimit))
|
||||
|
||||
|
||||
|
||||
// AnteHandlers must have their own defer/recover in order for the
|
||||
// BaseApp to know how much gas was used! This is because the GasMeter
|
||||
// is created in the AnteHandler, but if it panics the context won't be
|
||||
@@ -101,9 +94,6 @@ func handleEthTx(sdkCtx sdk.Context, tx sdk.Tx, am auth.AccountMapper) (sdk.Cont
|
||||
sdkCtx.GasMeter().ConsumeGas(verifySigCost, "ante verify")
|
||||
addr, err := ethTx.VerifySig(chainID)
|
||||
|
||||
fmt.Println(addr)
|
||||
fmt.Println(chainID)
|
||||
|
||||
if err != nil {
|
||||
return sdkCtx, sdk.ErrUnauthorized("signature verification failed").Result(), true
|
||||
}
|
||||
@@ -143,7 +133,7 @@ func handleEmbeddedTx(sdkCtx sdk.Context, tx sdk.Tx, am auth.AccountMapper) (sdk
|
||||
signer := ethcmn.BytesToAddress(signerAddrs[i].Bytes())
|
||||
|
||||
signerAcc, err := validateSignature(sdkCtx, etx, signer, sig, am)
|
||||
if err.Code() != sdk.CodeOK {
|
||||
if err != nil {
|
||||
return sdkCtx, err.Result(), true
|
||||
}
|
||||
|
||||
|
||||
+161
-35
@@ -16,9 +16,13 @@ import (
|
||||
|
||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Test EthTx Antehandler
|
||||
// -----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
func TestBadSig(t *testing.T) {
|
||||
tx := types.NewTestEthTxs(types.TestChainID, []*ecdsa.PrivateKey{types.TestPrivKey1}, []ethcmn.Address{types.TestAddr1})[0]
|
||||
|
||||
@@ -32,7 +36,7 @@ func TestBadSig(t *testing.T) {
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
require.True(t, abort, "Antehandler did not abort")
|
||||
assert.True(t, abort, "Antehandler did not abort")
|
||||
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, fmt.Sprintf("Result has wrong code on bad tx: %s", res.Log))
|
||||
|
||||
}
|
||||
@@ -50,15 +54,15 @@ func TestInsufficientGas(t *testing.T) {
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
require.True(t, abort, "Antehandler did not abort")
|
||||
assert.True(t, abort, "Antehandler did not abort")
|
||||
require.Equal(t, sdk.ABCICodeType(0x1000c), res.Code, fmt.Sprintf("Result has wrong code on bad tx: %s", res.Log))
|
||||
|
||||
}
|
||||
|
||||
func TestIncorrectNonce(t *testing.T) {
|
||||
tx := types.NewTestEthTxs(types.TestChainID, []*ecdsa.PrivateKey{types.TestPrivKey1}, []ethcmn.Address{types.TestAddr1})[0]
|
||||
|
||||
tx.Data.AccountNonce = 12
|
||||
// Create transaction with wrong nonce 12
|
||||
tx := types.NewTransaction(12, types.TestAddr2, sdk.NewInt(50), 1000, sdk.NewInt(1000), []byte("test_bytes"))
|
||||
tx.Sign(types.TestChainID, types.TestPrivKey1)
|
||||
|
||||
ms, key := SetupMultiStore()
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, log.NewNopLogger())
|
||||
@@ -71,14 +75,9 @@ func TestIncorrectNonce(t *testing.T) {
|
||||
|
||||
handler := AnteHandler(accountMapper)
|
||||
|
||||
fmt.Println("start test")
|
||||
fmt.Println(accountMapper.GetAccount(ctx, types.TestAddr1[:]))
|
||||
fmt.Println(types.TestAddr1)
|
||||
fmt.Println("end test")
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
require.True(t, abort, "Antehandler did not abort")
|
||||
assert.True(t, abort, "Antehandler did not abort")
|
||||
require.Equal(t, sdk.ABCICodeType(0x10003), res.Code, fmt.Sprintf("Result has wrong code on bad tx: %s", res.Log))
|
||||
|
||||
}
|
||||
@@ -90,51 +89,178 @@ func TestValidTx(t *testing.T) {
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
||||
|
||||
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
||||
|
||||
// Set account in accountMapper
|
||||
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
||||
accountMapper.SetAccount(ctx, acc)
|
||||
|
||||
handler := AnteHandler(accountMapper)
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
require.False(t, abort, "Antehandler abort on valid tx")
|
||||
require.Equal(t, sdk.CodeOK, res.Code, fmt.Sprintf("Result not OK on valid Tx: %s", res.Log))
|
||||
assert.False(t, abort, "Antehandler abort on valid tx")
|
||||
require.True(t, res.IsOK(), fmt.Sprintf("Result not OK on valid Tx: %s", res.Log))
|
||||
|
||||
// Ensure account state updated correctly
|
||||
seq, _ := accountMapper.GetSequence(ctx, types.TestAddr1[:])
|
||||
require.Equal(t, int64(1), seq, "AccountNonce did not increment correctly")
|
||||
}
|
||||
|
||||
func TestValidEmbeddedTx(t *testing.T) {
|
||||
cdc := types.NewTestCodec()
|
||||
// Test EmbeddedTx Antehandler
|
||||
// -----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
func TestInvalidSigEmbeddedTx(t *testing.T) {
|
||||
// Create msg to be embedded
|
||||
msgs := []sdk.Msg{stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)})}
|
||||
|
||||
tx := types.NewTestSDKTxs(cdc, types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1}, []int64{0}, []int64{0}, types.NewStdFee())[0]
|
||||
// Create transaction with incorrect signer
|
||||
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey2},
|
||||
[]int64{0}, []int64{0}, types.NewStdFee())
|
||||
|
||||
ms, key := SetupMultiStore()
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
||||
|
||||
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
||||
|
||||
// Set account in accountMapper
|
||||
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
||||
accountMapper.SetAccount(ctx, acc)
|
||||
|
||||
handler := AnteHandler(accountMapper)
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
assert.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
||||
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, "Result is OK on bad tx")
|
||||
}
|
||||
|
||||
func TestInvalidMultiMsgEmbeddedTx(t *testing.T) {
|
||||
// Create msg to be embedded
|
||||
msgs := []sdk.Msg{
|
||||
stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
||||
stake.NewMsgDelegate(types.TestAddr2[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
||||
}
|
||||
|
||||
// Create transaction with only one signer
|
||||
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1},
|
||||
[]int64{0}, []int64{0}, types.NewStdFee())
|
||||
|
||||
ms, key := SetupMultiStore()
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
||||
|
||||
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
||||
|
||||
// Set account in accountMapper
|
||||
acc1 := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
||||
accountMapper.SetAccount(ctx, acc1)
|
||||
acc2 := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
||||
accountMapper.SetAccount(ctx, acc2)
|
||||
|
||||
handler := AnteHandler(accountMapper)
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
assert.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
||||
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, "Result is OK on bad tx")
|
||||
}
|
||||
|
||||
func TestInvalidAccountNumberEmbeddedTx(t *testing.T) {
|
||||
// Create msg to be embedded
|
||||
msgs := []sdk.Msg{
|
||||
stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
||||
}
|
||||
|
||||
// Create transaction with wrong account number
|
||||
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1},
|
||||
[]int64{3}, []int64{0}, types.NewStdFee())
|
||||
|
||||
ms, key := SetupMultiStore()
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
||||
|
||||
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
||||
|
||||
// Set account in accountMapper
|
||||
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
||||
acc.SetAccountNumber(4)
|
||||
accountMapper.SetAccount(ctx, acc)
|
||||
|
||||
handler := AnteHandler(accountMapper)
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
assert.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
||||
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, "Result is OK on bad tx")
|
||||
}
|
||||
|
||||
func TestInvalidSequenceEmbeddedTx(t *testing.T) {
|
||||
// Create msg to be embedded
|
||||
msgs := []sdk.Msg{
|
||||
stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
||||
}
|
||||
|
||||
// Create transaction with wrong account number
|
||||
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1},
|
||||
[]int64{4}, []int64{2}, types.NewStdFee())
|
||||
|
||||
ms, key := SetupMultiStore()
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
||||
|
||||
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
||||
|
||||
// Set account in accountMapper
|
||||
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
||||
acc.SetAccountNumber(4)
|
||||
acc.SetSequence(3)
|
||||
accountMapper.SetAccount(ctx, acc)
|
||||
|
||||
handler := AnteHandler(accountMapper)
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
assert.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
||||
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, "Result is OK on bad tx")
|
||||
}
|
||||
|
||||
func TestValidEmbeddedTx(t *testing.T) {
|
||||
// Create msg to be embedded
|
||||
msgs := []sdk.Msg{
|
||||
stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
||||
stake.NewMsgDelegate(types.TestAddr2[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
||||
}
|
||||
|
||||
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1, types.TestPrivKey2},
|
||||
[]int64{4, 5}, []int64{3, 1}, types.NewStdFee())
|
||||
|
||||
ms, key := SetupMultiStore()
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
||||
|
||||
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
||||
|
||||
// Set account in accountMapper
|
||||
acc1 := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
||||
acc1.SetAccountNumber(4)
|
||||
acc1.SetSequence(3)
|
||||
accountMapper.SetAccount(ctx, acc1)
|
||||
acc2 := accountMapper.NewAccountWithAddress(ctx, types.TestAddr2[:])
|
||||
acc2.SetAccountNumber(5)
|
||||
acc2.SetSequence(1)
|
||||
accountMapper.SetAccount(ctx, acc2)
|
||||
|
||||
handler := AnteHandler(accountMapper)
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
require.False(t, abort, "Antehandler abort on valid embedded tx")
|
||||
require.Equal(t, sdk.CodeOK, res.Code, fmt.Sprintf("Result not OK on valid Tx: %s", res.Log))
|
||||
require.True(t, res.IsOK(), fmt.Sprintf("Result not OK on valid Tx: %s", res.Log))
|
||||
|
||||
// Ensure account state updated correctly
|
||||
seq1, _ := accountMapper.GetSequence(ctx, types.TestAddr1[:])
|
||||
seq2, _ := accountMapper.GetSequence(ctx, types.TestAddr2[:])
|
||||
|
||||
assert.Equal(t, int64(4), seq1, "Sequence1 did not increment correctly")
|
||||
assert.Equal(t, int64(2), seq2, "Sequence2 did not increment correctly")
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
func TestInvalidEmbeddedTx(t *testing.T) {
|
||||
cdc := types.NewTestCodec()
|
||||
// Create msg to be embedded
|
||||
msgs := []sdk.Msg{stake.NewMsgCreateValidator(types.TestAddr1[:], nil, sdk.Coin{"steak", sdk.NewInt(50)}, stake.Description{})}
|
||||
|
||||
tx := types.NewTestSDKTxs(cdc, types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1}, []int64{0}, []int64{0}, types.NewStdFee())[0]
|
||||
|
||||
ms, key := SetupMultiStore()
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
||||
|
||||
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
||||
handler := AnteHandler(accountMapper)
|
||||
|
||||
_, res, abort := handler(ctx, tx)
|
||||
|
||||
require.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
||||
require.Equal(t, sdk.ABCICodeType(0x1000c), res.Code, "Result is OK on bad tx")
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user