MCPcopy Create free account
hub / github.com/CPChain/chain / toMessage

Method toMessage

tests/state_test_util.go:190–238  ·  view source on GitHub ↗
(ps stPostState)

Source from the content-addressed store, hash-verified

188}
189
190func (tx *stTransaction) toMessage(ps stPostState) (core.Message, error) {
191 // Derive sender from private key if present.
192 var from common.Address
193 if len(tx.PrivateKey) > 0 {
194 key, err := crypto.ToECDSA(tx.PrivateKey)
195 if err != nil {
196 return nil, fmt.Errorf("invalid private key: %v", err)
197 }
198 from = crypto.PubkeyToAddress(key.PublicKey)
199 }
200 // Parse recipient if present.
201 var to *common.Address
202 if tx.To != "" {
203 to = new(common.Address)
204 if err := to.UnmarshalText([]byte(tx.To)); err != nil {
205 return nil, fmt.Errorf("invalid to address: %v", err)
206 }
207 }
208
209 // Get values specific to this post state.
210 if ps.Indexes.Data > len(tx.Data) {
211 return nil, fmt.Errorf("tx data index %d out of bounds", ps.Indexes.Data)
212 }
213 if ps.Indexes.Value > len(tx.Value) {
214 return nil, fmt.Errorf("tx value index %d out of bounds", ps.Indexes.Value)
215 }
216 if ps.Indexes.Gas > len(tx.GasLimit) {
217 return nil, fmt.Errorf("tx gas limit index %d out of bounds", ps.Indexes.Gas)
218 }
219 dataHex := tx.Data[ps.Indexes.Data]
220 valueHex := tx.Value[ps.Indexes.Value]
221 gasLimit := tx.GasLimit[ps.Indexes.Gas]
222 // Value, Data hex encoding is messy: https://github.com/ethereum/tests/issues/203
223 value := new(big.Int)
224 if valueHex != "0x" {
225 v, ok := math.ParseBig256(valueHex)
226 if !ok {
227 return nil, fmt.Errorf("invalid tx value %q", valueHex)
228 }
229 value = v
230 }
231 data, err := hex.DecodeString(strings.TrimPrefix(dataHex, "0x"))
232 if err != nil {
233 return nil, fmt.Errorf("invalid tx data %q", dataHex)
234 }
235
236 msg := types.NewMessage(from, to, tx.Nonce, value, gasLimit, tx.GasPrice, data, true)
237 return msg, nil
238}
239
240func rlpHash(x interface{}) (h common.Hash) {
241 hw := sha3.NewKeccak256()

Callers 1

RunMethod · 0.80

Calls 1

UnmarshalTextMethod · 0.45

Tested by

no test coverage detected