Browse Source

remove gas price and dont run txs before they come in a block

pull/32/head
Ethan Buchman 10 years ago
committed by Jae Kwon
parent
commit
9c4692c071
5 changed files with 15 additions and 37 deletions
  1. +0
    -4
      block/block.go
  2. +2
    -2
      block/tx.go
  3. +2
    -2
      mempool/mempool.go
  4. +8
    -26
      state/state.go
  5. +3
    -3
      state/state_test.go

+ 0
- 4
block/block.go View File

@ -107,16 +107,12 @@ type Header struct {
Height uint Height uint
Time time.Time Time time.Time
Fees uint64 Fees uint64
GasPrice uint64
NumTxs uint NumTxs uint
LastBlockHash []byte LastBlockHash []byte
LastBlockParts PartSetHeader LastBlockParts PartSetHeader
StateHash []byte StateHash []byte
} }
// possible for actual gas price to be less than 1
var GasPriceDivisor = 1000000
func (h *Header) Hash() []byte { func (h *Header) Hash() []byte {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
hasher, n, err := sha256.New(), new(int64), new(error) hasher, n, err := sha256.New(), new(int64), new(error)


+ 2
- 2
block/tx.go View File

@ -147,7 +147,7 @@ type CallTx struct {
Input *TxInput Input *TxInput
Address []byte Address []byte
GasLimit uint64 GasLimit uint64
FeeLimit uint64
Fee uint64
Data []byte Data []byte
} }
@ -157,7 +157,7 @@ func (tx *CallTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
tx.Input.WriteSignBytes(w, n, err) tx.Input.WriteSignBytes(w, n, err)
binary.WriteByteSlice(tx.Address, w, n, err) binary.WriteByteSlice(tx.Address, w, n, err)
binary.WriteUint64(tx.GasLimit, w, n, err) binary.WriteUint64(tx.GasLimit, w, n, err)
binary.WriteUint64(tx.FeeLimit, w, n, err)
binary.WriteUint64(tx.Fee, w, n, err)
binary.WriteByteSlice(tx.Data, w, n, err) binary.WriteByteSlice(tx.Data, w, n, err)
} }


+ 2
- 2
mempool/mempool.go View File

@ -32,7 +32,7 @@ func NewMempool(state *sm.State) *Mempool {
func (mem *Mempool) AddTx(tx blk.Tx) (err error) { func (mem *Mempool) AddTx(tx blk.Tx) (err error) {
mem.mtx.Lock() mem.mtx.Lock()
defer mem.mtx.Unlock() defer mem.mtx.Unlock()
err = mem.state.ExecTx(tx)
err = mem.state.ExecTx(tx, false)
if err != nil { if err != nil {
log.Debug("AddTx() error", "tx", tx, "error", err) log.Debug("AddTx() error", "tx", tx, "error", err)
return err return err
@ -82,7 +82,7 @@ func (mem *Mempool) ResetForBlockAndState(block *blk.Block, state *sm.State) {
// Next, filter all txs that aren't valid given new state. // Next, filter all txs that aren't valid given new state.
validTxs := []blk.Tx{} validTxs := []blk.Tx{}
for _, tx := range txs { for _, tx := range txs {
err := mem.state.ExecTx(tx)
err := mem.state.ExecTx(tx, false)
if err == nil { if err == nil {
log.Debug("Filter in, valid", "tx", tx) log.Debug("Filter in, valid", "tx", tx)
validTxs = append(validTxs, tx) validTxs = append(validTxs, tx)


+ 8
- 26
state/state.go View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"math/big"
"time" "time"
"github.com/tendermint/tendermint/account" "github.com/tendermint/tendermint/account"
@ -235,15 +234,14 @@ func (s *State) AdjustByOutputs(accounts map[string]*account.Account, outs []*bl
// If the tx is invalid, an error will be returned. // If the tx is invalid, an error will be returned.
// Unlike AppendBlock(), state will not be altered. // Unlike AppendBlock(), state will not be altered.
func (s *State) ExecTx(tx_ blk.Tx, blk_ *blk.Block) error {
func (s *State) ExecTx(tx_ blk.Tx, runCall bool) error {
// TODO: do something with fees // TODO: do something with fees
fees := uint64(0) fees := uint64(0)
// Exec tx // Exec tx
switch tx_.(type) {
switch tx := tx_.(type) {
case *blk.SendTx: case *blk.SendTx:
tx := tx_.(*blk.SendTx)
accounts, err := s.GetOrMakeAccounts(tx.Inputs, tx.Outputs) accounts, err := s.GetOrMakeAccounts(tx.Inputs, tx.Outputs)
if err != nil { if err != nil {
return err return err
@ -270,7 +268,6 @@ func (s *State) ExecTx(tx_ blk.Tx, blk_ *blk.Block) error {
return nil return nil
case *blk.CallTx: case *blk.CallTx:
tx := tx_.(*blk.CallTx)
accounts := map[string]*account.Account{} accounts := map[string]*account.Account{}
inAcc := s.GetAccount(tx.Input.Address) inAcc := s.GetAccount(tx.Input.Address)
if inAcc == nil { if inAcc == nil {
@ -297,29 +294,20 @@ func (s *State) ExecTx(tx_ blk.Tx, blk_ *blk.Block) error {
} }
accounts[string(tx.Address)] = outAcc accounts[string(tx.Address)] = outAcc
// ensure sufficient gas price
// we multiply to avoid dividing
bigProvidedFee := new(big.Int).Mul(big.NewInt(int64(tx.FeeLimit)), big.NewInt(int64(blk.GasPriceDivisor)))
bigMinFee := new(big.Int).Mul(big.NewInt(int64(blk_.GasPrice)), big.NewInt(int64(tx.GasLimit)))
if bigProvidedFee.Cmp(bigMinFee) < 0 {
return blk.ErrTxInsufficientGasPrice
}
inTotal -= tx.FeeLimit
inTotal -= tx.Fee
// Good! Adjust accounts // Good! Adjust accounts
s.AdjustByInputs(accounts, []*blk.TxInput{tx.Input}) s.AdjustByInputs(accounts, []*blk.TxInput{tx.Input})
outAcc.Balance += inTotal outAcc.Balance += inTotal
s.UpdateAccounts(accounts) s.UpdateAccounts(accounts)
// TODO: Run the contract call!
// TODO: refund some gas
if runCall {
// TODO: Run the contract call!
// TODO: refund some gas
}
return nil return nil
case *blk.BondTx: case *blk.BondTx:
tx := tx_.(*blk.BondTx)
valInfo := s.GetValidatorInfo(tx.PubKey.Address()) valInfo := s.GetValidatorInfo(tx.PubKey.Address())
if valInfo != nil { if valInfo != nil {
// TODO: In the future, check that the validator wasn't destroyed, // TODO: In the future, check that the validator wasn't destroyed,
@ -373,8 +361,6 @@ func (s *State) ExecTx(tx_ blk.Tx, blk_ *blk.Block) error {
return nil return nil
case *blk.UnbondTx: case *blk.UnbondTx:
tx := tx_.(*blk.UnbondTx)
// The validator must be active // The validator must be active
_, val := s.BondedValidators.GetByAddress(tx.Address) _, val := s.BondedValidators.GetByAddress(tx.Address)
if val == nil { if val == nil {
@ -397,8 +383,6 @@ func (s *State) ExecTx(tx_ blk.Tx, blk_ *blk.Block) error {
return nil return nil
case *blk.RebondTx: case *blk.RebondTx:
tx := tx_.(*blk.RebondTx)
// The validator must be inactive // The validator must be inactive
_, val := s.UnbondingValidators.GetByAddress(tx.Address) _, val := s.UnbondingValidators.GetByAddress(tx.Address)
if val == nil { if val == nil {
@ -421,8 +405,6 @@ func (s *State) ExecTx(tx_ blk.Tx, blk_ *blk.Block) error {
return nil return nil
case *blk.DupeoutTx: case *blk.DupeoutTx:
tx := tx_.(*blk.DupeoutTx)
// Verify the signatures // Verify the signatures
_, accused := s.BondedValidators.GetByAddress(tx.Address) _, accused := s.BondedValidators.GetByAddress(tx.Address)
if accused == nil { if accused == nil {
@ -650,7 +632,7 @@ func (s *State) appendBlock(block *blk.Block, blockPartsHeader blk.PartSetHeader
// Commit each tx // Commit each tx
for _, tx := range block.Data.Txs { for _, tx := range block.Data.Txs {
err := s.ExecTx(tx, block)
err := s.ExecTx(tx, true)
if err != nil { if err != nil {
return InvalidTxError{tx, err} return InvalidTxError{tx, err}
} }


+ 3
- 3
state/state_test.go View File

@ -183,7 +183,7 @@ func TestTxSequence(t *testing.T) {
tx := makeSendTx(sequence) tx := makeSendTx(sequence)
tx.Inputs[0].Signature = privAccounts[0].Sign(tx) tx.Inputs[0].Signature = privAccounts[0].Sign(tx)
stateCopy := state.Copy() stateCopy := state.Copy()
err := stateCopy.ExecTx(tx, &blk.Block{})
err := stateCopy.ExecTx(tx, true)
if i == 1 { if i == 1 {
// Sequence is good. // Sequence is good.
if err != nil { if err != nil {
@ -242,7 +242,7 @@ func TestTxs(t *testing.T) {
} }
tx.Inputs[0].Signature = privAccounts[0].Sign(tx) tx.Inputs[0].Signature = privAccounts[0].Sign(tx)
err := state.ExecTx(tx, &blk.Block{})
err := state.ExecTx(tx, true)
if err != nil { if err != nil {
t.Errorf("Got error in executing send transaction, %v", err) t.Errorf("Got error in executing send transaction, %v", err)
} }
@ -279,7 +279,7 @@ func TestTxs(t *testing.T) {
}, },
} }
tx.Inputs[0].Signature = privAccounts[0].Sign(tx) tx.Inputs[0].Signature = privAccounts[0].Sign(tx)
err := state.ExecTx(tx, &blk.Block{})
err := state.ExecTx(tx, true)
if err != nil { if err != nil {
t.Errorf("Got error in executing bond transaction, %v", err) t.Errorf("Got error in executing bond transaction, %v", err)
} }


Loading…
Cancel
Save