2019-08-06 05:40:28 -05:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum 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,
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
2019-10-23 08:19:02 -05:00
|
|
|
"bufio"
|
2019-08-06 05:40:28 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// journalAccount is an account entry in a diffLayer's disk journal.
|
|
|
|
type journalAccount struct {
|
|
|
|
Hash common.Hash
|
|
|
|
Blob []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// journalStorage is an account's storage map in a diffLayer's disk journal.
|
|
|
|
type journalStorage struct {
|
|
|
|
Hash common.Hash
|
|
|
|
Keys []common.Hash
|
|
|
|
Vals [][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadDiffLayer reads the next sections of a snapshot journal, reconstructing a new
|
|
|
|
// diff and verifying that it can be linked to the requested parent.
|
|
|
|
func loadDiffLayer(parent snapshot, r *rlp.Stream) (snapshot, error) {
|
|
|
|
// Read the next diff journal entry
|
2019-11-22 05:23:49 -06:00
|
|
|
var root common.Hash
|
|
|
|
if err := r.Decode(&root); err != nil {
|
2019-08-06 05:40:28 -05:00
|
|
|
// The first read may fail with EOF, marking the end of the journal
|
|
|
|
if err == io.EOF {
|
|
|
|
return parent, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("load diff root: %v", err)
|
|
|
|
}
|
|
|
|
var accounts []journalAccount
|
|
|
|
if err := r.Decode(&accounts); err != nil {
|
|
|
|
return nil, fmt.Errorf("load diff accounts: %v", err)
|
|
|
|
}
|
|
|
|
accountData := make(map[common.Hash][]byte)
|
|
|
|
for _, entry := range accounts {
|
|
|
|
accountData[entry.Hash] = entry.Blob
|
|
|
|
}
|
|
|
|
var storage []journalStorage
|
|
|
|
if err := r.Decode(&storage); err != nil {
|
|
|
|
return nil, fmt.Errorf("load diff storage: %v", err)
|
|
|
|
}
|
|
|
|
storageData := make(map[common.Hash]map[common.Hash][]byte)
|
|
|
|
for _, entry := range storage {
|
|
|
|
slots := make(map[common.Hash][]byte)
|
|
|
|
for i, key := range entry.Keys {
|
|
|
|
slots[key] = entry.Vals[i]
|
|
|
|
}
|
|
|
|
storageData[entry.Hash] = slots
|
|
|
|
}
|
2019-11-22 05:23:49 -06:00
|
|
|
return loadDiffLayer(newDiffLayer(parent, root, accountData, storageData), r)
|
2019-08-06 05:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// journal is the internal version of Journal that also returns the journal file
|
|
|
|
// so subsequent layers know where to write to.
|
|
|
|
func (dl *diffLayer) journal() (io.WriteCloser, error) {
|
|
|
|
// If we've reached the bottom, open the journal
|
|
|
|
var writer io.WriteCloser
|
|
|
|
if parent, ok := dl.parent.(*diskLayer); ok {
|
|
|
|
file, err := os.Create(parent.journal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
writer = file
|
|
|
|
}
|
|
|
|
// If we haven't reached the bottom yet, journal the parent first
|
|
|
|
if writer == nil {
|
|
|
|
file, err := dl.parent.(*diffLayer).journal()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
writer = file
|
|
|
|
}
|
2019-10-23 08:19:02 -05:00
|
|
|
dl.lock.RLock()
|
|
|
|
defer dl.lock.RUnlock()
|
|
|
|
|
|
|
|
if dl.stale {
|
|
|
|
writer.Close()
|
|
|
|
return nil, ErrSnapshotStale
|
|
|
|
}
|
2019-08-06 05:40:28 -05:00
|
|
|
// Everything below was journalled, persist this layer too
|
2019-11-22 05:23:49 -06:00
|
|
|
buf := bufio.NewWriter(writer)
|
2019-10-23 08:19:02 -05:00
|
|
|
if err := rlp.Encode(buf, dl.root); err != nil {
|
|
|
|
buf.Flush()
|
2019-08-06 05:40:28 -05:00
|
|
|
writer.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
accounts := make([]journalAccount, 0, len(dl.accountData))
|
|
|
|
for hash, blob := range dl.accountData {
|
|
|
|
accounts = append(accounts, journalAccount{Hash: hash, Blob: blob})
|
|
|
|
}
|
2019-10-23 08:19:02 -05:00
|
|
|
if err := rlp.Encode(buf, accounts); err != nil {
|
|
|
|
buf.Flush()
|
2019-08-06 05:40:28 -05:00
|
|
|
writer.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storage := make([]journalStorage, 0, len(dl.storageData))
|
|
|
|
for hash, slots := range dl.storageData {
|
|
|
|
keys := make([]common.Hash, 0, len(slots))
|
|
|
|
vals := make([][]byte, 0, len(slots))
|
|
|
|
for key, val := range slots {
|
|
|
|
keys = append(keys, key)
|
|
|
|
vals = append(vals, val)
|
|
|
|
}
|
|
|
|
storage = append(storage, journalStorage{Hash: hash, Keys: keys, Vals: vals})
|
|
|
|
}
|
2019-10-23 08:19:02 -05:00
|
|
|
if err := rlp.Encode(buf, storage); err != nil {
|
|
|
|
buf.Flush()
|
2019-08-06 05:40:28 -05:00
|
|
|
writer.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-23 08:19:02 -05:00
|
|
|
buf.Flush()
|
2019-08-06 05:40:28 -05:00
|
|
|
return writer, nil
|
|
|
|
}
|