diff options
author | Sonic <sonic@dexon.org> | 2018-11-09 17:24:54 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2018-12-19 20:54:27 +0800 |
commit | 770a1b7d8ce9d11b79d9e0f5ee8cc0cf33aaea8a (patch) | |
tree | 4ce0608a356ca4928b42bdc7a0305a567da9b1fd | |
parent | 6b9db92147972dacc9999f9fa63df1fa712b05d6 (diff) | |
download | dexon-770a1b7d8ce9d11b79d9e0f5ee8cc0cf33aaea8a.tar.gz dexon-770a1b7d8ce9d11b79d9e0f5ee8cc0cf33aaea8a.tar.zst dexon-770a1b7d8ce9d11b79d9e0f5ee8cc0cf33aaea8a.zip |
core: support extracting governance state from state trie
-rw-r--r-- | core/blockchain.go | 27 | ||||
-rw-r--r-- | core/state/gov.go | 31 | ||||
-rw-r--r-- | core/types/govstate.go | 15 |
3 files changed, 73 insertions, 0 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index ea1c9a210..63e16a17c 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1734,6 +1734,33 @@ func (bc *BlockChain) GetPending() (*types.Block, *state.StateDB) { return block, s } +// GetGovStateByHash extracts the governance contract's state from state trie +// (the merkle proof of governance contract address and the whole storage) +// at the given block hash. +func (bc *BlockChain) GetGovStateByHash(hash common.Hash) (*types.GovState, error) { + header := bc.GetHeaderByHash(hash) + if header == nil { + return nil, fmt.Errorf("header not found") + } + statedb, err := bc.StateAt(header.Root) + if err != nil { + return nil, err + } + return state.GetGovState(statedb, header, vm.GovernanceContractAddress) +} + +func (bc *BlockChain) GetGovStateByNumber(number uint64) (*types.GovState, error) { + header := bc.GetHeaderByNumber(number) + if header == nil { + return nil, fmt.Errorf("header not found") + } + statedb, err := bc.StateAt(header.Root) + if err != nil { + return nil, err + } + return state.GetGovState(statedb, header, vm.GovernanceContractAddress) +} + // reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them // to be part of the new canonical chain and accumulates potential missing transactions and post an // event about them diff --git a/core/state/gov.go b/core/state/gov.go new file mode 100644 index 000000000..8b94cfab2 --- /dev/null +++ b/core/state/gov.go @@ -0,0 +1,31 @@ +package state + +import ( + "github.com/dexon-foundation/dexon/common" + "github.com/dexon-foundation/dexon/core/types" + "github.com/dexon-foundation/dexon/trie" +) + +func GetGovState(statedb *StateDB, header *types.Header, + addr common.Address) (*types.GovState, error) { + proof, err := statedb.GetProof(addr) + if err != nil { + return nil, err + } + + govState := &types.GovState{ + BlockHash: header.Hash(), + Number: header.Number, + Root: header.Root, + Proof: proof, + } + + if t := statedb.StorageTrie(addr); t != nil { + it := trie.NewIterator(t.NodeIterator(nil)) + for it.Next() { + govState.Storage = append(govState.Storage, + [2][]byte{it.Key, it.Value}) + } + } + return govState, nil +} diff --git a/core/types/govstate.go b/core/types/govstate.go new file mode 100644 index 000000000..75f77cb0d --- /dev/null +++ b/core/types/govstate.go @@ -0,0 +1,15 @@ +package types + +import ( + "math/big" + + "github.com/dexon-foundation/dexon/common" +) + +type GovState struct { + BlockHash common.Hash + Number *big.Int + Root common.Hash + Proof [][]byte + Storage [][2][]byte +} |