-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cons messages): include in header hash for fraud protection (#1288)
- Loading branch information
Showing
17 changed files
with
565 additions
and
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package block_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
proto "github.com/gogo/protobuf/types" | ||
|
||
"github.com/dymensionxyz/dymint/block" | ||
"github.com/dymensionxyz/dymint/p2p" | ||
"github.com/dymensionxyz/dymint/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/proxy" | ||
) | ||
|
||
// produce a block, mutate it, confirm the sig check fails | ||
func TestProduceNewBlockMutation(t *testing.T) { | ||
// Init app | ||
app := testutil.GetAppMock(testutil.Commit, testutil.EndBlock) | ||
commitHash := [32]byte{1} | ||
app.On("Commit", mock.Anything).Return(abci.ResponseCommit{Data: commitHash[:]}) | ||
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{ | ||
RollappParamUpdates: &abci.RollappParams{ | ||
Da: "mock", | ||
DrsVersion: 0, | ||
}, | ||
ConsensusParamUpdates: &abci.ConsensusParams{ | ||
Block: &abci.BlockParams{ | ||
MaxGas: 40000000, | ||
MaxBytes: 500000, | ||
}, | ||
}, | ||
}) | ||
// Create proxy app | ||
clientCreator := proxy.NewLocalClientCreator(app) | ||
proxyApp := proxy.NewAppConns(clientCreator) | ||
err := proxyApp.Start() | ||
require.NoError(t, err) | ||
// Init manager | ||
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil) | ||
require.NoError(t, err) | ||
// Produce block | ||
b, c, err := manager.ProduceApplyGossipBlock(context.Background(), block.ProduceBlockOptions{AllowEmpty: true}) | ||
require.NoError(t, err) | ||
// Validate state is updated with the commit hash | ||
assert.Equal(t, uint64(1), manager.State.Height()) | ||
assert.Equal(t, commitHash, manager.State.AppHash) | ||
|
||
err = b.ValidateBasic() | ||
require.NoError(t, err) | ||
|
||
// TODO: better way to write test is to clone the block and commit and check a table of mutations | ||
bd := p2p.BlockData{ | ||
Block: *b, | ||
Commit: *c, | ||
} | ||
err = bd.Validate(manager.State.GetProposerPubKey()) | ||
require.NoError(t, err) | ||
|
||
b.Data.ConsensusMessages = []*proto.Any{{}} | ||
bd = p2p.BlockData{ | ||
Block: *b, | ||
Commit: *c, | ||
} | ||
err = bd.Validate(manager.State.GetProposerPubKey()) | ||
require.Error(t, err) | ||
} |
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
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,56 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
|
||
proto "github.com/gogo/protobuf/types" | ||
"github.com/tendermint/tendermint/crypto/merkle" | ||
cmtbytes "github.com/tendermint/tendermint/libs/bytes" | ||
_ "github.com/tendermint/tendermint/types" | ||
) | ||
|
||
// a convenience struct to make computing easier | ||
// persisted and over the wire types use a flat representation | ||
type DymHeader struct { | ||
ConsensusMessagesHash [32]byte | ||
} | ||
|
||
func MakeDymHeader(consMessages []*proto.Any) DymHeader { | ||
return DymHeader{ | ||
ConsensusMessagesHash: ConsMessagesHash(consMessages), | ||
} | ||
} | ||
|
||
func (h DymHeader) Hash() cmtbytes.HexBytes { | ||
// 32 bytes long | ||
return merkle.HashFromByteSlices([][]byte{ | ||
h.ConsensusMessagesHash[:], | ||
// can be extended with other things if we need to later | ||
}) | ||
} | ||
|
||
func (h *Header) SetDymHeader(dh DymHeader) { | ||
copy(h.ConsensusMessagesHash[:], dh.ConsensusMessagesHash[:]) | ||
} | ||
|
||
func (h *Header) DymHash() cmtbytes.HexBytes { | ||
ret := DymHeader{} | ||
copy(ret.ConsensusMessagesHash[:], h.ConsensusMessagesHash[:]) | ||
return ret.Hash() | ||
} | ||
|
||
func ConsMessagesHash(msgs []*proto.Any) [32]byte { | ||
bzz := make([][]byte, len(msgs)) | ||
for i, msg := range msgs { | ||
var err error | ||
bzz[i], err = msg.Marshal() | ||
if err != nil { | ||
// Not obvious how to recover here. Shouldn't happen. | ||
panic(fmt.Errorf("marshal consensus message: %w", err)) | ||
} | ||
} | ||
merkleRoot := merkle.HashFromByteSlices(bzz) | ||
ret := [32]byte{} | ||
copy(ret[:], merkleRoot) // merkleRoot is already 32 bytes | ||
return ret | ||
} |
Oops, something went wrong.