2014-02-15 00:56:09 +02:00
|
|
|
package ethchain
|
|
|
|
|
|
|
|
import (
|
2014-06-12 12:19:32 +03:00
|
|
|
"bytes"
|
2014-05-22 18:35:26 +03:00
|
|
|
"fmt"
|
2014-08-09 20:09:55 +03:00
|
|
|
"math/big"
|
|
|
|
|
2014-10-23 16:01:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/ethcrypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethstate"
|
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2014-10-27 12:44:16 +02:00
|
|
|
"github.com/ethereum/go-ethereum/vm"
|
2014-02-15 00:56:09 +02:00
|
|
|
"github.com/obscuren/secp256k1-go"
|
|
|
|
)
|
|
|
|
|
2014-02-19 12:35:17 +02:00
|
|
|
var ContractAddr = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
|
2014-06-19 14:42:47 +03:00
|
|
|
func IsContractAddr(addr []byte) bool {
|
2014-09-08 01:48:59 +03:00
|
|
|
return len(addr) == 0
|
|
|
|
//return bytes.Compare(addr, ContractAddr) == 0
|
2014-06-19 14:42:47 +03:00
|
|
|
}
|
|
|
|
|
2014-02-15 00:56:09 +02:00
|
|
|
type Transaction struct {
|
|
|
|
Nonce uint64
|
|
|
|
Recipient []byte
|
|
|
|
Value *big.Int
|
2014-03-21 01:04:31 +02:00
|
|
|
Gas *big.Int
|
2014-04-18 14:41:07 +03:00
|
|
|
GasPrice *big.Int
|
2014-04-09 19:28:16 +03:00
|
|
|
Data []byte
|
2014-02-15 00:56:09 +02:00
|
|
|
v byte
|
|
|
|
r, s []byte
|
2014-03-27 16:42:39 +02:00
|
|
|
|
2014-10-27 12:44:16 +02:00
|
|
|
logs []vm.Log
|
|
|
|
|
2014-03-27 16:42:39 +02:00
|
|
|
// Indicates whether this tx is a contract creation transaction
|
|
|
|
contractCreation bool
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2014-05-22 19:24:04 +03:00
|
|
|
func NewContractCreationTx(value, gas, gasPrice *big.Int, script []byte) *Transaction {
|
2014-09-08 01:48:59 +03:00
|
|
|
return &Transaction{Recipient: nil, Value: value, Gas: gas, GasPrice: gasPrice, Data: script, contractCreation: true}
|
2014-03-21 01:04:31 +02:00
|
|
|
}
|
|
|
|
|
2014-04-23 12:51:04 +03:00
|
|
|
func NewTransactionMessage(to []byte, value, gas, gasPrice *big.Int, data []byte) *Transaction {
|
2014-10-27 17:52:58 +02:00
|
|
|
return &Transaction{Recipient: to, Value: value, GasPrice: gasPrice, Gas: gas, Data: data, contractCreation: IsContractAddr(to)}
|
2014-03-21 01:04:31 +02:00
|
|
|
}
|
|
|
|
|
2014-02-25 12:22:18 +02:00
|
|
|
func NewTransactionFromBytes(data []byte) *Transaction {
|
2014-02-15 00:56:09 +02:00
|
|
|
tx := &Transaction{}
|
|
|
|
tx.RlpDecode(data)
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTransactionFromValue(val *ethutil.Value) *Transaction {
|
|
|
|
tx := &Transaction{}
|
|
|
|
tx.RlpValueDecode(val)
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2014-10-27 12:44:16 +02:00
|
|
|
func (self *Transaction) addLog(log vm.Log) {
|
|
|
|
self.logs = append(self.logs, log)
|
|
|
|
}
|
|
|
|
|
2014-06-13 13:45:11 +03:00
|
|
|
func (self *Transaction) GasValue() *big.Int {
|
|
|
|
return new(big.Int).Mul(self.Gas, self.GasPrice)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Transaction) TotalValue() *big.Int {
|
|
|
|
v := self.GasValue()
|
|
|
|
return v.Add(v, self.Value)
|
|
|
|
}
|
|
|
|
|
2014-02-15 00:56:09 +02:00
|
|
|
func (tx *Transaction) Hash() []byte {
|
2014-05-22 18:35:26 +03:00
|
|
|
data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data}
|
2014-04-23 12:51:04 +03:00
|
|
|
|
2014-10-08 13:06:39 +03:00
|
|
|
return ethcrypto.Sha3(ethutil.NewValue(data).Encode())
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2014-05-25 16:13:54 +03:00
|
|
|
func (tx *Transaction) CreatesContract() bool {
|
2014-03-28 00:17:14 +02:00
|
|
|
return tx.contractCreation
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 19:05:46 +03:00
|
|
|
/* Deprecated */
|
2014-05-25 16:13:54 +03:00
|
|
|
func (tx *Transaction) IsContract() bool {
|
|
|
|
return tx.CreatesContract()
|
|
|
|
}
|
|
|
|
|
2014-10-02 18:03:15 +03:00
|
|
|
func (tx *Transaction) CreationAddress(state *ethstate.State) []byte {
|
|
|
|
// Generate a new address
|
2014-10-08 13:06:39 +03:00
|
|
|
addr := ethcrypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:]
|
2014-10-02 18:03:15 +03:00
|
|
|
//for i := uint64(0); state.GetStateObject(addr) != nil; i++ {
|
2014-10-08 13:06:39 +03:00
|
|
|
// addr = ethcrypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce + i}).Encode())[12:]
|
2014-10-02 18:03:15 +03:00
|
|
|
//}
|
|
|
|
|
|
|
|
return addr
|
2014-05-02 15:08:54 +03:00
|
|
|
}
|
|
|
|
|
2014-02-15 00:56:09 +02:00
|
|
|
func (tx *Transaction) Signature(key []byte) []byte {
|
|
|
|
hash := tx.Hash()
|
|
|
|
|
|
|
|
sig, _ := secp256k1.Sign(hash, key)
|
|
|
|
|
|
|
|
return sig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) PublicKey() []byte {
|
|
|
|
hash := tx.Hash()
|
|
|
|
|
2014-07-02 01:05:48 +03:00
|
|
|
// TODO
|
|
|
|
r := ethutil.LeftPadBytes(tx.r, 32)
|
|
|
|
s := ethutil.LeftPadBytes(tx.s, 32)
|
2014-02-15 00:56:09 +02:00
|
|
|
|
2014-06-23 12:23:51 +03:00
|
|
|
sig := append(r, s...)
|
2014-02-15 00:56:09 +02:00
|
|
|
sig = append(sig, tx.v-27)
|
|
|
|
|
2014-10-29 11:34:40 +02:00
|
|
|
pubkey := ethcrypto.Ecrecover(append(hash, sig...))
|
|
|
|
//pubkey, _ := secp256k1.RecoverPubkey(hash, sig)
|
2014-02-15 00:56:09 +02:00
|
|
|
|
|
|
|
return pubkey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) Sender() []byte {
|
|
|
|
pubkey := tx.PublicKey()
|
|
|
|
|
|
|
|
// Validate the returned key.
|
|
|
|
// Return nil if public key isn't in full format
|
|
|
|
if pubkey[0] != 4 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-08 13:06:39 +03:00
|
|
|
return ethcrypto.Sha3(pubkey[1:])[12:]
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) Sign(privk []byte) error {
|
|
|
|
|
|
|
|
sig := tx.Signature(privk)
|
|
|
|
|
|
|
|
tx.r = sig[:32]
|
|
|
|
tx.s = sig[32:64]
|
|
|
|
tx.v = sig[64] + 27
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) RlpData() interface{} {
|
2014-05-20 14:06:47 +03:00
|
|
|
data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data}
|
2014-03-27 20:49:47 +02:00
|
|
|
|
2014-06-23 12:23:51 +03:00
|
|
|
// TODO Remove prefixing zero's
|
|
|
|
|
2014-08-09 20:09:55 +03:00
|
|
|
return append(data, tx.v, new(big.Int).SetBytes(tx.r).Bytes(), new(big.Int).SetBytes(tx.s).Bytes())
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) RlpValue() *ethutil.Value {
|
|
|
|
return ethutil.NewValue(tx.RlpData())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) RlpEncode() []byte {
|
|
|
|
return tx.RlpValue().Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) RlpDecode(data []byte) {
|
|
|
|
tx.RlpValueDecode(ethutil.NewValueFromBytes(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) {
|
|
|
|
tx.Nonce = decoder.Get(0).Uint()
|
2014-05-20 14:06:47 +03:00
|
|
|
tx.GasPrice = decoder.Get(1).BigInt()
|
|
|
|
tx.Gas = decoder.Get(2).BigInt()
|
|
|
|
tx.Recipient = decoder.Get(3).Bytes()
|
|
|
|
tx.Value = decoder.Get(4).BigInt()
|
2014-04-09 19:28:16 +03:00
|
|
|
tx.Data = decoder.Get(5).Bytes()
|
2014-05-22 18:35:26 +03:00
|
|
|
tx.v = byte(decoder.Get(6).Uint())
|
2014-06-20 21:12:08 +03:00
|
|
|
|
2014-06-23 12:23:51 +03:00
|
|
|
tx.r = decoder.Get(7).Bytes()
|
|
|
|
tx.s = decoder.Get(8).Bytes()
|
2014-06-12 12:19:32 +03:00
|
|
|
|
2014-06-19 14:42:47 +03:00
|
|
|
if IsContractAddr(tx.Recipient) {
|
2014-05-25 16:13:54 +03:00
|
|
|
tx.contractCreation = true
|
|
|
|
}
|
2014-05-22 18:35:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) String() string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
TX(%x)
|
|
|
|
Contract: %v
|
|
|
|
From: %x
|
2014-05-22 18:56:33 +03:00
|
|
|
To: %x
|
2014-05-22 18:35:26 +03:00
|
|
|
Nonce: %v
|
|
|
|
GasPrice: %v
|
|
|
|
Gas: %v
|
|
|
|
Value: %v
|
|
|
|
Data: 0x%x
|
|
|
|
V: 0x%x
|
|
|
|
R: 0x%x
|
|
|
|
S: 0x%x
|
|
|
|
`,
|
|
|
|
tx.Hash(),
|
2014-05-25 16:13:54 +03:00
|
|
|
len(tx.Recipient) == 0,
|
2014-06-23 12:23:51 +03:00
|
|
|
tx.Sender(),
|
2014-05-22 18:56:33 +03:00
|
|
|
tx.Recipient,
|
2014-05-22 18:35:26 +03:00
|
|
|
tx.Nonce,
|
|
|
|
tx.GasPrice,
|
|
|
|
tx.Gas,
|
|
|
|
tx.Value,
|
|
|
|
tx.Data,
|
|
|
|
tx.v,
|
|
|
|
tx.r,
|
|
|
|
tx.s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Receipt struct {
|
|
|
|
PostState []byte
|
|
|
|
CumulativeGasUsed *big.Int
|
2014-10-29 11:34:40 +02:00
|
|
|
Bloom []byte
|
|
|
|
Logs vm.Logs
|
2014-05-22 18:35:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRecieptFromValue(val *ethutil.Value) *Receipt {
|
|
|
|
r := &Receipt{}
|
|
|
|
r.RlpValueDecode(val)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Receipt) RlpValueDecode(decoder *ethutil.Value) {
|
2014-10-29 11:34:40 +02:00
|
|
|
self.PostState = decoder.Get(0).Bytes()
|
|
|
|
self.CumulativeGasUsed = decoder.Get(1).BigInt()
|
|
|
|
self.Bloom = decoder.Get(2).Bytes()
|
|
|
|
|
|
|
|
it := decoder.Get(3).NewIterator()
|
|
|
|
for it.Next() {
|
|
|
|
self.Logs = append(self.Logs, vm.NewLogFromValue(it.Value()))
|
|
|
|
}
|
2014-05-22 18:35:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Receipt) RlpData() interface{} {
|
2014-10-29 11:34:40 +02:00
|
|
|
return []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.Logs.RlpData()}
|
2014-05-22 18:35:26 +03:00
|
|
|
}
|
|
|
|
|
2014-10-29 11:34:40 +02:00
|
|
|
func (self *Receipt) RlpEncode() []byte {
|
|
|
|
return ethutil.Encode(self.RlpData())
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
2014-05-28 13:06:09 +03:00
|
|
|
|
2014-07-14 23:52:44 +03:00
|
|
|
func (self *Receipt) Cmp(other *Receipt) bool {
|
|
|
|
if bytes.Compare(self.PostState, other.PostState) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:34:40 +02:00
|
|
|
type Receipts []*Receipt
|
|
|
|
|
|
|
|
func (self Receipts) Len() int { return len(self) }
|
|
|
|
func (self Receipts) GetRlp(i int) []byte { return ethutil.Rlp(self[i]) }
|
|
|
|
|
2014-05-28 13:06:09 +03:00
|
|
|
// Transaction slice type for basic sorting
|
|
|
|
type Transactions []*Transaction
|
|
|
|
|
2014-10-29 11:34:40 +02:00
|
|
|
func (self Transactions) RlpData() interface{} {
|
|
|
|
// Marshal the transactions of this block
|
|
|
|
enc := make([]interface{}, len(self))
|
|
|
|
for i, tx := range self {
|
|
|
|
// Cast it to a string (safe)
|
|
|
|
enc[i] = tx.RlpData()
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc
|
|
|
|
}
|
|
|
|
func (s Transactions) Len() int { return len(s) }
|
|
|
|
func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s Transactions) GetRlp(i int) []byte { return ethutil.Rlp(s[i]) }
|
2014-05-28 13:06:09 +03:00
|
|
|
|
|
|
|
type TxByNonce struct{ Transactions }
|
|
|
|
|
|
|
|
func (s TxByNonce) Less(i, j int) bool {
|
|
|
|
return s.Transactions[i].Nonce < s.Transactions[j].Nonce
|
|
|
|
}
|