mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
make_txs > tx_utils
This commit is contained in:
parent
2ec3d0611f
commit
d78a39ade3
@ -1,127 +0,0 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint/account"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// SendTx interface for adding inputs/outputs and adding signatures
|
||||
|
||||
func NewSendTx() *types.SendTx {
|
||||
return &types.SendTx{
|
||||
Inputs: []*types.TxInput{},
|
||||
Outputs: []*types.TxOutput{},
|
||||
}
|
||||
}
|
||||
|
||||
func SendTxAddInput(st AccountGetter, tx *types.SendTx, pubkey account.PubKey, amt uint64) error {
|
||||
addr := pubkey.Address()
|
||||
acc := st.GetAccount(addr)
|
||||
if acc == nil {
|
||||
return fmt.Errorf("Invalid address %X from pubkey %X", addr, pubkey)
|
||||
}
|
||||
|
||||
tx.Inputs = append(tx.Inputs, &types.TxInput{
|
||||
Address: addr,
|
||||
Amount: amt,
|
||||
Sequence: uint(acc.Sequence) + 1,
|
||||
Signature: account.SignatureEd25519{},
|
||||
PubKey: pubkey,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func SendTxAddOutput(tx *types.SendTx, addr []byte, amt uint64) error {
|
||||
tx.Outputs = append(tx.Outputs, &types.TxOutput{
|
||||
Address: addr,
|
||||
Amount: amt,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func SignSendTx(tx *types.SendTx, i int, privAccount *account.PrivAccount) error {
|
||||
if i >= len(tx.Inputs) {
|
||||
return fmt.Errorf("Index %v is greater than number of inputs (%v)", i, len(tx.Inputs))
|
||||
}
|
||||
tx.Inputs[i].PubKey = privAccount.PubKey
|
||||
tx.Inputs[i].Signature = privAccount.Sign(tx)
|
||||
return nil
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// CallTx interface for creating tx
|
||||
|
||||
func NewCallTx(st AccountGetter, from account.PubKey, to, data []byte, amt, gasLimit, fee uint64) (*types.CallTx, error) {
|
||||
addr := from.Address()
|
||||
acc := st.GetAccount(addr)
|
||||
if acc == nil {
|
||||
return nil, fmt.Errorf("Invalid address %X from pubkey %X", addr, from)
|
||||
}
|
||||
|
||||
input := &types.TxInput{
|
||||
Address: addr,
|
||||
Amount: amt,
|
||||
Sequence: uint(acc.Sequence) + 1,
|
||||
Signature: account.SignatureEd25519{},
|
||||
PubKey: from,
|
||||
}
|
||||
|
||||
return &types.CallTx{
|
||||
Input: input,
|
||||
Address: to,
|
||||
GasLimit: gasLimit,
|
||||
Fee: fee,
|
||||
Data: data,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func SignCallTx(tx *types.CallTx, privAccount *account.PrivAccount) {
|
||||
tx.Input.PubKey = privAccount.PubKey
|
||||
tx.Input.Signature = privAccount.Sign(tx)
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// BondTx interface for adding inputs/outputs and adding signatures
|
||||
|
||||
func NewBondTx() *types.BondTx {
|
||||
return &types.BondTx{
|
||||
Inputs: []*types.TxInput{},
|
||||
UnbondTo: []*types.TxOutput{},
|
||||
}
|
||||
}
|
||||
|
||||
func BondTxAddInput(st AccountGetter, tx *types.BondTx, pubkey account.PubKey, amt uint64) error {
|
||||
addr := pubkey.Address()
|
||||
acc := st.GetAccount(addr)
|
||||
if acc == nil {
|
||||
return fmt.Errorf("Invalid address %X from pubkey %X", addr, pubkey)
|
||||
}
|
||||
|
||||
tx.Inputs = append(tx.Inputs, &types.TxInput{
|
||||
Address: addr,
|
||||
Amount: amt,
|
||||
Sequence: uint(acc.Sequence) + 1,
|
||||
Signature: account.SignatureEd25519{},
|
||||
PubKey: pubkey,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func BondTxAddOutput(tx *types.BondTx, addr []byte, amt uint64) error {
|
||||
tx.UnbondTo = append(tx.UnbondTo, &types.TxOutput{
|
||||
Address: addr,
|
||||
Amount: amt,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func SignBondTx(tx *types.BondTx, i int, privAccount *account.PrivAccount) error {
|
||||
if i >= len(tx.Inputs) {
|
||||
return fmt.Errorf("Index %v is greater than number of inputs (%v)", i, len(tx.Inputs))
|
||||
}
|
||||
tx.Inputs[i].PubKey = privAccount.PubKey
|
||||
tx.Inputs[i].Signature = privAccount.Sign(tx)
|
||||
return nil
|
||||
}
|
@ -231,27 +231,27 @@ func TestSendPermission(t *testing.T) {
|
||||
blockCache := NewBlockCache(st)
|
||||
|
||||
// A single input, having the permission, should succeed
|
||||
tx := NewSendTx()
|
||||
if err := SendTxAddInput(blockCache, tx, user[0].PubKey, 5); err != nil {
|
||||
tx := types.NewSendTx()
|
||||
if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
SendTxAddOutput(tx, user[1].Address, 5)
|
||||
SignSendTx(tx, 0, user[0])
|
||||
tx.AddOutput(user[1].Address, 5)
|
||||
tx.SignInput(0, user[0])
|
||||
if err := ExecTx(blockCache, tx, true, nil); err != nil {
|
||||
t.Fatal("Transaction failed", err)
|
||||
}
|
||||
|
||||
// Two inputs, one with permission, one without, should fail
|
||||
tx = NewSendTx()
|
||||
if err := SendTxAddInput(blockCache, tx, user[0].PubKey, 5); err != nil {
|
||||
tx = types.NewSendTx()
|
||||
if err := tx.AddInput(blockCache, user[0].PubKey, 5); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SendTxAddInput(blockCache, tx, user[1].PubKey, 5); err != nil {
|
||||
if err := tx.AddInput(blockCache, user[1].PubKey, 5); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
SendTxAddOutput(tx, user[2].Address, 10)
|
||||
SignSendTx(tx, 0, user[0])
|
||||
SignSendTx(tx, 1, user[1])
|
||||
tx.AddOutput(user[2].Address, 10)
|
||||
tx.SignInput(0, user[0])
|
||||
tx.SignInput(1, user[1])
|
||||
if err := ExecTx(blockCache, tx, true, nil); err == nil {
|
||||
t.Fatal("Expected error")
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user