cosmos-sdk/state/execution.go

308 lines
8.8 KiB
Go
Raw Normal View History

2016-03-20 03:00:43 -07:00
package state
import (
"bytes"
"github.com/tendermint/basecoin/types"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-events"
2016-03-22 13:07:03 -07:00
tmsp "github.com/tendermint/tmsp/types"
2016-03-20 03:00:43 -07:00
)
2016-03-22 13:07:03 -07:00
// If the tx is invalid, a TMSP error will be returned.
2016-03-29 14:25:17 -07:00
func ExecTx(s *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc events.Fireable) tmsp.Result {
2016-03-22 13:07:03 -07:00
// TODO: do something with fees
2016-04-01 15:19:07 -07:00
fees := types.Coins{}
2016-03-29 14:25:17 -07:00
chainID := s.GetChainID()
// Get the state. If isCheckTx, then we use a cache.
// The idea is to throw away this cache after every EndBlock().
var state types.AccountGetterSetter
if isCheckTx {
state = s.GetCheckCache()
} else {
state = s
}
2016-03-22 13:07:03 -07:00
// Exec tx
switch tx := tx.(type) {
case *types.SendTx:
// First, get inputs
accounts, res := getInputs(state, tx.Inputs)
if res.IsErr() {
2016-03-24 11:27:44 -07:00
return res.PrependLog("in getInputs()")
2016-03-22 13:07:03 -07:00
}
// Then, get or make outputs.
accounts, res = getOrMakeOutputs(state, accounts, tx.Outputs)
if res.IsErr() {
2016-03-24 11:27:44 -07:00
return res.PrependLog("in getOrMakeOutputs()")
2016-03-22 13:07:03 -07:00
}
// Validate inputs and outputs
2016-03-29 14:25:17 -07:00
signBytes := tx.SignBytes(chainID)
inTotal, res := validateInputs(accounts, signBytes, tx.Inputs)
if res.IsErr() {
2016-03-24 11:27:44 -07:00
return res.PrependLog("in validateInputs()")
2016-03-22 13:07:03 -07:00
}
outTotal, res := validateOutputs(tx.Outputs)
if res.IsErr() {
2016-03-24 11:27:44 -07:00
return res.PrependLog("in validateOutputs()")
2016-03-22 13:07:03 -07:00
}
2016-04-01 15:19:07 -07:00
if !inTotal.IsEqual(outTotal.Plus(types.Coins{{"", tx.Fee}})) {
return tmsp.ErrBaseInvalidOutput.AppendLog("Input total != output total + fees")
2016-03-22 13:07:03 -07:00
}
2016-04-01 15:19:07 -07:00
fees = fees.Plus(types.Coins{{"", tx.Fee}})
2016-03-22 13:07:03 -07:00
// TODO: Fee validation for SendTx
// Good! Adjust accounts
2016-03-29 14:25:17 -07:00
adjustByInputs(state, accounts, tx.Inputs)
2016-03-22 13:07:03 -07:00
adjustByOutputs(state, accounts, tx.Outputs, isCheckTx)
/*
// Fire events
if !isCheckTx {
if evc != nil {
for _, i := range tx.Inputs {
evc.FireEvent(types.EventStringAccInput(i.Address), types.EventDataTx{tx, nil, ""})
}
for _, o := range tx.Outputs {
evc.FireEvent(types.EventStringAccOutput(o.Address), types.EventDataTx{tx, nil, ""})
}
}
}
*/
return tmsp.OK
2016-03-22 13:07:03 -07:00
2016-03-27 12:47:50 -07:00
case *types.AppTx:
2016-03-22 13:07:03 -07:00
// First, get input account
inAcc := state.GetAccount(tx.Input.Address)
if inAcc == nil {
2016-03-24 11:27:44 -07:00
return tmsp.ErrBaseUnknownAddress
2016-03-22 13:07:03 -07:00
}
// Validate input
// pubKey should be present in either "inAcc" or "tx.Input"
if res := checkInputPubKey(tx.Input.Address, inAcc, tx.Input); res.IsErr() {
2016-03-22 13:07:03 -07:00
log.Info(Fmt("Can't find pubkey for %X", tx.Input.Address))
return res
}
2016-03-29 14:25:17 -07:00
signBytes := tx.SignBytes(chainID)
res := validateInput(inAcc, signBytes, tx.Input)
if res.IsErr() {
2016-03-22 13:07:03 -07:00
log.Info(Fmt("validateInput failed on %X: %v", tx.Input.Address, res))
2016-03-24 11:27:44 -07:00
return res.PrependLog("in validateInput()")
2016-03-22 13:07:03 -07:00
}
2016-04-01 15:19:07 -07:00
if !tx.Input.Coins.IsGTE(types.Coins{{"", tx.Fee}}) {
2016-03-22 13:07:03 -07:00
log.Info(Fmt("Sender did not send enough to cover the fee %X", tx.Input.Address))
return tmsp.ErrBaseInsufficientFunds
2016-03-22 13:07:03 -07:00
}
// Validate call address
2016-03-27 12:47:50 -07:00
plugin := pgz.GetByByte(tx.Type)
2016-03-24 12:17:26 -07:00
if plugin != nil {
2016-03-27 12:47:50 -07:00
return tmsp.ErrBaseUnknownAddress.AppendLog(
Fmt("Unrecognized type byte %v", tx.Type))
2016-03-22 13:07:03 -07:00
}
// Good!
2016-04-01 15:19:07 -07:00
coins := tx.Input.Coins.Minus(types.Coins{{"", tx.Fee}})
2016-03-22 13:07:03 -07:00
inAcc.Sequence += 1
2016-04-01 15:19:07 -07:00
inAcc.Balance = inAcc.Balance.Minus(tx.Input.Coins)
2016-03-22 13:07:03 -07:00
2016-03-27 12:47:50 -07:00
// If this is a CheckTx, stop now.
2016-03-24 12:17:26 -07:00
if isCheckTx {
2016-03-29 14:25:17 -07:00
state.SetAccount(tx.Input.Address, inAcc)
2016-03-24 12:17:26 -07:00
return tmsp.OK
}
2016-03-29 14:25:17 -07:00
// Create inAcc checkpoint
inAccCopy := inAcc.Copy()
2016-03-24 12:17:26 -07:00
// Run the tx.
2016-03-29 14:25:17 -07:00
cache := types.NewAccountCache(state)
2016-03-24 12:17:26 -07:00
cache.SetAccount(tx.Input.Address, inAcc)
2016-04-01 15:19:07 -07:00
ctx := types.NewCallContext(cache, inAcc, coins)
2016-03-27 12:47:50 -07:00
res = plugin.RunTx(ctx, tx.Data)
2016-03-24 12:17:26 -07:00
if res.IsOK() {
cache.Sync()
2016-03-22 13:07:03 -07:00
log.Info("Successful execution")
// Fire events
/*
if evc != nil {
exception := ""
if res.IsErr() {
2016-03-22 13:07:03 -07:00
exception = res.Error()
}
evc.FireEvent(types.EventStringAccInput(tx.Input.Address), types.EventDataTx{tx, ret, exception})
evc.FireEvent(types.EventStringAccOutput(tx.Address), types.EventDataTx{tx, ret, exception})
}
*/
2016-03-24 12:17:26 -07:00
} else {
2016-03-27 12:47:50 -07:00
log.Info("AppTx failed", "error", res)
2016-04-01 15:19:07 -07:00
// Just return the coins and return.
inAccCopy.Balance = inAccCopy.Balance.Plus(coins)
// But take the gas
// TODO
2016-03-24 12:17:26 -07:00
state.SetAccount(tx.Input.Address, inAccCopy)
2016-03-22 13:07:03 -07:00
}
2016-03-24 12:17:26 -07:00
return res
2016-03-22 13:07:03 -07:00
default:
return tmsp.ErrBaseEncodingError.SetLog("Unknown tx type")
2016-03-22 13:07:03 -07:00
}
}
//--------------------------------------------------------------------------------
2016-03-20 03:00:43 -07:00
// The accounts from the TxInputs must either already have
// crypto.PubKey.(type) != nil, (it must be known),
// or it must be specified in the TxInput. If redeclared,
// the TxInput is modified and input.PubKey set to nil.
2016-03-22 13:07:03 -07:00
func getInputs(state types.AccountGetter, ins []types.TxInput) (map[string]*types.Account, tmsp.Result) {
2016-03-20 03:00:43 -07:00
accounts := map[string]*types.Account{}
for _, in := range ins {
// Account shouldn't be duplicated
if _, ok := accounts[string(in.Address)]; ok {
return nil, tmsp.ErrBaseDuplicateAddress
2016-03-20 03:00:43 -07:00
}
acc := state.GetAccount(in.Address)
if acc == nil {
2016-03-24 11:27:44 -07:00
return nil, tmsp.ErrBaseUnknownAddress
2016-03-20 03:00:43 -07:00
}
// PubKey should be present in either "account" or "in"
if res := checkInputPubKey(in.Address, acc, in); res.IsErr() {
2016-03-22 13:07:03 -07:00
return nil, res
2016-03-20 03:00:43 -07:00
}
accounts[string(in.Address)] = acc
}
return accounts, tmsp.OK
2016-03-20 03:00:43 -07:00
}
2016-03-22 13:07:03 -07:00
func getOrMakeOutputs(state types.AccountGetter, accounts map[string]*types.Account, outs []types.TxOutput) (map[string]*types.Account, tmsp.Result) {
2016-03-20 03:00:43 -07:00
if accounts == nil {
accounts = make(map[string]*types.Account)
}
for _, out := range outs {
// Account shouldn't be duplicated
if _, ok := accounts[string(out.Address)]; ok {
return nil, tmsp.ErrBaseDuplicateAddress
2016-03-20 03:00:43 -07:00
}
acc := state.GetAccount(out.Address)
// output account may be nil (new)
if acc == nil {
acc = &types.Account{
PubKey: nil,
Sequence: 0,
}
}
accounts[string(out.Address)] = acc
}
return accounts, tmsp.OK
2016-03-20 03:00:43 -07:00
}
// Input must not have a redundant PubKey (i.e. Account already has PubKey).
// NOTE: Account has PubKey if Sequence > 0
2016-03-22 13:07:03 -07:00
func checkInputPubKey(address []byte, acc *types.Account, in types.TxInput) tmsp.Result {
2016-03-20 03:00:43 -07:00
if acc.PubKey == nil {
if in.PubKey == nil {
2016-03-24 11:27:44 -07:00
return tmsp.ErrBaseUnknownPubKey.AppendLog("PubKey not present in either acc or input")
2016-03-20 03:00:43 -07:00
}
if !bytes.Equal(in.PubKey.Address(), address) {
2016-03-24 11:27:44 -07:00
return tmsp.ErrBaseInvalidPubKey.AppendLog("Input PubKey address does not match address")
2016-03-20 03:00:43 -07:00
}
acc.PubKey = in.PubKey
} else {
if in.PubKey != nil {
2016-03-24 11:27:44 -07:00
// NOTE: allow redundant pubkey.
if !bytes.Equal(in.PubKey.Address(), address) {
return tmsp.ErrBaseInvalidPubKey.AppendLog("Input PubKey address does not match address")
}
2016-03-20 03:00:43 -07:00
}
}
return tmsp.OK
2016-03-20 03:00:43 -07:00
}
2016-04-01 15:19:07 -07:00
// Validate inputs and compute total amount of coins
func validateInputs(accounts map[string]*types.Account, signBytes []byte, ins []types.TxInput) (total types.Coins, res tmsp.Result) {
2016-03-22 13:07:03 -07:00
2016-03-20 03:00:43 -07:00
for _, in := range ins {
acc := accounts[string(in.Address)]
if acc == nil {
PanicSanity("validateInputs() expects account in accounts")
}
2016-03-29 14:25:17 -07:00
res = validateInput(acc, signBytes, in)
if res.IsErr() {
2016-03-20 03:00:43 -07:00
return
}
// Good. Add amount to total
2016-04-01 15:19:07 -07:00
total = total.Plus(in.Coins)
2016-03-20 03:00:43 -07:00
}
return total, tmsp.OK
2016-03-20 03:00:43 -07:00
}
2016-03-29 14:25:17 -07:00
func validateInput(acc *types.Account, signBytes []byte, in types.TxInput) (res tmsp.Result) {
2016-03-20 03:00:43 -07:00
// Check TxInput basic
if res := in.ValidateBasic(); res.IsErr() {
2016-03-22 13:07:03 -07:00
return res
2016-03-20 03:00:43 -07:00
}
2016-04-01 15:19:07 -07:00
// Check sequence/coins
2016-03-29 14:25:17 -07:00
seq, balance := acc.Sequence, acc.Balance
2016-03-22 13:07:03 -07:00
if seq+1 != in.Sequence {
return tmsp.ErrBaseInvalidSequence.AppendLog(Fmt("Got %v, expected %v. (acc.seq=%v)", in.Sequence, seq+1, acc.Sequence))
2016-03-20 03:00:43 -07:00
}
// Check amount
2016-04-01 15:19:07 -07:00
if !balance.IsGTE(in.Coins) {
return tmsp.ErrBaseInsufficientFunds
2016-03-20 03:00:43 -07:00
}
2016-03-22 13:07:03 -07:00
// Check signatures
if !acc.PubKey.VerifyBytes(signBytes, in.Signature) {
2016-03-24 11:27:44 -07:00
return tmsp.ErrBaseInvalidSignature.AppendLog(Fmt("SignBytes: %X", signBytes))
2016-03-22 13:07:03 -07:00
}
return tmsp.OK
2016-03-20 03:00:43 -07:00
}
2016-04-01 15:19:07 -07:00
func validateOutputs(outs []types.TxOutput) (total types.Coins, res tmsp.Result) {
2016-03-20 03:00:43 -07:00
for _, out := range outs {
// Check TxOutput basic
if res := out.ValidateBasic(); res.IsErr() {
2016-04-01 15:19:07 -07:00
return nil, res
2016-03-20 03:00:43 -07:00
}
// Good. Add amount to total
2016-04-01 15:19:07 -07:00
total = total.Plus(out.Coins)
2016-03-20 03:00:43 -07:00
}
return total, tmsp.OK
2016-03-20 03:00:43 -07:00
}
2016-03-29 14:25:17 -07:00
func adjustByInputs(state types.AccountSetter, accounts map[string]*types.Account, ins []types.TxInput) {
2016-03-20 03:00:43 -07:00
for _, in := range ins {
acc := accounts[string(in.Address)]
if acc == nil {
PanicSanity("adjustByInputs() expects account in accounts")
}
2016-04-01 15:19:07 -07:00
if !acc.Balance.IsGTE(in.Coins) {
2016-03-20 03:00:43 -07:00
PanicSanity("adjustByInputs() expects sufficient funds")
}
2016-04-01 15:19:07 -07:00
acc.Balance = acc.Balance.Minus(in.Coins)
2016-03-20 03:00:43 -07:00
acc.Sequence += 1
2016-03-29 14:25:17 -07:00
state.SetAccount(in.Address, acc)
2016-03-20 03:00:43 -07:00
}
}
2016-03-29 14:25:17 -07:00
func adjustByOutputs(state types.AccountSetter, accounts map[string]*types.Account, outs []types.TxOutput, isCheckTx bool) {
2016-03-20 03:00:43 -07:00
for _, out := range outs {
acc := accounts[string(out.Address)]
if acc == nil {
PanicSanity("adjustByOutputs() expects account in accounts")
}
2016-04-01 15:19:07 -07:00
acc.Balance = acc.Balance.Plus(out.Coins)
2016-03-22 13:07:03 -07:00
if !isCheckTx {
2016-03-24 11:27:44 -07:00
state.SetAccount(out.Address, acc)
2016-03-20 03:00:43 -07:00
}
}
}