github.com/turingchain2020/turingchain@v1.1.21/account/genesis.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package account 6 7 import ( 8 "github.com/turingchain2020/turingchain/types" 9 "github.com/golang/protobuf/proto" 10 ) 11 12 func safeAdd(balance, amount int64) (int64, error) { 13 if balance+amount < amount || balance+amount > types.MaxTokenBalance { 14 return balance, types.ErrAmount 15 } 16 return balance + amount, nil 17 } 18 19 // GenesisInit 生成创世地址账户收据 20 func (acc *DB) GenesisInit(addr string, amount int64) (receipt *types.Receipt, err error) { 21 accTo := acc.LoadAccount(addr) 22 copyto := *accTo 23 accTo.Balance, err = safeAdd(accTo.GetBalance(), amount) 24 if err != nil { 25 return nil, err 26 } 27 receiptBalanceTo := &types.ReceiptAccountTransfer{ 28 Prev: ©to, 29 Current: accTo, 30 } 31 acc.SaveAccount(accTo) 32 receipt = acc.genesisReceipt(accTo, receiptBalanceTo) 33 return receipt, nil 34 } 35 36 // GenesisInitExec 生成创世地址执行器账户收据 37 func (acc *DB) GenesisInitExec(addr string, amount int64, execaddr string) (receipt *types.Receipt, err error) { 38 accTo := acc.LoadAccount(execaddr) 39 copyto := *accTo 40 accTo.Balance, err = safeAdd(accTo.GetBalance(), amount) 41 if err != nil { 42 return nil, err 43 } 44 receiptBalanceTo := &types.ReceiptAccountTransfer{ 45 Prev: ©to, 46 Current: accTo, 47 } 48 acc.SaveAccount(accTo) 49 receipt = acc.genesisReceipt(accTo, receiptBalanceTo) 50 receipt2, err := acc.ExecDeposit(addr, execaddr, amount) 51 if err != nil { 52 panic(err) 53 } 54 ty := int32(types.TyLogGenesisDeposit) 55 receipt2.Ty = ty 56 receipt = acc.mergeReceipt(receipt, receipt2) 57 return receipt, nil 58 } 59 60 func (acc *DB) genesisReceipt(accTo *types.Account, receiptTo proto.Message) *types.Receipt { 61 ty := int32(types.TyLogGenesisTransfer) 62 log2 := &types.ReceiptLog{ 63 Ty: ty, 64 Log: types.Encode(receiptTo), 65 } 66 kv := acc.GetKVSet(accTo) 67 return &types.Receipt{ 68 Ty: types.ExecOk, 69 KV: kv, 70 Logs: []*types.ReceiptLog{log2}, 71 } 72 }