From 126d1b09fa50f8973c355f79d87a19e737d41eb1 Mon Sep 17 00:00:00 2001 From: yeahnotsewerside Date: Sun, 4 May 2025 17:53:03 +0000 Subject: [PATCH] add initial implementation of Aplo locking --- Makefile | 2 +- builtin/gaplo/gaplo.go | 99 ++++++++++++++++++++++++++++++++++++++++ core/state_transition.go | 42 ++++------------- params/bootnodes.go | 2 +- 4 files changed, 109 insertions(+), 36 deletions(-) create mode 100644 builtin/gaplo/gaplo.go diff --git a/Makefile b/Makefile index e97acbef2..f980d0956 100644 --- a/Makefile +++ b/Makefile @@ -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." diff --git a/builtin/gaplo/gaplo.go b/builtin/gaplo/gaplo.go new file mode 100644 index 000000000..558496583 --- /dev/null +++ b/builtin/gaplo/gaplo.go @@ -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 +} diff --git a/core/state_transition.go b/core/state_transition.go index 16bc71dc7..0a7684d73 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -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" @@ -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. @@ -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, @@ -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, } } @@ -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 @@ -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() @@ -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) } @@ -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) } @@ -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. diff --git a/params/bootnodes.go b/params/bootnodes.go index 4bb251286..8316d7039 100644 --- a/params/bootnodes.go +++ b/params/bootnodes.go @@ -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")