forked from cosmos/ibc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tx Method implementation (cosmos#272)
- Loading branch information
1 parent
2aaa9d0
commit ec78aa3
Showing
5 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,43 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/tendermint/tendermint/crypto/merkle" | ||
"github.com/tendermint/tendermint/crypto/tmhash" | ||
tmbytes "github.com/tendermint/tendermint/libs/bytes" | ||
) | ||
|
||
// Tx represents transactoin. | ||
type Tx []byte | ||
|
||
// Txs represents a slice of transactions. | ||
type Txs []Tx | ||
|
||
// Hash computes the TMHASH hash of the wire encoded transaction. | ||
func (tx Tx) Hash() []byte { | ||
return tmhash.Sum(tx) | ||
} | ||
|
||
// 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) | ||
for i := 0; i < l; i++ { | ||
bzs[i] = txs[i].Hash() | ||
} | ||
root, proofs := merkle.ProofsFromByteSlices(bzs) | ||
|
||
return TxProof{ | ||
RootHash: root, | ||
Data: txs[i], | ||
Proof: *proofs[i], | ||
} | ||
} | ||
|
||
// TxProof represents a Merkle proof of the presence of a transaction in the Merkle tree. | ||
type TxProof struct { | ||
RootHash tmbytes.HexBytes `json:"root_hash"` | ||
Data Tx `json:"data"` | ||
Proof merkle.Proof `json:"proof"` | ||
} |