aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/state.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-02-24 19:10:45 +0800
committerobscuren <geffobscura@gmail.com>2014-02-24 19:10:45 +0800
commit377c9951033d4f8d157221fd36d15c39ae17cddc (patch)
tree41c1c6ec8e2e4998f10fba488ea2664c8d8e8ff3 /ethchain/state.go
parent3a45cdeaf9682dea0407f827571353220eaf257b (diff)
downloadgo-tangerine-377c9951033d4f8d157221fd36d15c39ae17cddc.tar
go-tangerine-377c9951033d4f8d157221fd36d15c39ae17cddc.tar.gz
go-tangerine-377c9951033d4f8d157221fd36d15c39ae17cddc.tar.bz2
go-tangerine-377c9951033d4f8d157221fd36d15c39ae17cddc.tar.lz
go-tangerine-377c9951033d4f8d157221fd36d15c39ae17cddc.tar.xz
go-tangerine-377c9951033d4f8d157221fd36d15c39ae17cddc.tar.zst
go-tangerine-377c9951033d4f8d157221fd36d15c39ae17cddc.zip
Separated the VM from the block manager and added states
Diffstat (limited to 'ethchain/state.go')
-rw-r--r--ethchain/state.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/ethchain/state.go b/ethchain/state.go
new file mode 100644
index 000000000..1a18ea1d7
--- /dev/null
+++ b/ethchain/state.go
@@ -0,0 +1,56 @@
+package ethchain
+
+import (
+ "github.com/ethereum/eth-go/ethutil"
+ "math/big"
+)
+
+type State struct {
+ trie *ethutil.Trie
+}
+
+func NewState(trie *ethutil.Trie) *State {
+ return &State{trie: trie}
+}
+
+func (s *State) GetContract(addr []byte) *Contract {
+ data := s.trie.Get(string(addr))
+ if data == "" {
+ return nil
+ }
+
+ contract := &Contract{}
+ contract.RlpDecode([]byte(data))
+
+ return contract
+}
+
+func (s *State) UpdateContract(addr []byte, contract *Contract) {
+ s.trie.Update(string(addr), string(contract.RlpEncode()))
+}
+
+func Compile(code []string) (script []string) {
+ script = make([]string, len(code))
+ for i, val := range code {
+ instr, _ := ethutil.CompileInstr(val)
+
+ script[i] = string(instr)
+ }
+
+ return
+}
+
+func (s *State) GetAccount(addr []byte) (account *Address) {
+ data := s.trie.Get(string(addr))
+ if data == "" {
+ account = NewAddress(big.NewInt(0))
+ } else {
+ account = NewAddressFromData([]byte(data))
+ }
+
+ return
+}
+
+func (s *State) UpdateAccount(addr []byte, account *Address) {
+ s.trie.Update(string(addr), string(account.RlpEncode()))
+}