Skip to content

Commit

Permalink
Merge pull request ethereum#280 from nextyio/stablecoin
Browse files Browse the repository at this point in the history
Implementation of Seignoriage Share stablecoin
  • Loading branch information
Zergity authored Sep 11, 2019
2 parents c54ec91 + a040cca commit 5db4663
Show file tree
Hide file tree
Showing 39 changed files with 20,630 additions and 227 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ profile.cov
# intermediate files
*.json
*.key
passwd
passwd
!package.json
node_modules
20 changes: 10 additions & 10 deletions accounts/abi/bind/backends/real.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
// Copyright 2019 The gonex Authors
// This file is part of the gonex library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// The gonex library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// The gonex library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// along with the gonex library. If not, see <http://www.gnu.org/licenses/>.

package backends

Expand All @@ -38,15 +38,15 @@ import (
// This nil assignment ensures compile time that RealBackend implements bind.ContractBackend.
var _ bind.ContractBackend = (*RealBackend)(nil)

// RealBackend implements bind.ContractBackend, simulating a blockchain in
// the background. Its main purpose is to allow easily testing contract bindings.
// RealBackend implements bind.ContractBackend, directly modifying the blockchain state.
// Its main purpose is to allow easily inter-operating between consensus and contract code.
type RealBackend struct {
blockchain consensus.ChainReader
header *types.Header
caller *common.Address

mu sync.Mutex
state *state.StateDB // Currently pending state that will be the active on on request
state *state.StateDB // The current mutating state.

events *filters.EventSystem // Event system for filtering log events live

Expand All @@ -56,6 +56,7 @@ type RealBackend struct {
// NewRealBackend creates a new binding backend for modifying the real blockchain state.
func NewRealBackend(chain consensus.ChainReader, header *types.Header, state *state.StateDB, caller *common.Address) *RealBackend {
if caller == nil {
// zero sender to make sure no one has any special permission
caller = &params.ZeroAddress
}
backend := &RealBackend{
Expand Down Expand Up @@ -176,8 +177,7 @@ func (b *RealBackend) PendingNonceAt(ctx context.Context, account common.Address
return b.state.GetOrNewStateObject(account).Nonce(), nil
}

// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
// chain doesn't have miners, we just return a gas price of 1 for any call.
// SuggestGasPrice implements ContractTransactor.SuggestGasPrice.
func (b *RealBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return common.Big0, nil
}
Expand Down
35 changes: 32 additions & 3 deletions cmd/puppeth/wizard_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/deployer"
"github.com/ethereum/go-ethereum/contracts/nexty/token"
"github.com/ethereum/go-ethereum/contracts/nexty/ntf"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -128,6 +128,12 @@ func (w *wizard) makeGenesis() {
LeakDuration: 1024,
ApplicationConfirmation: 128,
RandomSeedIteration: 20000000, // around 128 seconds
PriceSamplingDuration: 7 * 24 * 60 * 60 / 2,
PriceSamplingInterval: 10*60/2 - 7,
AbsorptionDuration: 7 * 24 * 60 * 60 / 2 / 2,
AbsorptionExpiration: 7 * 24 * 60 * 60 / 2,
SlashingDuration: 7 * 24 * 60 * 60 / 2 / 2,
LockdownExpiration: 7 * 24 * 60 * 60 / 2 * 2,
}
fmt.Println()
fmt.Println("How many seconds should blocks take? (default = 2)")
Expand Down Expand Up @@ -195,7 +201,7 @@ func (w *wizard) makeGenesis() {
}
if onwer != nil {
code, storage, err := deployer.DeployContract(func(sim *backends.SimulatedBackend, auth *bind.TransactOpts) (common.Address, error) {
address, _, _, err := token.DeployNtfToken(auth, sim, *onwer)
address, _, _, err := ntf.DeployNtfToken(auth, sim, *onwer)
return address, err
})
if err != nil {
Expand All @@ -215,7 +221,6 @@ func (w *wizard) makeGenesis() {
fmt.Printf("Which block should CoLoa come into effect? (default = %v)\n", genesis.Config.Dccs.CoLoaBlock)
genesis.Config.Dccs.CoLoaBlock = w.readDefaultBigInt(genesis.Config.Dccs.CoLoaBlock)

// CoLoa hardfork enabled
fmt.Println()
fmt.Printf("After how many block of inactivity should a sealer is kicked out? (default = %v)\n", genesis.Config.Dccs.LeakDuration)
genesis.Config.Dccs.LeakDuration = uint64(w.readDefaultInt(int(genesis.Config.Dccs.LeakDuration)))
Expand All @@ -228,6 +233,30 @@ func (w *wizard) makeGenesis() {
fmt.Printf("How often should the sealer queue is re-shuffled, in VDF iteration? (default = %v)\n", genesis.Config.Dccs.RandomSeedIteration)
genesis.Config.Dccs.RandomSeedIteration = uint64(w.readDefaultInt(int(genesis.Config.Dccs.RandomSeedIteration)))

fmt.Println()
fmt.Printf("How long should the price be sampled for supply absorption? (default = %v)\n", genesis.Config.Dccs.PriceSamplingDuration)
genesis.Config.Dccs.PriceSamplingDuration = uint64(w.readDefaultInt(int(genesis.Config.Dccs.PriceSamplingDuration)))

fmt.Println()
fmt.Printf("How often should the price be sampled for supply absorption? (default = %v)\n", genesis.Config.Dccs.PriceSamplingInterval)
genesis.Config.Dccs.PriceSamplingInterval = uint64(w.readDefaultInt(int(genesis.Config.Dccs.PriceSamplingInterval)))

fmt.Println()
fmt.Printf("How quick should an absorption take? (default = %v)\n", genesis.Config.Dccs.AbsorptionDuration)
genesis.Config.Dccs.AbsorptionDuration = uint64(w.readDefaultInt(int(genesis.Config.Dccs.AbsorptionDuration)))

fmt.Println()
fmt.Printf("How long should an absorption expire? (default = %v)\n", genesis.Config.Dccs.AbsorptionExpiration)
genesis.Config.Dccs.AbsorptionExpiration = uint64(w.readDefaultInt(int(genesis.Config.Dccs.AbsorptionExpiration)))

fmt.Println()
fmt.Printf("How quick should a violated initiator is slashed? (default = %v)\n", genesis.Config.Dccs.SlashingDuration)
genesis.Config.Dccs.SlashingDuration = uint64(w.readDefaultInt(int(genesis.Config.Dccs.SlashingDuration)))

fmt.Println()
fmt.Printf("How long should a lockdown expire? (default = %v)\n", genesis.Config.Dccs.LockdownExpiration)
genesis.Config.Dccs.LockdownExpiration = uint64(w.readDefaultInt(int(genesis.Config.Dccs.LockdownExpiration)))

default:
log.Crit("Invalid consensus engine choice", "choice", choice)
}
Expand Down
14 changes: 14 additions & 0 deletions common/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,18 @@ var (
Big32 = big.NewInt(32)
Big256 = big.NewInt(256)
Big257 = big.NewInt(257)

Big1000 = big.NewInt(1000)
Big1e12 = big.NewInt(1e12)
Big1e24 = new(big.Int).Mul(Big1e12, Big1e12)
)

var (
Rat0 = big.NewRat(0, 1)
Rat1 = big.NewRat(1, 1)
Rat2 = big.NewRat(2, 1)
Rat1_2 = big.NewRat(1, 2)
Rat1_4 = big.NewRat(1, 4)

RatNeg1_2 = big.NewRat(-1, 2)
)
35 changes: 31 additions & 4 deletions common/deployer/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ package deployer

import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
)

// DeployContract deploy a smart contract to simulated chain to get out the contract's code and state
func DeployContract(deployCallback func(sim *backends.SimulatedBackend, auth *bind.TransactOpts) (common.Address, error)) (code []byte, storage map[common.Hash]common.Hash, err error) {
type DeployCallbackFn = func(*backends.SimulatedBackend, *bind.TransactOpts) (common.Address, error)

// DeployContract deploy a smart contract to simulated chain to the target address in the StateDB
func DeployContract(deployCallback DeployCallbackFn) (code []byte, storage map[common.Hash]common.Hash, err error) {
// Generate a new random account and a funded simulator
prvKey, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactor(prvKey)
auth.GasLimit = 12344321
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: new(big.Int).Lsh(big.NewInt(1), 256-7)}}, auth.GasLimit)
address, err := deployCallback(sim, auth)
if err != nil {
fmt.Println("Can't deploy nexty governance smart contract")
log.Error("Unable to deploy consensus contract", "error", err)
return nil, nil, err
}
sim.Commit()

Expand All @@ -37,3 +41,26 @@ func DeployContract(deployCallback func(sim *backends.SimulatedBackend, auth *bi
})
return code, storage, err
}

func CopyContractToAddress(state *state.StateDB, address common.Address, code []byte, storage map[common.Hash]common.Hash, overwrite bool) {
// Ensure there's no existing contract already at the designated address
contractHash := state.GetCodeHash(address)
// this is an consensus upgrade
exist := state.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != vm.EmptyCodeHash)
if !exist {
// Create a new account on the state
state.CreateAccount(address)
// Assuming chainConfig.IsEIP158(BlockNumber)
state.SetNonce(address, 1)
} else if !overwrite {
// disable overwrite flag to prevent unintentional contract upgrade
return
}

// Transfer the code and state from simulated backend to the real state db
state.SetCode(address, code)
for key, value := range storage {
state.SetState(address, key, value)
}
state.Commit(false)
}
9 changes: 5 additions & 4 deletions consensus/dccs/1_dccs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"math/big"
"time"

"github.com/ethereum/go-ethereum/contracts/nexty/governance"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
Expand All @@ -33,8 +35,7 @@ import (
"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/contracts/nexty/contract"
"github.com/ethereum/go-ethereum/contracts/nexty/token"
"github.com/ethereum/go-ethereum/contracts/nexty/ntf"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -715,7 +716,7 @@ func deployConsensusContracts(state *state.StateDB, chainConfig *params.ChainCon
owner := common.HexToAddress("0x000000270840d8ebdffc7d162193cc5ba1ad8707")
// Generate contract code and data using a simulated backend
code, storage, err := deployer.DeployContract(func(sim *backends.SimulatedBackend, auth *bind.TransactOpts) (common.Address, error) {
address, _, _, err := token.DeployNtfToken(auth, sim, owner)
address, _, _, err := ntf.DeployNtfToken(auth, sim, owner)
return address, err
})
if err != nil {
Expand All @@ -735,7 +736,7 @@ func deployConsensusContracts(state *state.StateDB, chainConfig *params.ChainCon
code, storage, err := deployer.DeployContract(func(sim *backends.SimulatedBackend, auth *bind.TransactOpts) (common.Address, error) {
stakeRequire := new(big.Int).Mul(new(big.Int).SetUint64(chainConfig.Dccs.StakeRequire), new(big.Int).SetUint64(1e+18))
stakeLockHeight := new(big.Int).SetUint64(chainConfig.Dccs.StakeLockHeight)
address, _, _, err := contract.DeployNextyGovernance(auth, sim, params.TokenAddress, stakeRequire, stakeLockHeight, signers)
address, _, _, err := governance.DeployNextyGovernance(auth, sim, params.TokenAddress, stakeRequire, stakeLockHeight, signers)
return address, err
})
if err != nil {
Expand Down
Loading

0 comments on commit 5db4663

Please sign in to comment.