Skip to content

Commit

Permalink
Delegated staking support in code
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-papyrus committed Aug 7, 2019
1 parent ca9f324 commit ee90042
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
12 changes: 11 additions & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package core

import (
"bytes"
"encoding/hex"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/state"
Expand Down Expand Up @@ -196,6 +198,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
return receipts, allLogs, *usedGas, nil
}

var fnFreezeForContract = hexutil.MustDecode("0x3d0d42c0") // freezeForContract(address)

// ApplyTransaction attempts to apply a transaction to the given state database
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
Expand All @@ -218,7 +222,13 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
if tx.To() != nil {
biosAddress := getBiosAddress(statedb)
if *tx.To() == biosAddress {
FetchLimit(msg.From(), statedb, header.GasLimit, true)
if len(msg.Data()) == 36 &&
bytes.Equal(msg.Data()[:4], fnFreezeForContract) {
FetchLimit(common.BytesToAddress(msg.Data()[4:36]),
statedb, header.GasLimit, true)
} else {
FetchLimit(msg.From(), statedb, header.GasLimit, true)
}
}
}
// Update the state with pending changes
Expand Down
25 changes: 20 additions & 5 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type StateTransition struct {
data []byte
state vm.StateDB
evm *vm.EVM
donor common.Address
}

// Message represents a message sent to a contract.
Expand Down Expand Up @@ -120,6 +121,7 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
value: msg.Value(),
data: msg.Data(),
state: evm.StateDB,
donor: common.Address{},
}
}

Expand Down Expand Up @@ -174,16 +176,29 @@ func (st *StateTransition) isFree() bool {
return *to == biosAddress
}

func (st *StateTransition) whoPays() (common.Address, uint64) {
if st.msg.To() != nil && st.state.GetCodeSize(*st.msg.To()) != 0 {
donor := st.msg.To()
limit := st.state.GetLimit(*donor)
if limit > st.msg.Gas() {
return *donor, limit
}
}
donor := st.msg.From()
return donor, st.state.GetLimit(donor)
}

func (st *StateTransition) buyGas() error {
/// mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.gasPrice)
/// if st.state.GetBalance(st.msg.From()).Cmp(mgval) < 0 && !st.msg.Unmetered() {
limit := FetchLimit(st.msg.From(), st.state, st.evm.GasLimit, true)
donor, limit := st.whoPays()
st.donor = donor
if limit < st.msg.Gas() && !st.msg.Unmetered() && !st.isCall() {
return errInsufficientBalanceForGas
}
log.Warn("/// buyGas running", "from", st.msg.From(), "to", st.msg.To(),
"limit", st.state.GetLimit(st.msg.From()),
"unmetered", st.msg.Unmetered(), "isCall", st.isCall())
"limit", limit,
"unmetered", st.msg.Unmetered(), "isCall", st.isCall(), "data", st.msg.Data())
if err := st.gp.SubGas(st.msg.Gas()); err != nil {
return err
}
Expand All @@ -196,7 +211,7 @@ func (st *StateTransition) buyGas() error {
if !st.isFree() {
log.Warn("/// buyGas taking limit", "from", st.msg.From(), "to", st.msg.To(),
"gas", st.msg.Gas(), "limit", st.state.GetLimit(st.msg.From()))
st.state.SubLimit(st.msg.From(), st.msg.Gas())
st.state.SubLimit(donor, st.msg.Gas())
}
return nil
}
Expand Down Expand Up @@ -281,7 +296,7 @@ func (st *StateTransition) refundGas() {
/// remaining := new(big.Int).Mul(new(big.Int).SetUint64(st.gas), st.gasPrice)
/// st.state.AddBalance(st.msg.From(), remaining)
if !st.isFree() {
st.state.AddLimit(st.msg.From(), st.gas)
st.state.AddLimit(st.donor, st.gas)
}

// Also return remaining gas to the block gas counter so it is
Expand Down
6 changes: 3 additions & 3 deletions papyrus-stuff/contracts/Bios.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ contract Bios is BiosHeader, QueueHelper, Ownable {
contractStakeOwner[contract_] = msg.sender;
}
require(contractStakeOwner[contract_] == msg.sender);
stakes[contract_] += msg.value;
stakes[contract_] += msg.value;
}

/// Unstake the specified value of money.
Expand All @@ -95,13 +95,13 @@ contract Bios is BiosHeader, QueueHelper, Ownable {
QueueHelper.push(melting[msg.sender], Entry(val, uint32(now)));
stakes[msg.sender] -= val;
}

/// Unstake the specified value of money from the contract account.
/// @dev The value is put to the melting queue and can be withdrawn after `kFreezeStake`.
/// @param contract_ the contract to unstake from.
/// @param val value to unstake.
function meltFromContract(address contract_, uint224 val) public {
require(contractStakeOwner[contract_] == msg.sender);
require(contractStakeOwner[contract_] == msg.sender);
require(val != 0 && stakes[contract_] >= val, "not enough stake");
QueueHelper.push(melting[msg.sender], Entry(val, uint32(now)));
stakes[contract_] -= val;
Expand Down
3 changes: 2 additions & 1 deletion papyrus-stuff/restart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
cd $(dirname $0)
. config/config.sh

./stop.sh
./stop.sh all
rm -rf data.*
./runnode.sh 1 --mine
./runnode.sh 2 --mine
./runnode.sh 3 --mine
Expand Down

0 comments on commit ee90042

Please sign in to comment.