From 49e75bbc22f9e83794b9bd156f0d3d0a5d662cac Mon Sep 17 00:00:00 2001 From: Omri Date: Mon, 20 Mar 2023 11:04:56 +0100 Subject: [PATCH] fix: merge and cherry picked fixes from main (#290) Co-authored-by: Michael Tsitrin <114929630+mtsitrin@users.noreply.github.com> --- block/manager.go | 9 ++- go.mod | 1 + go.sum | 4 +- proto/types/dymint/state.proto | 2 + rpc/client/client.go | 4 +- rpc/json/handler.go | 6 ++ rpc/json/service.go | 29 ++++++-- rpc/json/ws.go | 15 +++- rpc/json/ws_test.go | 80 ++++++++++++++++++-- settlement/dymension/dymension.go | 2 +- state/executor.go | 8 +- state/executor_test.go | 1 + store/store.go | 2 +- store/store_test.go | 1 + types/pb/dymint/state.pb.go | 117 ++++++++++++++++++++---------- types/serialization.go | 8 ++ types/serialization_test.go | 1 + types/state.go | 3 + 18 files changed, 226 insertions(+), 67 deletions(-) diff --git a/block/manager.go b/block/manager.go index d4b3f3b9b..9ced761db 100644 --- a/block/manager.go +++ b/block/manager.go @@ -473,7 +473,10 @@ func (m *Manager) applyBlock(ctx context.Context, block *types.Block, commit *ty return err } - // Update the state with the new app hash from the commit. + // Update the state with the new app hash, last validators and store height from the commit. + // Every one of those, if happens before commit, prevents us from re-executing the block in case failed during commit. + newState.LastValidators = m.lastState.Validators.Copy() + newState.LastStoreHeight = block.Header.Height _, err = m.store.UpdateState(newState, nil) if err != nil { m.logger.Error("Failed to update state", "error", err) @@ -499,8 +502,10 @@ func (m *Manager) alignStoreWithApp(ctx context.Context, block *types.Block) (bo if uint64(proxyAppInfo.LastBlockHeight) == block.Header.Height { isRequired = true m.logger.Info("Skipping block application and only updating store height and state hash", "height", block.Header.Height) - // update the state with the hash + // update the state with the hash, last store height and last validators. m.lastState.AppHash = *(*[32]byte)(proxyAppInfo.LastBlockAppHash) + m.lastState.LastStoreHeight = block.Header.Height + m.lastState.LastValidators = m.lastState.Validators.Copy() _, err := m.store.UpdateState(m.lastState, nil) if err != nil { m.logger.Error("Failed to update state", "error", err) diff --git a/go.mod b/go.mod index 28b8faea2..d7454022c 100644 --- a/go.mod +++ b/go.mod @@ -248,6 +248,7 @@ require ( replace ( github.com/cosmos/ibc-go/v3 => github.com/dymensionxyz/ibc-go/v3 v3.0.0-rc2.0.20230105134315-1870174ab6da github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.4 + github.com/gorilla/rpc => github.com/dymensionxyz/rpc v1.3.1 google.golang.org/grpc => google.golang.org/grpc v1.33.2 ) diff --git a/go.sum b/go.sum index a2ff86dba..ee2e8e4fd 100644 --- a/go.sum +++ b/go.sum @@ -278,6 +278,8 @@ github.com/dymensionxyz/dymension v0.1.0-alpha.0.20230110174626-23b70dccd1e5 h1: github.com/dymensionxyz/dymension v0.1.0-alpha.0.20230110174626-23b70dccd1e5/go.mod h1:C5nQlAlDqVtNigjQVNLSL4SuC3gUVoRhqAsfYZZcDgs= github.com/dymensionxyz/ibc-go/v3 v3.0.0-rc2.0.20230105134315-1870174ab6da h1:driSbrBc4deT0iRjDKjA4royFDsUsDcE0IgFkFxhopo= github.com/dymensionxyz/ibc-go/v3 v3.0.0-rc2.0.20230105134315-1870174ab6da/go.mod h1:VwB/vWu4ysT5DN2aF78d17LYmx3omSAdq6gpKvM7XRA= +github.com/dymensionxyz/rpc v1.3.1 h1:7EXWIobaBes5zldRvTIg7TmNsEKjicrWA/OjCc0NaGs= +github.com/dymensionxyz/rpc v1.3.1/go.mod h1:f+WpX8ysy8wt95iGc6auYlHcnHj2bUkhiRVkkKNys8c= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= @@ -466,8 +468,6 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/gorilla/rpc v1.2.0 h1:WvvdC2lNeT1SP32zrIce5l0ECBfbAlmrmSBsuc57wfk= -github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= diff --git a/proto/types/dymint/state.proto b/proto/types/dymint/state.proto index 5b16233f2..fb37f87fe 100755 --- a/proto/types/dymint/state.proto +++ b/proto/types/dymint/state.proto @@ -35,4 +35,6 @@ message State { bytes last_results_hash = 14; bytes app_hash = 15; + + uint64 last_store_height = 16 [(gogoproto.customname) = "LastStoreHeight"]; } diff --git a/rpc/client/client.go b/rpc/client/client.go index f7425fa9b..cc2067e58 100644 --- a/rpc/client/client.go +++ b/rpc/client/client.go @@ -893,7 +893,7 @@ func validatePage(pagePtr *int, perPage, totalCount int) (int, error) { panic(fmt.Sprintf("zero or negative perPage: %d", perPage)) } - if pagePtr == nil { // no page parameter + if pagePtr == nil || *pagePtr <= 0 { // no page parameter return 1, nil } @@ -902,7 +902,7 @@ func validatePage(pagePtr *int, perPage, totalCount int) (int, error) { pages = 1 // one page (even if it's empty) } page := *pagePtr - if page <= 0 || page > pages { + if page > pages { return 1, fmt.Errorf("page should be within [1, %d] range, given %d", pages, page) } diff --git a/rpc/json/handler.go b/rpc/json/handler.go index d3fb055d0..955b96403 100644 --- a/rpc/json/handler.go +++ b/rpc/json/handler.go @@ -86,6 +86,12 @@ func (h *handler) serveJSONRPCforWS(w http.ResponseWriter, r *http.Request, wsCo } if methodSpec.ws { callArgs = append(callArgs, reflect.ValueOf(wsConn)) + rpcID, err := codecReq.ID() + if err != nil { + codecReq.WriteError(w, http.StatusBadRequest, err) + return + } + callArgs = append(callArgs, reflect.ValueOf(rpcID)) } rets := methodSpec.m.Call(callArgs) diff --git a/rpc/json/service.go b/rpc/json/service.go index 131fa59b1..e99ed98be 100644 --- a/rpc/json/service.go +++ b/rpc/json/service.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "reflect" + "strconv" "time" "github.com/gorilla/rpc/v2/json2" @@ -13,6 +14,7 @@ import ( tmquery "github.com/tendermint/tendermint/libs/pubsub/query" rpcclient "github.com/tendermint/tendermint/rpc/client" ctypes "github.com/tendermint/tendermint/rpc/core/types" + rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types" "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/rpc/client" @@ -37,7 +39,7 @@ func newMethod(m interface{}) *method { m: reflect.ValueOf(m), argsType: mType.In(1).Elem(), returnType: mType.Out(0).Elem(), - ws: mType.NumIn() == 3, + ws: mType.NumIn() == 4, } } @@ -86,7 +88,7 @@ func newService(c *client.Client, l log.Logger) *service { return &s } -func (s *service) Subscribe(req *http.Request, args *subscribeArgs, wsConn *wsConn) (*ctypes.ResultSubscribe, error) { +func (s *service) Subscribe(req *http.Request, args *subscribeArgs, wsConn *wsConn, subscriptionID []byte) (*ctypes.ResultSubscribe, error) { addr := req.RemoteAddr // TODO(tzdybal): pass config and check subscriptions limits @@ -108,18 +110,29 @@ func (s *service) Subscribe(req *http.Request, args *subscribeArgs, wsConn *wsCo if err != nil { return nil, fmt.Errorf("failed to subscribe: %w", err) } - - go func() { + go func(subscriptionID []byte) { for { select { case msg := <-sub.Out(): - data, err := json.Marshal(msg.Data()) + // build the base response + resultEvent := &ctypes.ResultEvent{Query: args.Query, Data: msg.Data(), Events: msg.Events()} + var resp rpctypes.RPCResponse + // Check if subscriptionID is string or int and generate the rest of the response accordingly + subscriptionIDInt, err := strconv.Atoi(string(subscriptionID)) + if err != nil { + s.logger.Info("Failed to convert subscriptionID to int") + resp = rpctypes.NewRPCSuccessResponse(rpctypes.JSONRPCStringID(subscriptionID), resultEvent) + } else { + resp = rpctypes.NewRPCSuccessResponse(rpctypes.JSONRPCIntID(subscriptionIDInt), resultEvent) + } + // Marshal response to JSON and send it to the websocket queue + jsonBytes, err := json.MarshalIndent(resp, "", " ") if err != nil { - s.logger.Error("failed to marshal response data", "error", err) + s.logger.Error("Failed to marshal RPCResponse to JSON", "err", err) continue } if wsConn != nil { - wsConn.queue <- data + wsConn.queue <- jsonBytes } case <-sub.Cancelled(): if sub.Err() != pubsub.ErrUnsubscribed { @@ -134,7 +147,7 @@ func (s *service) Subscribe(req *http.Request, args *subscribeArgs, wsConn *wsCo return } } - }() + }(subscriptionID) return &ctypes.ResultSubscribe{}, nil } diff --git a/rpc/json/ws.go b/rpc/json/ws.go index 98c28e9f2..84139c8a4 100644 --- a/rpc/json/ws.go +++ b/rpc/json/ws.go @@ -2,12 +2,14 @@ package json import ( "bytes" + "context" "io" "net/http" "github.com/gorilla/websocket" "github.com/dymensionxyz/dymint/log" + tmpubsub "github.com/tendermint/tendermint/libs/pubsub" ) type wsConn struct { @@ -38,6 +40,9 @@ func (h *handler) wsHandler(w http.ResponseWriter, r *http.Request) { upgrader := websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { + return true + }, } wsc, err := upgrader.Upgrade(w, r, nil) @@ -63,7 +68,15 @@ func (h *handler) wsHandler(w http.ResponseWriter, r *http.Request) { for { mt, r, err := wsc.NextReader() if err != nil { - h.logger.Error("failed to read next WebSocket message", "error", err) + if _, ok := err.(*websocket.CloseError); ok { + h.logger.Debug("WebSocket connection closed") + err := h.srv.client.EventBus.UnsubscribeAll(context.Background(), remoteAddr) + if err != nil && err != tmpubsub.ErrSubscriptionNotFound { + h.logger.Error("Failed to unsubscribe addr from events", "addr", remoteAddr, "err", err) + } + } else { + h.logger.Error("failed to read next WebSocket message", "error", err) + } break } diff --git a/rpc/json/ws_test.go b/rpc/json/ws_test.go index c4b1da705..1de2edf6a 100644 --- a/rpc/json/ws_test.go +++ b/rpc/json/ws_test.go @@ -14,9 +14,17 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/libs/log" - tmtypes "github.com/tendermint/tendermint/types" + rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types" ) +type NestedRPCResponse struct { + Query string `json:"query"` + Data struct { + Type string `json:"type"` + Value string `json:"value"` + } `json:"data"` +} + func TestWebSockets(t *testing.T) { assert := assert.New(t) require := require.New(t) @@ -49,7 +57,7 @@ func TestWebSockets(t *testing.T) { `)) assert.NoError(err) - err = conn.SetReadDeadline(time.Now().Add(100 * time.Second)) + err = conn.SetReadDeadline(time.Now().Add(3 * time.Second)) assert.NoError(err) typ, msg, err := conn.ReadMessage() assert.NoError(err) @@ -63,13 +71,25 @@ func TestWebSockets(t *testing.T) { assert.NoError(err) assert.Equal(websocket.TextMessage, typ) assert.NotEmpty(msg) - var payload tmtypes.EventDataNewBlock - err = json.Unmarshal(msg, &payload) + var responsePayload rpctypes.RPCResponse + err = json.Unmarshal(msg, &responsePayload) assert.NoError(err) - assert.NotNil(payload.ResultBeginBlock) - assert.NotNil(payload.Block) - assert.GreaterOrEqual(payload.Block.Height, int64(1)) - assert.NotNil(payload.ResultEndBlock) + assert.Equal(rpctypes.JSONRPCIntID(7), responsePayload.ID) + var m map[string]interface{} + err = json.Unmarshal([]byte(responsePayload.Result), &m) + require.NoError(err) + + // TODO(omritoptix): json unmarshalling of the dataPayload fails as dataPayload was encoded with amino and not json (and as such encodes 64bit numbers as strings). + // we need to unmarshal using the tendermint json library for it to populate the dataPayload correctly. Currently skipping this part of the test. + // valueField := m["data"].(map[string]interface{})["value"] + // valueJSON, err := json.Marshal(valueField) + // var dataPayload tmtypes.EventDataNewBlock + // err = tmjson.Unmarshal(valueJSON, &dataPayload) + // require.NoError(err) + // assert.NotNil(dataPayload.ResultBeginBlock) + // assert.NotNil(dataPayload.Block) + // assert.GreaterOrEqual(dataPayload.Block.Height, int64(1)) + // assert.NotNil(dataPayload.ResultEndBlock) unsubscribeAllReq, err := json2.EncodeClientRequest("unsubscribe_all", &unsubscribeAllArgs{}) require.NoError(err) @@ -83,3 +103,47 @@ func TestWebSockets(t *testing.T) { assert.NoError(json.Unmarshal(rsp.Body.Bytes(), &jsonResp)) assert.Nil(jsonResp.Error) } + +// Test that when a websocket connection is closed, the corresponding +// subscription is also removed. +func TestWebsocketCloseUnsubscribe(t *testing.T) { + assert := assert.New(t) + require := require.New(t) + + _, local := getRPC(t) + handler, err := GetHTTPHandler(local, log.TestingLogger()) + require.NoError(err) + + srv := httptest.NewServer(handler) + + conn, resp, err := websocket.DefaultDialer.Dial(strings.Replace(srv.URL, "http://", "ws://", 1)+"/websocket", nil) + require.NoError(err) + require.NotNil(resp) + require.NotNil(conn) + assert.Equal(http.StatusSwitchingProtocols, resp.StatusCode) + subscribed_clients := local.EventBus.NumClients() + + err = conn.WriteMessage(websocket.TextMessage, []byte(` +{ + "jsonrpc": "2.0", + "method": "subscribe", + "id": 7, + "params": { + "query": "tm.event='NewBlock'" + } +} +`)) + assert.NoError(err) + // Vaildate we have a new client + assert.Eventually(func() bool { + return subscribed_clients+1 == local.EventBus.NumClients() + }, 3*time.Second, 100*time.Millisecond) + // disconnect websocket + err = conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second)) + assert.NoError(err) + // Validate we have one less client + assert.Eventually(func() bool { + return subscribed_clients == local.EventBus.NumClients() + }, 3*time.Second, 100*time.Millisecond) + +} diff --git a/settlement/dymension/dymension.go b/settlement/dymension/dymension.go index fd12d4c47..c5db7e53c 100644 --- a/settlement/dymension/dymension.go +++ b/settlement/dymension/dymension.go @@ -299,7 +299,7 @@ func (d *HubClient) eventHandler() { panic("Settlement WS disconnected") case event := <-eventsChannel: // Assert value is in map and publish it to the event bus - d.logger.Debug("Received event from settlement layer", "event", event) + d.logger.Debug("Received event from settlement layer") _, ok := d.eventMap[event.Query] if !ok { d.logger.Debug("Ignoring event. Type not supported", "event", event) diff --git a/state/executor.go b/state/executor.go index 8971def3d..66082e646 100644 --- a/state/executor.go +++ b/state/executor.go @@ -210,11 +210,13 @@ func (e *BlockExecutor) updateState(state types.State, block *types.Block, abciR }, NextValidators: nValSet, Validators: state.NextValidators.Copy(), - LastValidators: state.Validators.Copy(), LastHeightValidatorsChanged: lastHeightValSetChanged, ConsensusParams: state.ConsensusParams, LastHeightConsensusParamsChanged: state.LastHeightConsensusParamsChanged, - AppHash: state.AppHash, + // We're gonna update those fields only after we commit the blocks + AppHash: state.AppHash, + LastValidators: state.LastValidators.Copy(), + LastStoreHeight: state.LastStoreHeight, } copy(s.LastResultsHash[:], tmtypes.NewResults(abciResponses.DeliverTxs).Hash()) @@ -262,7 +264,7 @@ func (e *BlockExecutor) validateBlock(state types.State, block *types.Block) err if state.LastBlockHeight <= 0 && block.Header.Height != uint64(state.InitialHeight) { return errors.New("initial block height mismatch") } - if state.LastBlockHeight > 0 && block.Header.Height != uint64(state.LastBlockHeight)+1 { + if state.LastBlockHeight > 0 && block.Header.Height != uint64(state.LastStoreHeight)+1 { return errors.New("block height mismatch") } if !bytes.Equal(block.Header.AppHash[:], state.AppHash[:]) { diff --git a/state/executor_test.go b/state/executor_test.go index bfc3c070f..d6ca4b2a5 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -181,6 +181,7 @@ func TestApplyBlock(t *testing.T) { err = executor.Commit(context.Background(), &newState, block, resp) require.NoError(err) assert.Equal(mockAppHash, newState.AppHash) + newState.LastStoreHeight = uint64(newState.LastBlockHeight) // Create another block with multiple Tx from mempool require.NoError(mpool.CheckTx([]byte{0, 1, 2, 3, 4}, func(r *abci.Response) {}, mempool.TxInfo{})) diff --git a/store/store.go b/store/store.go index 273fecb2d..c5dbc20a9 100644 --- a/store/store.go +++ b/store/store.go @@ -207,7 +207,7 @@ func (s *DefaultStore) LoadState() (types.State, error) { var state types.State err = state.FromProto(&pbState) - atomic.StoreUint64(&s.height, uint64(state.LastBlockHeight)) + atomic.StoreUint64(&s.height, state.LastStoreHeight) return state, err } diff --git a/store/store_test.go b/store/store_test.go index af766a35e..4a41e0697 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -136,6 +136,7 @@ func TestRestart(t *testing.T) { expectedHeight := uint64(10) _, err := s1.UpdateState(types.State{ LastBlockHeight: int64(expectedHeight), + LastStoreHeight: uint64(expectedHeight), NextValidators: validatorSet, Validators: validatorSet, LastValidators: validatorSet, diff --git a/types/pb/dymint/state.pb.go b/types/pb/dymint/state.pb.go index 691cafe56..d7d1b816f 100644 --- a/types/pb/dymint/state.pb.go +++ b/types/pb/dymint/state.pb.go @@ -46,6 +46,7 @@ type State struct { LastHeightConsensusParamsChanged int64 `protobuf:"varint,13,opt,name=last_height_consensus_params_changed,json=lastHeightConsensusParamsChanged,proto3" json:"last_height_consensus_params_changed,omitempty"` LastResultsHash []byte `protobuf:"bytes,14,opt,name=last_results_hash,json=lastResultsHash,proto3" json:"last_results_hash,omitempty"` AppHash []byte `protobuf:"bytes,15,opt,name=app_hash,json=appHash,proto3" json:"app_hash,omitempty"` + LastStoreHeight uint64 `protobuf:"varint,16,opt,name=last_store_height,json=lastStoreHeight,proto3" json:"last_store_height,omitempty"` } func (m *State) Reset() { *m = State{} } @@ -186,6 +187,13 @@ func (m *State) GetAppHash() []byte { return nil } +func (m *State) GetLastStoreHeight() uint64 { + if m != nil { + return m.LastStoreHeight + } + return 0 +} + func init() { proto.RegisterType((*State)(nil), "dymint.State") } @@ -193,45 +201,47 @@ func init() { func init() { proto.RegisterFile("types/dymint/state.proto", fileDescriptor_4b679420add07272) } var fileDescriptor_4b679420add07272 = []byte{ - // 603 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x94, 0x4d, 0x6f, 0xda, 0x30, - 0x1c, 0xc6, 0xc9, 0x4a, 0x81, 0x9a, 0xb7, 0x2e, 0xdb, 0x21, 0x65, 0x52, 0x48, 0xbb, 0x17, 0xa1, - 0x1d, 0x12, 0x69, 0x95, 0x76, 0xdc, 0x21, 0x54, 0x1a, 0x48, 0x68, 0x9a, 0xc2, 0xd4, 0xc3, 0x2e, - 0x91, 0x93, 0x78, 0x89, 0xb5, 0x10, 0x47, 0xb1, 0xa9, 0x60, 0x1f, 0x60, 0xe7, 0x7e, 0xac, 0x1e, - 0x7b, 0xdc, 0x89, 0x4d, 0xf0, 0x45, 0x26, 0xdb, 0x09, 0xa4, 0x45, 0x48, 0xbd, 0xc5, 0x8f, 0x7f, - 0xff, 0x27, 0x8f, 0xfd, 0xb7, 0x0d, 0x34, 0xb6, 0x4c, 0x11, 0xb5, 0x82, 0xe5, 0x0c, 0x27, 0xcc, - 0xa2, 0x0c, 0x32, 0x64, 0xa6, 0x19, 0x61, 0x44, 0xad, 0x49, 0xad, 0xf7, 0x32, 0x24, 0x21, 0x11, - 0x92, 0xc5, 0xbf, 0xe4, 0x6c, 0xaf, 0x1f, 0x12, 0x12, 0xc6, 0xc8, 0x12, 0x23, 0x6f, 0xfe, 0xc3, - 0x62, 0x78, 0x86, 0x28, 0x83, 0xb3, 0x34, 0x07, 0xce, 0xa5, 0x31, 0x43, 0x49, 0x80, 0x32, 0x61, - 0x0e, 0x3d, 0x1f, 0x5b, 0x42, 0xcd, 0x91, 0x8b, 0x3d, 0x24,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f, 0x38, 0xed, 0xad, 0x17, 0x9f, 0x55, 0x3f, 0x82, 0x0e, 0x8d, 0x5d, 0xb1, 0x59, 0x2e, 0x4e, - 0x02, 0xb4, 0xd0, 0xea, 0x86, 0x32, 0xa8, 0xda, 0xa7, 0xeb, 0x55, 0xbf, 0x35, 0x9d, 0x88, 0x9d, - 0x1e, 0x73, 0xdd, 0x69, 0xd1, 0x78, 0x37, 0x52, 0x3f, 0x83, 0x6e, 0x82, 0x16, 0xcc, 0xdd, 0xf6, - 0x99, 0x6a, 0x0d, 0x91, 0x42, 0xdf, 0x5f, 0xdb, 0x75, 0xc1, 0x4c, 0x11, 0x73, 0x3a, 0xbc, 0x6c, - 0xab, 0x50, 0xf5, 0x13, 0x00, 0x25, 0x8f, 0x93, 0x27, 0x79, 0x94, 0x2a, 0x78, 0x10, 0xb1, 0x1d, - 0x25, 0x13, 0xf0, 0xb4, 0x20, 0xbc, 0xac, 0x14, 0x64, 0x08, 0x74, 0x61, 0x24, 0x3b, 0x5a, 0xf2, - 0x73, 0xfd, 0x08, 0x26, 0x21, 0x0a, 0xb4, 0xa6, 0x68, 0xf2, 0x2b, 0x4e, 0xc9, 0xfe, 0xee, 0xaa, - 0x87, 0x12, 0x51, 0x1d, 0x70, 0xea, 0x93, 0x84, 0xa2, 0x84, 0xce, 0xa9, 0x2b, 0xcf, 0xb6, 0xd6, - 0x12, 0x71, 0xce, 0xf7, 0xe3, 0x0c, 0x0b, 0xf2, 0xab, 0x00, 0xed, 0x2a, 0x6f, 0x92, 0xd3, 0xf5, - 0x1f, 0xca, 0xea, 0x17, 0xf0, 0xa6, 0x1c, 0xec, 0xb1, 0xff, 0x36, 0x5e, 0x5b, 0xc4, 0x33, 0x76, - 0xf1, 0x1e, 0xf9, 0x17, 0x19, 0x8b, 0x03, 0x9c, 0x21, 0x3a, 0x8f, 0x19, 0x75, 0x23, 0x48, 0x23, - 0xad, 0x63, 0x28, 0x83, 0x96, 0x3c, 0xc0, 0x8e, 0xd4, 0x47, 0x90, 0x46, 0xfc, 0xba, 0xc0, 0x34, - 0x95, 0x48, 0x57, 0x20, 0x75, 0x98, 0xa6, 0x7c, 0xca, 0x1e, 0xdd, 0xad, 0x75, 0xe5, 0x7e, 0xad, - 0x2b, 0xff, 0xd6, 0xba, 0x72, 0xbb, 0xd1, 0x2b, 0xf7, 0x1b, 0xbd, 0xf2, 0x67, 0xa3, 0x57, 0xbe, - 0x9b, 0x21, 0x66, 0xd1, 0xdc, 0x33, 0x7d, 0x32, 0xe3, 0x0f, 0x17, 0x4a, 0xf8, 0xcd, 0x5b, 0x2c, - 0x7f, 0x15, 0xaf, 0x58, 0x7e, 0xf3, 0xbd, 0x7c, 0xec, 0xd5, 0xc4, 0x81, 0xbd, 0xfc, 0x1f, 0x00, - 0x00, 0xff, 0xff, 0xcb, 0x3d, 0x3d, 0x1d, 0xec, 0x04, 0x00, 0x00, + // 631 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x94, 0x4d, 0x6b, 0xdb, 0x3e, + 0x1c, 0xc7, 0xe3, 0x7f, 0x1f, 0xd2, 0x2a, 0x0f, 0xee, 0xdf, 0xdd, 0xc1, 0xed, 0xc0, 0x76, 0xbb, + 0x07, 0xc2, 0x0e, 0x36, 0xac, 0xb0, 0xe3, 0x06, 0x6e, 0x61, 0x0d, 0x84, 0x31, 0x9c, 0xd1, 0xc3, + 0x2e, 0x46, 0xb6, 0x35, 0x5b, 0xcc, 0xb1, 0x8c, 0xa5, 0x94, 0x64, 0xaf, 0xa2, 0xef, 0x6a, 0x3d, + 0xf6, 0xb8, 0x53, 0x36, 0x9c, 0x37, 0x32, 0x24, 0xd9, 0x89, 0xd7, 0x10, 0xe8, 0x2d, 0xfa, 0xea, + 0xf3, 0xfb, 0xe6, 0x6b, 0xfd, 0x7e, 0x12, 0xd0, 0xd9, 0x3c, 0x47, 0xd4, 0x89, 0xe6, 0x13, 0x9c, + 0x31, 0x87, 0x32, 0xc8, 0x90, 0x9d, 0x17, 0x84, 0x11, 0x6d, 0x5f, 0x6a, 0xa7, 0xcf, 0x62, 0x12, + 0x13, 0x21, 0x39, 0xfc, 0x97, 0xdc, 0x3d, 0x35, 0x63, 0x42, 0xe2, 0x14, 0x39, 0x62, 0x15, 0x4c, + 0xbf, 0x39, 0x0c, 0x4f, 0x10, 0x65, 0x70, 0x92, 0x57, 0xc0, 0x99, 0x34, 0x66, 0x28, 0x8b, 0x50, + 0x21, 0xcc, 0x61, 0x10, 0x62, 0x47, 0xa8, 0x15, 0x72, 0xbe, 0x81, 0x54, 0x42, 0x83, 0x79, 0xbd, + 0x85, 0xb9, 0x85, 0x29, 0x8e, 0x20, 0x23, 0x45, 0xc5, 0xbd, 0xd8, 0xc2, 0xe5, 0xb0, 0x80, 0x93, + 0xed, 0x7f, 0x28, 0x3e, 0xb8, 0xf9, 0x87, 0xe7, 0x3f, 0xdb, 0x60, 0x6f, 0xcc, 0x55, 0xed, 0x02, + 0xb4, 0x6f, 0x51, 0x41, 0x31, 0xc9, 0x74, 0xc5, 0x52, 0x06, 0x9d, 0xb7, 0x27, 0xf6, 0xba, 0xd2, + 0x96, 0x47, 0x75, 0x23, 0x01, 0xaf, 0x26, 0xb5, 0x13, 0x70, 0x10, 0x26, 0x10, 0x67, 0x3e, 0x8e, + 0xf4, 0xff, 0x2c, 0x65, 0x70, 0xe8, 0xb5, 0xc5, 0x7a, 0x18, 0x69, 0xaf, 0x40, 0x1f, 0x67, 0x98, + 0x61, 0x98, 0xfa, 0x09, 0xc2, 0x71, 0xc2, 0xf4, 0x1d, 0x4b, 0x19, 0xec, 0x78, 0xbd, 0x4a, 0xbd, + 0x16, 0xa2, 0xf6, 0x06, 0xfc, 0x9f, 0x42, 0xca, 0xfc, 0x20, 0x25, 0xe1, 0xf7, 0x9a, 0xdc, 0x15, + 0xa4, 0xca, 0x37, 0x5c, 0xae, 0x57, 0xac, 0x07, 0x7a, 0x0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0x72, + 0xb6, 0xf5, 0xae, 0x88, 0x73, 0xb6, 0x19, 0xe7, 0xb2, 0x26, 0x3f, 0x0b, 0xd0, 0xdd, 0xe5, 0x4d, + 0xf2, 0xd4, 0xf0, 0x5f, 0x59, 0xfb, 0x04, 0x5e, 0x36, 0x83, 0x3d, 0xf6, 0x5f, 0xc5, 0xeb, 0x89, + 0x78, 0xd6, 0x3a, 0xde, 0x23, 0xff, 0x3a, 0x63, 0x3d, 0xc0, 0x05, 0xa2, 0xd3, 0x94, 0x51, 0x3f, + 0x81, 0x34, 0xd1, 0xfb, 0x96, 0x32, 0xe8, 0xca, 0x01, 0xf6, 0xa4, 0x7e, 0x0d, 0x69, 0xc2, 0xaf, + 0x0b, 0xcc, 0x73, 0x89, 0xa8, 0x02, 0x69, 0xc3, 0x3c, 0x17, 0x5b, 0x1f, 0x2a, 0x1b, 0xca, 0x48, + 0x81, 0xea, 0x7b, 0x70, 0x24, 0x86, 0xe7, 0xb8, 0x5c, 0x98, 0x2a, 0x1f, 0xe0, 0x31, 0xdf, 0x93, + 0x61, 0xa4, 0x77, 0x43, 0x70, 0xaf, 0xef, 0x4b, 0x43, 0x79, 0x28, 0x0d, 0xe5, 0x4f, 0x69, 0x28, + 0x77, 0x4b, 0xa3, 0xf5, 0xb0, 0x34, 0x5a, 0xbf, 0x96, 0x46, 0xeb, 0xab, 0x1d, 0x63, 0x96, 0x4c, + 0x03, 0x3b, 0x24, 0x13, 0xfe, 0xf2, 0xa1, 0x8c, 0x5f, 0xdd, 0xd9, 0xfc, 0x47, 0xfd, 0x0c, 0x56, + 0x4f, 0x47, 0x50, 0xad, 0x83, 0x7d, 0x31, 0xf1, 0x17, 0x7f, 0x03, 0x00, 0x00, 0xff, 0xff, 0x45, + 0x74, 0xfd, 0xf5, 0x2d, 0x05, 0x00, 0x00, } func (m *State) Marshal() (dAtA []byte, err error) { @@ -254,6 +264,13 @@ func (m *State) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.LastStoreHeight != 0 { + i = encodeVarintState(dAtA, i, uint64(m.LastStoreHeight)) + i-- + dAtA[i] = 0x1 + i-- + dAtA[i] = 0x80 + } if len(m.AppHash) > 0 { i -= len(m.AppHash) copy(dAtA[i:], m.AppHash) @@ -445,6 +462,9 @@ func (m *State) Size() (n int) { if l > 0 { n += 1 + l + sovState(uint64(l)) } + if m.LastStoreHeight != 0 { + n += 2 + sovState(uint64(m.LastStoreHeight)) + } return n } @@ -921,6 +941,25 @@ func (m *State) Unmarshal(dAtA []byte) error { m.AppHash = []byte{} } iNdEx = postIndex + case 16: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field LastStoreHeight", wireType) + } + m.LastStoreHeight = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowState + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.LastStoreHeight |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipState(dAtA[iNdEx:]) diff --git a/types/serialization.go b/types/serialization.go index 6842b253b..8f79d43f2 100644 --- a/types/serialization.go +++ b/types/serialization.go @@ -251,6 +251,7 @@ func (s *State) ToProto() (*pb.State, error) { NextValidators: nextValidators, Validators: validators, LastValidators: lastValidators, + LastStoreHeight: s.LastStoreHeight, LastHeightValidatorsChanged: s.LastHeightValidatorsChanged, ConsensusParams: s.ConsensusParams, LastHeightConsensusParamsChanged: s.LastHeightConsensusParamsChanged, @@ -266,6 +267,13 @@ func (s *State) FromProto(other *pb.State) error { s.ChainID = other.ChainId s.InitialHeight = other.InitialHeight s.LastBlockHeight = other.LastBlockHeight + //TODO(omritoptix): remove this as this is only for backwards compatibility + // with old state files that don't have this field. + if other.LastStoreHeight == 0 && other.LastBlockHeight > 1 { + s.LastStoreHeight = uint64(other.LastBlockHeight) + } else { + s.LastStoreHeight = other.LastStoreHeight + } s.SLStateIndex = other.SLStateIndex lastBlockID, err := types.BlockIDFromProto(&other.LastBlockID) if err != nil { diff --git a/types/serialization_test.go b/types/serialization_test.go index 25b0302db..a6540cb44 100644 --- a/types/serialization_test.go +++ b/types/serialization_test.go @@ -122,6 +122,7 @@ func TestStateRoundTrip(t *testing.T) { ChainID: "testchain", InitialHeight: 987, LastBlockHeight: 987654321, + LastStoreHeight: 987654321, LastBlockID: tmtypes.BlockID{ Hash: nil, PartSetHeader: tmtypes.PartSetHeader{ diff --git a/types/state.go b/types/state.go index fac6be7bf..614e8d743 100644 --- a/types/state.go +++ b/types/state.go @@ -54,6 +54,9 @@ type State struct { // Merkle root of the results from executing prev block LastResultsHash [32]byte + // LastStore height is the last height we've saved to the store. + LastStoreHeight uint64 + // the latest AppHash we've received from calling abci.Commit() AppHash [32]byte }