forked from iotexproject/iotex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
88 lines (76 loc) · 2.76 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
// Copyright (c) 2018 IoTeX
// 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 execution
import (
"context"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/log"
)
const (
// ExecutionSizeLimit is the maximum size of execution allowed
ExecutionSizeLimit = 32 * 1024
// ProtocolID is the protocol ID
// TODO: it works only for one instance per protocol definition now
ProtocolID = "smart_contract"
)
// Protocol defines the protocol of handling executions
type Protocol struct {
cm protocol.ChainManager
addr address.Address
}
// NewProtocol instantiates the protocol of exeuction
func NewProtocol(cm protocol.ChainManager) *Protocol {
h := hash.Hash160b([]byte(ProtocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
log.L().Panic("Error when constructing the address of vote protocol", zap.Error(err))
}
return &Protocol{cm: cm, addr: addr}
}
// Handle handles an execution
func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
exec, ok := act.(*action.Execution)
if !ok {
return nil, nil
}
_, receipt, err := evm.ExecuteContract(ctx, sm, exec, p.cm)
if err != nil {
return nil, errors.Wrap(err, "failed to execute contract")
}
return receipt, nil
}
// Validate validates an execution
func (p *Protocol) Validate(_ context.Context, act action.Action) error {
exec, ok := act.(*action.Execution)
if !ok {
return nil
}
// Reject oversized exeuction
if exec.TotalSize() > ExecutionSizeLimit {
return errors.Wrap(action.ErrActPool, "oversized data")
}
// Reject execution of negative amount
if exec.Amount().Sign() < 0 {
return errors.Wrap(action.ErrBalance, "negative value")
}
// check if contract's address is valid
if exec.Contract() != action.EmptyAddress {
if _, err := address.FromString(exec.Contract()); err != nil {
return errors.Wrapf(err, "error when validating contract's address %s", exec.Contract())
}
}
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
}