cosmos-sdk/state/execution.go

304 lines
8.2 KiB
Go
Raw Normal View History

2016-03-20 03:00:43 -07:00
package state
import (
"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:
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)
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-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 {
2016-03-24 11:27:44 -07:00
return tmsp.ErrBaseUnknownAddress
2016-03-22 13:07:03 -07:00
}
2016-04-18 08:09:19 -07:00
if tx.Input.PubKey != nil {
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() {
2016-04-18 08:09:19 -07:00
log.Info(Fmt("validateInputAdvanced failed on %X: %v", tx.Input.Address, res))
return res.PrependLog("in validateInputAdvanced()")
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-04-19 02:37:13 -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),
2016-04-18 08:09:19 -07:00
// or it must be specified in the TxInput.
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
}
2016-04-18 08:09:19 -07:00
if in.PubKey != nil {
acc.PubKey = in.PubKey
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
}
2016-04-18 08:09:19 -07:00
// Validate inputs basic structure
func validateInputsBasic(ins []types.TxInput) (res tmsp.Result) {
for _, in := range ins {
// Check TxInput basic
if res := in.ValidateBasic(); res.IsErr() {
return res
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
2016-04-18 08:09:19 -07:00
func validateInputsAdvanced(accounts map[string]*types.Account, signBytes []byte, ins []types.TxInput) (total types.Coins, res tmsp.Result) {
2016-03-20 03:00:43 -07:00
for _, in := range ins {
acc := accounts[string(in.Address)]
if acc == nil {
2016-04-18 08:09:19 -07:00
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
}
return total, tmsp.OK
2016-03-20 03:00:43 -07:00
}
2016-04-18 08:09:19 -07:00
func validateInputAdvanced(acc *types.Account, signBytes []byte, in types.TxInput) (res tmsp.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 {
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-18 08:09:19 -07:00
func validateOutputsBasic(outs []types.TxOutput) (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-18 08:09:19 -07:00
return res
2016-03-20 03:00:43 -07:00
}
2016-04-18 08:09:19 -07:00
}
return tmsp.OK
}
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 {
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
}
}
}