From 57e404f76467fd083ef713355af786bd0f982ffd Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 24 Jun 2024 14:41:27 +0200 Subject: [PATCH 1/7] remove 0 hash check --- core/header/service.go | 8 -------- server/v2/cometbft/abci.go | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/core/header/service.go b/core/header/service.go index 53b7a261d559..a6272db6fcda 100644 --- a/core/header/service.go +++ b/core/header/service.go @@ -33,14 +33,6 @@ func (i *Info) Bytes() ([]byte, error) { binary.LittleEndian.PutUint64(heightBytes, uint64(i.Height)) buf = append(buf, heightBytes...) - // TODO; permit empty hash OK for genesis block? - if len(i.Hash) == 0 { - i.Hash = make([]byte, hashSize) - } - // Encode Hash - if len(i.Hash) != hashSize { - return nil, errors.New("invalid hash size") - } buf = append(buf, i.Hash...) // Encode Time diff --git a/server/v2/cometbft/abci.go b/server/v2/cometbft/abci.go index a14a793a00ac..ec9afe116bd8 100644 --- a/server/v2/cometbft/abci.go +++ b/server/v2/cometbft/abci.go @@ -137,7 +137,7 @@ func (c *Consensus[T]) CheckTx(ctx context.Context, req *abciproto.CheckTxReques return nil, err } - cometResp := &abci.CheckTxResponse{ + cometResp := &abciproto.CheckTxResponse{ Code: resp.Code, GasWanted: uint64ToInt64(resp.GasWanted), GasUsed: uint64ToInt64(resp.GasUsed), @@ -171,7 +171,7 @@ func (c *Consensus[T]) Info(ctx context.Context, _ *abciproto.InfoRequest) (*abc return nil, err } - return &abci.InfoResponse{ + return &abciproto.InfoResponse{ Data: c.cfg.Name, Version: c.cfg.Version, // AppVersion: cp.GetVersion().App, @@ -294,7 +294,7 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRe return nil, fmt.Errorf("unable to commit the changeset: %w", err) } - return &abci.InitChainResponse{ + return &abciproto.InitChainResponse{ ConsensusParams: req.ConsensusParams, Validators: validatorUpdates, AppHash: stateRoot, @@ -339,7 +339,7 @@ func (c *Consensus[T]) PrepareProposal( encodedTxs[i] = tx.Bytes() } - return &abci.PrepareProposalResponse{ + return &abciproto.PrepareProposalResponse{ Txs: encodedTxs, }, nil } @@ -371,13 +371,13 @@ func (c *Consensus[T]) ProcessProposal( err := c.processProposalHandler(ciCtx, c.app, decodedTxs, req) if err != nil { c.logger.Error("failed to process proposal", "height", req.Height, "time", req.Time, "hash", fmt.Sprintf("%X", req.Hash), "err", err) - return &abci.ProcessProposalResponse{ - Status: abci.PROCESS_PROPOSAL_STATUS_REJECT, + return &abciproto.ProcessProposalResponse{ + Status: abciproto.PROCESS_PROPOSAL_STATUS_REJECT, }, nil } - return &abci.ProcessProposalResponse{ - Status: abci.PROCESS_PROPOSAL_STATUS_ACCEPT, + return &abciproto.ProcessProposalResponse{ + Status: abciproto.PROCESS_PROPOSAL_STATUS_ACCEPT, }, nil } @@ -505,7 +505,7 @@ func (c *Consensus[T]) Commit(ctx context.Context, _ *abciproto.CommitRequest) ( return nil, err } - return &abci.CommitResponse{ + return &abciproto.CommitResponse{ RetainHeight: c.GetBlockRetentionHeight(cp, lastCommittedBlock.Height), }, nil } @@ -542,7 +542,7 @@ func (c *Consensus[T]) VerifyVoteExtension( resp, err := c.verifyVoteExt(ctx, latestStore, req) if err != nil { c.logger.Error("failed to verify vote extension", "height", req.Height, "err", err) - return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_REJECT}, nil + return &abciproto.VerifyVoteExtensionResponse{Status: abciproto.VERIFY_VOTE_EXTENSION_STATUS_REJECT}, nil } return resp, err @@ -578,7 +578,7 @@ func (c *Consensus[T]) ExtendVote(ctx context.Context, req *abciproto.ExtendVote resp, err := c.extendVote(ctx, latestStore, req) if err != nil { c.logger.Error("failed to verify vote extension", "height", req.Height, "err", err) - return &abci.ExtendVoteResponse{}, nil + return &abciproto.ExtendVoteResponse{}, nil } return resp, err From 3a9557c015b415eef4ee9d089f62531fc6644097 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 24 Jun 2024 14:43:37 +0200 Subject: [PATCH 2/7] move things --- server/v2/stf/core_header_service.go | 40 ++++++++++++++++++++++++++++ server/v2/stf/stf.go | 39 --------------------------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/server/v2/stf/core_header_service.go b/server/v2/stf/core_header_service.go index 1931c28a6a70..4448627828ca 100644 --- a/server/v2/stf/core_header_service.go +++ b/server/v2/stf/core_header_service.go @@ -4,6 +4,7 @@ import ( "context" "cosmossdk.io/core/header" + "cosmossdk.io/core/store" ) var _ header.Service = (*HeaderService)(nil) @@ -13,3 +14,42 @@ type HeaderService struct{} func (h HeaderService) HeaderInfo(ctx context.Context) header.Info { return ctx.(*executionContext).headerInfo } + +const headerInfoPrefix = 0x37 + +// setHeaderInfo sets the header info in the state to be used by queries in the future. +func (s STF[T]) setHeaderInfo(state store.WriterMap, headerInfo header.Info) error { + // TODO storing header info is too low level here, stf should be stateless. + // We should have a keeper that does this. + runtimeStore, err := state.GetWriter(Identity) + if err != nil { + return err + } + bz, err := headerInfo.Bytes() + if err != nil { + return err + } + err = runtimeStore.Set([]byte{headerInfoPrefix}, bz) + if err != nil { + return err + } + return nil +} + +// getHeaderInfo gets the header info from the state. It should only be used for queries +func (s STF[T]) getHeaderInfo(state store.WriterMap) (i header.Info, err error) { + runtimeStore, err := state.GetWriter(Identity) + if err != nil { + return header.Info{}, err + } + v, err := runtimeStore.Get([]byte{headerInfoPrefix}) + if err != nil { + return header.Info{}, err + } + if v == nil { + return header.Info{}, nil + } + + err = i.FromBytes(v) + return i, err +} diff --git a/server/v2/stf/stf.go b/server/v2/stf/stf.go index 6707e1d8826d..f76dbc7f32e5 100644 --- a/server/v2/stf/stf.go +++ b/server/v2/stf/stf.go @@ -423,45 +423,6 @@ func (s STF[T]) validatorUpdates( return ctx.events, valSetUpdates, nil } -const headerInfoPrefix = 0x37 - -// setHeaderInfo sets the header info in the state to be used by queries in the future. -func (s STF[T]) setHeaderInfo(state store.WriterMap, headerInfo header.Info) error { - // TODO storing header info is too low level here, stf should be stateless. - // We should have a keeper that does this. - runtimeStore, err := state.GetWriter(Identity) - if err != nil { - return err - } - bz, err := headerInfo.Bytes() - if err != nil { - return err - } - err = runtimeStore.Set([]byte{headerInfoPrefix}, bz) - if err != nil { - return err - } - return nil -} - -// getHeaderInfo gets the header info from the state. It should only be used for queries -func (s STF[T]) getHeaderInfo(state store.WriterMap) (i header.Info, err error) { - runtimeStore, err := state.GetWriter(Identity) - if err != nil { - return header.Info{}, err - } - v, err := runtimeStore.Get([]byte{headerInfoPrefix}) - if err != nil { - return header.Info{}, err - } - if v == nil { - return header.Info{}, nil - } - - err = i.FromBytes(v) - return i, err -} - // Simulate simulates the execution of a tx on the provided state. func (s STF[T]) Simulate( ctx context.Context, From f742bae77d9a062c5c6e2030e450b62299aa7de7 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 24 Jun 2024 14:55:35 +0200 Subject: [PATCH 3/7] attempt --- server/v2/cometbft/abci.go | 7 ++++++- store/v2/root/store.go | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/server/v2/cometbft/abci.go b/server/v2/cometbft/abci.go index ec9afe116bd8..b8960b7c9f45 100644 --- a/server/v2/cometbft/abci.go +++ b/server/v2/cometbft/abci.go @@ -254,10 +254,15 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRe }) } + ci, err := c.store.LastCommitID() + if err != nil { + return nil, err + } + br := &coreappmgr.BlockRequest[T]{ Height: uint64(req.InitialHeight - 1), Time: req.Time, - Hash: nil, + Hash: ci.Hash, AppHash: nil, ChainId: req.ChainId, ConsensusMessages: consMessages, diff --git a/store/v2/root/store.go b/store/v2/root/store.go index e5543a91a2cc..4d6f9863a4fd 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -2,6 +2,7 @@ package root import ( "bytes" + "crypto/sha256" "errors" "fmt" "sync" @@ -148,7 +149,10 @@ func (s *Store) LastCommitID() (proof.CommitID, error) { return proof.CommitID{}, err } - return proof.CommitID{Version: latestVersion}, nil + // if the latest version is 0, we return a CommitID with version 0 and a hash of an empty byte slice + bz := sha256.Sum256([]byte{}) + + return proof.CommitID{Version: latestVersion, Hash: bz[:]}, nil } // GetLatestVersion returns the latest version based on the latest internal From ea34476b39d91b931ceb99cdc1cb8c6a641002cc Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 25 Jun 2024 16:21:51 +0200 Subject: [PATCH 4/7] test --- core/header/service.go | 8 ++++++++ scripts/build/build.mk | 8 ++++++-- store/v2/root/store.go | 1 - 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/header/service.go b/core/header/service.go index a6272db6fcda..de04f405356a 100644 --- a/core/header/service.go +++ b/core/header/service.go @@ -33,6 +33,14 @@ func (i *Info) Bytes() ([]byte, error) { binary.LittleEndian.PutUint64(heightBytes, uint64(i.Height)) buf = append(buf, heightBytes...) + if len(i.Hash) == 0 { + i.Hash = make([]byte, hashSize) + } + // Encode Hash + if len(i.Hash) != hashSize { + return nil, errors.New("invalid hash size") + } + buf = append(buf, i.Hash...) // Encode Time diff --git a/scripts/build/build.mk b/scripts/build/build.mk index d5b43eb9e784..1fb1908d7531 100644 --- a/scripts/build/build.mk +++ b/scripts/build/build.mk @@ -6,7 +6,7 @@ export COMMIT := $(shell git log -1 --format='%H') LEDGER_ENABLED ?= true BINDIR ?= $(GOPATH)/bin BUILDDIR ?= $(CURDIR)/build -SIMAPP = ./simapp +SIMAPP = simapp MOCKS_DIR = $(CURDIR)/tests/mocks HTTPS_GIT := https://github.com/cosmos/cosmos-sdk.git DOCKER := $(shell which docker) @@ -49,6 +49,10 @@ ifeq (legacy,$(findstring legacy,$(COSMOS_BUILD_OPTIONS))) build_tags += app_v1 endif +ifeq (v2,$(findstring v2,$(COSMOS_BUILD_OPTIONS))) + SIMAPP = simapp/v2 +endif + # DB backend selection ifeq (cleveldb,$(findstring cleveldb,$(COSMOS_BUILD_OPTIONS))) build_tags += gcc @@ -123,7 +127,7 @@ build-linux-arm64: GOOS=linux GOARCH=arm64 LEDGER_ENABLED=false $(MAKE) build $(BUILD_TARGETS): go.sum $(BUILDDIR)/ - cd ${CURRENT_DIR}/simapp && go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./... + cd ${CURRENT_DIR}/${SIMAPP} && go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./... $(BUILDDIR)/: mkdir -p $(BUILDDIR)/ diff --git a/store/v2/root/store.go b/store/v2/root/store.go index 4d6f9863a4fd..c8374372e6b6 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -148,7 +148,6 @@ func (s *Store) LastCommitID() (proof.CommitID, error) { if err != nil { return proof.CommitID{}, err } - // if the latest version is 0, we return a CommitID with version 0 and a hash of an empty byte slice bz := sha256.Sum256([]byte{}) From 5a365063cf8006508b30ba2c052f4e5ec9f687bc Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Fri, 28 Jun 2024 16:19:45 +0200 Subject: [PATCH 5/7] fix empty apphash --- core/header/service.go | 6 +++--- server/v2/cometbft/abci.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/header/service.go b/core/header/service.go index de04f405356a..6d15c6bb2af7 100644 --- a/core/header/service.go +++ b/core/header/service.go @@ -48,9 +48,9 @@ func (i *Info) Bytes() ([]byte, error) { binary.LittleEndian.PutUint64(timeBytes, uint64(i.Time.Unix())) buf = append(buf, timeBytes...) - if len(i.AppHash) == 0 { - i.AppHash = make([]byte, hashSize) - } + // if len(i.AppHash) == 0 { + // i.AppHash = make([]byte, hashSize) + // } // Encode AppHash if len(i.Hash) != hashSize { return nil, errors.New("invalid hash size") diff --git a/server/v2/cometbft/abci.go b/server/v2/cometbft/abci.go index 072b6816cfba..d2cb78751da4 100644 --- a/server/v2/cometbft/abci.go +++ b/server/v2/cometbft/abci.go @@ -234,8 +234,8 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRe br := &coreappmgr.BlockRequest[T]{ Height: uint64(req.InitialHeight - 1), Time: req.Time, - Hash: ci.Hash, - AppHash: nil, + Hash: nil, + AppHash: ci.Hash, ChainId: req.ChainId, ConsensusMessages: consMessages, IsGenesis: true, From 3f3f09c95d62f321c44c1b1ea81ae373f7682411 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Fri, 28 Jun 2024 16:25:46 +0200 Subject: [PATCH 6/7] use empty hash in place of nil --- core/header/service.go | 8 +------- server/v2/cometbft/abci.go | 6 +++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/core/header/service.go b/core/header/service.go index 6d15c6bb2af7..15e6d4257425 100644 --- a/core/header/service.go +++ b/core/header/service.go @@ -33,9 +33,6 @@ func (i *Info) Bytes() ([]byte, error) { binary.LittleEndian.PutUint64(heightBytes, uint64(i.Height)) buf = append(buf, heightBytes...) - if len(i.Hash) == 0 { - i.Hash = make([]byte, hashSize) - } // Encode Hash if len(i.Hash) != hashSize { return nil, errors.New("invalid hash size") @@ -48,11 +45,8 @@ func (i *Info) Bytes() ([]byte, error) { binary.LittleEndian.PutUint64(timeBytes, uint64(i.Time.Unix())) buf = append(buf, timeBytes...) - // if len(i.AppHash) == 0 { - // i.AppHash = make([]byte, hashSize) - // } // Encode AppHash - if len(i.Hash) != hashSize { + if len(i.AppHash) != hashSize { return nil, errors.New("invalid hash size") } buf = append(buf, i.AppHash...) diff --git a/server/v2/cometbft/abci.go b/server/v2/cometbft/abci.go index d2cb78751da4..0e01028e17c8 100644 --- a/server/v2/cometbft/abci.go +++ b/server/v2/cometbft/abci.go @@ -2,6 +2,7 @@ package cometbft import ( "context" + "crypto/sha256" "errors" "fmt" "sync/atomic" @@ -231,10 +232,13 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRe return nil, err } + // populate hash with empty byte slice instead of nil + bz := sha256.Sum256([]byte("")) + br := &coreappmgr.BlockRequest[T]{ Height: uint64(req.InitialHeight - 1), Time: req.Time, - Hash: nil, + Hash: bz[:], AppHash: ci.Hash, ChainId: req.ChainId, ConsensusMessages: consMessages, From a60bcdbe246b9556b25b23b444175bc490e1e4fc Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 1 Jul 2024 18:02:49 +0200 Subject: [PATCH 7/7] consistency --- server/v2/cometbft/abci.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/v2/cometbft/abci.go b/server/v2/cometbft/abci.go index 0e01028e17c8..b1a0afeb038c 100644 --- a/server/v2/cometbft/abci.go +++ b/server/v2/cometbft/abci.go @@ -233,7 +233,7 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRe } // populate hash with empty byte slice instead of nil - bz := sha256.Sum256([]byte("")) + bz := sha256.Sum256([]byte{}) br := &coreappmgr.BlockRequest[T]{ Height: uint64(req.InitialHeight - 1),