Skip to content

Commit

Permalink
Old code removed and renamed amount to balance
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed Jul 29, 2014
1 parent 27f8922 commit 1f9894c
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 144 deletions.
27 changes: 0 additions & 27 deletions ethchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,33 +125,6 @@ func (block *Block) Transactions() []*Transaction {
return block.transactions
}

func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
contract := block.state.GetStateObject(addr)
// If we can't pay the fee return
if contract == nil || contract.Amount.Cmp(fee) < 0 /* amount < fee */ {
fmt.Println("Contract has insufficient funds", contract.Amount, fee)

return false
}

base := new(big.Int)
contract.Amount = base.Sub(contract.Amount, fee)
block.state.Trie.Update(string(addr), string(contract.RlpEncode()))

data := block.state.Trie.Get(string(block.Coinbase))

// Get the ether (Coinbase) and add the fee (gief fee to miner)
account := ethstate.NewStateObjectFromBytes(block.Coinbase, []byte(data))

base = new(big.Int)
account.Amount = base.Add(account.Amount, fee)

//block.state.Trie.Update(string(block.Coinbase), string(ether.RlpEncode()))
block.state.UpdateStateObject(account)

return true
}

func (block *Block) CalcGasLimit(parent *Block) *big.Int {
if block.Number.Cmp(big.NewInt(0)) == 0 {
return ethutil.BigPow(10, 6)
Expand Down
7 changes: 4 additions & 3 deletions ethchain/block_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package ethchain

import (
"bytes"
"math"
"math/big"

"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
"math"
"math/big"
)

var chainlogger = ethlog.NewLogger("CHAIN")
Expand Down Expand Up @@ -280,7 +281,7 @@ func AddTestNetFunds(block *Block) {
} {
codedAddr := ethutil.Hex2Bytes(addr)
account := block.state.GetAccount(codedAddr)
account.Amount = ethutil.Big("1606938044258990275541962092341162602522202993782792835301376") //ethutil.BigPow(2, 200)
account.Balance = ethutil.Big("1606938044258990275541962092341162602522202993782792835301376") //ethutil.BigPow(2, 200)
block.state.UpdateStateObject(account)
}
}
Expand Down
15 changes: 8 additions & 7 deletions ethchain/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package ethchain

import (
"fmt"
"math/big"

"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethvm"
"math/big"
)

/*
Expand Down Expand Up @@ -94,8 +95,8 @@ func (self *StateTransition) BuyGas() error {
var err error

sender := self.Sender()
if sender.Amount.Cmp(self.tx.GasValue()) < 0 {
return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Amount)
if sender.Balance.Cmp(self.tx.GasValue()) < 0 {
return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance)
}

coinbase := self.Coinbase()
Expand Down Expand Up @@ -178,8 +179,8 @@ func (self *StateTransition) TransitionState() (err error) {
return
}

if sender.Amount.Cmp(self.value) < 0 {
return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Amount)
if sender.Balance.Cmp(self.value) < 0 {
return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Balance)
}

var snapshot *ethstate.State
Expand Down Expand Up @@ -240,8 +241,8 @@ func (self *StateTransition) TransitionState() (err error) {
}

func (self *StateTransition) transferValue(sender, receiver *ethstate.StateObject) error {
if sender.Amount.Cmp(self.value) < 0 {
return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Amount)
if sender.Balance.Cmp(self.value) < 0 {
return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Balance)
}

// Subtract the amount from the senders account
Expand Down
79 changes: 4 additions & 75 deletions ethchain/transaction_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"bytes"
"container/list"
"fmt"
"math/big"
"sync"

"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethwire"
"math/big"
"sync"
)

var txplogger = ethlog.NewLogger("TXP")
Expand Down Expand Up @@ -91,78 +92,6 @@ func (pool *TxPool) addTransaction(tx *Transaction) {
pool.Ethereum.Broadcast(ethwire.MsgTxTy, []interface{}{tx.RlpData()})
}

/*
// Process transaction validates the Tx and processes funds from the
// sender to the recipient.
func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract bool) (gas *big.Int, err error) {
fmt.Printf("state root before update %x\n", state.Root())
defer func() {
if r := recover(); r != nil {
txplogger.Infoln(r)
err = fmt.Errorf("%v", r)
}
}()
gas = new(big.Int)
addGas := func(g *big.Int) { gas.Add(gas, g) }
addGas(GasTx)
// Get the sender
sender := state.GetAccount(tx.Sender())
if sender.Nonce != tx.Nonce {
err = NonceError(tx.Nonce, sender.Nonce)
return
}
sender.Nonce += 1
defer func() {
//state.UpdateStateObject(sender)
// Notify all subscribers
pool.Ethereum.Reactor().Post("newTx:post", tx)
}()
txTotalBytes := big.NewInt(int64(len(tx.Data)))
txTotalBytes.Div(txTotalBytes, ethutil.Big32)
addGas(new(big.Int).Mul(txTotalBytes, GasSStore))
rGas := new(big.Int).Set(gas)
rGas.Mul(gas, tx.GasPrice)
// Make sure there's enough in the sender's account. Having insufficient
// funds won't invalidate this transaction but simple ignores it.
totAmount := new(big.Int).Add(tx.Value, rGas)
if sender.Amount.Cmp(totAmount) < 0 {
err = fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
return
}
state.UpdateStateObject(sender)
fmt.Printf("state root after sender update %x\n", state.Root())
// Get the receiver
receiver := state.GetAccount(tx.Recipient)
// Send Tx to self
if bytes.Compare(tx.Recipient, tx.Sender()) == 0 {
// Subtract the fee
sender.SubAmount(rGas)
} else {
// Subtract the amount from the senders account
sender.SubAmount(totAmount)
// Add the amount to receivers account which should conclude this transaction
receiver.AddAmount(tx.Value)
state.UpdateStateObject(receiver)
fmt.Printf("state root after receiver update %x\n", state.Root())
}
txplogger.Infof("[TXPL] Processed Tx %x\n", tx.Hash())
return
}
*/

func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
// Get the last block so we can retrieve the sender and receiver from
// the merkle trie
Expand All @@ -183,7 +112,7 @@ func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
totAmount := new(big.Int).Set(tx.Value)
// Make sure there's enough in the sender's account. Having insufficient
// funds won't invalidate this transaction but simple ignores it.
if sender.Amount.Cmp(totAmount) < 0 {
if sender.Balance.Cmp(totAmount) < 0 {
return fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
}

Expand Down
7 changes: 4 additions & 3 deletions ethpub/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package ethpub
import (
"encoding/json"
"fmt"
"strings"

"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"strings"
)

// Peer interface exposed to QML
Expand Down Expand Up @@ -175,9 +176,9 @@ func (c *PStateObject) GetStorage(address string) string {
return ""
}

func (c *PStateObject) Value() string {
func (c *PStateObject) Balance() string {
if c.object != nil {
return c.object.Amount.String()
return c.object.Balance.String()
}

return ""
Expand Down
7 changes: 4 additions & 3 deletions ethrpc/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package ethrpc
import (
"encoding/json"
"errors"
"github.com/ethereum/eth-go/ethpub"
"github.com/ethereum/eth-go/ethutil"
"math/big"
"strings"

"github.com/ethereum/eth-go/ethpub"
"github.com/ethereum/eth-go/ethutil"
)

type EthereumApi struct {
Expand Down Expand Up @@ -272,7 +273,7 @@ func (p *EthereumApi) GetBalanceAt(args *GetBalanceArgs, reply *string) error {
return err
}
state := p.ethp.GetStateObject(args.Address)
*reply = NewSuccessRes(BalanceRes{Balance: state.Value(), Address: args.Address})
*reply = NewSuccessRes(BalanceRes{Balance: state.Balance(), Address: args.Address})
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions ethstate/state.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ethstate

import (
"math/big"

"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"math/big"
)

var statelogger = ethlog.NewLogger("STATE")
Expand Down Expand Up @@ -33,7 +34,7 @@ func NewState(trie *ethtrie.Trie) *State {
func (self *State) GetBalance(addr []byte) *big.Int {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
return stateObject.Amount
return stateObject.Balance
}

return ethutil.Big0
Expand Down
Loading

0 comments on commit 1f9894c

Please sign in to comment.