github.com/amazechain/amc@v0.1.3/modules/rawdb/accessors_reward_test.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"context"
    21  	"github.com/amazechain/amc/modules"
    22  	"github.com/amazechain/amc/params"
    23  	"github.com/c2h5oh/datasize"
    24  	"github.com/ledgerwatch/erigon-lib/common/cmp"
    25  	"github.com/ledgerwatch/erigon-lib/kv"
    26  	"github.com/ledgerwatch/erigon-lib/kv/mdbx"
    27  	"golang.org/x/sync/semaphore"
    28  	"runtime"
    29  
    30  	log2 "github.com/ledgerwatch/log/v3"
    31  )
    32  
    33  //func TestPutReward(t *testing.T) {
    34  //	db, err := OpenDatabase()
    35  //	if err != nil {
    36  //		t.Error(err)
    37  //	}
    38  //	tx, err := db.BeginRw(context.TODO())
    39  //	if err != nil {
    40  //		t.Error(err)
    41  //	}
    42  //	if err := tx.CreateBucket("Reward"); err != nil {
    43  //		t.Error(err)
    44  //	}
    45  //	type args struct {
    46  //		key string
    47  //		val *RewardEntry
    48  //	}
    49  //	tests := []struct {
    50  //		name    string
    51  //		args    args
    52  //		wantErr bool
    53  //	}{{
    54  //		name: "t1",
    55  //		args: args{
    56  //			key: "qwe123",
    57  //			val: &RewardEntry{
    58  //				Address:   []byte("123"),
    59  //				Value:     uint256.NewInt(123),
    60  //				Sediment:  uint256.NewInt(123),
    61  //				Timestamp: 123,
    62  //			},
    63  //		},
    64  //	}, {
    65  //		name: "t2",
    66  //		args: args{
    67  //			key: "qwe456",
    68  //			val: &RewardEntry{
    69  //				Address:   []byte("456"),
    70  //				Value:     uint256.NewInt(456),
    71  //				Sediment:  uint256.NewInt(456),
    72  //				Timestamp: 456,
    73  //			},
    74  //		}}, {
    75  //		name: "t3",
    76  //		args: args{
    77  //			key: "qwe789",
    78  //			val: &RewardEntry{
    79  //				Address:   []byte("789"),
    80  //				Value:     uint256.NewInt(789),
    81  //				Sediment:  uint256.NewInt(789),
    82  //				Timestamp: 789,
    83  //			},
    84  //		},
    85  //	}}
    86  //
    87  //	for _, tt := range tests {
    88  //		if err := PutEpochReward(tx, tt.args.key, tt.args.val); (err != nil) != tt.wantErr {
    89  //			t.Errorf("PutReward() error = %v, wantErr %v", err, tt.wantErr)
    90  //		}
    91  //	}
    92  //
    93  //	m, err := GetRewards(tx, "qwe")
    94  //	if err != nil {
    95  //		t.Error(err)
    96  //	}
    97  //
    98  //	fmt.Println(m)
    99  //	t.Log(m)
   100  //}
   101  
   102  func OpenDatabase() (kv.RwDB, error) {
   103  	var chainKv kv.RwDB
   104  	var err error
   105  	logger := log2.New()
   106  
   107  	dbPath := "./mdbx.db"
   108  
   109  	var openFunc = func(exclusive bool) (kv.RwDB, error) {
   110  		//if config.Http.DBReadConcurrency > 0 {
   111  		//	roTxLimit = int64(config.Http.DBReadConcurrency)
   112  		//}
   113  		roTxsLimiter := semaphore.NewWeighted(int64(cmp.Max(32, runtime.GOMAXPROCS(-1)*8))) // 1 less than max to allow unlocking to happen
   114  		opts := mdbx.NewMDBX(logger).
   115  			WriteMergeThreshold(4 * 8192).
   116  			Path(dbPath).Label(kv.ChainDB).
   117  			DBVerbosity(kv.DBVerbosityLvl(2)).RoTxsLimiter(roTxsLimiter)
   118  		if exclusive {
   119  			opts = opts.Exclusive()
   120  		}
   121  
   122  		modules.AmcInit()
   123  		kv.ChaindataTablesCfg = modules.AmcTableCfg
   124  
   125  		opts = opts.MapSize(8 * datasize.TB)
   126  		return opts.Open()
   127  	}
   128  	chainKv, err = openFunc(false)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	if err = chainKv.Update(context.Background(), func(tx kv.RwTx) (err error) {
   134  		return params.SetAmcVersion(tx, params.VersionKeyCreated)
   135  	}); err != nil {
   136  		return nil, err
   137  	}
   138  	return chainKv, nil
   139  }