tendermint/types/tx.go

129 lines
3.2 KiB
Go
Raw Permalink 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
2018-06-21 21:59:02 -07:00
abci "github.com/tendermint/tendermint/abci/types"
2018-06-30 22:40:03 -07:00
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
2016-03-12 10:01:08 -08:00
)
2017-08-28 17:09:18 -07:00
// Tx is an arbitrary byte array.
2018-02-15 11:26:49 -08:00
// NOTE: Tx has no types at this level, so when wire encoded it's just length-prefixed.
2018-06-30 22:40:03 -07:00
// Might we want types here ?
2015-11-01 11:34:08 -08:00
type Tx []byte
2016-03-12 10:01:08 -08:00
2018-06-30 22:40:03 -07:00
// Hash computes the TMHASH hash of the wire encoded transaction.
2016-06-27 17:43:09 -07:00
func (tx Tx) Hash() []byte {
2018-06-30 22:40:03 -07:00
return tmhash.Sum(tx)
2016-06-27 17:43:09 -07:00
}
2017-08-28 17:09:18 -07:00
// String returns the hex-encoded transaction as a string.
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-08-28 17:09:18 -07:00
// Hash returns the simple Merkle root hash of the transactions.
2016-03-12 10:01:08 -08:00
func (txs Txs) Hash() []byte {
// Recursive impl.
2018-06-30 22:40:03 -07:00
// Copied from tendermint/crypto/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)
2018-02-03 00:23:10 -08:00
hashers := make([]merkle.Hasher, l)
2017-04-03 06:45:09 -07:00
for i := 0; i < l; i++ {
2018-02-03 00:23:10 -08:00
hashers[i] = txs[i]
2017-04-03 06:45:09 -07:00
}
2018-02-03 00:23:10 -08:00
root, proofs := merkle.SimpleProofsFromHashers(hashers)
2017-04-03 06:45:09 -07:00
return TxProof{
Index: i,
Total: l,
RootHash: root,
Data: txs[i],
Proof: *proofs[i],
}
}
2017-08-28 17:09:18 -07:00
// TxProof represents a Merkle proof of the presence of a transaction in the Merkle tree.
2017-04-03 06:45:09 -07:00
type TxProof struct {
Index, Total int
2018-02-03 00:42:59 -08:00
RootHash cmn.HexBytes
2017-04-03 06:45:09 -07:00
Data Tx
Proof merkle.SimpleProof
}
// LeadHash returns the hash of the this proof refers to.
2017-04-03 06:45:09 -07:00
func (tp TxProof) LeafHash() []byte {
return tp.Data.Hash()
}
2017-08-28 17:09:18 -07:00
// Validate verifies the proof. It returns nil if the RootHash matches the dataHash argument,
// and if the proof is internally consistent. Otherwise, it returns a sensible error.
2017-04-03 06:45:09 -07:00
func (tp TxProof) Validate(dataHash []byte) error {
if !bytes.Equal(dataHash, tp.RootHash) {
return errors.New("Proof matches different data hash")
}
if tp.Index < 0 {
return errors.New("Proof index cannot be negative")
}
if tp.Total <= 0 {
return errors.New("Proof total must be positive")
}
2017-04-03 06:45:09 -07:00
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 int64 `json:"height"`
Index uint32 `json:"index"`
Tx Tx `json:"tx"`
Result abci.ResponseDeliverTx `json:"result"`
2017-04-12 15:18:17 -07:00
}