tendermint/mempool/mempool.go

49 lines
1.2 KiB
Go
Raw Normal View History

2014-09-10 02:43:16 -07:00
package mempool
import (
"sync"
. "github.com/tendermint/tendermint/blocks"
. "github.com/tendermint/tendermint/state"
)
/*
Mempool receives new transactions and applies them to the latest committed state.
If the transaction is acceptable, then it broadcasts a fingerprint to peers.
The transaction fingerprint is a short sequence of bytes (shorter than a full hash).
Each peer connection uses a different algorithm for turning the tx hash into a
fingerprint in order to prevent transaction blocking attacks. Upon inspecting a
tx fingerprint, the receiver may query the source for the full tx bytes.
When this node happens to be the next proposer, it simply takes the recently
modified state (and the associated transactions) and use that as the proposal.
*/
//-----------------------------------------------------------------------------
type Mempool struct {
mtx sync.Mutex
state *State
txs []Tx
2014-09-10 02:43:16 -07:00
}
func NewMempool(state *State) *Mempool {
return &Mempool{
state: state,
}
}
func (mem *Mempool) AddTx(tx Tx) (err error) {
2014-09-10 02:43:16 -07:00
mem.mtx.Lock()
defer mem.mtx.Unlock()
// Add the tx to the state.
err = mem.state.CommitTx(tx)
if err != nil {
return err
2014-09-10 02:43:16 -07:00
} else {
mem.txs = append(mem.txs, tx)
return nil
2014-09-10 02:43:16 -07:00
}
}