-
Notifications
You must be signed in to change notification settings - Fork 321
/
stopsubchain.go
114 lines (94 loc) · 3.29 KB
/
stopsubchain.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
// 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 action
import (
"math/big"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/version"
"github.com/iotexproject/iotex-core/protogen/iotextypes"
)
const (
// StopSubChainIntrinsicGas is the instrinsic gas for stop sub chain action
StopSubChainIntrinsicGas = uint64(1000)
)
var _ hasDestination = (*StopSubChain)(nil)
// StopSubChain defines the action to stop sub chain
type StopSubChain struct {
AbstractAction
chainID uint32
chainAddress string
stopHeight uint64
}
// NewStopSubChain returns a StopSubChain instance
func NewStopSubChain(
nonce uint64,
chainAddress string,
stopHeight uint64,
gasLimit uint64,
gasPrice *big.Int,
) *StopSubChain {
return &StopSubChain{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
chainAddress: chainAddress,
stopHeight: stopHeight,
}
}
// ChainAddress returns the address of the sub chain
func (ssc *StopSubChain) ChainAddress() string { return ssc.chainAddress }
// Destination returns the address of the sub chain
func (ssc *StopSubChain) Destination() string { return ssc.ChainAddress() }
// StopHeight returns the height to stop the sub chain
func (ssc *StopSubChain) StopHeight() uint64 { return ssc.stopHeight }
// TotalSize returns the total size of this instance
func (ssc *StopSubChain) TotalSize() uint32 {
return ssc.BasicActionSize() + 4 + 8 // chain id size + stop height size
}
// ByteStream returns a raw byte stream of this instance
func (ssc *StopSubChain) ByteStream() []byte {
return byteutil.Must(proto.Marshal(ssc.Proto()))
}
// Proto converts StopSubChain to protobuf's Action
func (ssc *StopSubChain) Proto() *iotextypes.StopSubChain {
return &iotextypes.StopSubChain{
ChainID: ssc.chainID,
StopHeight: ssc.stopHeight,
SubChainAddress: ssc.chainAddress,
}
}
// LoadProto converts a protobuf's Action to StopSubChain
func (ssc *StopSubChain) LoadProto(pbSSC *iotextypes.StopSubChain) error {
if ssc == nil {
return errors.New("nil action to load proto")
}
*ssc = StopSubChain{}
if pbSSC == nil {
return errors.New("empty action proto to load")
}
ssc.chainID = pbSSC.GetChainID()
ssc.chainAddress = pbSSC.GetSubChainAddress()
ssc.stopHeight = pbSSC.GetStopHeight()
return nil
}
// IntrinsicGas returns the intrinsic gas of a StopSubChain
func (ssc *StopSubChain) IntrinsicGas() (uint64, error) {
return StopSubChainIntrinsicGas, nil
}
// Cost returns the total cost of a StopSubChain
func (ssc *StopSubChain) Cost() (*big.Int, error) {
intrinsicGas, err := ssc.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed to get intrinsic gas for the stop sub-chain action")
}
fee := big.NewInt(0).Mul(ssc.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return fee, nil
}