block/state: gas price for block and tx

This commit is contained in:
Ethan Buchman 2015-03-18 02:12:03 -07:00 committed by Jae Kwon
parent 7a33aba6e5
commit f384d10a05
4 changed files with 30 additions and 15 deletions

View File

@ -107,12 +107,16 @@ 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)

View File

@ -10,14 +10,15 @@ import (
) )
var ( var (
ErrTxInvalidAddress = errors.New("Error invalid address") ErrTxInvalidAddress = errors.New("Error invalid address")
ErrTxDuplicateAddress = errors.New("Error duplicate address") ErrTxDuplicateAddress = errors.New("Error duplicate address")
ErrTxInvalidAmount = errors.New("Error invalid amount") ErrTxInvalidAmount = errors.New("Error invalid amount")
ErrTxInsufficientFunds = errors.New("Error insufficient funds") ErrTxInsufficientFunds = errors.New("Error insufficient funds")
ErrTxUnknownPubKey = errors.New("Error unknown pubkey") ErrTxInsufficientGasPrice = errors.New("Error insufficient gas price")
ErrTxInvalidPubKey = errors.New("Error invalid pubkey") ErrTxUnknownPubKey = errors.New("Error unknown pubkey")
ErrTxInvalidSignature = errors.New("Error invalid signature") ErrTxInvalidPubKey = errors.New("Error invalid pubkey")
ErrTxInvalidSequence = errors.New("Error invalid sequence") ErrTxInvalidSignature = errors.New("Error invalid signature")
ErrTxInvalidSequence = errors.New("Error invalid sequence")
) )
/* /*

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"math/big"
"time" "time"
"github.com/tendermint/tendermint/account" "github.com/tendermint/tendermint/account"
@ -234,7 +235,7 @@ 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) error { func (s *State) ExecTx(tx_ blk.Tx, blk_ *blk.Block) error {
// TODO: do something with fees // TODO: do something with fees
fees := uint64(0) fees := uint64(0)
@ -296,8 +297,15 @@ func (s *State) ExecTx(tx_ blk.Tx) error {
} }
accounts[string(tx.Address)] = outAcc accounts[string(tx.Address)] = outAcc
// TODO: fees // ensure sufficient gas price
// inTotal -= fees // 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
// Good! Adjust accounts // Good! Adjust accounts
s.AdjustByInputs(accounts, []*blk.TxInput{tx.Input}) s.AdjustByInputs(accounts, []*blk.TxInput{tx.Input})
@ -306,6 +314,8 @@ func (s *State) ExecTx(tx_ blk.Tx) error {
// TODO: Run the contract call! // TODO: Run the contract call!
// TODO: refund some gas
return nil return nil
case *blk.BondTx: case *blk.BondTx:
@ -640,7 +650,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) err := s.ExecTx(tx, block)
if err != nil { if err != nil {
return InvalidTxError{tx, err} return InvalidTxError{tx, err}
} }

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) err := stateCopy.ExecTx(tx, &blk.Block{})
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) err := state.ExecTx(tx, &blk.Block{})
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) err := state.ExecTx(tx, &blk.Block{})
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)
} }