tendermint/types/tx.go

124 lines
2.9 KiB
Go
Raw Normal View History

package types
2014-06-05 02:34:45 -07:00
2016-03-12 10:01:08 -08:00
import (
2017-04-03 06:45:09 -07:00
"bytes"
"errors"
2017-04-30 16:03:30 -07:00
"fmt"
2017-04-03 06:45:09 -07:00
2017-04-12 15:18:17 -07:00
abci "github.com/tendermint/abci/types"
2017-04-27 16:01:18 -07:00
"github.com/tendermint/go-wire/data"
"github.com/tendermint/tmlibs/merkle"
2016-03-12 10:01:08 -08:00
)
2017-09-22 09:00:37 -07:00
// Tx represents a transaction, which may contain arbitrary bytes.
2015-11-01 11:34:08 -08:00
type Tx []byte
2016-03-12 10:01:08 -08:00
2017-09-22 09:00:37 -07:00
// Hash returns the hash of the go-wire encoded Tx.
// Tx has no types at this level, so go-wire encoding only adds length-prefix.
// NOTE: It may make sense to add types here one day and let []byte be type 0x1
// so we can have versioned txs if need be in the future.
2016-06-27 17:43:09 -07:00
func (tx Tx) Hash() []byte {
return merkle.SimpleHashFromBinary(tx)
}
2017-09-22 09:00:37 -07:00
// String returns a string representation of the Tx.
2017-04-30 16:03:30 -07:00
func (tx Tx) String() string {
2017-05-16 05:12:48 -07:00
return fmt.Sprintf("Tx{%X}", []byte(tx))
2017-04-30 16:03:30 -07:00
}
2017-09-22 09:00:37 -07:00
// Txs is a slice of Tx.
2016-03-12 10:01:08 -08:00
type Txs []Tx
2017-09-22 09:00:37 -07:00
// Hash returns the simple Merkle root hash of the Txs.
2016-03-12 10:01:08 -08:00
func (txs Txs) Hash() []byte {
// Recursive impl.
// Copied from tmlibs/merkle to avoid allocations
2016-03-12 10:01:08 -08:00
switch len(txs) {
case 0:
return nil
case 1:
2016-06-27 17:43:09 -07:00
return txs[0].Hash()
2016-03-12 10:01:08 -08:00
default:
left := Txs(txs[:(len(txs)+1)/2]).Hash()
right := Txs(txs[(len(txs)+1)/2:]).Hash()
return merkle.SimpleHashFromTwoHashes(left, right)
}
}
2017-04-12 15:18:17 -07:00
2017-04-03 06:45:09 -07:00
// Index returns the index of this transaction in the list, or -1 if not found
func (txs Txs) Index(tx Tx) int {
for i := range txs {
if bytes.Equal(txs[i], tx) {
return i
}
}
2017-04-03 06:45:09 -07:00
return -1
}
2017-09-22 09:00:37 -07:00
// IndexByHash returns the index of this transaction hash in the list, or -1 if not found
2017-04-13 13:04:20 -07:00
func (txs Txs) IndexByHash(hash []byte) int {
for i := range txs {
if bytes.Equal(txs[i].Hash(), hash) {
return i
}
}
return -1
}
2017-04-03 06:45:09 -07:00
// Proof returns a simple merkle proof for this node.
//
// Panics if i < 0 or i >= len(txs)
//
// TODO: optimize this!
func (txs Txs) Proof(i int) TxProof {
l := len(txs)
hashables := make([]merkle.Hashable, l)
for i := 0; i < l; i++ {
hashables[i] = txs[i]
}
root, proofs := merkle.SimpleProofsFromHashables(hashables)
return TxProof{
Index: i,
Total: l,
RootHash: root,
Data: txs[i],
Proof: *proofs[i],
}
}
type TxProof struct {
Index, Total int
2017-04-27 16:01:18 -07:00
RootHash data.Bytes
2017-04-03 06:45:09 -07:00
Data Tx
Proof merkle.SimpleProof
}
func (tp TxProof) LeafHash() []byte {
return tp.Data.Hash()
}
// Validate returns nil if it matches the dataHash, and is internally consistent
// otherwise, returns a sensible error
func (tp TxProof) Validate(dataHash []byte) error {
if !bytes.Equal(dataHash, tp.RootHash) {
return errors.New("Proof matches different data hash")
}
valid := tp.Proof.Verify(tp.Index, tp.Total, tp.LeafHash(), tp.RootHash)
if !valid {
return errors.New("Proof is not internally consistent")
}
return nil
}
2017-04-12 15:18:17 -07:00
// TxResult contains results of executing the transaction.
//
// One usage is indexing transaction results.
type TxResult struct {
Height uint64 `json:"height"`
Index uint32 `json:"index"`
Tx Tx `json:"tx"`
Result abci.ResponseDeliverTx `json:"result"`
2017-04-12 15:18:17 -07:00
}