// Dump the ledger states from the tree into a directory.
// Existing files will be truncate.
//
// dir is the directory path which will be used to dump the states.
//
// isDumpContract is a flag which if true, the dump will include all the contracts
//
// useContractFolder is a flag which if true, each contract dump has it's own folder.
// The folder name will be hex-encoded of the contract ID.
Dump the ledger states from the tree into a directory.
Existing files will be truncate.

dir is the directory path which will be used to dump the states.

isDumpContract is a flag which if true, the dump will include all the contracts

useContractFolder is a flag which if true, each contract dump has it's own folder.
The folder name will be hex-encoded of the contract ID.
// Dump the ledger states from the tree into a directory . // Existing files will be truncate . // // dir is the directory path which will be used to dump the states . // // isDumpContract is a flag which if true the dump will include all the contracts // // useContractFolder is a flag which if true each contract dump has it ' s own folder . // The folder name will be hex - encoded of the contract ID .
func Dump(tree *avl.Tree, dir string, isDumpContract bool, useContractFolder bool) error {
	if err := os.MkdirAll(dir, 0700); err != nil {
		return errors.Wrapf(err, "failed to create directory %s", dir)
	}
	type account struct {
		balance *uint64
		stake   *uint64
		reward  *uint64
		isContract bool
		gasBalance *uint64
	}
	var (
		filePerm os.FileMode = 0644
		err      error
	)
	accounts := make(map[AccountID]*account)
	tree.IteratePrefix(keyAccounts[:], func(key, value []byte) bool {
		var prefix [1]byte
		copy(prefix[:], key)
		if prefix != keyAccountBalance &&
			prefix != keyAccountStake &&
			prefix != keyAccountReward &&
			prefix != keyAccountContractCode {
			return true
		}
		var id AccountID
		copy(id[:], key[1:])
		if _, exist := accounts[id]; exist {
			return true
		}
		_, isContract := ReadAccountContractCode(tree, id)
		acc := &account{
			isContract: isContract,
		}
		accounts[id] = acc
		if !isDumpContract && acc.isContract {
			return true
		}
		if balance, exist := ReadAccountBalance(tree, id); exist {
			acc.balance = &balance
		}
		if stake, exist := ReadAccountStake(tree, id); exist {
			acc.stake = &stake
		}
		if reward, exist := ReadAccountReward(tree, id); exist {
			acc.reward = &reward
		}
		if gasBalance, exist := ReadAccountContractGasBalance(tree, id); exist {
			acc.gasBalance = &gasBalance
		}
		var folder = dir
		if useContractFolder {
			folder = filepath.Join(dir, fmt.Sprintf("%x", id))
			if fErr := os.MkdirAll(folder, 0700); fErr != nil {
				err = errors.Wrapf(fErr, "failed to create directory %s", dir)
				return true
			}
		}
		err = dumpContract(tree, id, folder)
		return true
	})
	if err != nil {
		return err
	}
	arena := &fastjson.Arena{}
	data := make([]byte, 0, 512)
	for id, v := range accounts {
		if !isDumpContract && v.isContract {
			continue
		}
		o := arena.NewObject()
		if v.isContract {
			o.Set("is_contract", arena.NewTrue())
		} else {
			o.Set("is_contract", arena.NewFalse())
		}
		if v.gasBalance != nil {
			o.Set("gas_balance", arena.NewNumberString(strconv.FormatUint(*v.gasBalance, 10)))
		}
		if v.balance != nil {
			o.Set("balance", arena.NewNumberString(strconv.FormatUint(*v.balance, 10)))
		}
		if v.stake != nil {
			o.Set("stake", arena.NewNumberString(strconv.FormatUint(*v.stake, 10)))
		}
		if v.reward != nil {
			o.Set("reward", arena.NewNumberString(strconv.FormatUint(*v.reward, 10)))
		}
		data = o.MarshalTo(data)
		filename := fmt.Sprintf("%x.json", id)
		err := ioutil.WriteFile(filepath.Join(dir, filename), data, filePerm)
		if err != nil {
			return errors.Wrapf(err, "failed to write %s", filename)
		}
		data = data[:0]
		arena.Reset()
	}
	return nil
}
func Dump ( tree * avl . Tree , dir string , isDumpContract bool , useContractFolder bool ) error { if err := os . MkdirAll ( dir , 0700 ) ; err != nil { return errors . Wrapf ( err , " " , dir ) } type account struct { balance * uint64 stake * uint64 reward * uint64 isContract bool gasBalance * uint64 } var ( filePerm os . FileMode = 0644 err error ) accounts := make ( map [ AccountID ] * account ) tree . IteratePrefix ( keyAccounts [ : ] , func ( key , value [ ] byte ) bool { var prefix [ 1 ] byte copy ( prefix [ : ] , key ) if prefix != keyAccountBalance && prefix != keyAccountStake && prefix != keyAccountReward && prefix != keyAccountContractCode { return true } var id AccountID copy ( id [ : ] , key [ 1 : ] ) if _ , exist := accounts [ id ] ; exist { return true } _ , isContract := ReadAccountContractCode ( tree , id ) acc := & account { isContract : isContract , } accounts [ id ] = acc if ! isDumpContract && acc . isContract { return true } if balance , exist := ReadAccountBalance ( tree , id ) ; exist { acc . balance = & balance } if stake , exist := ReadAccountStake ( tree , id ) ; exist { acc . stake = & stake } if reward , exist := ReadAccountReward ( tree , id ) ; exist { acc . reward = & reward } if gasBalance , exist := ReadAccountContractGasBalance ( tree , id ) ; exist { acc . gasBalance = & gasBalance } var folder = dir if useContractFolder { folder = filepath . Join ( dir , fmt . Sprintf ( " " , id ) ) if fErr := os . MkdirAll ( folder , 0700 ) ; fErr != nil { err = errors . Wrapf ( fErr , " " , dir ) return true } } err = dumpContract ( tree , id , folder ) return true } ) if err != nil { return err } arena := & fastjson . Arena { } data := make ( [ ] byte , 0 , 512 ) for id , v := range accounts { if ! isDumpContract && v . isContract { continue } o := arena . NewObject ( ) if v . isContract { o . Set ( " " , arena . NewTrue ( ) ) } else { o . Set ( " " , arena . NewFalse ( ) ) } if v . gasBalance != nil { o . Set ( " " , arena . NewNumberString ( strconv . FormatUint ( * v . gasBalance , 10 ) ) ) } if v . balance != nil { o . Set ( " " , arena . NewNumberString ( strconv . FormatUint ( * v . balance , 10 ) ) ) } if v . stake != nil { o . Set ( " " , arena . NewNumberString ( strconv . FormatUint ( * v . stake , 10 ) ) ) } if v . reward != nil { o . Set ( " " , arena . NewNumberString ( strconv . FormatUint ( * v . reward , 10 ) ) ) } data = o . MarshalTo ( data ) filename := fmt . Sprintf ( " " , id ) err := ioutil . WriteFile ( filepath . Join ( dir , filename ) , data , filePerm ) if err != nil { return errors . Wrapf ( err , " " , filename ) } data = data [ : 0 ] arena . Reset ( ) } return nil }
Dump the ledger states from the tree into a directory.
Dump the ledger states from the tree into a directory .
// Make sure the directory exists // Filter by prefixes relevant to wallet and contract code. // Make sure the folder exists