forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransaction.cpp
205 lines (163 loc) · 5.39 KB
/
Transaction.cpp
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
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file TransactionBase.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include <libdevcore/vector_ref.h>
#include <libdevcore/CommonIO.h>
#include <libdevcrypto/Common.h>
#include <libevmcore/EVMSchedule.h>
#include <libethcore/Exceptions.h>
#include "Transaction.h"
#include <libdevcore/Log.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
u256 TransactionBase::maxGas=20000000;//默认二千万
TransactionBase::TransactionBase(TransactionSkeleton const& _ts, Secret const& _s):
m_type(_ts.creation ? ContractCreation : MessageCall),
m_randomid(_ts.randomid),
m_value(_ts.value),
m_receiveAddress(_ts.to),
m_gasPrice(_ts.gasPrice),
m_gas(_ts.gas),
m_blockLimit(_ts.blockLimit),
m_data(_ts.data),
m_sender(_ts.from)
{
if (_s)
sign(_s);
}
TransactionBase::TransactionBase(bytesConstRef _rlpData, CheckTransaction _checkSig)
{
int field = 0;
RLP rlp(_rlpData);
try
{
if (!rlp.isList())
BOOST_THROW_EXCEPTION(InvalidTransactionFormat() << errinfo_comment("transaction RLP must be a list"));
m_randomid = rlp[field = 0].toInt<u256>();
m_gasPrice = rlp[field = 1].toInt<u256>();
m_gas = rlp[field = 2].toInt<u256>();
m_blockLimit = rlp[field = 3].toInt<u256>();//新加的
m_type = rlp[field = 4].isEmpty() ? ContractCreation : MessageCall;
m_receiveAddress = rlp[field = 4].isEmpty() ? Address() : rlp[field = 4].toHash<Address>(RLP::VeryStrict);
m_value = rlp[field = 5].toInt<u256>();
if (!rlp[field = 6].isData())
BOOST_THROW_EXCEPTION(InvalidTransactionFormat() << errinfo_comment("transaction data RLP must be an array"));
m_data = rlp[field = 6].toBytes();
byte v = rlp[field = 7].toInt<byte>();
h256 r = rlp[field = 8].toInt<u256>();
h256 s = rlp[field = 9].toInt<u256>();
if (v > 36)
m_chainId = (v - 35) / 2;
else if (v == 27 || v == 28)
m_chainId = -4;
else
BOOST_THROW_EXCEPTION(InvalidSignature());
v = v - (m_chainId * 2 + 35);
if (rlp.itemCount() > 10)
BOOST_THROW_EXCEPTION(InvalidTransactionFormat() << errinfo_comment("to many fields in the transaction RLP"));
m_vrs = SignatureStruct{ r, s, v };
if (_checkSig >= CheckTransaction::Cheap && !m_vrs.isValid()){
BOOST_THROW_EXCEPTION(InvalidSignature());
}
if (_checkSig == CheckTransaction::Everything){
m_sender = sender();
}
}
catch (Exception& _e)
{
_e << errinfo_name("invalid transaction format: " + toString(rlp) + " RLP: " + toHex(rlp.data()));
throw;
}
}
Address const& TransactionBase::safeSender() const noexcept
{
try
{
return sender();
}
catch (...)
{
return ZeroAddress;
}
}
Address const& TransactionBase::sender() const
{
if (!m_sender)
{
auto p = recover(m_vrs, sha3(WithoutSignature));
if (!p)
BOOST_THROW_EXCEPTION(InvalidSignature());
m_sender = right160(dev::sha3(bytesConstRef(p.data(), sizeof(p))));
}
return m_sender;
}
void TransactionBase::sign(Secret const& _priv)
{
auto sig = dev::sign(_priv, sha3(WithoutSignature));
SignatureStruct sigStruct = *(SignatureStruct const*)&sig;
if (sigStruct.isValid())
m_vrs = sigStruct;
}
void TransactionBase::streamRLP(RLPStream& _s, IncludeSignature _sig, bool _forEip155hash) const
{
if (m_type == NullTransaction)
return;
_s.appendList((_sig || _forEip155hash ? 3 : 0) + 7);
_s << m_randomid << m_gasPrice << m_gas<< m_blockLimit;//这里加入新字段
if (m_type == MessageCall)
_s << m_receiveAddress;
else
_s << "";
_s << m_value << m_data;
if (_sig)
{
int vOffset = m_chainId*2 + 35;
_s << (m_vrs.v + vOffset) << (u256)m_vrs.r << (u256)m_vrs.s;
}
else if (_forEip155hash)
_s << m_chainId << 0 << 0;
}
static const u256 c_secp256k1n("115792089237316195423570985008687907852837564279074904382605163141518161494337");
void TransactionBase::checkLowS() const
{
if (m_vrs.s > c_secp256k1n / 2)
BOOST_THROW_EXCEPTION(InvalidSignature());
}
void TransactionBase::checkChainId(int chainId) const
{
if (m_chainId != chainId && m_chainId != -4)
BOOST_THROW_EXCEPTION(InvalidSignature());
}
bigint TransactionBase::gasRequired(bool _contractCreation, bytesConstRef _data, EVMSchedule const& _es, u256 const& _gas)
{
bigint ret = (_contractCreation ? _es.txCreateGas : _es.txGas) + _gas;
for (auto i: _data)
ret += i ? _es.txDataNonZeroGas : _es.txDataZeroGas;
return ret;
}
h256 TransactionBase::sha3(IncludeSignature _sig) const
{
if (_sig == WithSignature && m_hashWith)
return m_hashWith;
RLPStream s;
streamRLP(s, _sig, m_chainId > 0 && _sig == WithoutSignature);
auto ret = dev::sha3(s.out());
if (_sig == WithSignature)
m_hashWith = ret;
return ret;
}