-
Notifications
You must be signed in to change notification settings - Fork 320
/
protocol.go
179 lines (157 loc) · 5.4 KB
/
protocol.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package account
import (
"context"
"math/big"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state"
)
// protocolID is the protocol ID
// TODO: it works only for one instance per protocol definition now
const protocolID = "account"
// Protocol defines the protocol of handling account
type Protocol struct {
addr address.Address
depositGas DepositGas
}
// DepositGas deposits gas to some pool
type DepositGas func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.TransactionLog, error)
// NewProtocol instantiates the protocol of account
func NewProtocol(depositGas DepositGas) *Protocol {
h := hash.Hash160b([]byte(protocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
log.L().Panic("Error when constructing the address of account protocol", zap.Error(err))
}
return &Protocol{addr: addr, depositGas: depositGas}
}
// ProtocolAddr returns the address generated from protocol id
func ProtocolAddr() address.Address {
return protocol.HashStringToAddress(protocolID)
}
// FindProtocol finds the registered protocol from registry
func FindProtocol(registry *protocol.Registry) *Protocol {
if registry == nil {
return nil
}
p, ok := registry.Find(protocolID)
if !ok {
return nil
}
ap, ok := p.(*Protocol)
if !ok {
log.S().Panic("fail to cast account protocol")
}
return ap
}
// Handle handles an account
func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
switch act := act.(type) {
case *action.Transfer:
return p.handleTransfer(ctx, act, sm)
}
return nil, nil
}
// Validate validates an account action
func (p *Protocol) Validate(ctx context.Context, act action.Action, sr protocol.StateReader) error {
switch act := act.(type) {
case *action.Transfer:
if err := p.validateTransfer(ctx, act); err != nil {
return errors.Wrap(err, "error when validating transfer action")
}
}
return nil
}
// ReadState read the state on blockchain via protocol
func (p *Protocol) ReadState(context.Context, protocol.StateReader, []byte, ...[]byte) ([]byte, uint64, error) {
return nil, uint64(0), protocol.ErrUnimplemented
}
// Register registers the protocol with a unique ID
func (p *Protocol) Register(r *protocol.Registry) error {
return r.Register(protocolID, p)
}
// ForceRegister registers the protocol with a unique ID and force replacing the previous protocol if it exists
func (p *Protocol) ForceRegister(r *protocol.Registry) error {
return r.ForceRegister(protocolID, p)
}
// Name returns the name of protocol
func (p *Protocol) Name() string {
return protocolID
}
func createAccount(sm protocol.StateManager, encodedAddr string, init *big.Int) error {
var account state.Account
addr, err := address.FromString(encodedAddr)
if err != nil {
return errors.Wrap(err, "failed to get address public key hash from encoded address")
}
addrHash := hash.BytesToHash160(addr.Bytes())
_, err = sm.State(&account, protocol.LegacyKeyOption(addrHash))
switch errors.Cause(err) {
case nil:
return errors.Errorf("failed to create account %s", encodedAddr)
case state.ErrStateNotExist:
account.Balance = init
account.VotingWeight = big.NewInt(0)
if _, err := sm.PutState(account, protocol.LegacyKeyOption(addrHash)); err != nil {
return errors.Wrapf(err, "failed to put state for account %x", addrHash)
}
return nil
}
return err
}
// CreateGenesisStates initializes the protocol by setting the initial balances to some addresses
func (p *Protocol) CreateGenesisStates(ctx context.Context, sm protocol.StateManager) error {
blkCtx := protocol.MustGetBlockCtx(ctx)
g := genesis.MustExtractGenesisContext(ctx)
if err := p.assertZeroBlockHeight(blkCtx.BlockHeight); err != nil {
return err
}
addrs, amounts := g.InitBalances()
if err := p.assertEqualLength(addrs, amounts); err != nil {
return err
}
if err := p.assertAmounts(amounts); err != nil {
return err
}
for i, addr := range addrs {
if err := createAccount(sm, addr.String(), amounts[i]); err != nil {
return err
}
}
return nil
}
func (p *Protocol) assertZeroBlockHeight(height uint64) error {
if height != 0 {
return errors.Errorf("current block height %d is not zero", height)
}
return nil
}
func (p *Protocol) assertEqualLength(addrs []address.Address, amounts []*big.Int) error {
if len(addrs) != len(amounts) {
return errors.Errorf(
"address slice length %d and amounts slice length %d don't match",
len(addrs),
len(amounts),
)
}
return nil
}
func (p *Protocol) assertAmounts(amounts []*big.Int) error {
for _, amount := range amounts {
if amount.Cmp(big.NewInt(0)) < 0 {
return errors.Errorf("account amount %s shouldn't be negative", amount.String())
}
}
return nil
}