mirror of
https://github.com/tendermint/tendermint.git
synced 2026-07-20 23:12:44 +00:00
move abci types into pkg
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package types
|
||||
package abci
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,4 +1,4 @@
|
||||
package types
|
||||
package abci
|
||||
|
||||
import (
|
||||
"io"
|
||||
@@ -1,4 +1,4 @@
|
||||
package types
|
||||
package abci
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -1,4 +1,4 @@
|
||||
package types
|
||||
package abci
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package types
|
||||
package abci
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -0,0 +1,53 @@
|
||||
package abci
|
||||
|
||||
import (
|
||||
"github.com/tendermint/tendermint/crypto/merkle"
|
||||
)
|
||||
|
||||
// ABCIResults wraps the deliver tx results to return a proof.
|
||||
type ABCIResults []*ResponseDeliverTx
|
||||
|
||||
// NewResults strips non-deterministic fields from ResponseDeliverTx responses
|
||||
// and returns ABCIResults.
|
||||
func NewResults(responses []*ResponseDeliverTx) ABCIResults {
|
||||
res := make(ABCIResults, len(responses))
|
||||
for i, d := range responses {
|
||||
res[i] = deterministicResponseDeliverTx(d)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Hash returns a merkle hash of all results.
|
||||
func (a ABCIResults) Hash() []byte {
|
||||
return merkle.HashFromByteSlices(a.toByteSlices())
|
||||
}
|
||||
|
||||
// ProveResult returns a merkle proof of one result from the set
|
||||
func (a ABCIResults) ProveResult(i int) merkle.Proof {
|
||||
_, proofs := merkle.ProofsFromByteSlices(a.toByteSlices())
|
||||
return *proofs[i]
|
||||
}
|
||||
|
||||
func (a ABCIResults) toByteSlices() [][]byte {
|
||||
l := len(a)
|
||||
bzs := make([][]byte, l)
|
||||
for i := 0; i < l; i++ {
|
||||
bz, err := a[i].Marshal()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bzs[i] = bz
|
||||
}
|
||||
return bzs
|
||||
}
|
||||
|
||||
// deterministicResponseDeliverTx strips non-deterministic fields from
|
||||
// ResponseDeliverTx and returns another ResponseDeliverTx.
|
||||
func deterministicResponseDeliverTx(response *ResponseDeliverTx) *ResponseDeliverTx {
|
||||
return &ResponseDeliverTx{
|
||||
Code: response.Code,
|
||||
Data: response.Data,
|
||||
GasWanted: response.GasWanted,
|
||||
GasUsed: response.GasUsed,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package abci_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/tendermint/tendermint/pkg/abci"
|
||||
)
|
||||
|
||||
func TestABCIResults(t *testing.T) {
|
||||
a := &abci.ResponseDeliverTx{Code: 0, Data: nil}
|
||||
b := &abci.ResponseDeliverTx{Code: 0, Data: []byte{}}
|
||||
c := &abci.ResponseDeliverTx{Code: 0, Data: []byte("one")}
|
||||
d := &abci.ResponseDeliverTx{Code: 14, Data: nil}
|
||||
e := &abci.ResponseDeliverTx{Code: 14, Data: []byte("foo")}
|
||||
f := &abci.ResponseDeliverTx{Code: 14, Data: []byte("bar")}
|
||||
|
||||
// Nil and []byte{} should produce the same bytes
|
||||
bzA, err := a.Marshal()
|
||||
require.NoError(t, err)
|
||||
bzB, err := b.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, bzA, bzB)
|
||||
|
||||
// a and b should be the same, don't go in results.
|
||||
results := abci.ABCIResults{a, c, d, e, f}
|
||||
|
||||
// Make sure each result serializes differently
|
||||
last := []byte{}
|
||||
assert.Equal(t, last, bzA) // first one is empty
|
||||
for i, res := range results[1:] {
|
||||
bz, err := res.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.NotEqual(t, last, bz, "%d", i)
|
||||
last = bz
|
||||
}
|
||||
|
||||
// Make sure that we can get a root hash from results and verify proofs.
|
||||
root := results.Hash()
|
||||
assert.NotEmpty(t, root)
|
||||
|
||||
for i, res := range results {
|
||||
bz, err := res.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
proof := results.ProveResult(i)
|
||||
valid := proof.Verify(root, bz)
|
||||
assert.NoError(t, valid, "%d", i)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by protoc-gen-gogo. DO NOT EDIT.
|
||||
// source: tendermint/abci/types.proto
|
||||
|
||||
package types
|
||||
package abci
|
||||
|
||||
import (
|
||||
context "context"
|
||||
@@ -1,4 +1,4 @@
|
||||
package types
|
||||
package abci
|
||||
|
||||
import (
|
||||
"sort"
|
||||
Reference in New Issue
Block a user