Skip to content

Commit

Permalink
Merge branch 'consensus'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zergity committed Oct 7, 2019
2 parents 04fb36d + bd08f1f commit 44fe281
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 184 deletions.
1 change: 0 additions & 1 deletion consensus/dccs/2_dccs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ var (
errInvalidExtendedData = errors.New("Extended data does not matches")
errInvalidRandomData = errors.New("Invalid random data in extra data")
errInvalidRandomDataSize = errors.New("Invalid random data size from relayer")
errInvalidPrioritiezedDiff = errors.New("Invalid prioritized block difficulty")

errInvalidPriceData = errors.New("price block contains invalid price value")
errUnexpectPriceData = errors.New("non-price block contains price value")
Expand Down
5 changes: 1 addition & 4 deletions consensus/dccs/2_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,8 @@ func (c *Context) getSealingQueue(parentHash common.Hash) (*SealingQueue, error)
// use the difficulty for total number of recently active sealer count
diff := header.Difficulty.Uint64()
if header.Nonce == (types.BlockNonce{}) {
if diff&0x01 != 0 {
// prioritized difficulty cannot be odd number
return nil, errInvalidPrioritiezedDiff
}
// correct difficulty for prioritized block
// Note: incorrect for pre-CoLoa block
diff >>= 1
}
if diff > maxDiff {
Expand Down
110 changes: 100 additions & 10 deletions contracts/nexty/endurio/Seigniorage.go

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions contracts/nexty/endurio/src/Preemptivable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ contract Preemptivable is Absorbable {
internal
{
require(stake >= globalSuccessStake - globalSuccessStake / PARAM_TOLERANCE, "stake too low");
require(amount != 0, "zero absorption");

absn.Proposal memory proposal;

Expand Down Expand Up @@ -207,17 +208,30 @@ contract Preemptivable is Absorbable {
return false;
}
absn.Proposal storage proposal = proposals.get(maker);
adaptGlobalParams(proposal, rank);
triggerPreemptive(proposal);
adaptParams(proposal, rank);
return true;
}

// adapt the global params to the last winning preemptive
function adaptParams(absn.Proposal storage proposal, uint rank) internal {
globalSuccessRank = (globalSuccessRank + rank) >> 1;
globalSuccessStake = (globalSuccessStake + proposal.stake) >> 1;
globalLockdownExpiration = (globalLockdownExpiration + proposal.lockdownExpiration) >> 1;
globalSlashingDuration = (globalSlashingDuration + proposal.slashingDuration) >> 1;
function adaptGlobalParams(absn.Proposal storage proposal, uint rank) internal {
globalSuccessStake = util.avgCap(globalSuccessStake, proposal.stake);
globalSlashingDuration = util.avgCap(globalSlashingDuration, proposal.slashingDuration);
globalLockdownExpiration = util.avgCap(globalLockdownExpiration, proposal.lockdownExpiration);
globalSuccessRank = util.avgCap(globalSuccessRank, rank);
}

function getGlobalParams()
external
view
returns(
uint stake,
uint slashingDuration,
uint lockdownExpiration,
uint rank
)
{
return (globalSuccessStake, globalSlashingDuration, globalLockdownExpiration, globalSuccessRank);
}

// trigger an absorption from a maker's proposal
Expand Down
16 changes: 16 additions & 0 deletions contracts/nexty/endurio/src/lib/util.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,20 @@ library util {
// positive overflown
return MaxInt256;
}

// capped average
// if the calculation is overflown, return the max value of uint
function avgCap(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
if (c >= a) {
return c >> 1;
}
// (a+b) is overflown
uint d = (a >> 1) + (b >> 1);
if (d >= a) {
return d;
}
// (a/2+b/2) is also overflown
return MaxUint256;
}
}
12 changes: 6 additions & 6 deletions contracts/nexty/endurio/stable/StableToken.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions contracts/nexty/endurio/volatile/VolatileToken.go

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ func ApplyTransaction(config *params.ChainConfig, bc consensus.ChainReader, auth
}
// Set the receipt logs and create a bloom for filtering
receipt.Logs = statedb.GetLogs(tx.Hash())
if len(vmenv.Logs) > 0 {
receipt.Logs = append(receipt.Logs, vmenv.Logs...)
}
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = statedb.BlockHash()
receipt.BlockNumber = header.Number
Expand Down
5 changes: 0 additions & 5 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,6 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
// sufficient balance to make the transfer happen. The first
// balance transfer may never fail.
if vmerr == vm.ErrInsufficientBalance {
if msg.To() == nil {
evm.LogFailure(common.Address{}, params.TopicError, params.ErrorLogInsufficientBalance)
} else {
evm.LogFailure(*msg.To(), params.TopicError, params.ErrorLogInsufficientBalance)
}
return nil, 0, false, vmerr
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ var PrecompiledContractsCoLoa = map[common.Address]PrecompiledContract{
}

// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
func (evm *EVM) RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) {
func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) {
gas := p.RequiredGas(input)
if contract.UseGas(gas) {
return p.Run(input)
}
evm.LogFailure(contract.Address(), params.TopicError, params.ErrorLogOutOfGas)
return nil, ErrOutOfGas
}

Expand Down
16 changes: 6 additions & 10 deletions core/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

package vm

import (
"errors"

"github.com/ethereum/go-ethereum/params"
)
import "errors"

// List execution errors
var (
ErrOutOfGas = errors.New(params.ErrorLogOutOfGas)
ErrCodeStoreOutOfGas = errors.New(params.ErrorLogCodeStoreOutOfGas)
ErrDepth = errors.New(params.ErrorLogDepth)
ErrInsufficientBalance = errors.New(params.ErrorLogInsufficientBalance)
ErrContractAddressCollision = errors.New(params.ErrorLogContractAddressCollision)
ErrOutOfGas = errors.New("out of gas")
ErrCodeStoreOutOfGas = errors.New("contract creation code storage out of gas")
ErrDepth = errors.New("max call depth exceeded")
ErrTraceLimitReached = errors.New("the number of logs reached the specified limit")
ErrInsufficientBalance = errors.New("insufficient balance for transfer")
ErrContractAddressCollision = errors.New("contract address collision")
ErrNoCompatibleInterpreter = errors.New("no compatible interpreter")
)
37 changes: 3 additions & 34 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package vm

import (
"errors"
"fmt"
"math/big"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -60,7 +60,7 @@ func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, err
precompiles = PrecompiledContractsCoLoa
}
if p := precompiles[*contract.CodeAddr]; p != nil {
return evm.RunPrecompiledContract(p, input, contract)
return RunPrecompiledContract(p, input, contract)
}
}
for _, interpreter := range evm.interpreters {
Expand Down Expand Up @@ -100,9 +100,6 @@ type Context struct {
BlockNumber *big.Int // Provides information for NUMBER
Time *big.Int // Provides information for TIME
Difficulty *big.Int // Provides information for DIFFICULTY

// extra logs to append to the receipt after the regular logs
Logs []*types.Log
}

// EVM is the Ethereum Virtual Machine base object and provides
Expand Down Expand Up @@ -178,20 +175,6 @@ func NewEVM(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmCon
return evm
}

// LogFailure appends the failure reason or revert message to receipt log
// after the state has be reverted
func (evm *EVM) LogFailure(address common.Address, topic common.Hash, reason string) {
if !evm.chainRules.IsCoLoa {
return
}
log := types.Log{
Address: address,
Topics: []common.Hash{topic},
Data: []byte(reason),
}
evm.Logs = append(evm.Logs, &log)
}

func (evm *EVM) IgnoreNonce() bool {
return evm.vmConfig.IgnoreNonce
}
Expand Down Expand Up @@ -223,12 +206,10 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas

// Fail if we're trying to execute above the call depth limit
if evm.depth > int(params.CallCreateDepth) {
evm.LogFailure(addr, params.TopicError, params.ErrorLogDepth)
return nil, gas, ErrDepth
}
// Fail if we're trying to transfer more than the available balance
if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
evm.LogFailure(addr, params.TopicError, params.ErrorLogInsufficientBalance)
return nil, gas, ErrInsufficientBalance
}

Expand Down Expand Up @@ -306,7 +287,6 @@ func (evm *EVM) ExecCall(caller ContractRef, code []byte, gas uint64, value *big

// Fail if we're trying to execute above the call depth limit
if evm.depth > int(params.CallCreateDepth) {
evm.LogFailure(to, params.TopicError, params.ErrorLogDepth)
return nil, gas, ErrDepth
}

Expand Down Expand Up @@ -348,9 +328,7 @@ func (evm *EVM) ExecCall(caller ContractRef, code []byte, gas uint64, value *big
// revert the state
evm.StateDB.RevertToSnapshot(snapshot)
contract.UseGas(contract.Gas)
// log the failure error
evm.LogFailure(to, params.TopicError, params.ErrorLogTxCodeOverspent)
return nil, contract.Gas, errTxCodeOverspent
return nil, contract.Gas, errors.New("tx code value limit overspent")
}
}
return ret, contract.Gas, err
Expand All @@ -370,12 +348,10 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,

// Fail if we're trying to execute above the call depth limit
if evm.depth > int(params.CallCreateDepth) {
evm.LogFailure(addr, params.TopicError, params.ErrorLogDepth)
return nil, gas, ErrDepth
}
// Fail if we're trying to transfer more than the available balance
if !evm.CanTransfer(evm.StateDB, caller.Address(), value) {
evm.LogFailure(addr, params.TopicError, params.ErrorLogInsufficientBalance)
return nil, gas, ErrInsufficientBalance
}

Expand Down Expand Up @@ -409,7 +385,6 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
}
// Fail if we're trying to execute above the call depth limit
if evm.depth > int(params.CallCreateDepth) {
evm.LogFailure(addr, params.TopicError, params.ErrorLogDepth)
return nil, gas, ErrDepth
}

Expand Down Expand Up @@ -442,7 +417,6 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
}
// Fail if we're trying to execute above the call depth limit
if evm.depth > int(params.CallCreateDepth) {
evm.LogFailure(addr, params.TopicError, params.ErrorLogDepth)
return nil, gas, ErrDepth
}

Expand Down Expand Up @@ -491,11 +465,9 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
// Depth check execution. Fail if we're trying to execute above the
// limit.
if evm.depth > int(params.CallCreateDepth) {
evm.LogFailure(common.Address{}, params.TopicError, params.ErrorLogDepth)
return nil, common.Address{}, gas, ErrDepth
}
if !evm.CanTransfer(evm.StateDB, caller.Address(), value) {
evm.LogFailure(common.Address{}, params.TopicError, params.ErrorLogInsufficientBalance)
return nil, common.Address{}, gas, ErrInsufficientBalance
}
nonce := evm.StateDB.GetNonce(caller.Address())
Expand All @@ -504,7 +476,6 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
// Ensure there's no existing contract already at the designated address
contractHash := evm.StateDB.GetCodeHash(address)
if evm.StateDB.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != EmptyCodeHash) {
evm.LogFailure(common.Address{}, params.TopicError, params.ErrorLogContractAddressCollision)
return nil, common.Address{}, 0, ErrContractAddressCollision
}
// Create a new account on the state
Expand Down Expand Up @@ -542,7 +513,6 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
if contract.UseGas(createDataGas) {
evm.StateDB.SetCode(address, ret)
} else {
evm.LogFailure(common.Address{}, params.TopicError, params.ErrorLogCodeStoreOutOfGas)
err = ErrCodeStoreOutOfGas
}
}
Expand All @@ -558,7 +528,6 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
}
// Assign err if contract code size exceeds the max while the err is still empty.
if maxCodeSizeExceeded && err == nil {
evm.LogFailure(common.Address{}, params.TopicError, params.ErrorLogMaxCodeSizeExceeded)
err = errMaxCodeSizeExceeded
}
if evm.vmConfig.Debug && evm.depth == 0 {
Expand Down
12 changes: 4 additions & 8 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ import (
var (
bigZero = new(big.Int)
tt255 = math.BigPow(2, 255)
errWriteProtection = errors.New("evm: " + params.ErrorLogWriteProtection)
errReturnDataOutOfBounds = errors.New("evm: " + params.ErrorLogReturnDataOutOfBounds)
errMaxCodeSizeExceeded = errors.New("evm: " + params.ErrorLogMaxCodeSizeExceeded)
errInvalidJump = errors.New("evm: " + params.ErrorLogInvalidJump)
errTxCodeOverspent = errors.New("evm: " + params.ErrorLogTxCodeOverspent)
errWriteProtection = errors.New("evm: write protection")
errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
errExecutionReverted = errors.New("evm: execution reverted")
errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded")
errInvalidJump = errors.New("evm: invalid jump destination")
)

func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
Expand Down Expand Up @@ -469,7 +468,6 @@ func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contrac
defer interpreter.intPool.put(memOffset, dataOffset, length, end)

if !end.IsUint64() || uint64(len(interpreter.returnData)) < end.Uint64() {
interpreter.evm.LogFailure(contract.Address(), params.TopicError, params.ErrorLogReturnDataOutOfBounds)
return nil, errReturnDataOutOfBounds
}
memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()])
Expand Down Expand Up @@ -647,7 +645,6 @@ func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memor
func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
pos := stack.pop()
if !contract.validJumpdest(pos) {
interpreter.evm.LogFailure(contract.Address(), params.TopicError, params.ErrorLogInvalidJump)
return nil, errInvalidJump
}
*pc = pos.Uint64()
Expand All @@ -660,7 +657,6 @@ func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
pos, cond := stack.pop(), stack.pop()
if cond.Sign() != 0 {
if !contract.validJumpdest(pos) {
interpreter.evm.LogFailure(contract.Address(), params.TopicError, params.ErrorLogInvalidJump)
return nil, errInvalidJump
}
*pc = pos.Uint64()
Expand Down
Loading

0 comments on commit 44fe281

Please sign in to comment.