add tests for ABCIResults#Bytes and tx#IndexByHash
This commit is contained in:
parent
715ec19c96
commit
20bb522592
|
@ -24,15 +24,16 @@ func (a ABCIResult) Hash() []byte {
|
|||
// ABCIResults wraps the deliver tx results to return a proof
|
||||
type ABCIResults []ABCIResult
|
||||
|
||||
// NewResults creates ABCIResults from ResponseDeliverTx
|
||||
func NewResults(del []*abci.ResponseDeliverTx) ABCIResults {
|
||||
res := make(ABCIResults, len(del))
|
||||
for i, d := range del {
|
||||
// NewResults creates ABCIResults from the list of ResponseDeliverTx.
|
||||
func NewResults(responses []*abci.ResponseDeliverTx) ABCIResults {
|
||||
res := make(ABCIResults, len(responses))
|
||||
for i, d := range responses {
|
||||
res[i] = NewResultFromResponse(d)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// NewResultFromResponse creates ABCIResult from ResponseDeliverTx.
|
||||
func NewResultFromResponse(response *abci.ResponseDeliverTx) ABCIResult {
|
||||
return ABCIResult{
|
||||
Code: response.Code,
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
)
|
||||
|
||||
func TestABCIResults(t *testing.T) {
|
||||
|
@ -41,3 +42,14 @@ func TestABCIResults(t *testing.T) {
|
|||
assert.True(t, valid, "%d", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestABCIBytes(t *testing.T) {
|
||||
results := NewResults([]*abci.ResponseDeliverTx{
|
||||
{Code: 0, Data: []byte{}},
|
||||
{Code: 0, Data: []byte("one")},
|
||||
{Code: 14, Data: nil},
|
||||
{Code: 14, Data: []byte("foo")},
|
||||
{Code: 14, Data: []byte("bar")},
|
||||
})
|
||||
assert.NotNil(t, results.Bytes())
|
||||
}
|
||||
|
|
|
@ -24,21 +24,32 @@ func randInt(low, high int) int {
|
|||
}
|
||||
|
||||
func TestTxIndex(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
for i := 0; i < 20; i++ {
|
||||
txs := makeTxs(15, 60)
|
||||
for j := 0; j < len(txs); j++ {
|
||||
tx := txs[j]
|
||||
idx := txs.Index(tx)
|
||||
assert.Equal(j, idx)
|
||||
assert.Equal(t, j, idx)
|
||||
}
|
||||
assert.Equal(-1, txs.Index(nil))
|
||||
assert.Equal(-1, txs.Index(Tx("foodnwkf")))
|
||||
assert.Equal(t, -1, txs.Index(nil))
|
||||
assert.Equal(t, -1, txs.Index(Tx("foodnwkf")))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTxIndexByHash(t *testing.T) {
|
||||
for i := 0; i < 20; i++ {
|
||||
txs := makeTxs(15, 60)
|
||||
for j := 0; j < len(txs); j++ {
|
||||
tx := txs[j]
|
||||
idx := txs.IndexByHash(tx.Hash())
|
||||
assert.Equal(t, j, idx)
|
||||
}
|
||||
assert.Equal(t, -1, txs.IndexByHash(nil))
|
||||
assert.Equal(t, -1, txs.IndexByHash(Tx("foodnwkf").Hash()))
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidTxProof(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cases := []struct {
|
||||
txs Txs
|
||||
}{
|
||||
|
@ -58,21 +69,21 @@ func TestValidTxProof(t *testing.T) {
|
|||
leaf := txs[i]
|
||||
leafHash := leaf.Hash()
|
||||
proof := txs.Proof(i)
|
||||
assert.Equal(i, proof.Index, "%d: %d", h, i)
|
||||
assert.Equal(len(txs), proof.Total, "%d: %d", h, i)
|
||||
assert.EqualValues(root, proof.RootHash, "%d: %d", h, i)
|
||||
assert.EqualValues(leaf, proof.Data, "%d: %d", h, i)
|
||||
assert.EqualValues(leafHash, proof.LeafHash(), "%d: %d", h, i)
|
||||
assert.Nil(proof.Validate(root), "%d: %d", h, i)
|
||||
assert.NotNil(proof.Validate([]byte("foobar")), "%d: %d", h, i)
|
||||
assert.Equal(t, i, proof.Index, "%d: %d", h, i)
|
||||
assert.Equal(t, len(txs), proof.Total, "%d: %d", h, i)
|
||||
assert.EqualValues(t, root, proof.RootHash, "%d: %d", h, i)
|
||||
assert.EqualValues(t, leaf, proof.Data, "%d: %d", h, i)
|
||||
assert.EqualValues(t, leafHash, proof.LeafHash(), "%d: %d", h, i)
|
||||
assert.Nil(t, proof.Validate(root), "%d: %d", h, i)
|
||||
assert.NotNil(t, proof.Validate([]byte("foobar")), "%d: %d", h, i)
|
||||
|
||||
// read-write must also work
|
||||
var p2 TxProof
|
||||
bin, err := cdc.MarshalBinary(proof)
|
||||
assert.Nil(err)
|
||||
assert.Nil(t, err)
|
||||
err = cdc.UnmarshalBinary(bin, &p2)
|
||||
if assert.Nil(err, "%d: %d: %+v", h, i, err) {
|
||||
assert.Nil(p2.Validate(root), "%d: %d", h, i)
|
||||
if assert.Nil(t, err, "%d: %d: %+v", h, i, err) {
|
||||
assert.Nil(t, p2.Validate(root), "%d: %d", h, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,8 +97,6 @@ func TestTxProofUnchangable(t *testing.T) {
|
|||
}
|
||||
|
||||
func testTxProofUnchangable(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// make some proof
|
||||
txs := makeTxs(randInt(2, 100), randInt(16, 128))
|
||||
root := txs.Hash()
|
||||
|
@ -95,9 +104,9 @@ func testTxProofUnchangable(t *testing.T) {
|
|||
proof := txs.Proof(i)
|
||||
|
||||
// make sure it is valid to start with
|
||||
assert.Nil(proof.Validate(root))
|
||||
assert.Nil(t, proof.Validate(root))
|
||||
bin, err := cdc.MarshalBinary(proof)
|
||||
assert.Nil(err)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// try mutating the data and make sure nothing breaks
|
||||
for j := 0; j < 500; j++ {
|
||||
|
|
Loading…
Reference in New Issue