|
|
@ -31,10 +31,12 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
//go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
|
|
|
|
//go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Errors constants for Transaction.
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
ErrInvalidSig = errors.New("invalid transaction v, r, s values") |
|
|
|
ErrInvalidSig = errors.New("invalid transaction v, r, s values") |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Transaction struct.
|
|
|
|
type Transaction struct { |
|
|
|
type Transaction struct { |
|
|
|
data txdata |
|
|
|
data txdata |
|
|
|
// caches
|
|
|
|
// caches
|
|
|
@ -45,7 +47,7 @@ type Transaction struct { |
|
|
|
|
|
|
|
|
|
|
|
type txdata struct { |
|
|
|
type txdata struct { |
|
|
|
AccountNonce uint64 `json:"nonce" gencodec:"required"` |
|
|
|
AccountNonce uint64 `json:"nonce" gencodec:"required"` |
|
|
|
ShardID uint32 `json:"shardId" gencodec:"required"` |
|
|
|
ShardID uint32 `json:"shardID" gencodec:"required"` |
|
|
|
Price *big.Int `json:"gasPrice" gencodec:"required"` |
|
|
|
Price *big.Int `json:"gasPrice" gencodec:"required"` |
|
|
|
GasLimit uint64 `json:"gas" gencodec:"required"` |
|
|
|
GasLimit uint64 `json:"gas" gencodec:"required"` |
|
|
|
Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
|
|
|
|
Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
|
|
|
@ -72,22 +74,24 @@ type txdataMarshaling struct { |
|
|
|
S *hexutil.Big |
|
|
|
S *hexutil.Big |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewTransaction(nonce uint64, to common.Address, shardId uint32, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction { |
|
|
|
// NewTransaction returns new transaction.
|
|
|
|
return newTransaction(nonce, &to, shardId, amount, gasLimit, gasPrice, data) |
|
|
|
func NewTransaction(nonce uint64, to common.Address, shardID uint32, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction { |
|
|
|
|
|
|
|
return newTransaction(nonce, &to, shardID, amount, gasLimit, gasPrice, data) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewContractCreation(nonce uint64, shardId uint32, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction { |
|
|
|
// NewContractCreation returns contract transaction.
|
|
|
|
return newTransaction(nonce, nil, shardId, amount, gasLimit, gasPrice, data) |
|
|
|
func NewContractCreation(nonce uint64, shardID uint32, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction { |
|
|
|
|
|
|
|
return newTransaction(nonce, nil, shardID, amount, gasLimit, gasPrice, data) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newTransaction(nonce uint64, to *common.Address, shardId uint32, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction { |
|
|
|
func newTransaction(nonce uint64, to *common.Address, shardID uint32, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction { |
|
|
|
if len(data) > 0 { |
|
|
|
if len(data) > 0 { |
|
|
|
data = common.CopyBytes(data) |
|
|
|
data = common.CopyBytes(data) |
|
|
|
} |
|
|
|
} |
|
|
|
d := txdata{ |
|
|
|
d := txdata{ |
|
|
|
AccountNonce: nonce, |
|
|
|
AccountNonce: nonce, |
|
|
|
Recipient: to, |
|
|
|
Recipient: to, |
|
|
|
ShardID: shardId, |
|
|
|
ShardID: shardID, |
|
|
|
Payload: data, |
|
|
|
Payload: data, |
|
|
|
Amount: new(big.Int), |
|
|
|
Amount: new(big.Int), |
|
|
|
GasLimit: gasLimit, |
|
|
|
GasLimit: gasLimit, |
|
|
@ -106,9 +110,9 @@ func newTransaction(nonce uint64, to *common.Address, shardId uint32, amount *bi |
|
|
|
return &Transaction{data: d} |
|
|
|
return &Transaction{data: d} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ChainId returns which chain id this transaction was signed for (if at all)
|
|
|
|
// ChainID returns which chain id this transaction was signed for (if at all)
|
|
|
|
func (tx *Transaction) ChainId() *big.Int { |
|
|
|
func (tx *Transaction) ChainID() *big.Int { |
|
|
|
return deriveChainId(tx.data.V) |
|
|
|
return deriveChainID(tx.data.V) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Protected returns whether the transaction is protected from replay protection.
|
|
|
|
// Protected returns whether the transaction is protected from replay protection.
|
|
|
@ -160,7 +164,7 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error { |
|
|
|
if withSignature { |
|
|
|
if withSignature { |
|
|
|
var V byte |
|
|
|
var V byte |
|
|
|
if isProtectedV(dec.V) { |
|
|
|
if isProtectedV(dec.V) { |
|
|
|
chainID := deriveChainId(dec.V).Uint64() |
|
|
|
chainID := deriveChainID(dec.V).Uint64() |
|
|
|
V = byte(dec.V.Uint64() - 35 - 2*chainID) |
|
|
|
V = byte(dec.V.Uint64() - 35 - 2*chainID) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
V = byte(dec.V.Uint64() - 27) |
|
|
|
V = byte(dec.V.Uint64() - 27) |
|
|
@ -174,12 +178,35 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) } |
|
|
|
// Data returns data payload of Transaction.
|
|
|
|
func (tx *Transaction) Gas() uint64 { return tx.data.GasLimit } |
|
|
|
func (tx *Transaction) Data() []byte { |
|
|
|
func (tx *Transaction) GasPrice() *big.Int { return new(big.Int).Set(tx.data.Price) } |
|
|
|
return common.CopyBytes(tx.data.Payload) |
|
|
|
func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amount) } |
|
|
|
} |
|
|
|
func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce } |
|
|
|
|
|
|
|
func (tx *Transaction) CheckNonce() bool { return true } |
|
|
|
// Gas returns gas of Transaction.
|
|
|
|
|
|
|
|
func (tx *Transaction) Gas() uint64 { |
|
|
|
|
|
|
|
return tx.data.GasLimit |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GasPrice returns gas price of Transaction.
|
|
|
|
|
|
|
|
func (tx *Transaction) GasPrice() *big.Int { |
|
|
|
|
|
|
|
return new(big.Int).Set(tx.data.Price) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Value returns data payload of Transaction.
|
|
|
|
|
|
|
|
func (tx *Transaction) Value() *big.Int { |
|
|
|
|
|
|
|
return new(big.Int).Set(tx.data.Amount) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Nonce returns account nonce from Transaction.
|
|
|
|
|
|
|
|
func (tx *Transaction) Nonce() uint64 { |
|
|
|
|
|
|
|
return tx.data.AccountNonce |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CheckNonce returns check nonce from Transaction.
|
|
|
|
|
|
|
|
func (tx *Transaction) CheckNonce() bool { |
|
|
|
|
|
|
|
return true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// To returns the recipient address of the transaction.
|
|
|
|
// To returns the recipient address of the transaction.
|
|
|
|
// It returns nil if the transaction is a contract creation.
|
|
|
|
// It returns nil if the transaction is a contract creation.
|
|
|
@ -409,11 +436,42 @@ func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *b |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (m Message) From() common.Address { return m.from } |
|
|
|
// From returns from address from Message.
|
|
|
|
func (m Message) To() *common.Address { return m.to } |
|
|
|
func (m Message) From() common.Address { |
|
|
|
func (m Message) GasPrice() *big.Int { return m.gasPrice } |
|
|
|
return m.from |
|
|
|
func (m Message) Value() *big.Int { return m.amount } |
|
|
|
} |
|
|
|
func (m Message) Gas() uint64 { return m.gasLimit } |
|
|
|
|
|
|
|
func (m Message) Nonce() uint64 { return m.nonce } |
|
|
|
// To returns to address from Message.
|
|
|
|
func (m Message) Data() []byte { return m.data } |
|
|
|
func (m Message) To() *common.Address { |
|
|
|
func (m Message) CheckNonce() bool { return m.checkNonce } |
|
|
|
return m.to |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GasPrice returns gas price from Message.
|
|
|
|
|
|
|
|
func (m Message) GasPrice() *big.Int { |
|
|
|
|
|
|
|
return m.gasPrice |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Value returns the value amount from Message.
|
|
|
|
|
|
|
|
func (m Message) Value() *big.Int { |
|
|
|
|
|
|
|
return m.amount |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Gas returns gas limit of the Message.
|
|
|
|
|
|
|
|
func (m Message) Gas() uint64 { |
|
|
|
|
|
|
|
return m.gasLimit |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Nonce returns Nonce of the Message.
|
|
|
|
|
|
|
|
func (m Message) Nonce() uint64 { |
|
|
|
|
|
|
|
return m.nonce |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Data return data of the Message.
|
|
|
|
|
|
|
|
func (m Message) Data() []byte { |
|
|
|
|
|
|
|
return m.data |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CheckNonce returns checkNonce of Message.
|
|
|
|
|
|
|
|
func (m Message) CheckNonce() bool { |
|
|
|
|
|
|
|
return m.checkNonce |
|
|
|
|
|
|
|
} |
|
|
|