Skip to content

Commit

Permalink
feat: support event nonce ctor input for deployments (#1047)
Browse files Browse the repository at this point in the history
Adds support for setting the last observed event nonce from compass from within the constructor input during smart compass uploading. This allows updating the deployed compass contract without losing the current nonce value.
  • Loading branch information
byte-bandit authored Nov 28, 2023
1 parent 7e509e8 commit d51a7db
Show file tree
Hide file tree
Showing 14 changed files with 674 additions and 10 deletions.
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ func New(
app.EvmKeeper,
gravitymodulekeeper.NewGravityStoreGetter(keys[gravitymoduletypes.StoreKey]),
)
// TODO: Use proper dependency resolution instead of
// this abomination
app.EvmKeeper.Gravity = app.GravityKeeper

app.PalomaKeeper = *palomamodulekeeper.NewKeeper(
appCodec,
Expand Down
1 change: 1 addition & 0 deletions x/evm/keeper/attest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var _ = g.Describe("attest router", func() {
consensukeeper = ms.ConsensusKeeper
q = consensusmocks.NewQueuer(t)
isGoodcase = true
ms.GravityKeeper.On("GetLastObservedEventNonce", mock.Anything).Return(uint64(100), nil).Maybe()
})

g.BeforeEach(func() {
Expand Down
1 change: 1 addition & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type Keeper struct {
ConsensusKeeper types.ConsensusKeeper
SchedulerKeeper types.SchedulerKeeper
Valset types.ValsetKeeper
Gravity types.GravityKeeper
ider keeperutil.IDGenerator
msgSender types.MsgSender
msgAssigner types.MsgAssigner
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/keeper_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package keeper_test

import (
"fmt"
"io/ioutil"
"math/big"
"os"
"strings"
"testing"
"time"
Expand All @@ -30,8 +30,8 @@ import (
)

var (
contractAbi = string(whoops.Must(ioutil.ReadFile("testdata/sample-abi.json")))
contractBytecodeStr = string(whoops.Must(ioutil.ReadFile("testdata/sample-bytecode.out")))
contractAbi = string(whoops.Must(os.ReadFile("testdata/sample-abi.json")))
contractBytecodeStr = string(whoops.Must(os.ReadFile("testdata/sample-bytecode.out")))
)

func genValidators(numValidators, totalConsPower int) []stakingtypes.Validator {
Expand Down
8 changes: 8 additions & 0 deletions x/evm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ func buildKeeper(t *testing.T) (*Keeper, sdk.Context, mockedServices) {
// test-chain mocks
mockServices.ValsetKeeper.On("GetCurrentSnapshot", mock.Anything).Return(unpublishedSnapshot, nil)
mockServices.ConsensusKeeper.On("PutMessageInQueue", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(uint64(0), nil)
mockServices.GravityKeeper.On("GetLastObservedEventNonce", mock.Anything).Return(uint64(100), nil)

// invalid-test-chain mocks
mockServices.ValsetKeeper.On("GetCurrentSnapshot", mock.Anything).Return(unpublishedSnapshot, nil)
mockServices.ConsensusKeeper.On("PutMessageInQueue", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(uint64(0), nil)
mockServices.GravityKeeper.On("GetLastObservedEventNonce", mock.Anything).Return(uint64(100), nil)

// Add 2 new chains for our tests to use
err := k.AddSupportForNewChain(
Expand Down Expand Up @@ -135,6 +137,7 @@ func TestKeeper_PreJobExecution(t *testing.T) {
setupMocks: func(ctx sdk.Context, k *Keeper) {
valsetKeeperMock := mocks.NewValsetKeeper(t)
msgSenderMock := mocks.NewMsgSender(t)
gravityKeeperMock := mocks.NewGravityKeeper(t)

unpublishedSnapshot := &valsettypes.Snapshot{
Id: 1,
Expand Down Expand Up @@ -190,6 +193,7 @@ func TestKeeper_PreJobExecution(t *testing.T) {

k.Valset = valsetKeeperMock
k.msgSender = msgSenderMock
k.Gravity = gravityKeeperMock
},
expectedError: nil,
},
Expand All @@ -208,6 +212,7 @@ func TestKeeper_PreJobExecution(t *testing.T) {
chainReferenceID: "test-chain",
setupMocks: func(ctx sdk.Context, k *Keeper) {
valsetKeeperMock := mocks.NewValsetKeeper(t)
gravityKeeperMock := mocks.NewGravityKeeper(t)

publishedSnapshot := &valsettypes.Snapshot{
Id: 1,
Expand Down Expand Up @@ -251,6 +256,7 @@ func TestKeeper_PreJobExecution(t *testing.T) {
valsetKeeperMock.On("GetLatestSnapshotOnChain", mock.Anything, mock.Anything).Return(publishedSnapshot, nil)

k.Valset = valsetKeeperMock
k.Gravity = gravityKeeperMock
// Success is indicated by returning nil before calling msgSender.SendValsetMsgForChain
},
expectedError: nil,
Expand All @@ -260,6 +266,7 @@ func TestKeeper_PreJobExecution(t *testing.T) {
chainReferenceID: "inactive-test-chain",
setupMocks: func(ctx sdk.Context, k *Keeper) {
valsetKeeperMock := mocks.NewValsetKeeper(t)
gravityKeeperMock := mocks.NewGravityKeeper(t)

unpublishedSnapshot := &valsettypes.Snapshot{
Id: 1,
Expand Down Expand Up @@ -306,6 +313,7 @@ func TestKeeper_PreJobExecution(t *testing.T) {
valsetKeeperMock.On("GetLatestSnapshotOnChain", mock.Anything, mock.Anything).Return(publishedSnapshot, nil)

k.Valset = valsetKeeperMock
k.Gravity = gravityKeeperMock
// Success is indicated by returning nil before calling msgSender.SendValsetMsgForChain
},
expectedError: nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func addDeploymentToKeeper(t *testing.T, ctx sdk.Context, k *Keeper, mockService
// test-chain mocks
mockServices.ValsetKeeper.On("GetCurrentSnapshot", mock.Anything).Return(unpublishedSnapshot, nil)
mockServices.ConsensusKeeper.On("PutMessageInQueue", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(uint64(10), nil)
mockServices.GravityKeeper.On("GetLastObservedEventNonce", mock.Anything).Return(uint64(100), nil)

// Add a new chains for our test to use
err := k.AddSupportForNewChain(
Expand Down
3 changes: 3 additions & 0 deletions x/evm/keeper/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type mockedServices struct {
ConsensusKeeper *mocks.ConsensusKeeper
ValsetKeeper *mocks.ValsetKeeper
MsgSender *mocks.MsgSender
GravityKeeper *mocks.GravityKeeper
}

func NewEvmKeeper(t testutil.TB) (*Keeper, mockedServices, sdk.Context) {
Expand Down Expand Up @@ -48,6 +49,7 @@ func NewEvmKeeper(t testutil.TB) (*Keeper, mockedServices, sdk.Context) {
ConsensusKeeper: mocks.NewConsensusKeeper(t),
ValsetKeeper: mocks.NewValsetKeeper(t),
MsgSender: mocks.NewMsgSender(t),
GravityKeeper: mocks.NewGravityKeeper(t),
}
k := NewKeeper(
appCodec,
Expand All @@ -59,6 +61,7 @@ func NewEvmKeeper(t testutil.TB) (*Keeper, mockedServices, sdk.Context) {
)

k.msgSender = ms.MsgSender
k.Gravity = ms.GravityKeeper

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

Expand Down
8 changes: 7 additions & 1 deletion x/evm/keeper/smart_contract_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"errors"
"fmt"
"math/big"
"strings"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -229,8 +230,13 @@ func (k Keeper) deploySmartContractToChain(ctx sdk.Context, chainInfo *types.Cha

k.createSmartContractDeployment(ctx, smartContract, chainInfo, uniqueID[:])

lastEventNonce, err := k.Gravity.GetLastObservedEventNonce(ctx)
if err != nil {
return fmt.Errorf("failed to get last observed event nonce: %w", err)
}

// set the smart contract constructor arguments
input, err := contractABI.Pack("", uniqueID, types.TransformValsetToABIValset(valset))
input, err := contractABI.Pack("", uniqueID, (&big.Int{}).SetUint64(lastEventNonce), types.TransformValsetToABIValset(valset))

logger.Info(
"transform valset to abi valset",
Expand Down
Loading

0 comments on commit d51a7db

Please sign in to comment.