本文整理匯總了Golang中github.com/ethereum/go-ethereum/core/types.Transaction類的典型用法代碼示例。如果您正苦於以下問題:Golang Transaction類的具體用法?Golang Transaction怎麽用?Golang Transaction使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Transaction類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: queueTx
// queueTx will queue an unknown transaction
func (self *TxPool) queueTx(hash common.Hash, tx *types.Transaction) {
from, _ := tx.From() // already validated
if self.queue[from] == nil {
self.queue[from] = make(map[common.Hash]*types.Transaction)
}
self.queue[from][hash] = tx
}
示例2: ApplyTransaction
func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
// If we are mining this block and validating we want to set the logs back to 0
//statedb.EmptyLogs()
cb := statedb.GetStateObject(coinbase.Address())
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, block), tx, cb)
if err != nil && (IsNonceErr(err) || state.IsGasLimitErr(err) || IsInvalidTxErr(err)) {
// If the account is managed, remove the invalid nonce.
//from, _ := tx.From()
//self.bc.TxState().RemoveNonce(from, tx.Nonce())
return nil, nil, err
}
// Update the state with pending changes
statedb.Update()
cumulative := new(big.Int).Set(usedGas.Add(usedGas, gas))
receipt := types.NewReceipt(statedb.Root().Bytes(), cumulative)
logs := statedb.GetLogs(tx.Hash())
receipt.SetLogs(logs)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
glog.V(logger.Debug).Infoln(receipt)
// Notify all subscribers
if !transientProcess {
go self.eventMux.Post(TxPostEvent{tx})
go self.eventMux.Post(logs)
}
return receipt, gas, err
}
示例3: AppendTx
func (app *EthereumApplication) AppendTx(txBytes []byte) (retCode types.RetCode, result []byte, log string) {
// decode and run tx
tx := new(ethtypes.Transaction)
rlpStream := rlp.NewStream(bytes.NewBuffer(txBytes), 0)
if err := tx.DecodeRLP(rlpStream); err != nil {
return types.RetCodeEncodingError, result, log
}
gpi := big.NewInt(1000000000) // a billion ... TODO: configurable
gp := core.GasPool(*gpi) // XXX: this feels so wrong!?
ret, gas, err := core.ApplyMessage(NewEnv(app.stateDB, tx), tx, &gp)
if err != nil {
if err == ethtypes.ErrInvalidSig || err == ethtypes.ErrInvalidPubKey {
return types.RetCodeUnauthorized, result, err.Error()
} else if core.IsNonceErr(err) {
return types.RetCodeBadNonce, result, err.Error()
} else if core.IsInvalidTxErr(err) {
return types.RetCodeInsufficientFees, result, err.Error() // bad gas or value transfer
} else {
return types.RetCodeUnauthorized, result, err.Error() // bad pubkey recovery
}
}
_, _ = ret, gas
return types.RetCodeOK, result, log
}
示例4: ApplyTransaction
func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
// If we are mining this block and validating we want to set the logs back to 0
cb := statedb.GetStateObject(coinbase.Address())
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb)
if err != nil && (IsNonceErr(err) || state.IsGasLimitErr(err) || IsInvalidTxErr(err)) {
return nil, nil, err
}
// Update the state with pending changes
statedb.Update()
usedGas.Add(usedGas, gas)
receipt := types.NewReceipt(statedb.Root().Bytes(), usedGas)
logs := statedb.GetLogs(tx.Hash())
receipt.SetLogs(logs)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
glog.V(logger.Debug).Infoln(receipt)
// Notify all subscribers
if !transientProcess {
go self.eventMux.Post(TxPostEvent{tx})
go self.eventMux.Post(logs)
}
return receipt, gas, err
}
示例5: sign
func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) (*types.Transaction, error) {
hash := tx.SigHash()
sig, err := self.doSign(from, hash, didUnlock)
if err != nil {
return tx, err
}
return tx.WithSignature(sig)
}
示例6: sign
func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) error {
hash := tx.Hash()
sig, err := self.doSign(from, hash, didUnlock)
if err != nil {
return err
}
tx.SetSignatureValues(sig)
return nil
}
示例7: addTx
func (pool *TxPool) addTx(tx *types.Transaction) {
if _, ok := pool.txs[tx.Hash()]; !ok {
pool.txs[tx.Hash()] = tx
// Notify the subscribers. This event is posted in a goroutine
// because it's possible that somewhere during the post "Remove transaction"
// gets called which will then wait for the global tx pool lock and deadlock.
go pool.eventMux.Post(TxPreEvent{tx})
}
}
示例8: importTx
func (self *ImportMaster) importTx(tx *types.Transaction, blockId *bson.ObjectId) {
if glog.V(logger.Info) {
glog.Infoln("Importing tx", tx.Hash().Hex())
}
err := self.txCollection.Insert(self.parseTx(tx, blockId))
if err != nil {
clilogger.Infoln(err)
}
}
示例9: AddTx
// AddTx adds a transaction to the generated block. If no coinbase has
// been set, the block's coinbase is set to the zero address.
//
// AddTx panics if the transaction cannot be executed. In addition to
// the protocol-imposed limitations (gas limit, etc.), there are some
// further limitations on the content of transactions that can be
// added. Notably, contract code relying on the BLOCKHASH instruction
// will panic during execution.
func (b *BlockGen) AddTx(tx *types.Transaction) {
if b.gasPool == nil {
b.SetCoinbase(common.Address{})
}
b.statedb.StartRecord(tx.Hash(), common.Hash{}, len(b.txs))
receipt, _, _, err := ApplyTransaction(nil, b.gasPool, b.statedb, b.header, tx, b.header.GasUsed, nil)
if err != nil {
panic(err)
}
b.txs = append(b.txs, tx)
b.receipts = append(b.receipts, receipt)
}
示例10: BroadcastTx
func (app *EthereumApplication) BroadcastTx(tx *ethtypes.Transaction) error {
var result ctypes.TMResult
buf := new(bytes.Buffer)
if err := tx.EncodeRLP(buf); err != nil {
return err
}
params := map[string]interface{}{
"tx": hex.EncodeToString(buf.Bytes()),
}
_, err := app.client.Call("broadcast_tx", params, &result)
return err
}
示例11: parseTx
func (self *ImportMaster) parseTx(tx *types.Transaction, blockId *bson.ObjectId) *Transaction {
hash := tx.Hash().Hex()
from, err := tx.From()
if err != nil {
utils.Fatalf("Could not parse from address: %v", err)
}
var recipient string
if tx.Recipient != nil {
recipient = tx.Recipient.Hex()
}
txx := &Transaction{hash, recipient, from.Hex(), tx.Amount.String(), tx.Price.String(), tx.GasLimit.String(), tx.Payload, blockId}
return txx
}
示例12: addTx
// addTx will add a transaction to the pending (processable queue) list of transactions
func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Transaction) {
if _, ok := pool.pending[hash]; !ok {
pool.pending[hash] = tx
// Increment the nonce on the pending state. This can only happen if
// the nonce is +1 to the previous one.
pool.pendingState.SetNonce(addr, tx.Nonce()+1)
// Notify the subscribers. This event is posted in a goroutine
// because it's possible that somewhere during the post "Remove transaction"
// gets called which will then wait for the global tx pool lock and deadlock.
go pool.eventMux.Post(TxPreEvent{tx})
}
}
示例13: ApplyTransaction
func (self *BlockProcessor) ApplyTransaction(gp GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, gp)
if err != nil {
return nil, nil, err
}
// Update the state with pending changes
statedb.SyncIntermediate()
usedGas.Add(usedGas, gas)
receipt := types.NewReceipt(statedb.Root().Bytes(), usedGas)
receipt.TxHash = tx.Hash()
receipt.GasUsed = new(big.Int).Set(gas)
if MessageCreatesContract(tx) {
from, _ := tx.From()
receipt.ContractAddress = crypto.CreateAddress(from, tx.Nonce())
}
logs := statedb.GetLogs(tx.Hash())
receipt.SetLogs(logs)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
glog.V(logger.Debug).Infoln(receipt)
// Notify all subscribers
if !transientProcess {
go self.eventMux.Post(TxPostEvent{tx})
go self.eventMux.Post(logs)
}
return receipt, gas, err
}
示例14: validateTx
// validateTx checks whether a transaction is valid according
// to the consensus rules.
func (pool *TxPool) validateTx(tx *types.Transaction) error {
// Validate sender
var (
from common.Address
err error
)
// Drop transactions under our own minimal accepted gas price
if pool.minGasPrice.Cmp(tx.GasPrice()) > 0 {
return ErrCheap
}
// Validate the transaction sender and it's sig. Throw
// if the from fields is invalid.
if from, err = tx.From(); err != nil {
return ErrInvalidSender
}
// Make sure the account exist. Non existent accounts
// haven't got funds and well therefor never pass.
if !pool.currentState().HasAccount(from) {
return ErrNonExistentAccount
}
// Last but not least check for nonce errors
if pool.currentState().GetNonce(from) > tx.Nonce() {
return ErrNonce
}
// Check the transaction doesn't exceed the current
// block limit gas.
if pool.gasLimit().Cmp(tx.GasLimit) < 0 {
return ErrGasLimit
}
// Transactions can't be negative. This may never happen
// using RLP decoded transactions but may occur if you create
// a transaction using the RPC for example.
if tx.Amount.Cmp(common.Big0) < 0 {
return ErrNegativeValue
}
// Transactor should have enough funds to cover the costs
// cost == V + GP * GL
total := new(big.Int).Mul(tx.Price, tx.GasLimit)
total.Add(total, tx.Value())
if pool.currentState().GetBalance(from).Cmp(total) < 0 {
return ErrInsufficientFunds
}
// Should supply enough intrinsic gas
if tx.GasLimit.Cmp(IntrinsicGas(tx)) < 0 {
return ErrIntrinsicGas
}
return nil
}
示例15: AddTx
// AddTx adds a transaction to the generated block. If no coinbase has
// been set, the block's coinbase is set to the zero address.
//
// AddTx panics if the transaction cannot be executed. In addition to
// the protocol-imposed limitations (gas limit, etc.), there are some
// further limitations on the content of transactions that can be
// added. Notably, contract code relying on the BLOCKHASH instruction
// will panic during execution.
func (b *BlockGen) AddTx(tx *types.Transaction) {
if b.coinbase == nil {
b.SetCoinbase(common.Address{})
}
_, gas, err := ApplyMessage(NewEnv(b.statedb, nil, tx, b.header), tx, b.coinbase)
if err != nil {
panic(err)
}
b.statedb.SyncIntermediate()
b.header.GasUsed.Add(b.header.GasUsed, gas)
receipt := types.NewReceipt(b.statedb.Root().Bytes(), b.header.GasUsed)
logs := b.statedb.GetLogs(tx.Hash())
receipt.SetLogs(logs)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
b.txs = append(b.txs, tx)
b.receipts = append(b.receipts, receipt)
}