Skip to content

Commit

Permalink
Less use rawdb deprecated methods (erigontech#1861)
Browse files Browse the repository at this point in the history
* less use deprecated methods

* less use deprecated methods

* auto Dp limit

* auto Dp limit

* add test
  • Loading branch information
AskAlexSharov authored May 3, 2021
1 parent 5168287 commit f230ba1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestNewSimulatedBackend(t *testing.T) {
}
defer tx.Rollback()

statedb := state.New(state.NewPlainDBState(tx, rawdb.ReadCurrentBlockDeprecated(sim.database).NumberU64()))
statedb := state.New(state.NewPlainDBState(tx, rawdb.ReadCurrentHeader(sim.database).Number.Uint64()))
bal := statedb.GetBalance(testAddr)
if !bal.Eq(expectedBal) {
t.Errorf("expected balance for test address not received. expected: %v actual: %v", expectedBal, bal)
Expand Down
8 changes: 4 additions & 4 deletions cmd/rpctest/rpctest/fixState.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func FixState(chaindata string, url string) {
panic(err1)
}
defer tx.Rollback()
currentBlock := rawdb.ReadCurrentBlock(tx)
blockNum := currentBlock.NumberU64()
blockHash := currentBlock.Hash()
currentHeader := rawdb.ReadCurrentHeader(tx)
blockNum := currentHeader.Number.Uint64()
blockHash := currentHeader.Hash()
fmt.Printf("Block number: %d\n", blockNum)
fmt.Printf("Block root hash: %x\n", currentBlock.Root())
fmt.Printf("Block root hash: %x\n", currentHeader.Root)
reqID := 0
roots := make(map[common.Hash]*accounts.Account)
var client = &http.Client{
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/commands/check_change_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func CheckChangeSets(genesis *core.Genesis, blockNum uint64, chaindata string, h
if err != nil {
return err
}
block := rawdb.ReadBlockDeprecated(ethdb.NewRoTxDb(rwtx), blockHash, blockNum)
block := rawdb.ReadBlock(rwtx, blockHash, blockNum)
if block == nil {
break
}
Expand Down
46 changes: 23 additions & 23 deletions eth/protocols/eth/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
unknown[i] = byte(i)
}
getBlockHash := func(n uint64) common.Hash {
b, _ := rawdb.ReadBlockByNumberDeprecated(backend.db, n)
return b.Hash()
h, _ := rawdb.ReadCanonicalHash(backend.db, n)
return h

}
// Create a batch of tests for various scenarios
Expand Down Expand Up @@ -185,20 +185,20 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: 0}, Amount: 1},
[]common.Hash{getBlockHash(0)},
}, {
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64()}, Amount: 1},
[]common.Hash{rawdb.ReadCurrentBlockDeprecated(backend.db).Hash()},
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentHeader(backend.db).Number.Uint64()}, Amount: 1},
[]common.Hash{rawdb.ReadCurrentHeader(backend.db).Hash()},
},
// Ensure protocol limits are honored
{
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64() - 1}, Amount: limit + 10, Reverse: true},
backend.chain.GetBlockHashesFromHash(rawdb.ReadCurrentBlockDeprecated(backend.db).Hash(), limit),
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentHeader(backend.db).Number.Uint64() - 1}, Amount: limit + 10, Reverse: true},
backend.chain.GetBlockHashesFromHash(rawdb.ReadCurrentHeader(backend.db).Hash(), limit),
},
// Check that requesting more than available is handled gracefully
{
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64() - 4}, Skip: 3, Amount: 3},
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentHeader(backend.db).Number.Uint64() - 4}, Skip: 3, Amount: 3},
[]common.Hash{
getBlockHash(rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64() - 4),
getBlockHash(rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64()),
getBlockHash(rawdb.ReadCurrentHeader(backend.db).Number.Uint64() - 4),
getBlockHash(rawdb.ReadCurrentHeader(backend.db).Number.Uint64()),
},
}, {
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: 4}, Skip: 3, Amount: 3, Reverse: true},
Expand All @@ -209,10 +209,10 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
},
// Check that requesting more than available is handled gracefully, even if mid skip
{
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64() - 4}, Skip: 2, Amount: 3},
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentHeader(backend.db).Number.Uint64() - 4}, Skip: 2, Amount: 3},
[]common.Hash{
getBlockHash(rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64() - 4),
getBlockHash(rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64() - 1),
getBlockHash(rawdb.ReadCurrentHeader(backend.db).Number.Uint64() - 4),
getBlockHash(rawdb.ReadCurrentHeader(backend.db).Number.Uint64() - 1),
},
}, {
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: 4}, Skip: 2, Amount: 3, Reverse: true},
Expand Down Expand Up @@ -249,7 +249,7 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
&GetBlockHeadersPacket{Origin: HashOrNumber{Hash: unknown}, Amount: 1},
[]common.Hash{},
}, {
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64() + 1}, Amount: 1},
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: rawdb.ReadCurrentHeader(backend.db).Number.Uint64() + 1}, Amount: 1},
[]common.Hash{},
},
}
Expand All @@ -271,7 +271,7 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
}
// If the test used number origins, repeat with hashes as the too
if tt.query.Origin.Hash == (common.Hash{}) {
if origin, _ := rawdb.ReadBlockByNumberDeprecated(backend.db, tt.query.Origin.Number); origin != nil {
if origin := rawdb.ReadHeaderByNumber(backend.db, tt.query.Origin.Number); origin != nil {
tt.query.Origin.Hash, tt.query.Origin.Number = origin.Hash(), 0

if err := p2p.Send(peer.app, GetBlockHeadersMsg, tt.query); err != nil {
Expand All @@ -296,9 +296,9 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
peer, _ := newTestPeer("peer", protocol, backend)
defer peer.close()

block1, _ := rawdb.ReadBlockByNumberDeprecated(backend.db, 1)
block10, _ := rawdb.ReadBlockByNumberDeprecated(backend.db, 10)
block100, _ := rawdb.ReadBlockByNumberDeprecated(backend.db, 100)
block1 := rawdb.ReadHeaderByNumber(backend.db, 1)
block10 := rawdb.ReadHeaderByNumber(backend.db, 10)
block100 := rawdb.ReadHeaderByNumber(backend.db, 100)

// Create a batch of tests for various scenarios
limit := maxBodiesServe
Expand All @@ -312,9 +312,9 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
{10, nil, nil, 10}, // Multiple random blocks should be retrievable
{limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
{limit + 1, nil, nil, limit}, // No more than the possible block count should be returned
{0, []common.Hash{backend.chain.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
{0, []common.Hash{rawdb.ReadCurrentBlockDeprecated(backend.db).Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
{0, []common.Hash{{}}, []bool{false}, 0}, // A non existent block should not be returned
{0, []common.Hash{backend.chain.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
{0, []common.Hash{rawdb.ReadCurrentHeader(backend.db).Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
{0, []common.Hash{{}}, []bool{false}, 0}, // A non existent block should not be returned

// Existing and non-existing blocks interleaved should not cause problems
{0, []common.Hash{
Expand All @@ -337,7 +337,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
)
for j := 0; j < tt.random; j++ {
for {
num := rand.Int63n(int64(rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64()))
num := rand.Int63n(int64(rawdb.ReadCurrentHeader(backend.db).Number.Uint64()))
if !seen[num] {
seen[num] = true

Expand Down Expand Up @@ -419,8 +419,8 @@ func testGetBlockReceipts(t *testing.T, protocol uint) {
hashes []common.Hash
receipts []types.Receipts
)
for i := uint64(0); i <= rawdb.ReadCurrentBlockDeprecated(backend.db).NumberU64(); i++ {
block, _ := rawdb.ReadBlockByNumberDeprecated(backend.db, i)
for i := uint64(0); i <= rawdb.ReadCurrentHeader(backend.db).Number.Uint64(); i++ {
block := rawdb.ReadHeaderByNumber(backend.db, i)

hashes = append(hashes, block.Hash())
receipts = append(receipts, rawdb.ReadReceiptsByHash(backend.db, block.Hash()))
Expand Down
2 changes: 1 addition & 1 deletion ethdb/tx_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type TxDb struct {
len uint64
}

func WrapIntoTxDB(tx Tx) *TxDb {
func WrapIntoTxDB(tx RwTx) *TxDb {
return &TxDb{tx: tx, cursors: map[string]Cursor{}}
}

Expand Down
28 changes: 14 additions & 14 deletions turbo/stages/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,22 +1250,22 @@ func TestEIP161AccountRemoval(t *testing.T) {

blocks, _, err := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 3, func(i int, block *core.BlockGen) {
var (
tx types.Transaction
txn types.Transaction
err error
signer = types.LatestSigner(gspec.Config)
)
switch i {
case 0:
tx, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(uint256.Int), 21000, new(uint256.Int), nil), *signer, key)
txn, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(uint256.Int), 21000, new(uint256.Int), nil), *signer, key)
case 1:
tx, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(uint256.Int), 21000, new(uint256.Int), nil), *signer, key)
txn, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(uint256.Int), 21000, new(uint256.Int), nil), *signer, key)
case 2:
tx, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(uint256.Int), 21000, new(uint256.Int), nil), *signer, key)
txn, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(uint256.Int), 21000, new(uint256.Int), nil), *signer, key)
}
if err != nil {
t.Fatal(err)
}
block.AddTx(tx)
block.AddTx(txn)
}, false /* intemediateHashes */)
if err != nil {
t.Fatalf("generate blocks: %v", err)
Expand All @@ -1274,23 +1274,23 @@ func TestEIP161AccountRemoval(t *testing.T) {
if _, err = stagedsync.InsertBlockInStages(db, gspec.Config, &vm.Config{}, ethash.NewFaker(), blocks[0], true /* checkRoot */); err != nil {
t.Fatal(err)
}
if st := state.New(state.NewDbStateReader(db)); !st.Exist(theAddr) {
if st := state.New(state.NewPlainStateReader(db)); !st.Exist(theAddr) {
t.Error("expected account to exist")
}

// account needs to be deleted post eip 161
if _, err = stagedsync.InsertBlockInStages(db, gspec.Config, &vm.Config{}, ethash.NewFaker(), blocks[1], true /* checkRoot */); err != nil {
t.Fatal(err)
}
if st := state.New(state.NewDbStateReader(db)); st.Exist(theAddr) {
if st := state.New(state.NewPlainStateReader(db)); st.Exist(theAddr) {
t.Error("account should not exist")
}

// account mustn't be created post eip 161
if _, err = stagedsync.InsertBlockInStages(db, gspec.Config, &vm.Config{}, ethash.NewFaker(), blocks[2], true /* checkRoot */); err != nil {
t.Fatal(err)
}
if st := state.New(state.NewDbStateReader(db)); st.Exist(theAddr) {
if st := state.New(state.NewPlainStateReader(db)); st.Exist(theAddr) {
t.Error("account should not exist")
}
}
Expand Down Expand Up @@ -1328,18 +1328,18 @@ func TestDoubleAccountRemoval(t *testing.T) {
block.AddTx(tx)
theAddr = crypto.CreateAddress(bankAddress, nonce)
case 1:
tx, err := types.SignTx(types.NewTransaction(nonce, theAddr, new(uint256.Int), 90000, new(uint256.Int), input), *signer, bankKey)
txn, err := types.SignTx(types.NewTransaction(nonce, theAddr, new(uint256.Int), 90000, new(uint256.Int), input), *signer, bankKey)
assert.NoError(t, err)
block.AddTx(tx)
block.AddTx(txn)
case 2:
tx, err := types.SignTx(types.NewTransaction(nonce, theAddr, new(uint256.Int), 90000, new(uint256.Int), kill), *signer, bankKey)
txn, err := types.SignTx(types.NewTransaction(nonce, theAddr, new(uint256.Int), 90000, new(uint256.Int), kill), *signer, bankKey)
assert.NoError(t, err)
block.AddTx(tx)
block.AddTx(txn)

// sending kill messsage to an already suicided account
tx, err = types.SignTx(types.NewTransaction(nonce+1, theAddr, new(uint256.Int), 90000, new(uint256.Int), kill), *signer, bankKey)
txn, err = types.SignTx(types.NewTransaction(nonce+1, theAddr, new(uint256.Int), 90000, new(uint256.Int), kill), *signer, bankKey)
assert.NoError(t, err)
block.AddTx(tx)
block.AddTx(txn)
}
}, false /* intermediateHashes */)
if err != nil {
Expand Down

0 comments on commit f230ba1

Please sign in to comment.