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