generated from dymensionxyz/rollapp
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: consensus messages #351
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4413d83
this is working now, admission handler basic.
bd42e72
first attempt to make test pass
5aa752a
add begin blocker test
44b4400
fix pr
2662ced
goimports
5a6db76
comment admission handler example
9e43f29
goimports
36fe674
Merge remote-tracking branch 'origin/main' into feat/consensus-messages
8716c18
fix some comments
7bd66c2
add marshall error
e053b23
update cometbft version (#354)
omritoptix ed2d775
move ProcessConsensusMessages function to rdk
96f66b6
Merge branch 'feat/consensus-messages' of github.com-faulty:dymension…
1175373
Merge remote-tracking branch 'origin/main' into feat/consensus-messages
e60fa7f
update with real dependencies
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package app | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/gogo/protobuf/proto" | ||
prototypes "github.com/gogo/protobuf/types" | ||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/proto/tendermint/types" | ||
) | ||
|
||
func TestBeginBlocker(t *testing.T) { | ||
app, valAccount := SetupWithOneValidator(t) | ||
ctx := app.NewUncachedContext(true, types.Header{ | ||
Height: 1, | ||
ChainID: "testchain_9000-1", | ||
}) | ||
|
||
bankSend := &banktypes.MsgSend{ | ||
FromAddress: valAccount.GetAddress().String(), | ||
ToAddress: valAccount.GetAddress().String(), | ||
Amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))), | ||
} | ||
msgBz, err := proto.Marshal(bankSend) | ||
require.NoError(t, err) | ||
|
||
goodMessage := &prototypes.Any{ | ||
TypeUrl: proto.MessageName(&banktypes.MsgSend{}), | ||
Value: msgBz, | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
consensusMsgs []*prototypes.Any | ||
expectError bool | ||
}{ | ||
{ | ||
name: "ValidConsensusMessage", | ||
consensusMsgs: []*prototypes.Any{ | ||
goodMessage, | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "InvalidUnpackMessage", | ||
consensusMsgs: []*prototypes.Any{ | ||
{ | ||
TypeUrl: "/path.to.InvalidMsg", | ||
Value: []byte("invalid unpack data"), | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "InvalidExecutionMessage", | ||
consensusMsgs: []*prototypes.Any{ | ||
{ | ||
TypeUrl: "/path.to.ExecErrorMsg", | ||
Value: []byte("execution error data"), | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
req := abci.RequestBeginBlock{ | ||
Header: types.Header{ | ||
Height: 1, | ||
Time: ctx.BlockTime(), | ||
ChainID: "testchain_9000-1", | ||
}, | ||
LastCommitInfo: abci.LastCommitInfo{}, | ||
ByzantineValidators: []abci.Evidence{}, | ||
ConsensusMessages: tc.consensusMsgs, | ||
} | ||
|
||
res := app.BeginBlocker(ctx, req) | ||
require.NotNil(t, res) | ||
|
||
if tc.expectError { | ||
require.NotEmpty(t, res.ConsensusMessagesResponses) | ||
for _, response := range res.ConsensusMessagesResponses { | ||
_, isError := response.Response.(*abci.ConsensusMessageResponse_Error) | ||
require.True(t, isError, "Expected an error response but got a success") | ||
} | ||
} else { | ||
require.NotEmpty(t, res.ConsensusMessagesResponses) | ||
for _, response := range res.ConsensusMessagesResponses { | ||
_, isOk := response.Response.(*abci.ConsensusMessageResponse_Ok) | ||
require.True(t, isOk, "Expected a success response but got an error") | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,208 @@ | ||
package app | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
appcodec "github.com/cosmos/cosmos-sdk/codec" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/crypto/codec" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/testutil/mock" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"github.com/dymensionxyz/dymension-rdk/testutil/utils" | ||
rollappparamstypes "github.com/dymensionxyz/dymension-rdk/x/rollappparams/types" | ||
"github.com/tendermint/tendermint/crypto/encoding" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
types2 "github.com/tendermint/tendermint/types" | ||
dbm "github.com/tendermint/tm-db" | ||
|
||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
) | ||
|
||
const TestChainID = "testchain_9000-1" | ||
|
||
func setup(withGenesis bool, invCheckPeriod uint) (*App, GenesisState) { | ||
db := dbm.NewMemDB() | ||
|
||
app := NewRollapp( | ||
log.NewNopLogger(), | ||
db, | ||
nil, | ||
true, | ||
map[int64]bool{}, | ||
DefaultNodeHome, | ||
invCheckPeriod, | ||
MakeEncodingConfig(), | ||
simapp.EmptyAppOptions{}, | ||
) | ||
|
||
if withGenesis { | ||
return app, NewDefaultGenesisState(app.appCodec) | ||
} | ||
|
||
return app, GenesisState{} | ||
} | ||
|
||
// SetupWithOneValidator initializes a new App. A Nop logger is set in App. | ||
func SetupWithOneValidator(t *testing.T) (*App, authtypes.AccountI) { | ||
t.Helper() | ||
|
||
privVal := mock.NewPV() | ||
pubKey, err := privVal.GetPubKey() | ||
require.NoError(t, err) | ||
|
||
// create validator set with single validator | ||
validator := types2.NewValidator(pubKey, 1) | ||
valSet := types2.NewValidatorSet([]*types2.Validator{validator}) | ||
|
||
// generate genesis account | ||
senderPrivKey := secp256k1.GenPrivKey() | ||
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) | ||
balance := banktypes.Balance{ | ||
Address: acc.GetAddress().String(), | ||
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))), | ||
} | ||
fmt.Printf("address: %s\n", acc.GetAddress().String()) | ||
|
||
app := SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance) | ||
|
||
return app, acc | ||
} | ||
|
||
// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts | ||
// that also act as delegators. For simplicity, each validator is bonded with a delegation | ||
// of one consensus engine unit in the default token of the simapp from first genesis | ||
// account. A Nop logger is set in SimApp. | ||
func SetupWithGenesisValSet(t *testing.T, valSet *types2.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *App { | ||
t.Helper() | ||
|
||
app, genesisState := setup(true, 5) | ||
genesisState = genesisStateWithValSet(t, app, genesisState, valSet, genAccs, balances...) | ||
|
||
genesisState = setRollappVersion(app.appCodec, genesisState, "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0") | ||
|
||
stateBytes, err := json.MarshalIndent(genesisState, "", " ") | ||
require.NoError(t, err) | ||
|
||
proto, err := encoding.PubKeyToProto(valSet.Validators[0].PubKey) | ||
require.NoError(t, err) | ||
|
||
// init chain will set the validator set and initialize the genesis accounts | ||
app.InitChain( | ||
abci.RequestInitChain{ | ||
Validators: []abci.ValidatorUpdate{ | ||
{ | ||
PubKey: proto, | ||
Power: valSet.Validators[0].VotingPower, | ||
}, | ||
}, | ||
ConsensusParams: utils.DefaultConsensusParams, | ||
AppStateBytes: stateBytes, | ||
ChainId: TestChainID, | ||
}, | ||
) | ||
|
||
// commit genesis changes | ||
app.Commit() | ||
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ | ||
ChainID: TestChainID, | ||
Height: app.LastBlockHeight() + 1, | ||
AppHash: app.LastCommitID().Hash, | ||
ValidatorsHash: valSet.Hash(), | ||
NextValidatorsHash: valSet.Hash(), | ||
}}) | ||
|
||
return app | ||
} | ||
|
||
func genesisStateWithValSet(t *testing.T, | ||
app *App, genesisState GenesisState, | ||
valSet *types2.ValidatorSet, genAccs []authtypes.GenesisAccount, | ||
balances ...banktypes.Balance, | ||
) GenesisState { | ||
// set genesis accounts | ||
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) | ||
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) | ||
|
||
validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) | ||
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) | ||
|
||
bondAmt := sdk.DefaultPowerReduction | ||
|
||
for _, val := range valSet.Validators { | ||
pk, err := codec.FromTmPubKeyInterface(val.PubKey) | ||
require.NoError(t, err) | ||
pkAny, err := codectypes.NewAnyWithValue(pk) | ||
require.NoError(t, err) | ||
validator := stakingtypes.Validator{ | ||
OperatorAddress: sdk.ValAddress(val.Address).String(), | ||
ConsensusPubkey: pkAny, | ||
Jailed: false, | ||
Status: stakingtypes.Bonded, | ||
Tokens: bondAmt, | ||
DelegatorShares: sdk.OneDec(), | ||
Description: stakingtypes.Description{}, | ||
UnbondingHeight: int64(0), | ||
UnbondingTime: time.Unix(0, 0).UTC(), | ||
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), | ||
MinSelfDelegation: sdk.ZeroInt(), | ||
} | ||
validators = append(validators, validator) | ||
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) | ||
|
||
} | ||
// set validators and delegations | ||
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) | ||
genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) | ||
|
||
totalSupply := sdk.NewCoins() | ||
for _, b := range balances { | ||
// add genesis acc tokens to total supply | ||
totalSupply = totalSupply.Add(b.Coins...) | ||
} | ||
|
||
for range delegations { | ||
// add delegated tokens to total supply | ||
totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)) | ||
} | ||
|
||
// add bonded amount to bonded pool module account | ||
balances = append(balances, banktypes.Balance{ | ||
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), | ||
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, | ||
}) | ||
|
||
// update total supply | ||
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}) | ||
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) | ||
|
||
return genesisState | ||
} | ||
|
||
func setRollappVersion(appCodec appcodec.Codec, genesisState GenesisState, version string) GenesisState { | ||
var rollappParamsGenesis rollappparamstypes.GenesisState | ||
if genesisState["rollappparams"] != nil { | ||
appCodec.MustUnmarshalJSON(genesisState["rollappparams"], &rollappParamsGenesis) | ||
} else { | ||
rollappParamsGenesis = rollappparamstypes.GenesisState{ | ||
Params: rollappparamstypes.Params{ | ||
Version: version, | ||
}, | ||
} | ||
} | ||
|
||
rollappParamsGenesis.Params.Version = version | ||
|
||
genesisState["rollappparams"] = appCodec.MustMarshalJSON(&rollappParamsGenesis) | ||
|
||
return genesisState | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd also suggest verifying the returned error string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danwt did not like to check the strings of errors