-
Notifications
You must be signed in to change notification settings - Fork 321
/
protocol.go
124 lines (110 loc) · 3.54 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
// 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"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/pkg/log"
)
// 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
hu config.HeightUpgrade
}
// NewProtocol instantiates the protocol of account
func NewProtocol(hu config.HeightUpgrade) *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,
hu: hu,
}
}
// 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
func (p *Protocol) Validate(ctx context.Context, act action.Action) 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.StateManager, []byte, ...[]byte) ([]byte, error) {
return nil, protocol.ErrUnimplemented
}
// Initialize initializes the protocol by setting the initial balances to some addresses
func (p *Protocol) Initialize(
ctx context.Context,
sm protocol.StateManager,
addrs []address.Address,
amounts []*big.Int,
) error {
raCtx := protocol.MustGetRunActionsCtx(ctx)
if err := p.assertZeroBlockHeight(raCtx.BlockHeight); err != nil {
return err
}
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 := accountutil.LoadOrCreateAccount(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 nil
}
return errors.Errorf("account amount %s shouldn't be negative", amount.String())
}
return nil
}