Remove tds in few tests (#1657)

This commit is contained in:
Alex Sharov 2021-04-02 12:14:43 +07:00 committed by GitHub
parent 7b7c341602
commit fbd9690251
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 141 deletions

View File

@ -3,7 +3,6 @@ package state
import ( import (
"context" "context"
"fmt" "fmt"
"math/big"
"math/rand" "math/rand"
"reflect" "reflect"
"sort" "sort"
@ -283,129 +282,6 @@ func randomAccount(t *testing.T) (*accounts.Account, common.Address, common.Hash
return &acc, addr, addrHash return &acc, addr, addrHash
} }
func TestUnwindTruncateHistory(t *testing.T) {
t.Skip("tds.Unwind is not supported")
db := ethdb.NewMemDatabase()
defer db.Close()
mutDB := db.NewBatch()
tds := NewTrieDbState(common.Hash{}, db, 1)
ctx := context.Background()
acc1 := accounts.NewAccount()
acc := &acc1
acc.Initialised = true
var addr common.Address = common.HexToAddress("0x1234567890")
acc.Balance.SetUint64(0)
// We will so that balance (in wei) always matches the block number
// We will also insert a extra storage item every block
for blockNumber := uint64(1); blockNumber < uint64(100); blockNumber++ {
tds.StartNewBuffer()
newAcc := acc.SelfCopy()
newAcc.Balance.SetUint64(blockNumber)
tds.SetBlockNr(blockNumber)
txWriter := tds.TrieStateWriter()
blockWriter := tds.DbStateWriter()
if blockNumber == 1 {
err := txWriter.CreateContract(addr)
if err != nil {
t.Fatal(err)
}
newAcc.Incarnation = FirstContractIncarnation
}
var oldValue uint256.Int
var newValue uint256.Int
newValue[0] = 1
var location common.Hash
location.SetBytes(big.NewInt(int64(blockNumber)).Bytes())
if err := txWriter.WriteAccountStorage(ctx, addr, newAcc.Incarnation, &location, &oldValue, &newValue); err != nil {
t.Fatal(err)
}
if err := txWriter.UpdateAccountData(ctx, addr, acc /* original */, newAcc /* new account */); err != nil {
t.Fatal(err)
}
if _, err := tds.ComputeTrieRoots(); err != nil {
t.Fatal(err)
}
if blockNumber == 1 {
err := blockWriter.CreateContract(addr)
if err != nil {
t.Fatal(err)
}
}
if err := blockWriter.WriteAccountStorage(ctx, addr, newAcc.Incarnation, &location, &oldValue, &newValue); err != nil {
t.Fatal(err)
}
if err := blockWriter.UpdateAccountData(ctx, addr, acc /* original */, newAcc /* new account */); err != nil {
t.Fatal(err)
}
if err := blockWriter.WriteChangeSets(); err != nil {
t.Fatal(err)
}
if err := blockWriter.WriteHistory(); err != nil {
t.Fatal(err)
}
acc = newAcc
}
// Recreate tds not to rely on the trie
tds = NewTrieDbState(tds.LastRoot(), mutDB, tds.blockNr)
a, err := tds.ReadAccountData(addr)
if err != nil {
t.Fatal(err)
}
if a.Balance.Uint64() != 99 {
t.Errorf("wrong balance on the account, expected %d, got %d", 99, a.Balance.Uint64())
}
// Check 100 storage locations
for l := 0; l <= 100; l++ {
var location common.Hash
location.SetBytes(big.NewInt(int64(l)).Bytes())
enc, err1 := tds.ReadAccountStorage(addr, a.Incarnation, &location)
if err1 != nil {
t.Fatal(err1)
}
if l > 0 && l < 100 {
if len(enc) == 0 {
t.Errorf("expected non-empty storage at location %d, got empty", l)
}
} else {
if len(enc) > 0 {
t.Errorf("expected empty storage at location %d, got non-empty", l)
}
}
}
// New we are goint to unwind 50 blocks back and check the balance
if err = tds.UnwindTo(50); err != nil {
t.Fatal(err)
}
if err = mutDB.Commit(); err != nil {
t.Fatal(err)
}
a, err = tds.ReadAccountData(addr)
if err != nil {
t.Fatal(err)
}
if a.Balance.Uint64() != 50 {
t.Errorf("wrong balance on the account, expected %d, got %d", 50, a.Balance.Uint64())
}
// Check 100 storage locations
for l := 0; l <= 100; l++ {
var location common.Hash
location.SetBytes(big.NewInt(int64(l)).Bytes())
enc, err1 := tds.ReadAccountStorage(addr, a.Incarnation, &location)
if err1 != nil {
t.Fatal(err1)
}
if l > 0 && l <= 50 {
if len(enc) == 0 {
t.Errorf("expected non-empty storage at location %d, got empty", l)
}
} else {
if len(enc) > 0 {
t.Errorf("expected empty storage at location %d, got non-empty", l)
}
}
}
}
/* /*
before 3: before 3:
addr1(f22b):"" addr1(f22b):""
@ -808,7 +684,6 @@ func TestWalkAsOfUsingFixedBytesStatePlain(t *testing.T) {
func TestWalkAsOfAccountPlain(t *testing.T) { func TestWalkAsOfAccountPlain(t *testing.T) {
db := ethdb.NewMemDatabase() db := ethdb.NewMemDatabase()
defer db.Close() defer db.Close()
tds := NewTrieDbState(common.Hash{}, db, 1)
emptyValAcc := accounts.NewAccount() emptyValAcc := accounts.NewAccount()
emptyVal := make([]byte, emptyValAcc.EncodingLengthForStorage()) emptyVal := make([]byte, emptyValAcc.EncodingLengthForStorage())
emptyValAcc.EncodeForStorage(emptyVal) emptyValAcc.EncodeForStorage(emptyVal)
@ -842,7 +717,7 @@ func TestWalkAsOfAccountPlain(t *testing.T) {
Changes: make([]changeset.Change, 0), Changes: make([]changeset.Change, 0),
} }
writeBlockData(t, tds, 3, []accData{ writeBlockData(t, NewPlainStateWriter(db, db, 3), []accData{
{ {
addr: addrs[0], addr: addrs[0],
oldVal: &emptyValAcc, oldVal: &emptyValAcc,
@ -860,7 +735,7 @@ func TestWalkAsOfAccountPlain(t *testing.T) {
}, },
}) })
writeBlockData(t, tds, 5, []accData{ writeBlockData(t, NewPlainStateWriter(db, db, 5), []accData{
{ {
addr: addrs[0], addr: addrs[0],
oldVal: block3ValAcc, oldVal: block3ValAcc,
@ -962,7 +837,6 @@ func TestWalkAsOfAccountPlain(t *testing.T) {
func TestWalkAsOfAccountPlain_WithChunks(t *testing.T) { func TestWalkAsOfAccountPlain_WithChunks(t *testing.T) {
db := ethdb.NewMemDatabase() db := ethdb.NewMemDatabase()
defer db.Close() defer db.Close()
tds := NewTrieDbState(common.Hash{}, db, 1)
emptyValAcc := accounts.NewAccount() emptyValAcc := accounts.NewAccount()
emptyVal := make([]byte, emptyValAcc.EncodingLengthForStorage()) emptyVal := make([]byte, emptyValAcc.EncodingLengthForStorage())
emptyValAcc.EncodeForStorage(emptyVal) emptyValAcc.EncodeForStorage(emptyVal)
@ -1000,7 +874,7 @@ func TestWalkAsOfAccountPlain_WithChunks(t *testing.T) {
var addr1New, addr2New, addr3New *accounts.Account var addr1New, addr2New, addr3New *accounts.Account
writeBlockData(t, tds, 1, []accData{ writeBlockData(t, NewPlainStateWriter(db, db, 1), []accData{
{ {
addr: addrs[0], addr: addrs[0],
oldVal: &emptyValAcc, oldVal: &emptyValAcc,
@ -1025,7 +899,7 @@ func TestWalkAsOfAccountPlain_WithChunks(t *testing.T) {
addr2New.Nonce = uint64(i) addr2New.Nonce = uint64(i)
addr3New = addr3Old.SelfCopy() addr3New = addr3Old.SelfCopy()
addr3New.Nonce = uint64(i) addr3New.Nonce = uint64(i)
writeBlockData(t, tds, uint64(i), []accData{ writeBlockData(t, NewPlainStateWriter(db, db, uint64(i)), []accData{
{ {
addr: addrs[0], addr: addrs[0],
oldVal: addr1Old, oldVal: addr1Old,
@ -1054,7 +928,7 @@ func TestWalkAsOfAccountPlain_WithChunks(t *testing.T) {
addr3New = addr3Old.SelfCopy() addr3New = addr3Old.SelfCopy()
addr3New.Nonce = 1100 addr3New.Nonce = 1100
writeBlockData(t, tds, 1100, []accData{ writeBlockData(t, NewPlainStateWriter(db, db, 1100), []accData{
{ {
addr: addrs[0], addr: addrs[0],
oldVal: addr1Old, oldVal: addr1Old,
@ -1258,10 +1132,7 @@ type accData struct {
newVal *accounts.Account newVal *accounts.Account
} }
func writeBlockData(t *testing.T, tds *TrieDbState, blockNum uint64, data []accData) { func writeBlockData(t *testing.T, blockWriter *PlainStateWriter, data []accData) {
tds.SetBlockNr(blockNum)
var blockWriter = tds.PlainStateWriter()
for i := range data { for i := range data {
if data[i].newVal != nil { if data[i].newVal != nil {
if err := blockWriter.UpdateAccountData(context.Background(), data[i].addr, data[i].oldVal, data[i].newVal); err != nil { if err := blockWriter.UpdateAccountData(context.Background(), data[i].addr, data[i].oldVal, data[i].newVal); err != nil {

View File

@ -93,7 +93,7 @@ func TestState(t *testing.T) {
} }
// Transactions with gasLimit above this value will not get a VM trace on failure. // Transactions with gasLimit above this value will not get a VM trace on failure.
const traceErrorLimit = 400000 const traceErrorLimit = 4000000000
func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) { func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
// Use config from command line arguments. // Use config from command line arguments.

View File

@ -185,11 +185,13 @@ func (t *StateTest) RunNoVerify(ctx context.Context, tx ethdb.Database, subtest
ctx = config.WithEIPsFlags(ctx, big.NewInt(int64(writeBlockNr))) ctx = config.WithEIPsFlags(ctx, big.NewInt(int64(writeBlockNr)))
_, tds, err := MakePreState(context.Background(), tx, t.json.Pre, readBlockNr) _, tds, err := MakePreState(context.Background(), tx, t.json.Pre, readBlockNr)
//_, err = MakePreState2(context.Background(), tx, t.json.Pre, readBlockNr)
if err != nil { if err != nil {
return nil, common.Hash{}, UnsupportedForkError{subtest.Fork} return nil, common.Hash{}, UnsupportedForkError{subtest.Fork}
} }
statedb := state.New(state.NewDbStateReader(tx)) statedb := state.New(state.NewDbStateReader(tx))
tds.StartNewBuffer() tds.StartNewBuffer()
w := state.NewDbStateWriter(tx, readBlockNr+1)
post := t.json.Post[subtest.Fork][subtest.Index] post := t.json.Post[subtest.Fork][subtest.Index]
msg, err := t.json.Tx.toMessage(post) msg, err := t.json.Tx.toMessage(post)
@ -211,7 +213,6 @@ func (t *StateTest) RunNoVerify(ctx context.Context, tx ethdb.Database, subtest
statedb.RevertToSnapshot(snapshot) statedb.RevertToSnapshot(snapshot)
} }
w := state.NewDbStateWriter(tx, block.NumberU64())
// Commit block // Commit block
if err = statedb.FinalizeTx(ctx, tds.TrieStateWriter()); err != nil { if err = statedb.FinalizeTx(ctx, tds.TrieStateWriter()); err != nil {
return nil, common.Hash{}, err return nil, common.Hash{}, err
@ -273,7 +274,7 @@ func MakePreState(ctx context.Context, db ethdb.Database, accounts core.GenesisA
} }
func MakePreState2(ctx context.Context, db ethdb.Database, accounts core.GenesisAlloc, blockNr uint64) (*state.IntraBlockState, error) { func MakePreState2(ctx context.Context, db ethdb.Database, accounts core.GenesisAlloc, blockNr uint64) (*state.IntraBlockState, error) {
r, w := state.NewDbStateReader(db), state.NewDbStateWriter(db, blockNr) r, _ := state.NewDbStateReader(db), state.NewDbStateWriter(db, blockNr)
statedb := state.New(r) statedb := state.New(r)
for addr, a := range accounts { for addr, a := range accounts {
statedb.SetCode(addr, a.Code) statedb.SetCode(addr, a.Code)
@ -287,9 +288,9 @@ func MakePreState2(ctx context.Context, db ethdb.Database, accounts core.Genesis
} }
} }
// Commit and re-open to start with a clean state. // Commit and re-open to start with a clean state.
if err := statedb.FinalizeTx(ctx, w); err != nil { //if err := statedb.FinalizeTx(ctx, state.NewNoopWriter()); err != nil {
return nil, err // return nil, err
} //}
if err := statedb.CommitBlock(ctx, state.NewDbStateWriter(db, blockNr+1)); err != nil { if err := statedb.CommitBlock(ctx, state.NewDbStateWriter(db, blockNr+1)); err != nil {
return nil, err return nil, err
} }