/* Copyright 2021 Erigon contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package mdbx_test import ( "context" "fmt" "net" "runtime" "testing" "github.com/ledgerwatch/erigon-lib/gointerfaces" "github.com/ledgerwatch/erigon-lib/gointerfaces/remote" "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon-lib/kv/mdbx" "github.com/ledgerwatch/erigon-lib/kv/remotedb" "github.com/ledgerwatch/erigon-lib/kv/remotedbserver" "github.com/ledgerwatch/log/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/grpc/test/bufconn" ) func TestSequence(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("fix me on win please") } writeDBs, _ := setupDatabases(t, log.New(), func(defaultBuckets kv.TableCfg) kv.TableCfg { return defaultBuckets }) ctx := context.Background() for _, db := range writeDBs { db := db tx, err := db.BeginRw(ctx) require.NoError(t, err) defer tx.Rollback() i, err := tx.ReadSequence(kv.ChaindataTables[0]) require.NoError(t, err) require.Equal(t, uint64(0), i) i, err = tx.IncrementSequence(kv.ChaindataTables[0], 1) require.NoError(t, err) require.Equal(t, uint64(0), i) i, err = tx.IncrementSequence(kv.ChaindataTables[0], 6) require.NoError(t, err) require.Equal(t, uint64(1), i) i, err = tx.IncrementSequence(kv.ChaindataTables[0], 1) require.NoError(t, err) require.Equal(t, uint64(7), i) i, err = tx.ReadSequence(kv.ChaindataTables[1]) require.NoError(t, err) require.Equal(t, uint64(0), i) i, err = tx.IncrementSequence(kv.ChaindataTables[1], 1) require.NoError(t, err) require.Equal(t, uint64(0), i) i, err = tx.IncrementSequence(kv.ChaindataTables[1], 6) require.NoError(t, err) require.Equal(t, uint64(1), i) i, err = tx.IncrementSequence(kv.ChaindataTables[1], 1) require.NoError(t, err) require.Equal(t, uint64(7), i) tx.Rollback() } } func TestManagedTx(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("fix me on win please") } logger := log.New() defaultConfig := kv.ChaindataTablesCfg defer func() { kv.ChaindataTablesCfg = defaultConfig }() bucketID := 0 bucket1 := kv.ChaindataTables[bucketID] bucket2 := kv.ChaindataTables[bucketID+1] writeDBs, readDBs := setupDatabases(t, logger, func(defaultBuckets kv.TableCfg) kv.TableCfg { return map[string]kv.TableCfgItem{ bucket1: { Flags: kv.DupSort, AutoDupSortKeysConversion: true, DupToLen: 4, DupFromLen: 6, }, bucket2: { Flags: 0, }, } }) ctx := context.Background() for _, db := range writeDBs { db := db tx, err := db.BeginRw(ctx) require.NoError(t, err) defer tx.Rollback() c, err := tx.RwCursor(bucket1) require.NoError(t, err) c1, err := tx.RwCursor(bucket2) require.NoError(t, err) require.NoError(t, c.Append([]byte{0}, []byte{1})) require.NoError(t, c1.Append([]byte{0}, []byte{1})) require.NoError(t, c.Append([]byte{0, 0, 0, 0, 0, 1}, []byte{1})) // prefixes of len=FromLen for DupSort test (other keys must be