Skip to content

Commit

Permalink
chore: self review minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Mar 4, 2024
1 parent c05402f commit 501f9c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
37 changes: 9 additions & 28 deletions abci/example/kvstore/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ func (state *kvState) UpdateAppHash(lastCommittedState State, _txs types1.Txs, t
return nil
}

// / Load state from the reader.
// / It expects json-encoded kvState, followed by all items from the state.
// /
// / As a special case, io.EOF when reading the header means that the state is empty.
// Load state from the reader.
// It expects json-encoded kvState, followed by all items from the state.
//
// As a special case, io.EOF when reading the header means that the state is empty.
func (state *kvState) Load(from io.Reader) error {
if state == nil || state.DB == nil {
return errors.New("cannot load into nil state")
Expand All @@ -198,8 +198,7 @@ func (state *kvState) Load(from io.Reader) error {
return fmt.Errorf("error reading state header: %w", err)
}

// Load items to state DB; we don't need to create and use newState.DB
// as we use atomic batches.
// Load items to state DB
batch := newState.DB.NewBatch()
defer batch.Close()

Expand All @@ -210,17 +209,6 @@ func (state *kvState) Load(from io.Reader) error {
item := exportItem{}
var err error
for err = decoder.Decode(&item); err == nil; err = decoder.Decode(&item) {
// var key []byte
// var value []byte
// // Ensure that value is not escaped

// if err := json.Unmarshal([]byte(item.Key), &key); err != nil {
// return fmt.Errorf("error restoring state item key %+v: %w", item, err)
// }
// if err := json.Unmarshal([]byte(item.Value), &value); err != nil {
// return fmt.Errorf("error restoring state item value %+v: %w", item, err)
// }

key, err := url.QueryUnescape(item.Key)
if err != nil {
return fmt.Errorf("error restoring state item key %+v: %w", item, err)
Expand All @@ -244,19 +232,21 @@ func (state *kvState) Load(from io.Reader) error {
return fmt.Errorf("error finalizing restore batch: %w", err)
}

// copy loaded values to the state
state.InitialHeight = newState.InitialHeight
state.Height = newState.Height
state.Round = newState.Round
state.AppHash = newState.AppHash
// apphash cannot be nil,zero-length
if len(state.AppHash) == 0 {
state.AppHash = make(tmbytes.HexBytes, crypto.DefaultAppHashSize)
}

return nil
}

// / Save saves state to the writer.
// / First it puts json-encoded kvState, followed by all items from the state.
// Save saves state to the writer.
// First it puts json-encoded kvState, followed by all items from the state.
func (state kvState) Save(to io.Writer) error {
encoder := json.NewEncoder(to)
if err := encoder.Encode(state); err != nil {
Expand All @@ -270,15 +260,6 @@ func (state kvState) Save(to io.Writer) error {
defer iter.Close()

for ; iter.Valid(); iter.Next() {
// key, err := json.Marshal(string(iter.Key()))
// if err != nil {
// return fmt.Errorf("error encoding state item key %+v: %w", iter.Key(), err)
// }

// value, err := json.Marshal(string(iter.Value()))
// if err != nil {
// return fmt.Errorf("error encoding state item value %+v: %w", iter.Value(), err)
// }
key := url.QueryEscape(string(iter.Key()))
value := url.QueryEscape(string(iter.Value()))
item := exportItem{Key: key, Value: value}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
"initialState": {
"{}",
`{}
{"key":"initial01","value": "a"}
{"key":"initial01","value":"a"}
{"key":"initial02","value":"b"}
{"key":"initial03","value":"c"}
`,
Expand Down
7 changes: 3 additions & 4 deletions test/e2e/tests/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ func TestApp_TxTooBig(t *testing.T) {
mainCtx, cancel := context.WithCancel(context.Background())
defer cancel()

t.Helper()

testnet := loadTestnet(t)
nodes := testnet.Nodes

Expand All @@ -238,12 +236,13 @@ func TestApp_TxTooBig(t *testing.T) {
})
}

// we will use last client to check if txs were included in block
// we will use last client to check if txs were included in block, so we
// define it outside the loop
var client *http.HTTP
outcome := make([]txPair, 0, len(nodes))

start := time.Now()
/// First we broadcast to all nodes
/// Send to each node more txs than we can fit into block
for _, node := range nodes {
ctx, cancel := context.WithTimeout(mainCtx, broadcastTimeout)
defer cancel()
Expand Down

0 comments on commit 501f9c6

Please sign in to comment.