Commit returns tmsp.Result; Move errors to tmsp/errors.go
This commit is contained in:
+33
-33
@@ -21,28 +21,28 @@ func ExecTx(state *State, tx types.Tx, isCheckTx bool, evc events.Fireable) tmsp
|
||||
case *types.SendTx:
|
||||
// First, get inputs
|
||||
accounts, res := getInputs(state, tx.Inputs)
|
||||
if !res.IsOK() {
|
||||
if res.IsErr() {
|
||||
return res
|
||||
}
|
||||
|
||||
// Then, get or make outputs.
|
||||
accounts, res = getOrMakeOutputs(state, accounts, tx.Outputs)
|
||||
if !res.IsOK() {
|
||||
if res.IsErr() {
|
||||
return res
|
||||
}
|
||||
|
||||
// Validate inputs and outputs
|
||||
signBytes := tx.SignBytes(state.GetChainID())
|
||||
inTotal, res := validateInputs(state, accounts, signBytes, tx.Inputs)
|
||||
if !res.IsOK() {
|
||||
if res.IsErr() {
|
||||
return res
|
||||
}
|
||||
outTotal, res := validateOutputs(tx.Outputs)
|
||||
if !res.IsOK() {
|
||||
if res.IsErr() {
|
||||
return res
|
||||
}
|
||||
if outTotal > inTotal {
|
||||
return types.ErrInsufficientFunds
|
||||
return tmsp.ErrBaseInsufficientFunds
|
||||
}
|
||||
fee := inTotal - outTotal
|
||||
fees += fee
|
||||
@@ -67,38 +67,38 @@ func ExecTx(state *State, tx types.Tx, isCheckTx bool, evc events.Fireable) tmsp
|
||||
}
|
||||
*/
|
||||
|
||||
return types.ResultOK
|
||||
return tmsp.OK
|
||||
|
||||
case *types.CallTx:
|
||||
// First, get input account
|
||||
inAcc := state.GetAccount(tx.Input.Address)
|
||||
if inAcc == nil {
|
||||
log.Info(Fmt("Can't find in account %X", tx.Input.Address))
|
||||
return types.ErrInvalidAddress
|
||||
return tmsp.ErrBaseInvalidAddress
|
||||
}
|
||||
|
||||
// Validate input
|
||||
// pubKey should be present in either "inAcc" or "tx.Input"
|
||||
if res := checkInputPubKey(tx.Input.Address, inAcc, tx.Input); !res.IsOK() {
|
||||
if res := checkInputPubKey(tx.Input.Address, inAcc, tx.Input); res.IsErr() {
|
||||
log.Info(Fmt("Can't find pubkey for %X", tx.Input.Address))
|
||||
return res
|
||||
}
|
||||
signBytes := tx.SignBytes(state.GetChainID())
|
||||
res := validateInput(state, inAcc, signBytes, tx.Input)
|
||||
if !res.IsOK() {
|
||||
if res.IsErr() {
|
||||
log.Info(Fmt("validateInput failed on %X: %v", tx.Input.Address, res))
|
||||
return res
|
||||
}
|
||||
if tx.Input.Amount < tx.Fee {
|
||||
log.Info(Fmt("Sender did not send enough to cover the fee %X", tx.Input.Address))
|
||||
return types.ErrInsufficientFunds
|
||||
return tmsp.ErrBaseInsufficientFunds
|
||||
}
|
||||
|
||||
// Validate call address
|
||||
if strings.HasPrefix(string(tx.Address), "gov/") {
|
||||
// This is a gov call.
|
||||
} else {
|
||||
return types.ErrInvalidAddress.AppendLog(Fmt("Unrecognized address %X", tx.Address))
|
||||
return tmsp.ErrBaseInvalidAddress.AppendLog(Fmt("Unrecognized address %X", tx.Address))
|
||||
}
|
||||
|
||||
// Good!
|
||||
@@ -121,7 +121,7 @@ func ExecTx(state *State, tx types.Tx, isCheckTx bool, evc events.Fireable) tmsp
|
||||
/*
|
||||
if evc != nil {
|
||||
exception := ""
|
||||
if !res.IsOK() {
|
||||
if res.IsErr() {
|
||||
exception = res.Error()
|
||||
}
|
||||
evc.FireEvent(types.EventStringAccInput(tx.Input.Address), types.EventDataTx{tx, ret, exception})
|
||||
@@ -130,10 +130,10 @@ func ExecTx(state *State, tx types.Tx, isCheckTx bool, evc events.Fireable) tmsp
|
||||
*/
|
||||
}
|
||||
|
||||
return types.ResultOK
|
||||
return tmsp.OK
|
||||
|
||||
default:
|
||||
return types.ErrEncodingError.SetLog("Unknown tx type")
|
||||
return tmsp.ErrBaseEncodingError.SetLog("Unknown tx type")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,19 +148,19 @@ func getInputs(state types.AccountGetter, ins []types.TxInput) (map[string]*type
|
||||
for _, in := range ins {
|
||||
// Account shouldn't be duplicated
|
||||
if _, ok := accounts[string(in.Address)]; ok {
|
||||
return nil, types.ErrDuplicateAddress
|
||||
return nil, tmsp.ErrBaseDuplicateAddress
|
||||
}
|
||||
acc := state.GetAccount(in.Address)
|
||||
if acc == nil {
|
||||
return nil, types.ErrInvalidAddress
|
||||
return nil, tmsp.ErrBaseInvalidAddress
|
||||
}
|
||||
// PubKey should be present in either "account" or "in"
|
||||
if res := checkInputPubKey(in.Address, acc, in); !res.IsOK() {
|
||||
if res := checkInputPubKey(in.Address, acc, in); res.IsErr() {
|
||||
return nil, res
|
||||
}
|
||||
accounts[string(in.Address)] = acc
|
||||
}
|
||||
return accounts, types.ResultOK
|
||||
return accounts, tmsp.OK
|
||||
}
|
||||
|
||||
func getOrMakeOutputs(state types.AccountGetter, accounts map[string]*types.Account, outs []types.TxOutput) (map[string]*types.Account, tmsp.Result) {
|
||||
@@ -171,7 +171,7 @@ func getOrMakeOutputs(state types.AccountGetter, accounts map[string]*types.Acco
|
||||
for _, out := range outs {
|
||||
// Account shouldn't be duplicated
|
||||
if _, ok := accounts[string(out.Address)]; ok {
|
||||
return nil, types.ErrDuplicateAddress
|
||||
return nil, tmsp.ErrBaseDuplicateAddress
|
||||
}
|
||||
acc := state.GetAccount(out.Address)
|
||||
// output account may be nil (new)
|
||||
@@ -184,7 +184,7 @@ func getOrMakeOutputs(state types.AccountGetter, accounts map[string]*types.Acco
|
||||
}
|
||||
accounts[string(out.Address)] = acc
|
||||
}
|
||||
return accounts, types.ResultOK
|
||||
return accounts, tmsp.OK
|
||||
}
|
||||
|
||||
// Input must not have a redundant PubKey (i.e. Account already has PubKey).
|
||||
@@ -192,18 +192,18 @@ func getOrMakeOutputs(state types.AccountGetter, accounts map[string]*types.Acco
|
||||
func checkInputPubKey(address []byte, acc *types.Account, in types.TxInput) tmsp.Result {
|
||||
if acc.PubKey == nil {
|
||||
if in.PubKey == nil {
|
||||
return types.ErrUnknownPubKey
|
||||
return tmsp.ErrBaseUnknownPubKey
|
||||
}
|
||||
if !bytes.Equal(in.PubKey.Address(), address) {
|
||||
return types.ErrInvalidPubKey
|
||||
return tmsp.ErrBaseInvalidPubKey
|
||||
}
|
||||
acc.PubKey = in.PubKey
|
||||
} else {
|
||||
if in.PubKey != nil {
|
||||
return types.ErrInvalidPubKey
|
||||
return tmsp.ErrBaseInvalidPubKey
|
||||
}
|
||||
}
|
||||
return types.ResultOK
|
||||
return tmsp.OK
|
||||
}
|
||||
|
||||
// Validate inputs and compute total amount
|
||||
@@ -215,46 +215,46 @@ func validateInputs(state *State, accounts map[string]*types.Account, signBytes
|
||||
PanicSanity("validateInputs() expects account in accounts")
|
||||
}
|
||||
res = validateInput(state, acc, signBytes, in)
|
||||
if !res.IsOK() {
|
||||
if res.IsErr() {
|
||||
return
|
||||
}
|
||||
// Good. Add amount to total
|
||||
total += in.Amount
|
||||
}
|
||||
return total, types.ResultOK
|
||||
return total, tmsp.OK
|
||||
}
|
||||
|
||||
func validateInput(state *State, acc *types.Account, signBytes []byte, in types.TxInput) (res tmsp.Result) {
|
||||
// Check TxInput basic
|
||||
if res := in.ValidateBasic(); !res.IsOK() {
|
||||
if res := in.ValidateBasic(); res.IsErr() {
|
||||
return res
|
||||
}
|
||||
// Check sequence/balance
|
||||
seq, balance := state.GetCheckAccount(in.Address, acc.Sequence, acc.Balance)
|
||||
if seq+1 != in.Sequence {
|
||||
return types.ErrInvalidSequence.AppendLog(Fmt("Got %v, expected %v. (acc.seq=%v)", in.Sequence, seq+1, acc.Sequence))
|
||||
return tmsp.ErrBaseInvalidSequence.AppendLog(Fmt("Got %v, expected %v. (acc.seq=%v)", in.Sequence, seq+1, acc.Sequence))
|
||||
}
|
||||
// Check amount
|
||||
if balance < in.Amount {
|
||||
return types.ErrInsufficientFunds
|
||||
return tmsp.ErrBaseInsufficientFunds
|
||||
}
|
||||
// Check signatures
|
||||
if !acc.PubKey.VerifyBytes(signBytes, in.Signature) {
|
||||
return types.ErrInvalidSignature
|
||||
return tmsp.ErrBaseInvalidSignature
|
||||
}
|
||||
return types.ResultOK
|
||||
return tmsp.OK
|
||||
}
|
||||
|
||||
func validateOutputs(outs []types.TxOutput) (total int64, res tmsp.Result) {
|
||||
for _, out := range outs {
|
||||
// Check TxOutput basic
|
||||
if res := out.ValidateBasic(); !res.IsOK() {
|
||||
if res := out.ValidateBasic(); res.IsErr() {
|
||||
return 0, res
|
||||
}
|
||||
// Good. Add amount to total
|
||||
total += out.Amount
|
||||
}
|
||||
return total, types.ResultOK
|
||||
return total, tmsp.OK
|
||||
}
|
||||
|
||||
func adjustByInputs(state *State, accounts map[string]*types.Account, ins []types.TxInput, isCheckTx bool) {
|
||||
|
||||
+8
-8
@@ -64,15 +64,15 @@ func (s *State) ResetCacheState() {
|
||||
//----------------------------------------
|
||||
|
||||
func (s *State) GetAccount(addr []byte) *types.Account {
|
||||
accBytes, err := s.eyesCli.GetSync(addr)
|
||||
if err != nil {
|
||||
panic("Error loading account: " + err.Error())
|
||||
res := s.eyesCli.GetSync(addr)
|
||||
if res.IsErr() {
|
||||
panic("Error loading account: " + res.Error())
|
||||
}
|
||||
if len(accBytes) == 0 {
|
||||
if len(res.Data) == 0 {
|
||||
return nil
|
||||
}
|
||||
var acc types.Account
|
||||
err = wire.ReadBinaryBytes(accBytes, &acc)
|
||||
err := wire.ReadBinaryBytes(res.Data, &acc)
|
||||
if err != nil {
|
||||
panic("Error reading account: " + err.Error())
|
||||
}
|
||||
@@ -81,8 +81,8 @@ func (s *State) GetAccount(addr []byte) *types.Account {
|
||||
|
||||
func (s *State) SetAccount(acc *types.Account) {
|
||||
accBytes := wire.BinaryBytes(acc)
|
||||
err := s.eyesCli.SetSync(acc.PubKey.Address(), accBytes)
|
||||
if err != nil {
|
||||
panic("Error storing account: " + err.Error())
|
||||
res := s.eyesCli.SetSync(acc.PubKey.Address(), accBytes)
|
||||
if res.IsErr() {
|
||||
panic("Error storing account: " + res.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user