Skip to content

Commit

Permalink
Merge pull request #9 from AlayaNetwork/feature/compatible-with-evm
Browse files Browse the repository at this point in the history
Feature/compatible with evm
  • Loading branch information
niuxiaojie81 authored Aug 27, 2021
2 parents 0b51265 + 805f28e commit b760765
Show file tree
Hide file tree
Showing 51 changed files with 142 additions and 1,820 deletions.
3 changes: 2 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ package utils
import (
"crypto/ecdsa"
"fmt"
"github.com/AlayaNetwork/Alaya-Go/miner"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/AlayaNetwork/Alaya-Go/miner"

"github.com/AlayaNetwork/Alaya-Go/core/snapshotdb"

"gopkg.in/urfave/cli.v1"
Expand Down
9 changes: 5 additions & 4 deletions common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"encoding/json"
"errors"
"fmt"
"golang.org/x/crypto/sha3"
"math/big"
"strings"

"github.com/btcsuite/btcutil/bech32"

"github.com/AlayaNetwork/Alaya-Go/common/bech32util"
"github.com/AlayaNetwork/Alaya-Go/common/hexutil"
"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/log"
)

Expand Down Expand Up @@ -137,7 +138,7 @@ func (a Address) Hex() string {

func (a Address) HexWithNoPrefix() string {
unchecksummed := hex.EncodeToString(a[:])
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
sha.Write([]byte(unchecksummed))
hash := sha.Sum(nil)

Expand Down Expand Up @@ -359,7 +360,7 @@ func (a NodeAddress) Hash() Hash { return BytesToHash(a[:]) }
// Hex returns an EIP55-compliant hex string representation of the address.
func (a NodeAddress) Hex() string {
unchecksummed := hex.EncodeToString(a[:])
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
sha.Write([]byte(unchecksummed))
hash := sha.Sum(nil)

Expand All @@ -380,7 +381,7 @@ func (a NodeAddress) Hex() string {

func (a NodeAddress) HexWithNoPrefix() string {
unchecksummed := hex.EncodeToString(a[:])
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
sha.Write([]byte(unchecksummed))
hash := sha.Sum(nil)

Expand Down
7 changes: 4 additions & 3 deletions common/mock/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package mock
import (
"encoding/json"
"fmt"
"golang.org/x/crypto/sha3"
"math/big"
"math/rand"
"reflect"
"time"

"github.com/AlayaNetwork/Alaya-Go/common/vm"
"github.com/AlayaNetwork/Alaya-Go/crypto"
"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/rlp"

"github.com/AlayaNetwork/Alaya-Go/core/snapshotdb"
Expand Down Expand Up @@ -92,7 +93,7 @@ func (c *Chain) execTx(miner bool, f Transaction) error {
type Transaction func(blockHash common.Hash, header *types.Header, statedb *MockStateDB, sdb snapshotdb.DB) error

func (T *Transaction) Hash() (h common.Hash) {
hw := sha3.NewKeccak256()
hw := sha3.NewLegacyKeccak256()
if err := rlp.Encode(hw, fmt.Sprint(T)); err != nil {
panic(err)
}
Expand Down Expand Up @@ -365,7 +366,7 @@ func (s *MockStateDB) SetCode(addr common.Address, code []byte) {
s.Code[addr] = code

var h common.Hash
hw := sha3.NewKeccak256()
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, code)
hw.Sum(h[:0])
s.CodeHash[addr] = h[:]
Expand Down
5 changes: 3 additions & 2 deletions common/mock/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package mock

import (
"golang.org/x/crypto/sha3"
"math/big"

"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/rlp"

"github.com/AlayaNetwork/Alaya-Go/common"
Expand Down Expand Up @@ -132,7 +133,7 @@ func (ch codeChange) revert(s *MockStateDB) {
s.Code[*ch.account] = ch.prevcode

var h common.Hash
hw := sha3.NewKeccak256()
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, ch.prevcode)
hw.Sum(h[:0])
s.CodeHash[*ch.account] = h[:]
Expand Down
4 changes: 2 additions & 2 deletions common/rlp_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package common

import (
"bytes"
"golang.org/x/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"
"github.com/AlayaNetwork/Alaya-Go/rlp"
)

Expand All @@ -40,7 +40,7 @@ func GenerateKVHash(k, v []byte, oldHash Hash) Hash {
}

func RlpHash(x interface{}) (h Hash) {
hw := sha3.NewKeccak256()
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, x)
hw.Sum(h[:0])
return h
Expand Down
5 changes: 5 additions & 0 deletions consensus/cbft/cbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ func (cbft *Cbft) Start(chain consensus.ChainReader, blockCacheWriter consensus.
// Start the handler to process the message.
go cbft.network.Start()

if cbft.config.Option.NodePriKey == nil {
cbft.config.Option.NodePriKey = cbft.nodeServiceContext.NodePriKey()
cbft.config.Option.NodeID = discover.PubkeyID(&cbft.config.Option.NodePriKey.PublicKey)
}

if isGenesis() {
cbft.validatorPool = validator.NewValidatorPool(agency, block.NumberU64(), cstate.DefaultEpoch, cbft.config.Option.NodeID)
cbft.changeView(cstate.DefaultEpoch, cstate.DefaultViewNumber, block, qc, nil)
Expand Down
3 changes: 2 additions & 1 deletion consensus/cbft/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package utils

import (
"bytes"
"golang.org/x/crypto/sha3"
"io"
"math/rand"
"sort"
"sync/atomic"

"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"

)

// RandomOffset returns a random offset between 0 and n
Expand Down
5 changes: 3 additions & 2 deletions consensus/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package consensus

import (
"errors"
"golang.org/x/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/core/types"
"github.com/AlayaNetwork/Alaya-Go/crypto"
"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/rlp"
lru "github.com/hashicorp/golang-lru"
)
Expand All @@ -43,7 +44,7 @@ var (
// panics. This is done to avoid accidentally using both forms (signature present
// or not), which could be abused to produce different hashes for the same header.
func SigHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()

rlp.Encode(hasher, []interface{}{
header.ParentHash,
Expand Down
2 changes: 1 addition & 1 deletion core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
// transition, such as amount of used gas, the receipt roots and the state root
// itself. ValidateState returns a database batch if the validation was a success
// otherwise nil and an error is returned.
func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error {
func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error {
header := block.Header()
if block.GasUsed() != usedGas {
return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ func (bc *BlockChain) ProcessDirectly(block *types.Block, state *state.StateDB,

// Validate the state using the default validator
start = time.Now()
err = bc.Validator().ValidateState(block, parent, state, receipts, usedGas)
err = bc.Validator().ValidateState(block, state, receipts, usedGas)
if err != nil {
bc.reportBlock(block, receipts, err)
return nil, err
Expand Down
8 changes: 0 additions & 8 deletions core/blockchain_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ func (it *insertIterator) current() *types.Block {
return it.chain[it.index]
}

// previous returns the previous block was being processed, or nil
func (it *insertIterator) previous() *types.Block {
if it.index < 1 {
return nil
}
return it.chain[it.index-1]
}

// first returns the first block in the it.
func (it *insertIterator) first() *types.Block {
return it.chain[0]
Expand Down
9 changes: 5 additions & 4 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"bytes"
"encoding/hex"
"fmt"
"golang.org/x/crypto/sha3"
"math/big"
"testing"

"github.com/AlayaNetwork/Alaya-Go/params"

"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/core/types"
"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/rlp"
)

Expand All @@ -50,7 +51,7 @@ func TestHeaderStorage(t *testing.T) {
if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
t.Fatalf("Stored header RLP not found")
} else {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
hasher.Write(entry)

if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
Expand All @@ -71,7 +72,7 @@ func TestBodyStorage(t *testing.T) {
// Create a test body to move around the database and make sure it's really new
body := &types.Body{}

hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
rlp.Encode(hasher, body)
hash := common.BytesToHash(hasher.Sum(nil))

Expand All @@ -88,7 +89,7 @@ func TestBodyStorage(t *testing.T) {
if entry := ReadBodyRLP(db, hash, 0); entry == nil {
t.Fatalf("Stored body RLP not found")
} else {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
hasher.Write(entry)

if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
Expand Down
5 changes: 3 additions & 2 deletions core/snapshotdb/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package snapshotdb

import (
"bytes"
"golang.org/x/crypto/sha3"
"io"
"math/big"
"math/rand"
Expand All @@ -28,7 +29,7 @@ import (
"github.com/AlayaNetwork/Alaya-Go/core/types"

"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/rlp"
)

Expand All @@ -41,7 +42,7 @@ func generateKVHash(k, v []byte, hash common.Hash) common.Hash {
}

func rlpHash(x interface{}) (h common.Hash) {
hw := sha3.NewKeccak256()
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, x)
hw.Sum(h[:0])
return h
Expand Down
5 changes: 3 additions & 2 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"bytes"
"fmt"
"github.com/AlayaNetwork/Alaya-Go/metrics"
"golang.org/x/crypto/sha3"
"io"
"math/big"
"time"

"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"


"github.com/AlayaNetwork/Alaya-Go/common"
cvm "github.com/AlayaNetwork/Alaya-Go/common/vm"
Expand Down Expand Up @@ -307,7 +308,7 @@ func (self *stateObject) getPrefixValue(pack, key, value []byte) []byte {
prefix := append(self.data.StorageKeyPrefix, pack...)
prefix = append(prefix, key...)
prefixHash := common.Hash{}
keccak := sha3.NewKeccak256()
keccak := sha3.NewLegacyKeccak256()
keccak.Write(prefix)
keccak.Sum(prefixHash[:0])
return append(prefixHash[:], value...)
Expand Down
2 changes: 1 addition & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func (self *StateDB) ListActiveVersion() ([]gov.ActiveVersionValue, error) {
// buffer.Write(value)
//
// valueKey := common.Hash{}
// keccak := sha3.NewKeccak256()
// keccak := sha3.NewLegacyKeccak256()
// keccak.Write(buffer.Bytes())
// keccak.Sum(valueKey[:0])
//
Expand Down
2 changes: 1 addition & 1 deletion core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Validator interface {

// ValidateState validates the given statedb and optionally the receipts and
// gas used.
ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error
ValidateState(block *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error
}

// Processor is an interface for processing blocks using a given initial state.
Expand Down
7 changes: 4 additions & 3 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package types
import (
"crypto/ecdsa"
"fmt"
"golang.org/x/crypto/sha3"
"io"
"math/big"
"sort"
Expand All @@ -31,7 +32,7 @@ import (
"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/common/hexutil"
"github.com/AlayaNetwork/Alaya-Go/crypto"
"github.com/AlayaNetwork/Alaya-Go/crypto/sha3"

"github.com/AlayaNetwork/Alaya-Go/log"
"github.com/AlayaNetwork/Alaya-Go/rlp"
)
Expand Down Expand Up @@ -146,7 +147,7 @@ func (h *Header) SealHash() (hash common.Hash) {
func (h *Header) _sealHash() (hash common.Hash) {
extra := h.Extra

hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
if len(h.Extra) > 32 {
extra = h.Extra[0:32]
}
Expand Down Expand Up @@ -198,7 +199,7 @@ func (h *Header) IsInvalid() bool {
// hasherPool holds Keccak hashers.
var hasherPool = sync.Pool{
New: func() interface{} {
return sha3.NewKeccak256()
return sha3.NewLegacyKeccak256()
},
}

Expand Down
Loading

0 comments on commit b760765

Please sign in to comment.