github.com/amazechain/amc@v0.1.3/modules/rawdb/accessors_deposit.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  	"fmt"
    21  	"github.com/amazechain/amc/common/crypto/bls"
    22  	"github.com/amazechain/amc/common/types"
    23  	"github.com/amazechain/amc/modules"
    24  	"github.com/holiman/uint256"
    25  	"github.com/ledgerwatch/erigon-lib/kv"
    26  )
    27  
    28  //// PutDeposit
    29  //func PutDeposit(db kv.Putter, key []byte, val []byte) error {
    30  //	return db.Put(modules.Deposit, key, val)
    31  //}
    32  
    33  func PutDeposit(db kv.Putter, addr types.Address, pub types.PublicKey, amount uint256.Int) error {
    34  
    35  	data := make([]byte, types.PublicKeyLength+amount.ByteLen())
    36  	copy(data[:types.PublicKeyLength], pub.Bytes())
    37  	copy(data[types.PublicKeyLength:], amount.Bytes())
    38  	//
    39  	if err := db.Put(modules.Deposit, addr[:], data); err != nil {
    40  		return fmt.Errorf("failed to store address Deposit: %w", err)
    41  	}
    42  	return nil
    43  }
    44  
    45  // GetDeposit
    46  func GetDeposit(db kv.Getter, addr types.Address) (types.PublicKey, *uint256.Int, error) {
    47  	valBytes, err := db.GetOne(modules.Deposit, addr[:])
    48  	if err != nil {
    49  		return types.PublicKey{}, nil, err
    50  	}
    51  	if len(valBytes) < types.PublicKeyLength {
    52  		return types.PublicKey{}, nil, fmt.Errorf("the data length wrong")
    53  	}
    54  	_, err = bls.PublicKeyFromBytes(valBytes[:types.PublicKeyLength])
    55  	if err != nil {
    56  		return types.PublicKey{}, nil, fmt.Errorf("cannot unmarshal pubkey from bytes")
    57  	}
    58  	pubkey := new(types.PublicKey)
    59  	if err = pubkey.SetBytes(valBytes[:types.PublicKeyLength]); err != nil {
    60  		return types.PublicKey{}, nil, fmt.Errorf("cannot unmarshal pubkey from bytes")
    61  	}
    62  	amount := uint256.NewInt(0).SetBytes(valBytes[types.PublicKeyLength:])
    63  
    64  	return *pubkey, amount, nil
    65  }
    66  
    67  // DeleteDeposit removes Deposit data associated with an address.
    68  func DeleteDeposit(db kv.Deleter, addr types.Address) error {
    69  	return db.Delete(modules.Deposit, addr[:])
    70  }
    71  
    72  // IsDeposit is deposit account
    73  func IsDeposit(db kv.Getter, addr types.Address) bool {
    74  	is, _ := db.Has(modules.Deposit, addr[:])
    75  	return is
    76  }
    77  
    78  func DepositNum(tx kv.Tx) (uint64, error) {
    79  	cur, err := tx.Cursor(modules.Deposit)
    80  	if nil != err {
    81  		return 0, err
    82  	}
    83  	defer cur.Close()
    84  	return cur.Count()
    85  }