Skip to content

Commit

Permalink
fine tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
Vizualni committed Jun 17, 2022
1 parent 2a6288b commit 984aece
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 117 deletions.
13 changes: 5 additions & 8 deletions x/evm/client/cli/tx_submit_new_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type submitNewJobPayloadJson struct {

func CmdSubmitNewJob() *cobra.Command {
cmd := &cobra.Command{
Use: "submit-new-job [chain-id] [smart contract address] [smart contract payload] [method] [smart contract JSON abi]",
Use: "submit-new-job [smart contract address] [smart contract payload]",
Short: "Broadcast message SubmitNewJob",
Args: cobra.ExactArgs(5),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {

clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -29,13 +29,10 @@ func CmdSubmitNewJob() *cobra.Command {

msg := &types.MsgSubmitNewJob{
Creator: clientCtx.GetFromAddress().String(),
ChainID: args[0],
HexSmartContractAddress: args[1],
HexPayload: args[2],
Method: args[3],
Abi: args[4],
ChainType: "EVM",
HexSmartContractAddress: args[0],
HexPayload: args[1],
}

if err := msg.ValidateBasic(); err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {

func (k Keeper) AddSmartContractExecutionToConsensus(
ctx sdk.Context,
chainType string,
chainID string,
msg *types.ArbitrarySmartContractCall,
msg *types.Message,
) error {
return k.ConsensusKeeper.PutMessageForSigning(
ctx,
consensustypes.Queue(ConsensusArbitraryContractCall, chainType, chainID),
consensustypes.Queue(
ConsensusTurnstoneMessage,
consensustypes.ChainTypeEVM,
msg.ChainID,
),
msg,
)
}
Expand Down
142 changes: 63 additions & 79 deletions x/evm/keeper/keeper_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
package keeper_test

import (
"math/big"
"strings"
"testing"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/palomachain/paloma/app"
"github.com/palomachain/paloma/testutil/rand"
"github.com/palomachain/paloma/testutil/sample"
consensustypes "github.com/palomachain/paloma/x/consensus/types"
"github.com/palomachain/paloma/x/evm/keeper"
"github.com/palomachain/paloma/x/evm/types"
valsettypes "github.com/palomachain/paloma/x/valset/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/vizualni/whoops"
)

func genValidators(t *testing.T, numValidators, totalConsPower int) []stakingtypes.Validator {
Expand Down Expand Up @@ -57,68 +43,66 @@ func genValidators(t *testing.T, numValidators, totalConsPower int) []stakingtyp
return validators
}

func TestEndToEndForEvmArbitraryCall(t *testing.T) {
chainType, chainID := consensustypes.ChainTypeEVM, "eth-main"
a := app.NewTestApp(t, false)
ctx := a.NewContext(false, tmproto.Header{
Height: 5,
})

validators := genValidators(t, 25, 25000)
for _, val := range validators {
a.StakingKeeper.SetValidator(ctx, val)
}

smartContractAddr := common.BytesToAddress(rand.Bytes(5))
err := a.EvmKeeper.AddSmartContractExecutionToConsensus(
ctx,
chainType,
chainID,
&types.ArbitrarySmartContractCall{
Payload: func() []byte {
evm := whoops.Must(abi.JSON(strings.NewReader(sample.SimpleABI)))
return whoops.Must(evm.Pack("store", big.NewInt(1337)))
}(),
HexAddress: smartContractAddr.Hex(),
Abi: []byte(sample.SimpleABI),
},
)

require.NoError(t, err)

private, err := crypto.GenerateKey()
require.NoError(t, err)

accAddr := crypto.PubkeyToAddress(private.PublicKey)
err = a.ValsetKeeper.AddExternalChainInfo(ctx, validators[0].GetOperator(), []*valsettypes.ExternalChainInfo{
{
ChainType: chainType,
ChainID: chainID,
Address: accAddr.Hex(),
Pubkey: accAddr[:],
},
})

require.NoError(t, err)
queue := consensustypes.Queue(keeper.ConsensusArbitraryContractCall, chainType, chainID)
msgs, err := a.ConsensusKeeper.GetMessagesForSigning(ctx, queue, validators[0].GetOperator())

for _, msg := range msgs {
sigbz, err := crypto.Sign(msg.GetBytesToSign(), private)
require.NoError(t, err)
err = a.ConsensusKeeper.AddMessageSignature(
ctx,
validators[0].GetOperator(),
[]*consensustypes.MsgAddMessagesSignatures_MsgSignedMessage{
{
Id: msg.GetId(),
QueueTypeName: queue,
Signature: sigbz,
SignedByAddress: accAddr.Hex(),
},
},
)
require.NoError(t, err)
}

}
// func TestEndToEndForEvmArbitraryCall(t *testing.T) {
// chainType, chainID := consensustypes.ChainTypeEVM, "eth-main"
// a := app.NewTestApp(t, false)
// ctx := a.NewContext(false, tmproto.Header{
// Height: 5,
// })

// validators := genValidators(t, 25, 25000)
// for _, val := range validators {
// a.StakingKeeper.SetValidator(ctx, val)
// }

// smartContractAddr := common.BytesToAddress(rand.Bytes(5))
// err := a.EvmKeeper.AddSmartContractExecutionToConsensus(
// ctx,
// &types.ArbitrarySmartContractCall{
// Payload: func() []byte {
// evm := whoops.Must(abi.JSON(strings.NewReader(sample.SimpleABI)))
// return whoops.Must(evm.Pack("store", big.NewInt(1337)))
// }(),
// HexAddress: smartContractAddr.Hex(),
// Abi: []byte(sample.SimpleABI),
// },
// )

// require.NoError(t, err)

// private, err := crypto.GenerateKey()
// require.NoError(t, err)

// accAddr := crypto.PubkeyToAddress(private.PublicKey)
// err = a.ValsetKeeper.AddExternalChainInfo(ctx, validators[0].GetOperator(), []*valsettypes.ExternalChainInfo{
// {
// ChainType: chainType,
// ChainID: chainID,
// Address: accAddr.Hex(),
// Pubkey: accAddr[:],
// },
// })

// require.NoError(t, err)
// queue := consensustypes.Queue(keeper.ConsensusArbitraryContractCall, chainType, chainID)
// msgs, err := a.ConsensusKeeper.GetMessagesForSigning(ctx, queue, validators[0].GetOperator())

// for _, msg := range msgs {
// sigbz, err := crypto.Sign(msg.GetBytesToSign(), private)
// require.NoError(t, err)
// err = a.ConsensusKeeper.AddMessageSignature(
// ctx,
// validators[0].GetOperator(),
// []*consensustypes.MsgAddMessagesSignatures_MsgSignedMessage{
// {
// Id: msg.GetId(),
// QueueTypeName: queue,
// Signature: sigbz,
// SignedByAddress: accAddr.Hex(),
// },
// },
// )
// require.NoError(t, err)
// }

// }
23 changes: 14 additions & 9 deletions x/evm/keeper/msg_server_submit_new_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -18,17 +19,21 @@ func (k msgServer) SubmitNewJob(goCtx context.Context, msg *types.MsgSubmitNewJo
return nil, err
}

// TODO: use real chain IDs
err := k.AddSmartContractExecutionToConsensus(
ctx,
msg.GetChainType(),
msg.GetChainID(),
&types.ArbitrarySmartContractCall{
Method: msg.Method,
Payload: common.Hex2Bytes(msg.GetHexPayload()),
Abi: []byte(msg.GetAbi()),
HexAddress: msg.HexSmartContractAddress,
})
&types.Message{
ChainID: msg.ChainID,
TurnstoneID: string(zero32Byte[:]),
Action: &types.Message_SubmitLogicCall{
SubmitLogicCall: &types.SubmitLogicCall{
HexContractAddress: msg.GetHexSmartContractAddress(),
Payload: common.Hex2Bytes(msg.GetHexPayload()),
Abi: []byte(msg.GetAbi()),
Deadline: ctx.BlockTime().UTC().Add(5 * time.Minute).Unix(),
},
},
},
)

if err != nil {
return nil, err
Expand Down
14 changes: 0 additions & 14 deletions x/evm/types/turnstone_abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"encoding/binary"
fmt "fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -43,19 +42,6 @@ func (_m *Message_UpdateValset) keccak256(orig *Message, _ uint64) []byte {
)
bytes = append(method.ID[:], bytes...)

bytes1, _ := arguments.Pack(
[]common.Address{
common.HexToAddress("0xe4ab6f4d62ba7e0bbc4cf6c5e8153e105108fba9"),
},
[]*big.Int{
big.NewInt(4294967296),
},
big.NewInt(int64(7)),
bytes32,
)
bytes1 = append(method.ID[:], bytes1...)
fmt.Println(common.Bytes2Hex(bytes1), common.Bytes2Hex(crypto.Keccak256(bytes1)))

if err != nil {
panic(err)
}
Expand Down
2 changes: 0 additions & 2 deletions x/valset/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -73,7 +72,6 @@ func TestRegisterRunner(t *testing.T) {
},
},
)
fmt.Println(err)
require.ErrorIs(t, err, ErrExternalChainAlreadyRegistered)
})

Expand Down
4 changes: 3 additions & 1 deletion x/valset/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
// returns no validator updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
// TODO: THIS IS ONLY TEMPORARY
am.keeper.TriggerSnapshotBuild(ctx)
if ctx.BlockHeader().Height%20 == 0 {
am.keeper.TriggerSnapshotBuild(ctx)
}
return []abci.ValidatorUpdate{}
}

0 comments on commit 984aece

Please sign in to comment.