Skip to content

Commit

Permalink
tx.RwCursor() to return err (first step of removing lazy cursors) (#1656
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AskAlexSharov committed Apr 6, 2021
1 parent 5fcb5e1 commit 946ebfb
Show file tree
Hide file tree
Showing 37 changed files with 731 additions and 430 deletions.
13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
run:
deadline: 10m

linters:
disable-all: true
enable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- unused
- varcheck

linters-settings:
govet:
disable:
Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,11 @@ lint: lintci

lintci: mdbx
@echo "--> Running linter for code"
@./build/bin/golangci-lint run \
--build-tags="mdbx"
@./build/bin/golangci-lint run --build-tags="mdbx" --config ./.golangci.yml

lintci-deps:
rm -f ./build/bin/golangci-lint
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ./build/bin v1.39.0
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ./build/bin v1.38.0

clean:
env GO111MODULE=on go clean -cache
Expand Down
62 changes: 48 additions & 14 deletions cmd/hack/db/lmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func nothing(kv ethdb.RwKV, _ ethdb.RwTx) (bool, error) {

// Generates a database with single table and two key-value pair in "t" DBI, and returns the file name
func generate2(tx ethdb.RwTx, entries int) error {
c := tx.RwCursor("t")
c, err := tx.RwCursor("t")
if err != nil {
return err
}
defer c.Close()
for i := 0; i < entries; i++ {
k := fmt.Sprintf("%05d", i)
Expand All @@ -66,7 +69,10 @@ func generate3(_ ethdb.RwKV, tx ethdb.RwTx) (bool, error) {

// Generates a database with one table, containing 1 short and 1 long (more than one page) values
func generate4(_ ethdb.RwKV, tx ethdb.RwTx) (bool, error) {
c := tx.RwCursor("t")
c, err := tx.RwCursor("t")
if err != nil {
return false, err
}
defer c.Close()
if err := c.Append([]byte("k1"), []byte("very_short_value")); err != nil {
return false, err
Expand All @@ -79,7 +85,10 @@ func generate4(_ ethdb.RwKV, tx ethdb.RwTx) (bool, error) {

// Generates a database with one table, containing some DupSort values
func generate5(_ ethdb.RwKV, tx ethdb.RwTx) (bool, error) {
c := tx.RwCursorDupSort("t")
c, err := tx.RwCursorDupSort("t")
if err != nil {
return false, err
}
defer c.Close()
if err := c.AppendDup([]byte("key1"), []byte("value11")); err != nil {
return false, err
Expand All @@ -104,7 +113,10 @@ func generate5(_ ethdb.RwKV, tx ethdb.RwTx) (bool, error) {

// Generate a database with one table, containing lots of dupsort values
func generate6(_ ethdb.RwKV, tx ethdb.RwTx) (bool, error) {
c := tx.RwCursorDupSort("t")
c, err := tx.RwCursorDupSort("t")
if err != nil {
return false, err
}
defer c.Close()
for i := 0; i < 1000; i++ {
v := fmt.Sprintf("dupval_%05d", i)
Expand Down Expand Up @@ -132,9 +144,15 @@ func dropT(_ ethdb.RwKV, tx ethdb.RwTx) (bool, error) {
}

func generate7(_ ethdb.RwKV, tx ethdb.RwTx) (bool, error) {
c1 := tx.RwCursor("t1")
c1, err := tx.RwCursor("t1")
if err != nil {
return false, err
}
defer c1.Close()
c2 := tx.RwCursor("t2")
c2, err := tx.RwCursor("t2")
if err != nil {
return false, err
}
defer c2.Close()
for i := 0; i < 1000; i++ {
k := fmt.Sprintf("%05d", i)
Expand Down Expand Up @@ -177,7 +195,10 @@ func generate9(tx ethdb.RwTx, entries int) error {
var cs []ethdb.RwCursor
for i := 0; i < 100; i++ {
k := fmt.Sprintf("table_%05d", i)
c := tx.RwCursor(k)
c, err := tx.RwCursor(k)
if err != nil {
return err
}
defer c.Close()
cs = append(cs, c)
}
Expand Down Expand Up @@ -217,7 +238,10 @@ func dropGradually(kv ethdb.RwKV, tx ethdb.RwTx) (bool, error) {
}

func change1(tx ethdb.RwTx) (bool, error) {
c := tx.RwCursor("t")
c, err := tx.RwCursor("t")
if err != nil {
return false, err
}
defer c.Close()
for i := 0; i < 1000; i++ {
k := fmt.Sprintf("%05d", i)
Expand All @@ -229,7 +253,10 @@ func change1(tx ethdb.RwTx) (bool, error) {
}

func change2(tx ethdb.RwTx) (bool, error) {
c := tx.RwCursor("t")
c, err := tx.RwCursor("t")
if err != nil {
return false, err
}
defer c.Close()
for i := 0; i < 1000; i++ {
k := fmt.Sprintf("%05d", i)
Expand All @@ -241,7 +268,10 @@ func change2(tx ethdb.RwTx) (bool, error) {
}

func change3(tx ethdb.RwTx) (bool, error) {
c := tx.RwCursor("t")
c, err := tx.RwCursor("t")
if err != nil {
return false, err
}
defer c.Close()
for i := 0; i < 1000; i++ {
k := fmt.Sprintf("%05d", i)
Expand All @@ -254,15 +284,19 @@ func change3(tx ethdb.RwTx) (bool, error) {

func launchReader(kv ethdb.RwKV, tx ethdb.Tx, expectVal string, startCh chan struct{}, errorCh chan error) (bool, error) {
tx.Rollback()
tx1, err := kv.Begin(context.Background())
if err != nil {
return false, err
tx1, err1 := kv.Begin(context.Background())
if err1 != nil {
return false, err1
}
// Wait for the signal to start reading
go func() {
defer tx1.Rollback()
<-startCh
c := tx1.Cursor("t")
c, err := tx1.Cursor("t")
if err != nil {
errorCh <- err
return
}
defer c.Close()
for i := 0; i < 1000; i++ {
k := fmt.Sprintf("%05d", i)
Expand Down
76 changes: 61 additions & 15 deletions cmd/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ func accountSavings(db ethdb.RwKV) (int, int) {
emptyRoots := 0
emptyCodes := 0
tool.Check(db.View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.HashedAccountsBucket)
c, err := tx.Cursor(dbutils.HashedAccountsBucket)
if err != nil {
return err
}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
Expand Down Expand Up @@ -601,7 +604,10 @@ func dbSlice(chaindata string, bucket string, prefix []byte) {
db := ethdb.MustOpen(chaindata)
defer db.Close()
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(bucket)
c, err := tx.Cursor(bucket)
if err != nil {
return err
}
for k, v, err := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v, err = c.Next() {
if err != nil {
return err
Expand Down Expand Up @@ -773,7 +779,10 @@ func readAccount(chaindata string, account common.Address) error {
}
fmt.Printf("CodeHash:%x\nIncarnation:%d\n", a.CodeHash, a.Incarnation)
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.PlainStateBucket)
c, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
return err
}
for k, v, e := c.Seek(account.Bytes()); k != nil && e == nil; k, v, e = c.Next() {
if e != nil {
return e
Expand Down Expand Up @@ -835,10 +844,16 @@ func repairCurrent() {
defer currentDb.Close()
tool.Check(historyDb.ClearBuckets(dbutils.HashedStorageBucket))
tool.Check(historyDb.RwKV().Update(context.Background(), func(tx ethdb.RwTx) error {
newB := tx.RwCursor(dbutils.HashedStorageBucket)
newB, err := tx.RwCursor(dbutils.HashedStorageBucket)
if err != nil {
return err
}
count := 0
if err := currentDb.RwKV().View(context.Background(), func(ctx ethdb.Tx) error {
c := ctx.Cursor(dbutils.HashedStorageBucket)
c, err := ctx.Cursor(dbutils.HashedStorageBucket)
if err != nil {
return err
}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
Expand All @@ -861,7 +876,10 @@ func dumpStorage() {
db := ethdb.MustOpen(node.DefaultDataDir() + "/geth/chaindata")
defer db.Close()
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.StorageHistoryBucket)
c, err := tx.Cursor(dbutils.StorageHistoryBucket)
if err != nil {
return err
}
return ethdb.ForEach(c, func(k, v []byte) (bool, error) {
fmt.Printf("%x %x\n", k, v)
return true, nil
Expand All @@ -880,7 +898,10 @@ func printBucket(chaindata string) {
fb := bufio.NewWriter(f)
defer fb.Flush()
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.StorageHistoryBucket)
c, err := tx.Cursor(dbutils.StorageHistoryBucket)
if err != nil {
return err
}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
Expand Down Expand Up @@ -1189,7 +1210,10 @@ func changeSetStats(chaindata string, block1, block2 uint64) error {
stAccounts := 0
stStorage := 0
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.PlainStateBucket)
c, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
return err
}
k, _, e := c.First()
for ; k != nil && e == nil; k, _, e = c.Next() {
if len(k) > 28 {
Expand Down Expand Up @@ -1278,7 +1302,10 @@ func supply(chaindata string) error {
supply := uint256.NewInt()
var a accounts.Account
if err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.PlainStateBucket)
c, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
return err
}
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
Expand Down Expand Up @@ -1308,7 +1335,10 @@ func extractCode(chaindata string) error {
defer db.Close()
var contractCount int
if err1 := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.CodeBucket)
c, err := tx.Cursor(dbutils.CodeBucket)
if err != nil {
return err
}
// This is a mapping of CodeHash => Byte code
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
Expand All @@ -1334,7 +1364,10 @@ func iterateOverCode(chaindata string) error {
var codeHashTotalLength int
var codeTotalLength int // Total length of all byte code (just to illustrate iterating)
if err1 := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.PlainContractCodeBucket)
c, err := tx.Cursor(dbutils.PlainContractCodeBucket)
if err != nil {
return err
}
// This is a mapping of contractAddress + incarnation => CodeHash
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
Expand All @@ -1343,7 +1376,10 @@ func iterateOverCode(chaindata string) error {
contractKeyTotalLength += len(k)
contractValTotalLength += len(v)
}
c = tx.Cursor(dbutils.CodeBucket)
c, err = tx.Cursor(dbutils.CodeBucket)
if err != nil {
return err
}
// This is a mapping of CodeHash => Byte code
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
Expand Down Expand Up @@ -1379,7 +1415,11 @@ func mint(chaindata string, block uint64) error {
blockEncoded := dbutils.EncodeBlockNumber(block)
canonical := make(map[common.Hash]struct{})
if err1 := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
c := tx.Cursor(dbutils.HeaderCanonicalBucket)
c, err := tx.Cursor(dbutils.HeaderCanonicalBucket)
if err != nil {
return err
}

// This is a mapping of contractAddress + incarnation => CodeHash
for k, v, err := c.Seek(blockEncoded); k != nil; k, v, err = c.Next() {
if err != nil {
Expand All @@ -1392,7 +1432,10 @@ func mint(chaindata string, block uint64) error {
}
}
log.Info("Read canonical hashes", "count", len(canonical))
c = tx.Cursor(dbutils.BlockBodyPrefix)
c, err = tx.Cursor(dbutils.BlockBodyPrefix)
if err != nil {
return err
}
var prevBlock uint64
var burntGas uint64
for k, _, err := c.Seek(blockEncoded); k != nil; k, _, err = c.Next() {
Expand Down Expand Up @@ -1547,7 +1590,10 @@ func extractBodies(chaindata string, block uint64) error {
return err
}
defer tx.Rollback()
c := tx.(ethdb.HasTx).Tx().Cursor(dbutils.BlockBodyPrefix)
c, err := tx.(ethdb.HasTx).Tx().Cursor(dbutils.BlockBodyPrefix)
if err != nil {
return err
}
defer c.Close()
blockEncoded := dbutils.EncodeBlockNumber(block)
for k, _, err := c.Seek(blockEncoded); k != nil; k, _, err = c.Next() {
Expand Down
Loading

0 comments on commit 946ebfb

Please sign in to comment.