From ed9dd875a74d5f7b2e91722b405afc3f0771e5cb Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 23 Mar 2016 02:47:05 -0700 Subject: [PATCH] Commit returns tmsp.Result; Move errors to tmsp/errors.go --- app/app.go | 36 +++++++++---------- scripts/print_test_account.go | 20 +++++++++++ state/execution.go | 66 +++++++++++++++++------------------ state/state.go | 16 ++++----- types/errors.go | 22 ------------ types/tx.go | 12 +++---- 6 files changed, 85 insertions(+), 87 deletions(-) create mode 100644 scripts/print_test_account.go delete mode 100644 types/errors.go diff --git a/app/app.go b/app/app.go index 715a70495..60e339561 100644 --- a/app/app.go +++ b/app/app.go @@ -59,58 +59,58 @@ func (app *Basecoin) SetOption(key string, value string) (log string) { // TMSP::AppendTx func (app *Basecoin) AppendTx(txBytes []byte) (res tmsp.Result) { if len(txBytes) > maxTxSize { - return types.ErrEncodingError.AppendLog("Tx size exceeds maximum") + return tmsp.ErrBaseEncodingError.AppendLog("Tx size exceeds maximum") } // Decode tx var tx types.Tx err := wire.ReadBinaryBytes(txBytes, &tx) if err != nil { - return types.ErrEncodingError.AppendLog("Error decoding tx: " + err.Error()) + return tmsp.ErrBaseEncodingError.AppendLog("Error decoding tx: " + err.Error()) } // Validate and exec tx res = state.ExecTx(app.state, tx, false, nil) - if !res.IsOK() { + if res.IsErr() { return res.PrependLog("Error in AppendTx") } - return types.ResultOK + return tmsp.OK } // TMSP::CheckTx func (app *Basecoin) CheckTx(txBytes []byte) (res tmsp.Result) { if len(txBytes) > maxTxSize { - return types.ErrEncodingError.AppendLog("Tx size exceeds maximum") + return tmsp.ErrBaseEncodingError.AppendLog("Tx size exceeds maximum") } // Decode tx var tx types.Tx err := wire.ReadBinaryBytes(txBytes, &tx) if err != nil { - return types.ErrEncodingError.AppendLog("Error decoding tx: " + err.Error()) + return tmsp.ErrBaseEncodingError.AppendLog("Error decoding tx: " + err.Error()) } // Validate tx res = state.ExecTx(app.state, tx, true, nil) - if !res.IsOK() { + if res.IsErr() { return res.PrependLog("Error in CheckTx") } - return types.ResultOK + return tmsp.OK } // TMSP::Query func (app *Basecoin) Query(query []byte) (res tmsp.Result) { - return types.ResultOK - value, err := app.eyesCli.GetSync(query) - if err != nil { - panic("Error making query: " + err.Error()) + return tmsp.OK + res = app.eyesCli.GetSync(query) + if res.IsErr() { + return res.PrependLog("Error querying eyesCli") } - return types.ResultOK.SetData(value).SetLog("Success") + return res } // TMSP::Commit -func (app *Basecoin) Commit() (hash []byte, log string) { - hash, log, err := app.eyesCli.CommitSync() - if err != nil { - panic("Error getting hash: " + err.Error()) +func (app *Basecoin) Commit() (res tmsp.Result) { + res = app.eyesCli.CommitSync() + if res.IsErr() { + panic("Error getting hash: " + res.Error()) } - return hash, "Success" + return res } // TMSP::InitChain diff --git a/scripts/print_test_account.go b/scripts/print_test_account.go new file mode 100644 index 000000000..a2f64ce7d --- /dev/null +++ b/scripts/print_test_account.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "github.com/tendermint/basecoin/tests" + "github.com/tendermint/go-wire" +) + +/* +PrivKey: 019F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A0867D3B5EAF0C0BF6B5A602D359DAECC86A7A74053490EC37AE08E71360587C870 +PubKey: 0167D3B5EAF0C0BF6B5A602D359DAECC86A7A74053490EC37AE08E71360587C870 +Address: D9B727742AA29FA638DC63D70813C976014C4CE0 +*/ +func main() { + tAcc := tests.PrivAccountFromSecret("test") + fmt.Println("PrivKey:", fmt.Sprintf("%X", tAcc.PrivKey.Bytes())) + fmt.Println("PubKey:", fmt.Sprintf("%X", tAcc.Account.PubKey.Bytes())) + fmt.Println("Address:", fmt.Sprintf("%X", tAcc.Account.PubKey.Address())) + fmt.Println(string(wire.JSONBytesPretty(tAcc))) +} diff --git a/state/execution.go b/state/execution.go index 3200b93cd..c8b41a1f5 100644 --- a/state/execution.go +++ b/state/execution.go @@ -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) { diff --git a/state/state.go b/state/state.go index 420322458..9d037a4f1 100644 --- a/state/state.go +++ b/state/state.go @@ -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()) } } diff --git a/types/errors.go b/types/errors.go deleted file mode 100644 index fd34602c9..000000000 --- a/types/errors.go +++ /dev/null @@ -1,22 +0,0 @@ -package types - -import ( - tmsp "github.com/tendermint/tmsp/types" -) - -var ( - ErrInternalError = tmsp.NewError(tmsp.CodeType_InternalError, "Internal error") - ErrDuplicateAddress = tmsp.NewError(tmsp.CodeType_BaseDuplicateAddress, "Error duplicate address") - ErrEncodingError = tmsp.NewError(tmsp.CodeType_BaseEncodingError, "Error encoding error") - ErrInsufficientFees = tmsp.NewError(tmsp.CodeType_BaseInsufficientFees, "Error insufficient fees") - ErrInsufficientFunds = tmsp.NewError(tmsp.CodeType_BaseInsufficientFunds, "Error insufficient funds") - ErrInsufficientGasPrice = tmsp.NewError(tmsp.CodeType_BaseInsufficientGasPrice, "Error insufficient gas price") - ErrInvalidAddress = tmsp.NewError(tmsp.CodeType_BaseInvalidAddress, "Error invalid address") - ErrInvalidAmount = tmsp.NewError(tmsp.CodeType_BaseInvalidAmount, "Error invalid amount") - ErrInvalidPubKey = tmsp.NewError(tmsp.CodeType_BaseInvalidPubKey, "Error invalid pubkey") - ErrInvalidSequence = tmsp.NewError(tmsp.CodeType_BaseInvalidSequence, "Error invalid sequence") - ErrInvalidSignature = tmsp.NewError(tmsp.CodeType_BaseInvalidSignature, "Error invalid signature") - ErrUnknownPubKey = tmsp.NewError(tmsp.CodeType_BaseUnknownPubKey, "Error unknown pubkey") - - ResultOK = tmsp.NewResultOK(nil, "") -) diff --git a/types/tx.go b/types/tx.go index 62438a1d1..ca9b67f22 100644 --- a/types/tx.go +++ b/types/tx.go @@ -48,12 +48,12 @@ type TxInput struct { func (txIn TxInput) ValidateBasic() tmsp.Result { if len(txIn.Address) != 20 { - return ErrInvalidAddress + return tmsp.ErrBaseInvalidAddress } if txIn.Amount == 0 { - return ErrInvalidAmount + return tmsp.ErrBaseInvalidAmount } - return ResultOK + return tmsp.OK } func (txIn TxInput) SignBytes() []byte { @@ -74,12 +74,12 @@ type TxOutput struct { func (txOut TxOutput) ValidateBasic() tmsp.Result { if len(txOut.Address) != 20 { - return ErrInvalidAddress + return tmsp.ErrBaseInvalidAddress } if txOut.Amount == 0 { - return ErrInvalidAmount + return tmsp.ErrBaseInvalidAmount } - return ResultOK + return tmsp.OK } func (txOut TxOutput) SignBytes() []byte {