tendermint/types/tx.go

138 lines
4.0 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
"github.com/tendermint/go-amino"
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 {
// These allocations will be removed once Txs is switched to [][]byte,
// ref #2603. This is because golang does not allow type casting slices without unsafe
txBzs := make([][]byte, len(txs))
for i := 0; i < len(txs); i++ {
txBzs[i] = txs[i]
2016-03-12 10:01:08 -08:00
}
return merkle.SimpleHashFromByteSlices(txBzs)
2016-03-12 10:01:08 -08:00
}
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)
bzs := make([][]byte, l)
2017-04-03 06:45:09 -07:00
for i := 0; i < l; i++ {
bzs[i] = txs[i]
2017-04-03 06:45:09 -07:00
}
root, proofs := merkle.SimpleProofsFromByteSlices(bzs)
2017-04-03 06:45:09 -07:00
return TxProof{
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 {
RootHash cmn.HexBytes
Data Tx
Proof merkle.SimpleProof
2017-04-03 06:45:09 -07:00
}
// 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.Proof.Index < 0 {
return errors.New("Proof index cannot be negative")
}
if tp.Proof.Total <= 0 {
return errors.New("Proof total must be positive")
}
valid := tp.Proof.Verify(tp.RootHash, tp.LeafHash())
if valid != nil {
2017-04-03 06:45:09 -07:00
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
}
// ComputeAminoOverhead calculates the overhead for amino encoding a transaction.
// The overhead consists of varint encoding the field number and the wire type
// (= length-delimited = 2), and another varint encoding the length of the
// transaction.
// The field number can be the field number of the particular transaction, or
// the field number of the parenting struct that contains the transactions []Tx
// as a field (this field number is repeated for each contained Tx).
// If some []Tx are encoded directly (without a parenting struct), the default
// fieldNum is also 1 (see BinFieldNum in amino.MarshalBinaryBare).
func ComputeAminoOverhead(tx Tx, fieldNum int) int64 {
fnum := uint64(fieldNum)
typ3AndFieldNum := (uint64(fnum) << 3) | uint64(amino.Typ3_ByteLength)
return int64(amino.UvarintSize(typ3AndFieldNum)) + int64(amino.UvarintSize(uint64(len(tx))))
}