cosmos-sdk/state/execution.go

297 lines
8.3 KiB
Go
Raw Normal View History

2016-03-20 03:00:43 -07:00
package state
import (
2017-01-14 20:42:47 -08:00
abci "github.com/tendermint/abci/types"
2016-03-20 03:00:43 -07:00
"github.com/tendermint/basecoin/types"
2017-02-24 14:12:05 -08:00
cmn "github.com/tendermint/go-common"
2016-03-20 03:00:43 -07:00
"github.com/tendermint/go-events"
)
2016-03-22 13:07:03 -07:00
// If the tx is invalid, a TMSP error will be returned.
2017-01-14 20:42:47 -08:00
func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc events.Fireable) abci.Result {
2016-03-22 13:07:03 -07:00
2016-05-01 13:52:08 -07:00
chainID := state.GetChainID()
2016-03-22 13:07:03 -07:00
// Exec tx
switch tx := tx.(type) {
case *types.SendTx:
2016-04-18 08:09:19 -07:00
// Validate inputs and outputs, basic
res := validateInputsBasic(tx.Inputs)
if res.IsErr() {
return res.PrependLog("in validateInputsBasic()")
}
res = validateOutputsBasic(tx.Outputs)
if res.IsErr() {
return res.PrependLog("in validateOutputsBasic()")
}
// Get inputs
2016-03-22 13:07:03 -07:00
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
}
2016-04-18 08:09:19 -07:00
// Get or make outputs.
2016-03-22 13:07:03 -07:00
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
}
2016-04-18 08:09:19 -07:00
// Validate inputs and outputs, advanced
2016-03-29 14:25:17 -07:00
signBytes := tx.SignBytes(chainID)
2016-04-18 08:09:19 -07:00
inTotal, res := validateInputsAdvanced(accounts, signBytes, tx.Inputs)
if res.IsErr() {
2016-04-18 08:09:19 -07:00
return res.PrependLog("in validateInputsAdvanced()")
2016-03-22 13:07:03 -07:00
}
2016-04-18 08:09:19 -07:00
outTotal := sumOutputs(tx.Outputs)
2017-02-24 14:12:05 -08:00
outPlusFees := outTotal
fees := types.Coins{tx.Fee}
if fees.IsValid() { // TODO: fix coins.Plus()
outPlusFees = outTotal.Plus(fees)
}
if !inTotal.IsEqual(outPlusFees) {
return abci.ErrBaseInvalidOutput.AppendLog(cmn.Fmt("Input total (%v) != output total + fees (%v)", inTotal, outPlusFees))
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, ""})
}
}
}
*/
2017-02-23 15:55:20 -08:00
return abci.NewResultOK(types.TxID(chainID, tx), "")
2016-03-22 13:07:03 -07:00
2016-03-27 12:47:50 -07:00
case *types.AppTx:
2016-04-18 08:09:19 -07:00
// Validate input, basic
res := tx.Input.ValidateBasic()
if res.IsErr() {
return res
}
// Get input account
2016-03-22 13:07:03 -07:00
inAcc := state.GetAccount(tx.Input.Address)
if inAcc == nil {
2017-01-14 20:42:47 -08:00
return abci.ErrBaseUnknownAddress
2016-03-22 13:07:03 -07:00
}
if !tx.Input.PubKey.Empty() {
2016-04-18 08:09:19 -07:00
inAcc.PubKey = tx.Input.PubKey
2016-03-22 13:07:03 -07:00
}
2016-04-18 08:09:19 -07:00
// Validate input, advanced
2016-03-29 14:25:17 -07:00
signBytes := tx.SignBytes(chainID)
2016-04-18 08:09:19 -07:00
res = validateInputAdvanced(inAcc, signBytes, tx.Input)
if res.IsErr() {
2017-02-24 14:12:05 -08:00
log.Info(cmn.Fmt("validateInputAdvanced failed on %X: %v", tx.Input.Address, res))
2016-04-18 08:09:19 -07:00
return res.PrependLog("in validateInputAdvanced()")
2016-03-22 13:07:03 -07:00
}
if !tx.Input.Coins.IsGTE(types.Coins{tx.Fee}) {
2017-02-24 14:12:05 -08:00
log.Info(cmn.Fmt("Sender did not send enough to cover the fee %X", tx.Input.Address))
return abci.ErrBaseInsufficientFunds.AppendLog(cmn.Fmt("input coins is %v, but fee is %v", tx.Input.Coins, types.Coins{tx.Fee}))
2016-03-22 13:07:03 -07:00
}
// Validate call address
2017-01-13 16:10:22 -08:00
plugin := pgz.GetByName(tx.Name)
2016-04-19 02:37:13 -07:00
if plugin == nil {
2017-01-14 20:42:47 -08:00
return abci.ErrBaseUnknownAddress.AppendLog(
2017-02-24 14:12:05 -08:00
cmn.Fmt("Unrecognized plugin name%v", tx.Name))
2016-03-22 13:07:03 -07:00
}
// Good!
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)
2017-01-14 20:42:47 -08:00
return abci.OK
2016-03-24 12:17:26 -07:00
}
2016-03-29 14:25:17 -07:00
// Create inAcc checkpoint
inAccCopy := inAcc.Copy()
2016-03-29 14:25:17 -07:00
2016-03-24 12:17:26 -07:00
// Run the tx.
2016-05-01 13:52:08 -07:00
cache := state.CacheWrap()
2016-03-24 12:17:26 -07:00
cache.SetAccount(tx.Input.Address, inAcc)
ctx := types.NewCallContext(tx.Input.Address, inAcc, coins)
2016-05-01 13:52:08 -07:00
res = plugin.RunTx(cache, ctx, tx.Data)
2016-03-24 12:17:26 -07:00
if res.IsOK() {
2016-05-01 13:52:08 -07:00
cache.CacheSync()
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)
2016-04-01 15:19:07 -07:00
// But take the gas
// TODO
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:
2017-01-14 20:42:47 -08:00
return abci.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),
2016-04-18 08:09:19 -07:00
// or it must be specified in the TxInput.
2017-01-14 20:42:47 -08:00
func getInputs(state types.AccountGetter, ins []types.TxInput) (map[string]*types.Account, abci.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 {
2017-01-14 20:42:47 -08:00
return nil, abci.ErrBaseDuplicateAddress
2016-03-20 03:00:43 -07:00
}
2017-01-12 12:25:04 -08:00
2016-03-20 03:00:43 -07:00
acc := state.GetAccount(in.Address)
if acc == nil {
2017-01-14 20:42:47 -08:00
return nil, abci.ErrBaseUnknownAddress
2016-03-20 03:00:43 -07:00
}
2017-01-12 12:25:04 -08:00
if !in.PubKey.Empty() {
2016-04-18 08:09:19 -07:00
acc.PubKey = in.PubKey
2016-03-20 03:00:43 -07:00
}
accounts[string(in.Address)] = acc
}
2017-01-14 20:42:47 -08:00
return accounts, abci.OK
2016-03-20 03:00:43 -07:00
}
2017-01-14 20:42:47 -08:00
func getOrMakeOutputs(state types.AccountGetter, accounts map[string]*types.Account, outs []types.TxOutput) (map[string]*types.Account, abci.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 {
2017-01-14 20:42:47 -08:00
return nil, abci.ErrBaseDuplicateAddress
2016-03-20 03:00:43 -07:00
}
acc := state.GetAccount(out.Address)
// output account may be nil (new)
if acc == nil {
// zero value is valid, empty account
acc = &types.Account{}
2016-03-20 03:00:43 -07:00
}
accounts[string(out.Address)] = acc
}
2017-01-14 20:42:47 -08:00
return accounts, abci.OK
2016-03-20 03:00:43 -07:00
}
2016-04-18 08:09:19 -07:00
// Validate inputs basic structure
2017-01-14 20:42:47 -08:00
func validateInputsBasic(ins []types.TxInput) (res abci.Result) {
2016-04-18 08:09:19 -07:00
for _, in := range ins {
// Check TxInput basic
if res := in.ValidateBasic(); res.IsErr() {
return res
2016-03-20 03:00:43 -07:00
}
}
2017-01-14 20:42:47 -08:00
return abci.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
2017-01-14 20:42:47 -08:00
func validateInputsAdvanced(accounts map[string]*types.Account, signBytes []byte, ins []types.TxInput) (total types.Coins, res abci.Result) {
2016-03-20 03:00:43 -07:00
for _, in := range ins {
acc := accounts[string(in.Address)]
if acc == nil {
2017-02-24 14:12:05 -08:00
cmn.PanicSanity("validateInputsAdvanced() expects account in accounts")
2016-03-20 03:00:43 -07:00
}
2016-04-18 08:09:19 -07:00
res = validateInputAdvanced(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
}
2017-01-14 20:42:47 -08:00
return total, abci.OK
2016-03-20 03:00:43 -07:00
}
2017-01-14 20:42:47 -08:00
func validateInputAdvanced(acc *types.Account, signBytes []byte, in types.TxInput) (res abci.Result) {
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 {
2017-02-24 14:12:05 -08:00
return abci.ErrBaseInvalidSequence.AppendLog(cmn.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) {
2017-02-24 14:12:05 -08:00
return abci.ErrBaseInsufficientFunds.AppendLog(cmn.Fmt("balance is %v, tried to send %v", balance, in.Coins))
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.Signature) {
2017-02-24 14:12:05 -08:00
return abci.ErrBaseInvalidSignature.AppendLog(cmn.Fmt("SignBytes: %X", signBytes))
2016-03-22 13:07:03 -07:00
}
2017-01-14 20:42:47 -08:00
return abci.OK
2016-03-20 03:00:43 -07:00
}
2017-01-14 20:42:47 -08:00
func validateOutputsBasic(outs []types.TxOutput) (res abci.Result) {
2016-03-20 03:00:43 -07:00
for _, out := range outs {
// Check TxOutput basic
if res := out.ValidateBasic(); res.IsErr() {
2016-04-18 08:09:19 -07:00
return res
2016-03-20 03:00:43 -07:00
}
2016-04-18 08:09:19 -07:00
}
2017-01-14 20:42:47 -08:00
return abci.OK
2016-04-18 08:09:19 -07:00
}
func sumOutputs(outs []types.TxOutput) (total types.Coins) {
for _, out := range outs {
2016-04-01 15:19:07 -07:00
total = total.Plus(out.Coins)
2016-03-20 03:00:43 -07:00
}
2016-04-18 08:09:19 -07:00
return total
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 {
2017-02-24 14:12:05 -08:00
cmn.PanicSanity("adjustByInputs() expects account in accounts")
2016-03-20 03:00:43 -07:00
}
2016-04-01 15:19:07 -07:00
if !acc.Balance.IsGTE(in.Coins) {
2017-02-24 14:12:05 -08:00
cmn.PanicSanity("adjustByInputs() expects sufficient funds")
2016-03-20 03:00:43 -07:00
}
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 {
2017-02-24 14:12:05 -08:00
cmn.PanicSanity("adjustByOutputs() expects account in accounts")
2016-03-20 03:00:43 -07:00
}
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
}
}
}