mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-26 19:21:44 +00:00
API to list accounts
This commit is contained in:
@ -22,8 +22,8 @@ type Node struct {
|
|||||||
pexReactor *p2p.PEXReactor
|
pexReactor *p2p.PEXReactor
|
||||||
blockStore *block.BlockStore
|
blockStore *block.BlockStore
|
||||||
mempoolReactor *mempool_.MempoolReactor
|
mempoolReactor *mempool_.MempoolReactor
|
||||||
|
consensusState *consensus.ConsensusState
|
||||||
consensusReactor *consensus.ConsensusReactor
|
consensusReactor *consensus.ConsensusReactor
|
||||||
state *state_.State
|
|
||||||
privValidator *state_.PrivValidator
|
privValidator *state_.PrivValidator
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +58,8 @@ func NewNode() *Node {
|
|||||||
mempoolReactor := mempool_.NewMempoolReactor(mempool)
|
mempoolReactor := mempool_.NewMempoolReactor(mempool)
|
||||||
|
|
||||||
// Get ConsensusReactor
|
// Get ConsensusReactor
|
||||||
consensusReactor := consensus.NewConsensusReactor(blockStore, mempoolReactor, state)
|
consensusState := consensus.NewConsensusState(state, blockStore, mempoolReactor)
|
||||||
|
consensusReactor := consensus.NewConsensusReactor(consensusState, blockStore)
|
||||||
if privValidator != nil {
|
if privValidator != nil {
|
||||||
consensusReactor.SetPrivValidator(privValidator)
|
consensusReactor.SetPrivValidator(privValidator)
|
||||||
}
|
}
|
||||||
@ -71,8 +72,8 @@ func NewNode() *Node {
|
|||||||
pexReactor: pexReactor,
|
pexReactor: pexReactor,
|
||||||
blockStore: blockStore,
|
blockStore: blockStore,
|
||||||
mempoolReactor: mempoolReactor,
|
mempoolReactor: mempoolReactor,
|
||||||
|
consensusState: consensusState,
|
||||||
consensusReactor: consensusReactor,
|
consensusReactor: consensusReactor,
|
||||||
state: state,
|
|
||||||
privValidator: privValidator,
|
privValidator: privValidator,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,7 +145,7 @@ func daemon() {
|
|||||||
// Run the RPC server.
|
// Run the RPC server.
|
||||||
if config.App.GetString("RPC.HTTP.ListenAddr") != "" {
|
if config.App.GetString("RPC.HTTP.ListenAddr") != "" {
|
||||||
rpc.SetRPCBlockStore(n.blockStore)
|
rpc.SetRPCBlockStore(n.blockStore)
|
||||||
rpc.SetRPCState(n.state)
|
rpc.SetRPCConsensusState(n.consensusState)
|
||||||
rpc.SetRPCMempoolReactor(n.mempoolReactor)
|
rpc.SetRPCMempoolReactor(n.mempoolReactor)
|
||||||
rpc.StartHTTPServer()
|
rpc.StartHTTPServer()
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
. "github.com/tendermint/tendermint/block"
|
. "github.com/tendermint/tendermint/block"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
. "github.com/tendermint/tendermint/consensus/types"
|
. "github.com/tendermint/tendermint/consensus/types"
|
||||||
"github.com/tendermint/tendermint/mempool"
|
|
||||||
"github.com/tendermint/tendermint/p2p"
|
"github.com/tendermint/tendermint/p2p"
|
||||||
"github.com/tendermint/tendermint/state"
|
"github.com/tendermint/tendermint/state"
|
||||||
)
|
)
|
||||||
@ -39,12 +38,11 @@ type ConsensusReactor struct {
|
|||||||
conS *ConsensusState
|
conS *ConsensusState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConsensusReactor(blockStore *BlockStore, mempoolReactor *mempool.MempoolReactor, state *state.State) *ConsensusReactor {
|
func NewConsensusReactor(consensusState *ConsensusState, blockStore *BlockStore) *ConsensusReactor {
|
||||||
conS := NewConsensusState(state, blockStore, mempoolReactor)
|
|
||||||
conR := &ConsensusReactor{
|
conR := &ConsensusReactor{
|
||||||
blockStore: blockStore,
|
blockStore: blockStore,
|
||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
conS: conS,
|
conS: consensusState,
|
||||||
}
|
}
|
||||||
return conR
|
return conR
|
||||||
}
|
}
|
||||||
|
@ -259,6 +259,12 @@ func NewConsensusState(state *state.State, blockStore *BlockStore, mempoolReacto
|
|||||||
return cs
|
return cs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cs *ConsensusState) GetState() *state.State {
|
||||||
|
cs.mtx.Lock()
|
||||||
|
defer cs.mtx.Unlock()
|
||||||
|
return cs.state.Copy()
|
||||||
|
}
|
||||||
|
|
||||||
func (cs *ConsensusState) GetRoundState() *RoundState {
|
func (cs *ConsensusState) GetRoundState() *RoundState {
|
||||||
cs.mtx.Lock()
|
cs.mtx.Lock()
|
||||||
defer cs.mtx.Unlock()
|
defer cs.mtx.Unlock()
|
||||||
|
@ -54,3 +54,18 @@ func SignSendTxHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
WriteAPIResponse(w, API_OK, res)
|
WriteAPIResponse(w, API_OK, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
type ListAccountsResponse struct {
|
||||||
|
Accounts []*account.Account
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
state := consensusState.GetState()
|
||||||
|
state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
|
||||||
|
log.Warn(">>", "key", key, "value", value)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
WriteAPIResponse(w, API_OK, state)
|
||||||
|
}
|
||||||
|
@ -14,6 +14,7 @@ func StartHTTPServer() {
|
|||||||
http.HandleFunc("/broadcast_tx", BroadcastTxHandler)
|
http.HandleFunc("/broadcast_tx", BroadcastTxHandler)
|
||||||
http.HandleFunc("/gen_priv_account", GenPrivAccountHandler)
|
http.HandleFunc("/gen_priv_account", GenPrivAccountHandler)
|
||||||
http.HandleFunc("/sign_send_tx", SignSendTxHandler)
|
http.HandleFunc("/sign_send_tx", SignSendTxHandler)
|
||||||
|
http.HandleFunc("/accounts", ListAccountsHandler)
|
||||||
|
|
||||||
log.Info(Fmt("Starting RPC HTTP server on %s", config.App.GetString("RPC.HTTP.ListenAddr")))
|
log.Info(Fmt("Starting RPC HTTP server on %s", config.App.GetString("RPC.HTTP.ListenAddr")))
|
||||||
|
|
||||||
|
@ -2,20 +2,20 @@ package rpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
block_ "github.com/tendermint/tendermint/block"
|
block_ "github.com/tendermint/tendermint/block"
|
||||||
|
"github.com/tendermint/tendermint/consensus"
|
||||||
mempool_ "github.com/tendermint/tendermint/mempool"
|
mempool_ "github.com/tendermint/tendermint/mempool"
|
||||||
state_ "github.com/tendermint/tendermint/state"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var blockStore *block_.BlockStore
|
var blockStore *block_.BlockStore
|
||||||
var state *state_.State
|
var consensusState *consensus.ConsensusState
|
||||||
var mempoolReactor *mempool_.MempoolReactor
|
var mempoolReactor *mempool_.MempoolReactor
|
||||||
|
|
||||||
func SetRPCBlockStore(bs *block_.BlockStore) {
|
func SetRPCBlockStore(bs *block_.BlockStore) {
|
||||||
blockStore = bs
|
blockStore = bs
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetRPCState(s *state_.State) {
|
func SetRPCConsensusState(cs *consensus.ConsensusState) {
|
||||||
state = s
|
consensusState = cs
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetRPCMempoolReactor(mr *mempool_.MempoolReactor) {
|
func SetRPCMempoolReactor(mr *mempool_.MempoolReactor) {
|
||||||
|
@ -254,7 +254,7 @@ func (s *State) ExecTx(tx_ Tx) error {
|
|||||||
// Good! Adjust accounts
|
// Good! Adjust accounts
|
||||||
s.AdjustByInputs(accounts, tx.Inputs)
|
s.AdjustByInputs(accounts, tx.Inputs)
|
||||||
s.AdjustByOutputs(accounts, tx.Outputs)
|
s.AdjustByOutputs(accounts, tx.Outputs)
|
||||||
s.SetAccounts(accounts)
|
s.UpdateAccounts(accounts)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case *BondTx:
|
case *BondTx:
|
||||||
@ -289,7 +289,7 @@ func (s *State) ExecTx(tx_ Tx) error {
|
|||||||
|
|
||||||
// Good! Adjust accounts
|
// Good! Adjust accounts
|
||||||
s.AdjustByInputs(accounts, tx.Inputs)
|
s.AdjustByInputs(accounts, tx.Inputs)
|
||||||
s.SetAccounts(accounts)
|
s.UpdateAccounts(accounts)
|
||||||
// Add ValidatorInfo
|
// Add ValidatorInfo
|
||||||
s.SetValidatorInfo(&ValidatorInfo{
|
s.SetValidatorInfo(&ValidatorInfo{
|
||||||
Address: tx.PubKey.Address(),
|
Address: tx.PubKey.Address(),
|
||||||
@ -355,8 +355,6 @@ func (s *State) ExecTx(tx_ Tx) error {
|
|||||||
return errors.New("Invalid rebond height")
|
return errors.New("Invalid rebond height")
|
||||||
}
|
}
|
||||||
|
|
||||||
// tx.Height must be
|
|
||||||
|
|
||||||
// Good!
|
// Good!
|
||||||
s.rebondValidator(val)
|
s.rebondValidator(val)
|
||||||
return nil
|
return nil
|
||||||
@ -444,7 +442,7 @@ func (s *State) releaseValidator(val *Validator) {
|
|||||||
panic("Couldn't get or make unbondTo accounts")
|
panic("Couldn't get or make unbondTo accounts")
|
||||||
}
|
}
|
||||||
s.AdjustByOutputs(accounts, valInfo.UnbondTo)
|
s.AdjustByOutputs(accounts, valInfo.UnbondTo)
|
||||||
s.SetAccounts(accounts)
|
s.UpdateAccounts(accounts)
|
||||||
|
|
||||||
// Remove validator from UnbondingValidators
|
// Remove validator from UnbondingValidators
|
||||||
_, removed := s.UnbondingValidators.Remove(val.Address)
|
_, removed := s.UnbondingValidators.Remove(val.Address)
|
||||||
@ -612,15 +610,21 @@ func (s *State) GetAccount(address []byte) *Account {
|
|||||||
return account.(*Account).Copy()
|
return account.(*Account).Copy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The returned Account is a copy, so mutating it
|
||||||
|
// has no side effects.
|
||||||
|
func (s *State) GetAccounts() merkle.Tree {
|
||||||
|
return s.accounts.Copy()
|
||||||
|
}
|
||||||
|
|
||||||
// The account is copied before setting, so mutating it
|
// The account is copied before setting, so mutating it
|
||||||
// afterwards has no side effects.
|
// afterwards has no side effects.
|
||||||
func (s *State) SetAccount(account *Account) {
|
func (s *State) UpdateAccount(account *Account) {
|
||||||
s.accounts.Set(account.Address, account.Copy())
|
s.accounts.Set(account.Address, account.Copy())
|
||||||
}
|
}
|
||||||
|
|
||||||
// The accounts are copied before setting, so mutating it
|
// The accounts are copied before setting, so mutating it
|
||||||
// afterwards has no side effects.
|
// afterwards has no side effects.
|
||||||
func (s *State) SetAccounts(accounts map[string]*Account) {
|
func (s *State) UpdateAccounts(accounts map[string]*Account) {
|
||||||
for _, account := range accounts {
|
for _, account := range accounts {
|
||||||
s.accounts.Set(account.Address, account.Copy())
|
s.accounts.Set(account.Address, account.Copy())
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ func TestCopyState(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Setting, however, should change the balance.
|
// Setting, however, should change the balance.
|
||||||
s0.SetAccount(acc)
|
s0.UpdateAccount(acc)
|
||||||
if s0.GetAccount(acc0Address).Balance != acc.Balance {
|
if s0.GetAccount(acc0Address).Balance != acc.Balance {
|
||||||
t.Error("Account balance wasn't set")
|
t.Error("Account balance wasn't set")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user