forked from BeamMW/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoke_data.h
209 lines (171 loc) · 4.64 KB
/
invoke_data.h
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2018 The Beam Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <core/block_crypt.h>
#include <map>
namespace beam::bvm2 {
struct FundsMap: public std::map<Asset::ID, AmountSigned>
{
void AddSpend(Asset::ID aid, AmountSigned val);
void operator += (const FundsMap&);
};
struct ContractInvokeEntry
{
uint32_t m_Flags = 0;
ContractID m_Cid;
uint32_t m_iMethod = 0;
ByteBuffer m_Data;
ByteBuffer m_Args;
std::vector<ECC::Hash::Value> m_vSig;
uint32_t m_Charge = 0;
HeightHash m_ParentCtx;
struct Advanced
{
HeightRange m_Height;
Amount m_Fee;
ECC::Signature m_Sig;
ECC::Hash::Value m_hvSk;
ECC::Point m_Commitment;
ECC::Point m_SigImage;
} m_Adv;
struct Flags {
static const uint8_t Adv = 1;
static const uint8_t Dependent = 2;
static const uint8_t Multisigned = 8;
static const uint8_t HasCommitment = 0x10;
static const uint8_t SaveAppInvoke = 0x20;
static const uint8_t SaveSpendMax = 0x40;
};
bool IsAdvanced() const {
return !!(Flags::Adv & m_Flags);
}
bool IsMultisigned() const {
return !!(Flags::Multisigned & m_Flags);
}
FundsMap m_Spend; // ins - outs, not including fee
std::string m_sComment;
template <typename Archive>
void serialize(Archive& ar)
{
static const uint32_t nHasFlags = 0x80000000;
uint32_t nVal = m_Flags ? (nHasFlags | m_Flags) : m_iMethod;
ar & nVal;
if (nHasFlags & nVal)
{
m_Flags = nVal & ~nHasFlags;
m_iMethod &= ~nHasFlags; // for more safety, malicious app shader can specify this method number
ar & m_iMethod;
}
else
m_iMethod = nVal;
ar
& m_Args
& m_vSig
& m_Charge
& m_sComment
& Cast::Down< std::map<Asset::ID, AmountSigned> >(m_Spend);
if (m_iMethod)
ar & m_Cid;
else
{
m_Cid = Zero;
ar & m_Data;
}
if (IsAdvanced())
{
ar
& m_Adv.m_Height
& m_Adv.m_Fee
& m_Adv.m_Sig
& m_Adv.m_hvSk;
}
if (Flags::Dependent & m_Flags)
ar & m_ParentCtx;
if (Flags::HasCommitment & m_Flags)
ar & m_Adv.m_Commitment;
if (Flags::Multisigned & m_Flags)
ar & m_Adv.m_SigImage;
}
void Generate(Transaction&, Key::IKdf&, const HeightRange& hr, Amount fee) const;
void GenerateAdv(Key::IKdf*, ECC::Scalar* pE, const ECC::Point& ptFullBlind, const ECC::Point& ptFullNonce, const ECC::Hash::Value* phvNonce, const ECC::Scalar* pForeignSig,
const ECC::Point* pPks, uint32_t nPks);
[[nodiscard]] Amount get_FeeMin(Height) const;
private:
void CreateKrnUnsigned(std::unique_ptr<TxKernelContractControl>&, ECC::Point::Native& ptFunds, const HeightRange& hr, Amount fee) const;
void get_SigPreimage(ECC::Hash::Value&, const ECC::Hash::Value& krnMsg) const;
};
struct ContractInvokeDataBase
{
std::vector<ContractInvokeEntry> m_vec;
// for multisig
std::vector<ECC::Point> m_vPeers;
FundsMap m_SpendExtra;
ECC::Hash::Value m_hvKey;
bool m_IsSender = true;
bool HasMultiSig() const;
bool HasDependent() const;
std::string get_FullComment() const;
beam::Amount get_FullFee(Height) const;
bvm2::FundsMap get_FullSpend() const;
template <typename Archive>
void serialize(Archive& ar)
{
ar & m_vec;
if (HasMultiSig())
{
ar
& m_IsSender
& m_hvKey
& m_vPeers
& Cast::Down< std::map<Asset::ID, AmountSigned> >(m_SpendExtra);
}
}
void Reset();
};
struct AppInvokeData
{
ByteBuffer m_App;
ByteBuffer m_Contract;
std::map<std::string, std::string> m_Args;
uint32_t m_Privilege = 0;
template <typename Archive>
void serialize(Archive& ar)
{
ar
& m_App
& m_Contract
& m_Args
& m_Privilege;
}
};
struct ContractInvokeData
:public ContractInvokeDataBase
{
AppInvokeData m_AppInvoke;
FundsMap m_SpendMax;
template <typename Archive>
void serialize(Archive& ar)
{
ar & Cast::Down<ContractInvokeDataBase>(*this);
if (!m_vec.empty())
{
auto& x = m_vec.front();
if (ContractInvokeEntry::Flags::SaveAppInvoke & x.m_Flags)
ar & m_AppInvoke;
if (ContractInvokeEntry::Flags::SaveSpendMax & x.m_Flags)
ar & Cast::Down< std::map<Asset::ID, AmountSigned> >(m_SpendMax);
}
}
};
}