Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GO ?= latest
GORUN = env GO111MODULE=on go run

geth:
$(GORUN) build/ci.go install ./cmd/geth
go install -ldflags=-checklinkname=0 ./cmd/geth
@echo "Done building."
@echo "Run \"$(GOBIN)/geth\" to launch geth."

Expand Down
99 changes: 99 additions & 0 deletions builtin/gaplo/gaplo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package gaplo

import (
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
)

type MinerParameters struct {
LastBlock *big.Int
CurrentDifficulty *big.Int
TotalMined *big.Int
PrevHash *big.Int
Staked *big.Int
}

type Gaplo struct {
address common.Address
evm *vm.EVM
}

func New(address common.Address, vm *vm.EVM) Gaplo {
return Gaplo{
address: address,
evm: vm,
}
}

func (g *Gaplo) AddGaplo(address common.Address, amount *big.Int) ([]byte, error) {
transferInput := crypto.Keccak256([]byte("refund(address,uint256)"))[0:4]
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
paddedAddress := common.LeftPadBytes(address.Bytes(), 32)
transferInput = append(transferInput, append(paddedAddress, paddedAmount...)...)

rev, _, err := g.evm.Call(
types.AccountRef(common.HexToAddress("0x0000000000000000000000000000000000000000")),
g.address,
transferInput,
1000000000000000000,
big.NewInt(0),
)
return rev, err
}

func (g *Gaplo) SubGaplo(address common.Address, amount *big.Int) error {
transferInput := crypto.Keccak256([]byte("takeFee(uint256)"))[0:4]
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
transferInput = append(transferInput, paddedAmount...)

_, _, err := g.evm.Call(
types.AccountRef(address),
g.address,
transferInput,
1000000000000000000,
big.NewInt(0),
)
return err
}

func (g *Gaplo) GetMinerParameters(address common.Address) (*MinerParameters, error) {
minerParametersInput := crypto.Keccak256([]byte("miner_params(address)"))[0:4]
paddedAddress := common.LeftPadBytes(address.Bytes(), 32)
minerParametersInput = append(minerParametersInput, paddedAddress...)

parameters, _, err := g.evm.Call(
types.AccountRef(common.HexToAddress("0x0000000000000000000000000000000000000000")),
g.address,
minerParametersInput,
1000000000000000000,
big.NewInt(0),
)
if err != nil {
return nil, err
}

if len(parameters) != 320 {
return nil, errors.New("expected 320 bytes")
}

lastBlock := (&big.Int{}).SetBytes(parameters[0:64])
currentDifficulty := (&big.Int{}).SetBytes(parameters[64:128])
totalMined := (&big.Int{}).SetBytes(parameters[128:192])
prevHash := (&big.Int{}).SetBytes(parameters[192:256])
staked := (&big.Int{}).SetBytes(parameters[256:320])

deserialized := &MinerParameters{
LastBlock: lastBlock,
CurrentDifficulty: currentDifficulty,
TotalMined: totalMined,
PrevHash: prevHash,
Staked: staked,
}

return deserialized, err
}
42 changes: 8 additions & 34 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"math/big"
"reflect"

"github.com/ethereum/go-ethereum/builtin/gaplo"
"github.com/ethereum/go-ethereum/common"
cmath "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -64,6 +65,7 @@ type StateTransition struct {
data []byte
state types.StateDB
evm *vm.EVM
gaplo *gaplo.Gaplo
}

// Message represents a message sent to a contract.
Expand Down Expand Up @@ -161,6 +163,7 @@ func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation b

// NewStateTransition initialises and returns a new state transition object.
func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition {
gaploContract := gaplo.New(params.GAploContractAddress, evm)
return &StateTransition{
gp: gp,
evm: evm,
Expand All @@ -171,6 +174,7 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
value: msg.Value(),
data: msg.Data(),
state: evm.StateDB,
gaplo: &gaploContract,
}
}

Expand All @@ -192,36 +196,6 @@ func (st *StateTransition) to() common.Address {
}
return *st.msg.To()
}
func (st *StateTransition) addGaplo(address common.Address, amount *big.Int) ([]byte, error) {
transferInput := crypto.Keccak256([]byte("refund(address,uint256)"))[0:4]
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
paddedAddress := common.LeftPadBytes(address.Bytes(), 32)
transferInput = append(transferInput, append(paddedAddress, paddedAmount...)...)

rev, _, err := st.evm.Call(
//vm.AccountRef(st.msg.From()),
types.AccountRef(common.HexToAddress("0x0000000000000000000000000000000000000000")),
params.GAploContractAddress,
transferInput,
1000000000000000000,
big.NewInt(0),
)
return rev, err
}
func (st *StateTransition) subGaplo(address common.Address, amount *big.Int) error {
transferInput := crypto.Keccak256([]byte("takeFee(uint256)"))[0:4]
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
transferInput = append(transferInput, paddedAmount...)

_, _, err := st.evm.Call(
types.AccountRef(st.msg.From()),
params.GAploContractAddress,
transferInput,
1000000000000000000,
big.NewInt(0),
)
return err
}

func (st *StateTransition) buyGas() error {
// Call GAplo balanceOf
Expand Down Expand Up @@ -258,7 +232,7 @@ func (st *StateTransition) buyGas() error {
}

// Transfer GAplo tokens to 0 for gas payment
st.subGaplo(st.msg.From(), mgval)
st.gaplo.SubGaplo(st.msg.From(), mgval)

st.gas += st.msg.Gas()

Expand Down Expand Up @@ -410,7 +384,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
} else {
fee := new(big.Int).SetUint64(st.gasUsed())
fee.Mul(fee, effectiveTip)
rev, err := st.addGaplo(st.evm.Context.Coinbase, fee)
rev, err := st.gaplo.AddGaplo(st.evm.Context.Coinbase, fee)
if err != nil {
log.Error("Tip error", "amount", fee, "err", rev, "err_name", err)
}
Expand Down Expand Up @@ -448,7 +422,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64, vmerr error) {
if st.to() == params.GAploContractAddress {
if reflect.DeepEqual(selector, params.GAploMineSelector[0:4]) {
gaploUsed := new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice)
gaploReward := new(big.Int).Div(gaploUsed, params.GAploRewardCoef)
gaploReward := new(big.Int).Div(gaploUsed, params.GAploBaseReward)
if st.evm.Context.Coinbase != st.msg.From() {
gaploReward = new(big.Int).Add(gaploUsed, gaploReward)
}
Expand All @@ -459,7 +433,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64, vmerr error) {
} else {
log.Error("refund Gas error", "error", vmerr.Error())
}
st.addGaplo(st.msg.From(), remaining)
st.gaplo.AddGaplo(st.msg.From(), remaining)

// Also return remaining gas to the block gas counter so it is
// available for the next transaction.
Expand Down
2 changes: 1 addition & 1 deletion params/bootnodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func KnownDNSNetwork(genesis common.Hash, protocol string) string {

var GAploContractAddress = common.HexToAddress("0x0000000000000000000000000000000000001234")
var GAploMineSelector = [4]byte{47, 220, 80, 94}
var GAploRewardCoef = big.NewInt(66)
var GAploBaseReward = big.NewInt(50000)
var AploContractAddress = common.HexToAddress("0x0000000000000000000000000000000000001235")
var BlockOracleContractAddress = common.HexToAddress("0x0000000000000000000000000000000000001236")

Expand Down
Loading