Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access list #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ func importChain(ctx *cli.Context) error {
}
chain.Stop()
fmt.Printf("Import done in %v.\n\n", time.Since(start))

// OutPut debug info
common.DebugInfo.Print()
// Output pre-compaction stats mostly to see the import trashing
stats, err := db.Stat("leveldb.stats")
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func ImportChain(chain *core.BlockChain, fn string) error {
if _, err := chain.InsertChain(missing); err != nil {
return fmt.Errorf("invalid block %d: %v", n, err)
}
// only test from 8M to 8.2M
if chain.CurrentBlock().NumberU64() >= 8200000 {
break
}
}
return nil
}
Expand Down
23 changes: 22 additions & 1 deletion common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
// Package common contains various helper functions.
package common

import "encoding/hex"
import (
"encoding/binary"
"encoding/hex"
)

// ToHex returns the hex representation of b, prefixed with '0x'.
// For empty slices, the return value is "0x0".
Expand Down Expand Up @@ -156,3 +159,21 @@ func TrimRightZeroes(s []byte) []byte {
}
return s[:idx]
}

func Uint64ToBytes(n uint64) []byte {
Bytes := make([]byte, 8)
binary.BigEndian.PutUint64(Bytes, n)
return Bytes
}

var (
// CalAccessList
// true:generate access list
// false: use access list
CalAccessList = true
scf0220 marked this conversation as resolved.
Show resolved Hide resolved
)

type AccessList struct {
Address Address
Hashs []Hash
}
32 changes: 32 additions & 0 deletions common/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"runtime"
"runtime/debug"
"strings"
"time"
)

// Report gives off a warning requesting the user to submit an issue to the github tracker.
Expand Down Expand Up @@ -50,3 +51,34 @@ func PrintDepricationWarning(str string) {

`, line, emptyLine, str, emptyLine, line)
}

type DebugTime struct {
ExecuteTx time.Duration
ValidateBlock time.Duration
WriteBlock time.Duration
CommitTrie time.Duration
TxLen int
}

func NewDebugTime() *DebugTime {
d := &DebugTime{
TxLen: 0,
ExecuteTx: time.Duration(0),
ValidateBlock: time.Duration(0),
WriteBlock: time.Duration(0),
CommitTrie: time.Duration(0),
}
return d
}

func (d *DebugTime) Print() {
fmt.Println("tx len", d.TxLen)
fmt.Println("process block time", d.ExecuteTx)
fmt.Println("validate block time", d.ValidateBlock)
fmt.Println("write block time", d.WriteBlock)
fmt.Println("write trie time", d.CommitTrie)
}

var (
DebugInfo = NewDebugTime()
)
7 changes: 7 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
bc.wg.Add(1)
defer bc.wg.Done()

ts := time.Now()
// Calculate the total difficulty of the block
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
if ptd == nil {
Expand All @@ -1456,6 +1457,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
if err := blockBatch.Write(); err != nil {
log.Crit("Failed to write block into disk", "err", err)
}
common.DebugInfo.WriteBlock += time.Since(ts)
ts = time.Now()
// Commit all cached state changes into underlying memory database.
root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()))
if err != nil {
Expand Down Expand Up @@ -1566,6 +1569,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
} else {
bc.chainSideFeed.Send(ChainSideEvent{Block: block})
}
common.DebugInfo.CommitTrie += time.Since(ts)
return status, nil
}

Expand Down Expand Up @@ -1819,6 +1823,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
atomic.StoreUint32(&followupInterrupt, 1)
return it.index, err
}
common.DebugInfo.ExecuteTx += time.Since(substart)
// Update the metrics touched during block processing
accountReadTimer.Update(statedb.AccountReads) // Account reads are complete, we can mark them
storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete, we can mark them
Expand All @@ -1841,6 +1846,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
return it.index, err
}
proctime := time.Since(start)
common.DebugInfo.ValidateBlock += time.Since(substart)
common.DebugInfo.TxLen += len(block.Transactions())

// Update the metrics touched during block validation
accountHashTimer.Update(statedb.AccountHashes) // Account hashes are complete, we can mark them
Expand Down
104 changes: 100 additions & 4 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,94 @@ package state

import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb/leveldb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rlp"
"io"
"math/big"
"sync"
"time"
)

var (
// AccessListDB: store generated data(key:blockNumber value:access list)
AccessListDB, _ = leveldb.New("./accessList_eth", 512, 512, "")

// BlockNumToAccessList: Preprocess the access list corresponding to the block
BlockNumToAccessList = make(map[int][]common.AccessList, 0)
)

func init() {
if common.CalAccessList {
return
}

// import block from 800w to 820w
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

800w to 820w --> 8M to 8.2M

start := 8000000
end := 8200000

log.Info("preLoad access list", "from", start, "to", end)
for index := start; index <= end; index++ {
data, err := AccessListDB.Get(common.Uint64ToBytes(uint64(index)))
list := make([]common.AccessList, 0)
if len(data) == 0 || err != nil {

} else {
if err := json.Unmarshal(data, &list); err != nil {
panic(err)
}
}
BlockNumToAccessList[index] = list
}
log.Info("preLoad access list", "size", len(BlockNumToAccessList))
}

type OriginStorage struct {
Data map[common.Address]Storage
mu sync.Mutex
}

func NewOriginStorage() *OriginStorage {
return &OriginStorage{
Data: make(map[common.Address]Storage, 0),
}
}

func (o *OriginStorage) SetAccount(addr common.Address) {
if !common.CalAccessList {
return
}
o.mu.Lock()
defer o.mu.Unlock()
if _, ok := o.Data[addr]; !ok {
o.Data[addr] = make(Storage)
}
}

func (o *OriginStorage) SetOrigin(addr common.Address, hash common.Hash) {
if !common.CalAccessList {
return
}
o.mu.Lock()
defer o.mu.Unlock()
if _, ok := o.Data[addr]; !ok {
o.Data[addr] = make(Storage)
}
o.Data[addr][hash] = common.Hash{}
}

func (o *OriginStorage) GetData(addr common.Address, hash common.Hash) common.Hash {
scf0220 marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := o.Data[addr]; !ok {
return common.Hash{}
}
return o.Data[addr][hash]
}

var emptyCodeHash = crypto.Keccak256(nil)

type Code []byte
Expand Down Expand Up @@ -182,6 +259,16 @@ func (s *stateObject) GetState(db Database, key common.Hash) common.Hash {
return s.GetCommittedState(db, key)
}

func (s *stateObject) getCommittedStateFromDB(db Database, key common.Hash) {
t := s.getTrie(db)
enc, _ := t.TryGet(key.Bytes())
var value common.Hash
if len(enc) > 0 {
_, content, _, _ := rlp.Split(enc)
value.SetBytes(content)
}
}

// GetCommittedState retrieves a value from the committed account storage trie.
func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Hash {
// If the fake storage is set, only lookup the state here(in the debugging mode)
Expand All @@ -195,6 +282,13 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
if value, cached := s.originStorage[key]; cached {
return value
}

if !common.CalAccessList {
load := s.db.OrigForPreLoad.GetData(s.address, key)
s.originStorage[key] = load
return load
}

// If no live objects are available, attempt to use snapshots
var (
enc []byte
Expand Down Expand Up @@ -222,6 +316,7 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
}
if enc, err = s.getTrie(db).TryGet(key.Bytes()); err != nil {
s.setError(err)
s.db.OrigForCalAccessList.SetOrigin(s.address, key)
return common.Hash{}
}
}
Expand All @@ -234,6 +329,7 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
value.SetBytes(content)
}
s.originStorage[key] = value
s.db.OrigForCalAccessList.SetOrigin(s.address, key)
return value
}

Expand Down
Loading