mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-25 17:34:36 +00:00
update package types
This commit is contained in:
+4
-113
@@ -6,15 +6,15 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tendermint/tendermint/pkg/consensus"
|
||||
meta "github.com/tendermint/tendermint/pkg/metadata"
|
||||
"github.com/tendermint/tendermint/pkg/metadata"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
)
|
||||
|
||||
// LightBlock is a SignedHeader and a ValidatorSet.
|
||||
// It is the basis of the light client
|
||||
type LightBlock struct {
|
||||
*SignedHeader `json:"signed_header"`
|
||||
ValidatorSet *consensus.ValidatorSet `json:"validator_set"`
|
||||
*metadata.SignedHeader `json:"signed_header"`
|
||||
ValidatorSet *consensus.ValidatorSet `json:"validator_set"`
|
||||
}
|
||||
|
||||
// ValidateBasic checks that the data is correct and consistent
|
||||
@@ -95,7 +95,7 @@ func LightBlockFromProto(pb *tmproto.LightBlock) (*LightBlock, error) {
|
||||
lb := new(LightBlock)
|
||||
|
||||
if pb.SignedHeader != nil {
|
||||
sh, err := SignedHeaderFromProto(pb.SignedHeader)
|
||||
sh, err := metadata.SignedHeaderFromProto(pb.SignedHeader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -112,112 +112,3 @@ func LightBlockFromProto(pb *tmproto.LightBlock) (*LightBlock, error) {
|
||||
|
||||
return lb, nil
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// SignedHeader is a header along with the commits that prove it.
|
||||
type SignedHeader struct {
|
||||
*meta.Header `json:"header"`
|
||||
|
||||
Commit *meta.Commit `json:"commit"`
|
||||
}
|
||||
|
||||
// ValidateBasic does basic consistency checks and makes sure the header
|
||||
// and commit are consistent.
|
||||
//
|
||||
// NOTE: This does not actually check the cryptographic signatures. Make sure
|
||||
// to use a Verifier to validate the signatures actually provide a
|
||||
// significantly strong proof for this header's validity.
|
||||
func (sh SignedHeader) ValidateBasic(chainID string) error {
|
||||
if sh.Header == nil {
|
||||
return errors.New("missing header")
|
||||
}
|
||||
if sh.Commit == nil {
|
||||
return errors.New("missing commit")
|
||||
}
|
||||
|
||||
if err := sh.Header.ValidateBasic(); err != nil {
|
||||
return fmt.Errorf("invalid header: %w", err)
|
||||
}
|
||||
if err := sh.Commit.ValidateBasic(); err != nil {
|
||||
return fmt.Errorf("invalid commit: %w", err)
|
||||
}
|
||||
|
||||
if sh.ChainID != chainID {
|
||||
return fmt.Errorf("header belongs to another chain %q, not %q", sh.ChainID, chainID)
|
||||
}
|
||||
|
||||
// Make sure the header is consistent with the commit.
|
||||
if sh.Commit.Height != sh.Height {
|
||||
return fmt.Errorf("header and commit height mismatch: %d vs %d", sh.Height, sh.Commit.Height)
|
||||
}
|
||||
if hhash, chash := sh.Header.Hash(), sh.Commit.BlockID.Hash; !bytes.Equal(hhash, chash) {
|
||||
return fmt.Errorf("commit signs block %X, header is block %X", chash, hhash)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// String returns a string representation of SignedHeader.
|
||||
func (sh SignedHeader) String() string {
|
||||
return sh.StringIndented("")
|
||||
}
|
||||
|
||||
// StringIndented returns an indented string representation of SignedHeader.
|
||||
//
|
||||
// Header
|
||||
// Commit
|
||||
func (sh SignedHeader) StringIndented(indent string) string {
|
||||
return fmt.Sprintf(`SignedHeader{
|
||||
%s %v
|
||||
%s %v
|
||||
%s}`,
|
||||
indent, sh.Header.StringIndented(indent+" "),
|
||||
indent, sh.Commit.StringIndented(indent+" "),
|
||||
indent)
|
||||
}
|
||||
|
||||
// ToProto converts SignedHeader to protobuf
|
||||
func (sh *SignedHeader) ToProto() *tmproto.SignedHeader {
|
||||
if sh == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
psh := new(tmproto.SignedHeader)
|
||||
if sh.Header != nil {
|
||||
psh.Header = sh.Header.ToProto()
|
||||
}
|
||||
if sh.Commit != nil {
|
||||
psh.Commit = sh.Commit.ToProto()
|
||||
}
|
||||
|
||||
return psh
|
||||
}
|
||||
|
||||
// FromProto sets a protobuf SignedHeader to the given pointer.
|
||||
// It returns an error if the header or the commit is invalid.
|
||||
func SignedHeaderFromProto(shp *tmproto.SignedHeader) (*SignedHeader, error) {
|
||||
if shp == nil {
|
||||
return nil, errors.New("nil SignedHeader")
|
||||
}
|
||||
|
||||
sh := new(SignedHeader)
|
||||
|
||||
if shp.Header != nil {
|
||||
h, err := meta.HeaderFromProto(shp.Header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sh.Header = &h
|
||||
}
|
||||
|
||||
if shp.Commit != nil {
|
||||
c, err := meta.CommitFromProto(shp.Commit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sh.Commit = c
|
||||
}
|
||||
|
||||
return sh, nil
|
||||
}
|
||||
|
||||
+7
-94
@@ -1,14 +1,11 @@
|
||||
package light_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
test "github.com/tendermint/tendermint/internal/test/factory"
|
||||
"github.com/tendermint/tendermint/pkg/consensus"
|
||||
"github.com/tendermint/tendermint/pkg/light"
|
||||
@@ -29,21 +26,21 @@ func TestLightBlockValidateBasic(t *testing.T) {
|
||||
vals3.Proposer = &consensus.Validator{}
|
||||
commit.BlockID.Hash = header.Hash()
|
||||
|
||||
sh := &light.SignedHeader{
|
||||
sh := &metadata.SignedHeader{
|
||||
Header: header,
|
||||
Commit: commit,
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
sh *light.SignedHeader
|
||||
sh *metadata.SignedHeader
|
||||
vals *consensus.ValidatorSet
|
||||
expectErr bool
|
||||
}{
|
||||
{"valid light block", sh, vals, false},
|
||||
{"hashes don't match", sh, vals2, true},
|
||||
{"invalid validator set", sh, vals3, true},
|
||||
{"invalid signed header", &light.SignedHeader{Header: header, Commit: test.MakeRandomCommit(time.Now())}, vals, true},
|
||||
{"invalid signed header", &metadata.SignedHeader{Header: header, Commit: test.MakeRandomCommit(time.Now())}, vals, true},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
@@ -73,22 +70,22 @@ func TestLightBlockProtobuf(t *testing.T) {
|
||||
vals3.Proposer = &consensus.Validator{}
|
||||
commit.BlockID.Hash = header.Hash()
|
||||
|
||||
sh := &light.SignedHeader{
|
||||
sh := &metadata.SignedHeader{
|
||||
Header: header,
|
||||
Commit: commit,
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
sh *light.SignedHeader
|
||||
sh *metadata.SignedHeader
|
||||
vals *consensus.ValidatorSet
|
||||
toProtoErr bool
|
||||
toBlockErr bool
|
||||
}{
|
||||
{"valid light block", sh, vals, false, false},
|
||||
{"empty signed header", &light.SignedHeader{}, vals, false, false},
|
||||
{"empty signed header", &metadata.SignedHeader{}, vals, false, false},
|
||||
{"empty validator set", sh, &consensus.ValidatorSet{}, false, true},
|
||||
{"empty light block", &light.SignedHeader{}, &consensus.ValidatorSet{}, false, true},
|
||||
{"empty light block", &metadata.SignedHeader{}, &consensus.ValidatorSet{}, false, true},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
@@ -113,87 +110,3 @@ func TestLightBlockProtobuf(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestSignedHeaderValidateBasic(t *testing.T) {
|
||||
commit := test.MakeRandomCommit(time.Now())
|
||||
chainID := "𠜎"
|
||||
timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
|
||||
h := metadata.Header{
|
||||
Version: version.Consensus{Block: version.BlockProtocol, App: math.MaxInt64},
|
||||
ChainID: chainID,
|
||||
Height: commit.Height,
|
||||
Time: timestamp,
|
||||
LastBlockID: commit.BlockID,
|
||||
LastCommitHash: commit.Hash(),
|
||||
DataHash: commit.Hash(),
|
||||
ValidatorsHash: commit.Hash(),
|
||||
NextValidatorsHash: commit.Hash(),
|
||||
ConsensusHash: commit.Hash(),
|
||||
AppHash: commit.Hash(),
|
||||
LastResultsHash: commit.Hash(),
|
||||
EvidenceHash: commit.Hash(),
|
||||
ProposerAddress: crypto.AddressHash([]byte("proposer_address")),
|
||||
}
|
||||
|
||||
validSignedHeader := light.SignedHeader{Header: &h, Commit: commit}
|
||||
validSignedHeader.Commit.BlockID.Hash = validSignedHeader.Hash()
|
||||
invalidSignedHeader := light.SignedHeader{}
|
||||
|
||||
testCases := []struct {
|
||||
testName string
|
||||
shHeader *metadata.Header
|
||||
shCommit *metadata.Commit
|
||||
expectErr bool
|
||||
}{
|
||||
{"Valid Signed Header", validSignedHeader.Header, validSignedHeader.Commit, false},
|
||||
{"Invalid Signed Header", invalidSignedHeader.Header, validSignedHeader.Commit, true},
|
||||
{"Invalid Signed Header", validSignedHeader.Header, invalidSignedHeader.Commit, true},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.testName, func(t *testing.T) {
|
||||
sh := light.SignedHeader{
|
||||
Header: tc.shHeader,
|
||||
Commit: tc.shCommit,
|
||||
}
|
||||
err := sh.ValidateBasic(validSignedHeader.Header.ChainID)
|
||||
assert.Equalf(
|
||||
t,
|
||||
tc.expectErr,
|
||||
err != nil,
|
||||
"Validate Basic had an unexpected result",
|
||||
err,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignedHeaderProtoBuf(t *testing.T) {
|
||||
commit := test.MakeRandomCommit(time.Now())
|
||||
h := test.MakeRandomHeader()
|
||||
|
||||
sh := light.SignedHeader{Header: h, Commit: commit}
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
sh1 *light.SignedHeader
|
||||
expPass bool
|
||||
}{
|
||||
{"empty SignedHeader 2", &light.SignedHeader{}, true},
|
||||
{"success", &sh, true},
|
||||
{"failure nil", nil, false},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
protoSignedHeader := tc.sh1.ToProto()
|
||||
|
||||
sh, err := light.SignedHeaderFromProto(protoSignedHeader)
|
||||
|
||||
if tc.expPass {
|
||||
require.NoError(t, err, tc.msg)
|
||||
require.Equal(t, tc.sh1, sh, tc.msg)
|
||||
} else {
|
||||
require.Error(t, err, tc.msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user