Remove IBC plugin and unneeded code
This commit is contained in:
parent
e95724d8b1
commit
5c813833a0
1196
plugins/ibc/ibc.go
1196
plugins/ibc/ibc.go
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,310 +0,0 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
abci "github.com/tendermint/abci/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
"github.com/tendermint/tmlibs/events"
|
||||
|
||||
"github.com/tendermint/basecoin/plugins/ibc"
|
||||
"github.com/tendermint/basecoin/types"
|
||||
)
|
||||
|
||||
// If the tx is invalid, a TMSP error will be returned.
|
||||
func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc events.Fireable) abci.Result {
|
||||
chainID := state.GetChainID()
|
||||
|
||||
// Exec tx
|
||||
switch tx := tx.(type) {
|
||||
case *types.SendTx:
|
||||
// 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
|
||||
accounts, res := getInputs(state, tx.Inputs)
|
||||
if res.IsErr() {
|
||||
return res.PrependLog("in getInputs()")
|
||||
}
|
||||
|
||||
// Get or make outputs.
|
||||
accounts, res = getOrMakeOutputs(state, accounts, tx.Outputs)
|
||||
if res.IsErr() {
|
||||
return res.PrependLog("in getOrMakeOutputs()")
|
||||
}
|
||||
|
||||
// Validate inputs and outputs, advanced
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
inTotal, res := validateInputsAdvanced(accounts, signBytes, tx.Inputs)
|
||||
if res.IsErr() {
|
||||
return res.PrependLog("in validateInputsAdvanced()")
|
||||
}
|
||||
outTotal := sumOutputs(tx.Outputs)
|
||||
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))
|
||||
}
|
||||
|
||||
// TODO: Fee validation for SendTx
|
||||
|
||||
// Good! Adjust accounts
|
||||
adjustByInputs(state, accounts, tx.Inputs)
|
||||
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 abci.NewResultOK(types.TxID(chainID, tx), "")
|
||||
|
||||
case *types.AppTx:
|
||||
// Validate input, basic
|
||||
res := tx.Input.ValidateBasic()
|
||||
if res.IsErr() {
|
||||
return res
|
||||
}
|
||||
|
||||
// Get input account
|
||||
inAcc := state.GetAccount(tx.Input.Address)
|
||||
if inAcc == nil {
|
||||
return abci.ErrBaseUnknownAddress
|
||||
}
|
||||
if !tx.Input.PubKey.Empty() {
|
||||
inAcc.PubKey = tx.Input.PubKey
|
||||
}
|
||||
|
||||
// Validate input, advanced
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
res = validateInputAdvanced(inAcc, signBytes, tx.Input)
|
||||
if res.IsErr() {
|
||||
state.logger.Info(cmn.Fmt("validateInputAdvanced failed on %X: %v", tx.Input.Address, res))
|
||||
return res.PrependLog("in validateInputAdvanced()")
|
||||
}
|
||||
if !tx.Input.Coins.IsGTE(types.Coins{tx.Fee}) {
|
||||
state.logger.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}))
|
||||
}
|
||||
|
||||
// Validate call address
|
||||
plugin := pgz.GetByName(tx.Name)
|
||||
if plugin == nil {
|
||||
return abci.ErrBaseUnknownAddress.AppendLog(
|
||||
cmn.Fmt("Unrecognized plugin name%v", tx.Name))
|
||||
}
|
||||
|
||||
// Good!
|
||||
coins := tx.Input.Coins.Minus(types.Coins{tx.Fee})
|
||||
inAcc.Sequence += 1
|
||||
inAcc.Balance = inAcc.Balance.Minus(tx.Input.Coins)
|
||||
|
||||
// If this is a CheckTx, stop now.
|
||||
if isCheckTx {
|
||||
state.SetAccount(tx.Input.Address, inAcc)
|
||||
return abci.OK
|
||||
}
|
||||
|
||||
// Create inAcc checkpoint
|
||||
inAccCopy := inAcc.Copy()
|
||||
|
||||
// Run the tx.
|
||||
cache := state.CacheWrap()
|
||||
cache.SetAccount(tx.Input.Address, inAcc)
|
||||
ctx := types.NewCallContext(tx.Input.Address, inAcc, coins)
|
||||
res = plugin.RunTx(cache, ctx, tx.Data)
|
||||
if res.IsOK() {
|
||||
cache.CacheSync()
|
||||
state.logger.Info("Successful execution")
|
||||
// Fire events
|
||||
/*
|
||||
if evc != nil {
|
||||
exception := ""
|
||||
if res.IsErr() {
|
||||
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})
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
state.logger.Info("AppTx failed", "error", res)
|
||||
// Just return the coins and return.
|
||||
inAccCopy.Balance = inAccCopy.Balance.Plus(coins)
|
||||
// But take the gas
|
||||
// TODO
|
||||
state.SetAccount(tx.Input.Address, inAccCopy)
|
||||
}
|
||||
return res
|
||||
|
||||
default:
|
||||
return abci.ErrBaseEncodingError.SetLog("Unknown tx type")
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
// 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.
|
||||
func getInputs(state types.AccountGetter, ins []types.TxInput) (map[string]*types.Account, abci.Result) {
|
||||
accounts := map[string]*types.Account{}
|
||||
for _, in := range ins {
|
||||
// Account shouldn't be duplicated
|
||||
if _, ok := accounts[string(in.Address)]; ok {
|
||||
return nil, abci.ErrBaseDuplicateAddress
|
||||
}
|
||||
|
||||
acc := state.GetAccount(in.Address)
|
||||
if acc == nil {
|
||||
return nil, abci.ErrBaseUnknownAddress
|
||||
}
|
||||
|
||||
if !in.PubKey.Empty() {
|
||||
acc.PubKey = in.PubKey
|
||||
}
|
||||
accounts[string(in.Address)] = acc
|
||||
}
|
||||
return accounts, abci.OK
|
||||
}
|
||||
|
||||
func getOrMakeOutputs(state types.AccountGetter, accounts map[string]*types.Account, outs []types.TxOutput) (map[string]*types.Account, abci.Result) {
|
||||
if accounts == nil {
|
||||
accounts = make(map[string]*types.Account)
|
||||
}
|
||||
|
||||
for _, out := range outs {
|
||||
chain, outAddress, _ := out.ChainAndAddress() // already validated
|
||||
if chain != nil {
|
||||
// we dont need an account for the other chain.
|
||||
// we'll just create an outgoing ibc packet
|
||||
continue
|
||||
}
|
||||
// Account shouldn't be duplicated
|
||||
if _, ok := accounts[string(outAddress)]; ok {
|
||||
return nil, abci.ErrBaseDuplicateAddress
|
||||
}
|
||||
acc := state.GetAccount(outAddress)
|
||||
// output account may be nil (new)
|
||||
if acc == nil {
|
||||
// zero value is valid, empty account
|
||||
acc = &types.Account{}
|
||||
}
|
||||
accounts[string(outAddress)] = acc
|
||||
}
|
||||
return accounts, abci.OK
|
||||
}
|
||||
|
||||
// Validate inputs basic structure
|
||||
func validateInputsBasic(ins []types.TxInput) (res abci.Result) {
|
||||
for _, in := range ins {
|
||||
// Check TxInput basic
|
||||
if res := in.ValidateBasic(); res.IsErr() {
|
||||
return res
|
||||
}
|
||||
}
|
||||
return abci.OK
|
||||
}
|
||||
|
||||
// Validate inputs and compute total amount of coins
|
||||
func validateInputsAdvanced(accounts map[string]*types.Account, signBytes []byte, ins []types.TxInput) (total types.Coins, res abci.Result) {
|
||||
for _, in := range ins {
|
||||
acc := accounts[string(in.Address)]
|
||||
if acc == nil {
|
||||
cmn.PanicSanity("validateInputsAdvanced() expects account in accounts")
|
||||
}
|
||||
res = validateInputAdvanced(acc, signBytes, in)
|
||||
if res.IsErr() {
|
||||
return
|
||||
}
|
||||
// Good. Add amount to total
|
||||
total = total.Plus(in.Coins)
|
||||
}
|
||||
return total, abci.OK
|
||||
}
|
||||
|
||||
func validateInputAdvanced(acc *types.Account, signBytes []byte, in types.TxInput) (res abci.Result) {
|
||||
// Check sequence/coins
|
||||
seq, balance := acc.Sequence, acc.Balance
|
||||
if seq+1 != in.Sequence {
|
||||
return abci.ErrBaseInvalidSequence.AppendLog(cmn.Fmt("Got %v, expected %v. (acc.seq=%v)", in.Sequence, seq+1, acc.Sequence))
|
||||
}
|
||||
// Check amount
|
||||
if !balance.IsGTE(in.Coins) {
|
||||
return abci.ErrBaseInsufficientFunds.AppendLog(cmn.Fmt("balance is %v, tried to send %v", balance, in.Coins))
|
||||
}
|
||||
// Check signatures
|
||||
if !acc.PubKey.VerifyBytes(signBytes, in.Signature) {
|
||||
return abci.ErrBaseInvalidSignature.AppendLog(cmn.Fmt("SignBytes: %X", signBytes))
|
||||
}
|
||||
return abci.OK
|
||||
}
|
||||
|
||||
func validateOutputsBasic(outs []types.TxOutput) (res abci.Result) {
|
||||
for _, out := range outs {
|
||||
// Check TxOutput basic
|
||||
if res := out.ValidateBasic(); res.IsErr() {
|
||||
return res
|
||||
}
|
||||
}
|
||||
return abci.OK
|
||||
}
|
||||
|
||||
func sumOutputs(outs []types.TxOutput) (total types.Coins) {
|
||||
for _, out := range outs {
|
||||
total = total.Plus(out.Coins)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func adjustByInputs(state types.AccountSetter, accounts map[string]*types.Account, ins []types.TxInput) {
|
||||
for _, in := range ins {
|
||||
acc := accounts[string(in.Address)]
|
||||
if acc == nil {
|
||||
cmn.PanicSanity("adjustByInputs() expects account in accounts")
|
||||
}
|
||||
if !acc.Balance.IsGTE(in.Coins) {
|
||||
cmn.PanicSanity("adjustByInputs() expects sufficient funds")
|
||||
}
|
||||
acc.Balance = acc.Balance.Minus(in.Coins)
|
||||
acc.Sequence += 1
|
||||
state.SetAccount(in.Address, acc)
|
||||
}
|
||||
}
|
||||
|
||||
func adjustByOutputs(state *State, accounts map[string]*types.Account, outs []types.TxOutput, isCheckTx bool) {
|
||||
for _, out := range outs {
|
||||
destChain, outAddress, _ := out.ChainAndAddress() // already validated
|
||||
if destChain != nil {
|
||||
payload := ibc.CoinsPayload{outAddress, out.Coins}
|
||||
ibc.SaveNewIBCPacket(state, state.GetChainID(), string(destChain), payload)
|
||||
continue
|
||||
}
|
||||
|
||||
acc := accounts[string(outAddress)]
|
||||
if acc == nil {
|
||||
cmn.PanicSanity("adjustByOutputs() expects account in accounts")
|
||||
}
|
||||
acc.Balance = acc.Balance.Plus(out.Coins)
|
||||
if !isCheckTx {
|
||||
state.SetAccount(outAddress, acc)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,365 +0,0 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
|
||||
"github.com/tendermint/basecoin/plugins/ibc"
|
||||
"github.com/tendermint/basecoin/types"
|
||||
)
|
||||
|
||||
//--------------------------------------------------------
|
||||
// test environment is a bunch of lists of accountns
|
||||
|
||||
type execTest struct {
|
||||
chainID string
|
||||
store types.KVStore
|
||||
state *State
|
||||
accIn types.PrivAccount
|
||||
accOut types.PrivAccount
|
||||
}
|
||||
|
||||
func newExecTest() *execTest {
|
||||
et := &execTest{
|
||||
chainID: "test_chain_id",
|
||||
}
|
||||
et.reset()
|
||||
return et
|
||||
}
|
||||
|
||||
func (et *execTest) signTx(tx *types.SendTx, accsIn ...types.PrivAccount) {
|
||||
types.SignTx(et.chainID, tx, accsIn...)
|
||||
}
|
||||
|
||||
// returns the final balance and expected balance for input and output accounts
|
||||
func (et *execTest) exec(tx *types.SendTx, checkTx bool) (res abci.Result, inGot, inExp, outGot, outExp types.Coins) {
|
||||
initBalIn := et.state.GetAccount(et.accIn.Account.PubKey.Address()).Balance
|
||||
initBalOut := et.state.GetAccount(et.accOut.Account.PubKey.Address()).Balance
|
||||
|
||||
res = ExecTx(et.state, nil, tx, checkTx, nil)
|
||||
|
||||
endBalIn := et.state.GetAccount(et.accIn.Account.PubKey.Address()).Balance
|
||||
endBalOut := et.state.GetAccount(et.accOut.Account.PubKey.Address()).Balance
|
||||
decrBalInExp := tx.Outputs[0].Coins.Plus(types.Coins{tx.Fee}) //expected decrease in balance In
|
||||
return res, endBalIn, initBalIn.Minus(decrBalInExp), endBalOut, initBalOut.Plus(tx.Outputs[0].Coins)
|
||||
}
|
||||
|
||||
func (et *execTest) acc2State(accs ...types.PrivAccount) {
|
||||
for _, acc := range accs {
|
||||
et.state.SetAccount(acc.Account.PubKey.Address(), &acc.Account)
|
||||
}
|
||||
}
|
||||
|
||||
//reset everything. state is empty
|
||||
func (et *execTest) reset() {
|
||||
et.accIn = types.MakeAcc("foo")
|
||||
et.accOut = types.MakeAcc("bar")
|
||||
|
||||
et.store = types.NewMemKVStore()
|
||||
et.state = NewState(et.store, log.TestingLogger())
|
||||
et.state.SetChainID(et.chainID)
|
||||
|
||||
// NOTE we dont run acc2State here
|
||||
// so we can test non-existing accounts
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------
|
||||
|
||||
func TestGetInputs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//nil submissions
|
||||
acc, res := getInputs(nil, nil)
|
||||
assert.True(res.IsOK(), "getInputs: error on nil submission")
|
||||
assert.Zero(len(acc), "getInputs: accounts returned on nil submission")
|
||||
|
||||
//test getInputs for registered, non-registered account
|
||||
et.reset()
|
||||
inputs := types.Accs2TxInputs(1, et.accIn)
|
||||
acc, res = getInputs(et.state, inputs)
|
||||
assert.True(res.IsErr(), "getInputs: expected error when using getInput with non-registered Input")
|
||||
|
||||
et.acc2State(et.accIn)
|
||||
acc, res = getInputs(et.state, inputs)
|
||||
assert.True(res.IsOK(), "getInputs: expected to getInput from registered Input")
|
||||
|
||||
//test sending duplicate accounts
|
||||
et.reset()
|
||||
et.acc2State(et.accIn, et.accIn, et.accIn)
|
||||
inputs = types.Accs2TxInputs(1, et.accIn, et.accIn, et.accIn)
|
||||
acc, res = getInputs(et.state, inputs)
|
||||
assert.True(res.IsErr(), "getInputs: expected error when sending duplicate accounts")
|
||||
}
|
||||
|
||||
func TestGetOrMakeOutputs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//nil submissions
|
||||
acc, res := getOrMakeOutputs(nil, nil, nil)
|
||||
assert.True(res.IsOK(), "getOrMakeOutputs: error on nil submission")
|
||||
assert.Zero(len(acc), "getOrMakeOutputs: accounts returned on nil submission")
|
||||
|
||||
//test sending duplicate accounts
|
||||
et.reset()
|
||||
outputs := types.Accs2TxOutputs(et.accIn, et.accIn, et.accIn)
|
||||
_, res = getOrMakeOutputs(et.state, nil, outputs)
|
||||
assert.True(res.IsErr(), "getOrMakeOutputs: expected error when sending duplicate accounts")
|
||||
|
||||
//test sending to existing/new account
|
||||
et.reset()
|
||||
outputs1 := types.Accs2TxOutputs(et.accIn)
|
||||
outputs2 := types.Accs2TxOutputs(et.accOut)
|
||||
|
||||
et.acc2State(et.accIn)
|
||||
_, res = getOrMakeOutputs(et.state, nil, outputs1)
|
||||
assert.True(res.IsOK(), "getOrMakeOutputs: error when sending to existing account")
|
||||
|
||||
mapRes2, res := getOrMakeOutputs(et.state, nil, outputs2)
|
||||
assert.True(res.IsOK(), "getOrMakeOutputs: error when sending to new account")
|
||||
|
||||
//test the map results
|
||||
_, map2ok := mapRes2[string(outputs2[0].Address)]
|
||||
assert.True(map2ok, "getOrMakeOutputs: account output does not contain new account map item")
|
||||
|
||||
}
|
||||
|
||||
func TestValidateInputsBasic(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//validate input basic
|
||||
inputs := types.Accs2TxInputs(1, et.accIn)
|
||||
res := validateInputsBasic(inputs)
|
||||
assert.True(res.IsOK(), "validateInputsBasic: expected no error on good tx input. Error: %v", res.Error())
|
||||
|
||||
inputs[0].Coins[0].Amount = 0
|
||||
res = validateInputsBasic(inputs)
|
||||
assert.True(res.IsErr(), "validateInputsBasic: expected error on bad tx input")
|
||||
|
||||
}
|
||||
|
||||
func TestValidateInputsAdvanced(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//create three temp accounts for the test
|
||||
accIn1 := types.MakeAcc("foox")
|
||||
accIn2 := types.MakeAcc("fooy")
|
||||
accIn3 := types.MakeAcc("fooz")
|
||||
|
||||
//validate inputs advanced
|
||||
tx := types.MakeSendTx(1, et.accOut, accIn1, accIn2, accIn3)
|
||||
|
||||
et.acc2State(accIn1, accIn2, accIn3, et.accOut)
|
||||
accMap, res := getInputs(et.state, tx.Inputs)
|
||||
assert.True(res.IsOK(), "validateInputsAdvanced: error retrieving accMap. Error: %v", res.Error())
|
||||
signBytes := tx.SignBytes(et.chainID)
|
||||
|
||||
//test bad case, unsigned
|
||||
totalCoins, res := validateInputsAdvanced(accMap, signBytes, tx.Inputs)
|
||||
assert.True(res.IsErr(), "validateInputsAdvanced: expected an error on an unsigned tx input")
|
||||
|
||||
//test good case sgined
|
||||
et.signTx(tx, accIn1, accIn2, accIn3, et.accOut)
|
||||
totalCoins, res = validateInputsAdvanced(accMap, signBytes, tx.Inputs)
|
||||
assert.True(res.IsOK(), "validateInputsAdvanced: expected no error on good tx input. Error: %v", res.Error())
|
||||
|
||||
txTotalCoins := tx.Inputs[0].Coins.
|
||||
Plus(tx.Inputs[1].Coins).
|
||||
Plus(tx.Inputs[2].Coins)
|
||||
|
||||
assert.True(totalCoins.IsEqual(txTotalCoins),
|
||||
"ValidateInputsAdvanced: transaction total coins are not equal: got %v, expected %v", txTotalCoins, totalCoins)
|
||||
}
|
||||
|
||||
func TestValidateInputAdvanced(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//validate input advanced
|
||||
tx := types.MakeSendTx(1, et.accOut, et.accIn)
|
||||
|
||||
et.acc2State(et.accIn, et.accOut)
|
||||
signBytes := tx.SignBytes(et.chainID)
|
||||
|
||||
//unsigned case
|
||||
res := validateInputAdvanced(&et.accIn.Account, signBytes, tx.Inputs[0])
|
||||
assert.True(res.IsErr(), "validateInputAdvanced: expected error on tx input without signature")
|
||||
|
||||
//good signed case
|
||||
et.signTx(tx, et.accIn, et.accOut)
|
||||
res = validateInputAdvanced(&et.accIn.Account, signBytes, tx.Inputs[0])
|
||||
assert.True(res.IsOK(), "validateInputAdvanced: expected no error on good tx input. Error: %v", res.Error())
|
||||
|
||||
//bad sequence case
|
||||
et.accIn.Sequence = 1
|
||||
et.signTx(tx, et.accIn, et.accOut)
|
||||
res = validateInputAdvanced(&et.accIn.Account, signBytes, tx.Inputs[0])
|
||||
assert.Equal(abci.CodeType_BaseInvalidSequence, res.Code, "validateInputAdvanced: expected error on tx input with bad sequence")
|
||||
et.accIn.Sequence = 0 //restore sequence
|
||||
|
||||
//bad balance case
|
||||
et.accIn.Balance = types.Coins{{"mycoin", 2}}
|
||||
et.signTx(tx, et.accIn, et.accOut)
|
||||
res = validateInputAdvanced(&et.accIn.Account, signBytes, tx.Inputs[0])
|
||||
assert.Equal(abci.CodeType_BaseInsufficientFunds, res.Code,
|
||||
"validateInputAdvanced: expected error on tx input with insufficient funds %v", et.accIn.Sequence)
|
||||
}
|
||||
|
||||
func TestValidateOutputsAdvanced(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//validateOutputsBasic
|
||||
tx := types.Accs2TxOutputs(et.accIn)
|
||||
res := validateOutputsBasic(tx)
|
||||
assert.True(res.IsOK(), "validateOutputsBasic: expected no error on good tx output. Error: %v", res.Error())
|
||||
|
||||
tx[0].Coins[0].Amount = 0
|
||||
res = validateOutputsBasic(tx)
|
||||
assert.True(res.IsErr(), "validateInputBasic: expected error on bad tx output. Error: %v", res.Error())
|
||||
}
|
||||
|
||||
func TestSumOutput(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//SumOutput
|
||||
tx := types.Accs2TxOutputs(et.accIn, et.accOut)
|
||||
total := sumOutputs(tx)
|
||||
assert.True(total.IsEqual(tx[0].Coins.Plus(tx[1].Coins)), "sumOutputs: total coins are not equal")
|
||||
}
|
||||
|
||||
func TestAdjustBy(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//adjustByInputs/adjustByOutputs
|
||||
//sending transaction from accIn to accOut
|
||||
initBalIn := et.accIn.Account.Balance
|
||||
initBalOut := et.accOut.Account.Balance
|
||||
et.acc2State(et.accIn, et.accOut)
|
||||
|
||||
txIn := types.Accs2TxInputs(1, et.accIn)
|
||||
txOut := types.Accs2TxOutputs(et.accOut)
|
||||
accMap, _ := getInputs(et.state, txIn)
|
||||
accMap, _ = getOrMakeOutputs(et.state, accMap, txOut)
|
||||
|
||||
adjustByInputs(et.state, accMap, txIn)
|
||||
adjustByOutputs(et.state, accMap, txOut, false)
|
||||
|
||||
endBalIn := accMap[string(et.accIn.Account.PubKey.Address())].Balance
|
||||
endBalOut := accMap[string(et.accOut.Account.PubKey.Address())].Balance
|
||||
decrBalIn := initBalIn.Minus(endBalIn)
|
||||
incrBalOut := endBalOut.Minus(initBalOut)
|
||||
|
||||
assert.True(decrBalIn.IsEqual(txIn[0].Coins),
|
||||
"adjustByInputs: total coins are not equal. diff: %v, tx: %v", decrBalIn.String(), txIn[0].Coins.String())
|
||||
assert.True(incrBalOut.IsEqual(txOut[0].Coins),
|
||||
"adjustByInputs: total coins are not equal. diff: %v, tx: %v", incrBalOut.String(), txOut[0].Coins.String())
|
||||
|
||||
}
|
||||
|
||||
func TestSendTx(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//ExecTx
|
||||
tx := types.MakeSendTx(1, et.accOut, et.accIn)
|
||||
et.acc2State(et.accIn)
|
||||
et.acc2State(et.accOut)
|
||||
et.signTx(tx, et.accIn)
|
||||
|
||||
//Bad Balance
|
||||
et.accIn.Balance = types.Coins{{"mycoin", 2}}
|
||||
et.acc2State(et.accIn)
|
||||
res, _, _, _, _ := et.exec(tx, true)
|
||||
assert.True(res.IsErr(), "ExecTx/Bad CheckTx: Expected error return from ExecTx, returned: %v", res)
|
||||
|
||||
res, balIn, balInExp, balOut, balOutExp := et.exec(tx, false)
|
||||
assert.True(res.IsErr(), "ExecTx/Bad DeliverTx: Expected error return from ExecTx, returned: %v", res)
|
||||
assert.False(balIn.IsEqual(balInExp),
|
||||
"ExecTx/Bad DeliverTx: balance shouldn't be equal for accIn: got %v, expected: %v", balIn, balInExp)
|
||||
assert.False(balOut.IsEqual(balOutExp),
|
||||
"ExecTx/Bad DeliverTx: balance shouldn't be equal for accOut: got %v, expected: %v", balOut, balOutExp)
|
||||
|
||||
//Regular CheckTx
|
||||
et.reset()
|
||||
et.acc2State(et.accIn)
|
||||
et.acc2State(et.accOut)
|
||||
res, _, _, _, _ = et.exec(tx, true)
|
||||
assert.True(res.IsOK(), "ExecTx/Good CheckTx: Expected OK return from ExecTx, Error: %v", res)
|
||||
|
||||
// rige: go hack the cli and abuse this to steal some coinz.... I'm not fixing, but re-writing SendTx
|
||||
//Negative CheckTx
|
||||
// et.reset()
|
||||
// et.acc2State(et.accIn)
|
||||
// et.acc2State(et.accOut)
|
||||
// tx2 := types.MakeSendTx(1, et.accOut, et.accIn)
|
||||
// tx2.Fee = types.Coin{"mycoin", 0}
|
||||
// tx2.Inputs[0].Coins = types.Coins{{"mycoin", -5}}
|
||||
// tx2.Outputs[0].Coins = types.Coins{{"mycoin", -5}}
|
||||
// et.signTx(tx2, et.accIn)
|
||||
// res, _, _, _, _ = et.exec(tx2, true)
|
||||
// assert.True(res.IsErr(), "ExecTx/Bad CheckTx: Expected error return from ExecTx, returned: %v", res)
|
||||
|
||||
//Regular DeliverTx
|
||||
et.reset()
|
||||
et.acc2State(et.accIn)
|
||||
et.acc2State(et.accOut)
|
||||
res, balIn, balInExp, balOut, balOutExp = et.exec(tx, false)
|
||||
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
|
||||
assert.True(balIn.IsEqual(balInExp),
|
||||
"ExecTx/good DeliverTx: unexpected change in input balance, got: %v, expected: %v", balIn, balInExp)
|
||||
assert.True(balOut.IsEqual(balOutExp),
|
||||
"ExecTx/good DeliverTx: unexpected change in output balance, got: %v, expected: %v", balOut, balOutExp)
|
||||
}
|
||||
|
||||
func TestSendTxIBC(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
et := newExecTest()
|
||||
|
||||
//ExecTx
|
||||
chainID2 := "otherchain"
|
||||
tx := types.MakeSendTx(1, et.accOut, et.accIn)
|
||||
dstAddress := tx.Outputs[0].Address
|
||||
tx.Outputs[0].Address = []byte(chainID2 + "/" + string(tx.Outputs[0].Address))
|
||||
et.acc2State(et.accIn)
|
||||
et.signTx(tx, et.accIn)
|
||||
|
||||
//Regular DeliverTx
|
||||
et.reset()
|
||||
et.acc2State(et.accIn)
|
||||
|
||||
initBalIn := et.state.GetAccount(et.accIn.Account.PubKey.Address()).Balance
|
||||
|
||||
res := ExecTx(et.state, nil, tx, false, nil)
|
||||
|
||||
balIn := et.state.GetAccount(et.accIn.Account.PubKey.Address()).Balance
|
||||
decrBalInExp := tx.Outputs[0].Coins.Plus(types.Coins{tx.Fee}) //expected decrease in balance In
|
||||
balInExp := initBalIn.Minus(decrBalInExp)
|
||||
|
||||
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
|
||||
assert.True(balIn.IsEqual(balInExp),
|
||||
"ExecTx/good DeliverTx: unexpected change in input balance, got: %v, expected: %v", balIn, balInExp)
|
||||
|
||||
packet, err := ibc.GetIBCPacket(et.state, et.chainID, chainID2, 0)
|
||||
assert.Nil(err)
|
||||
|
||||
assert.Equal(packet.SrcChainID, et.chainID)
|
||||
assert.Equal(packet.DstChainID, chainID2)
|
||||
assert.Equal(packet.Sequence, uint64(0))
|
||||
assert.Equal(packet.Type, "coin")
|
||||
|
||||
coins, ok := packet.Payload.(ibc.CoinsPayload)
|
||||
assert.True(ok)
|
||||
assert.Equal(coins.Coins, tx.Outputs[0].Coins)
|
||||
assert.EqualValues(coins.Address, dstAddress)
|
||||
}
|
||||
@ -57,14 +57,6 @@ func (s *State) Set(key []byte, value []byte) {
|
||||
s.store.Set(key, value)
|
||||
}
|
||||
|
||||
func (s *State) GetAccount(addr []byte) *types.Account {
|
||||
return types.GetAccount(s, addr)
|
||||
}
|
||||
|
||||
func (s *State) SetAccount(addr []byte, acc *types.Account) {
|
||||
types.SetAccount(s, addr, acc)
|
||||
}
|
||||
|
||||
func (s *State) CacheWrap() *State {
|
||||
cache := types.NewKVCache(s)
|
||||
return &State{
|
||||
|
||||
@ -20,9 +20,6 @@ func TestState(t *testing.T) {
|
||||
cache := state.CacheWrap()
|
||||
eyesCli := eyes.NewLocalClient("", 0)
|
||||
|
||||
//Account and address for tests
|
||||
dumAddr := []byte("dummyAddress")
|
||||
|
||||
acc := new(types.Account)
|
||||
acc.Sequence = 1
|
||||
|
||||
@ -73,10 +70,6 @@ func TestState(t *testing.T) {
|
||||
setRecords(state)
|
||||
assert.True(storeHasAll(state), "state doesn't retrieve after Set")
|
||||
|
||||
// Test account retrieve
|
||||
state.SetAccount(dumAddr, acc)
|
||||
assert.Equal(state.GetAccount(dumAddr).Sequence, 1, "GetAccount not retrieving")
|
||||
|
||||
//Test CacheWrap with local mem store
|
||||
reset()
|
||||
setRecords(cache)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user