Skip to content

Commit

Permalink
Merge PR: state delta amino ut (#1520)
Browse files Browse the repository at this point in the history
* ut and fix bug

* add test

* debug

* treedelta amino sub ut

* boundary ut case and watchdata sub ut

* abci response
  • Loading branch information
MoloZhang authored Feb 7, 2022
1 parent 1bb6f76 commit a1473de
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 132 deletions.
6 changes: 3 additions & 3 deletions libs/iavl/tree_delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ func (nj *NodeJson) MarshalToAmino(cdc *amino.Codec) ([]byte, error) {
if err != nil {
return nil, err
}
err = amino.EncodeUvarintToBuffer(&buf, uint64(nj.Height))
err = amino.EncodeInt8ToBuffer(&buf, nj.Height)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -771,12 +771,12 @@ func (nj *NodeJson) UnmarshalFromAmino(cdc *amino.Codec, data []byte) error {
nj.Size = int64(value)

case 8:
value, n, err := amino.DecodeUvarint(data)
value, n, err := amino.DecodeInt8(data)
if err != nil {
return err
}
dataLen = uint64(n)
nj.Height = int8(value)
nj.Height = value

case 9:
if data[0] != 0 && data[0] != 1 {
Expand Down
130 changes: 129 additions & 1 deletion libs/iavl/tree_delta_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iavl

import (
"fmt"
"math"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -103,7 +104,6 @@ func newTestTreeDeltaMap() TreeDeltaMap {
func TestTreeDeltaMapAmino(t *testing.T) { testTreeDeltaMapAmino(t) }
func testTreeDeltaMapAmino(t *testing.T) {
for i, tdm := range testTreeDeltaMap {

expect, err := cdc.MarshalBinaryBare(tdm)
require.NoError(t, err, fmt.Sprintf("num %v", i))

Expand Down Expand Up @@ -149,6 +149,134 @@ func testTreeDeltaImpAmino(t *testing.T) {
}
}

// test TreeDelta amino
func TestTreeDeltaAmino(t *testing.T) { testTreeDeltaAmino(t) }
func testTreeDeltaAmino(t *testing.T) {
testTreeDeltas := []*TreeDelta{
{},
{nil, nil, nil},
{[]*NodeJsonImp{}, []*NodeJson{}, []*CommitOrphansImp{}},
{
[]*NodeJsonImp{nil, {}, {"0x01", &NodeJson{Version: 1}}},
[]*NodeJson{nil, {}, {Version: 2}},
[]*CommitOrphansImp{nil, {}, {"0x01", 1}},
},
}
for i, td := range testTreeDeltas {
expect, err := cdc.MarshalBinaryBare(td)
require.NoError(t, err, fmt.Sprintf("num %v", i))

actual, err := td.MarshalToAmino(cdc)
require.NoError(t, err, fmt.Sprintf("num %v", i))
require.EqualValues(t, expect, actual, fmt.Sprintf("num %v", i))

expectValue := TreeDelta{}
err = cdc.UnmarshalBinaryBare(expect, &expectValue)
require.NoError(t, err, fmt.Sprintf("num %v", i))

actualValue := TreeDelta{}
err = actualValue.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err, fmt.Sprintf("num %v", i))
require.EqualValues(t, expectValue, actualValue, fmt.Sprintf("num %v", i))
}
}

// test NodeJsonImp amino
func TestNodeJsonImpAmino(t *testing.T) { testNodeJsonImpAmino(t) }
func testNodeJsonImpAmino(t *testing.T) {
testNodeJsomImps := []*NodeJsonImp{
{},
{"0x01", nil},
{"0x02", &NodeJson{}},
{"0x03", &NodeJson{Version: 1}},
}

for i, ni := range testNodeJsomImps {
expect, err := cdc.MarshalBinaryBare(ni)
require.NoError(t, err, fmt.Sprintf("num %v", i))

actual, err := ni.MarshalToAmino(cdc)
require.NoError(t, err, fmt.Sprintf("num %v", i))
require.EqualValues(t, expect, actual, fmt.Sprintf("num %v", i))

expectValue := NodeJsonImp{}
err = cdc.UnmarshalBinaryBare(expect, &expectValue)
require.NoError(t, err, fmt.Sprintf("num %v", i))

actualValue := NodeJsonImp{}
err = actualValue.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err, fmt.Sprintf("num %v", i))
require.EqualValues(t, expectValue, actualValue, fmt.Sprintf("num %v", i))

}
}

// test CommitOrphansImp amino
func TestCommitOrphansImpAmino(t *testing.T) { testCommitOrphansImpAmino(t) }
func testCommitOrphansImpAmino(t *testing.T) {
testCommitOrphansImps := []*CommitOrphansImp{
{},
{"0x01", -1},
{"0x01", math.MinInt64},
{"0x01", math.MaxInt64},
{"0x01", 1},
}

for i, ci := range testCommitOrphansImps {
expect, err := cdc.MarshalBinaryBare(ci)
require.NoError(t, err, fmt.Sprintf("num %v", i))

actual, err := ci.MarshalToAmino(cdc)
require.NoError(t, err, fmt.Sprintf("num %v", i))
require.EqualValues(t, expect, actual, fmt.Sprintf("num %v", i))

expectValue := CommitOrphansImp{}
err = cdc.UnmarshalBinaryBare(expect, &expectValue)
require.NoError(t, err, fmt.Sprintf("num %v", i))

actualValue := CommitOrphansImp{}
err = actualValue.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err, fmt.Sprintf("num %v", i))
require.EqualValues(t, expectValue, actualValue, fmt.Sprintf("num %v", i))
}
}

// test NodeJson amino
func TestNodeJsonAmino(t *testing.T) { testNodeJsonAmino(t) }
func testNodeJsonAmino(t *testing.T) {
testNodeJsons := []*NodeJson{
{},
{Key: []byte("0x01"), Value: []byte("0xff"), Hash: []byte("0xFF"), LeftHash: []byte("01"), RightHash: []byte("")},
{Version: -1, Size: 1},
{Version: math.MinInt64, Size: math.MaxInt64},
{Height: int8(1)},
{Height: int8(-1)},
{Height: math.MaxInt8},
{Height: math.MaxInt8},
{Persisted: true, PrePersisted: false},
{Key: []byte("0x01"), Value: []byte("0x02"), Hash: []byte("0x03"), LeftHash: []byte("0x04"), RightHash: []byte("0x05"),
Version: 1, Size: 1, Height: 1, Persisted: true, PrePersisted: true},
}

for i, nj := range testNodeJsons {
expect, err := cdc.MarshalBinaryBare(nj)
require.NoError(t, err, fmt.Sprintf("num %v", i))

actual, err := nj.MarshalToAmino(cdc)
require.NoError(t, err, fmt.Sprintf("num %v", i))
require.EqualValues(t, expect, actual, fmt.Sprintf("num %v", i))

expectValue := NodeJson{}
err = cdc.UnmarshalBinaryBare(expect, &expectValue)
require.NoError(t, err, fmt.Sprintf("num %v", i))

actualValue := NodeJson{}
err = actualValue.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err, fmt.Sprintf("num %v", i))
require.EqualValues(t, expectValue, actualValue, fmt.Sprintf("num %v", i))
}
}

// benchmark encode performance
func BenchmarkAminoEncodeDelta(b *testing.B) { benchmarkEncodeDelta(b, newEncoder("amino")) }
func BenchmarkJsonEncodeDelta(b *testing.B) { benchmarkEncodeDelta(b, newEncoder("json")) }
Expand Down
80 changes: 80 additions & 0 deletions libs/tendermint/abci/types/amino_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ func TestPubKeyAmino(t *testing.T) {
actual, err := pubkey.MarshalToAmino(cdc)
require.NoError(t, err)
require.EqualValues(t, expect, actual)

var value PubKey
err = cdc.UnmarshalBinaryBare(expect, &value)
require.NoError(t, err)

var value2 PubKey
err = value2.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err)

require.EqualValues(t, value, value2)
}
}

Expand Down Expand Up @@ -184,6 +194,16 @@ func TestValidatorUpdateAmino(t *testing.T) {
actual, err := validatorUpdate.MarshalToAmino(cdc)
require.NoError(t, err)
require.EqualValues(t, expect, actual)

var value ValidatorUpdate
err = cdc.UnmarshalBinaryBare(expect, &value)
require.NoError(t, err)

var value2 ValidatorUpdate
err = value2.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err)

require.EqualValues(t, value, value2)
}
}

Expand Down Expand Up @@ -215,6 +235,16 @@ func TestBlockParamsAmino(t *testing.T) {
actual, err := test.MarshalToAmino(cdc)
require.NoError(t, err)
require.EqualValues(t, expect, actual)

var value BlockParams
err = cdc.UnmarshalBinaryBare(expect, &value)
require.NoError(t, err)

var value2 BlockParams
err = value2.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err)

require.EqualValues(t, value, value2)
}
}

Expand Down Expand Up @@ -246,6 +276,16 @@ func TestEvidenceParamsAmino(t *testing.T) {
actual, err := test.MarshalToAmino(cdc)
require.NoError(t, err)
require.EqualValues(t, expect, actual)

var value EvidenceParams
err = cdc.UnmarshalBinaryBare(expect, &value)
require.NoError(t, err)

var value2 EvidenceParams
err = value2.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err)

require.EqualValues(t, value, value2)
}
}

Expand Down Expand Up @@ -273,6 +313,16 @@ func TestValidatorParamsAmino(t *testing.T) {
actual, err := test.MarshalToAmino(cdc)
require.NoError(t, err)
require.EqualValues(t, expect, actual)

var value ValidatorParams
err = cdc.UnmarshalBinaryBare(expect, &value)
require.NoError(t, err)

var value2 ValidatorParams
err = value2.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err)

require.EqualValues(t, value, value2)
}
}

Expand Down Expand Up @@ -321,6 +371,16 @@ func TestConsensusParamsAmino(t *testing.T) {
actual, err := test.MarshalToAmino(cdc)
require.NoError(t, err)
require.EqualValues(t, expect, actual)

var value ConsensusParams
err = cdc.UnmarshalBinaryBare(expect, &value)
require.NoError(t, err)

var value2 ConsensusParams
err = value2.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err)

require.EqualValues(t, value, value2)
}
}

Expand Down Expand Up @@ -443,6 +503,16 @@ func TestResponseBeginBlockAmino(t *testing.T) {
actual, err := resp.MarshalToAmino(cdc)
require.NoError(t, err)
require.EqualValues(t, expect, actual)

var value ResponseBeginBlock
err = cdc.UnmarshalBinaryBare(expect, &value)
require.NoError(t, err)

var value2 ResponseBeginBlock
err = value2.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err)

require.EqualValues(t, value, value2)
}
}

Expand Down Expand Up @@ -484,5 +554,15 @@ func TestResponseEndBlockAmino(t *testing.T) {
actual, err := resp.MarshalToAmino(cdc)
require.NoError(t, err)
require.EqualValues(t, expect, actual)

var value ResponseEndBlock
err = cdc.UnmarshalBinaryBare(expect, &value)
require.NoError(t, err)

var value2 ResponseEndBlock
err = value2.UnmarshalFromAmino(cdc, expect)
require.NoError(t, err)

require.EqualValues(t, value, value2)
}
}
Loading

0 comments on commit a1473de

Please sign in to comment.