| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | package ethchain | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/ethereum/eth-go/ethutil" | 
					
						
							|  |  |  | 	"math/big" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | type Account struct { | 
					
						
							| 
									
										
										
										
											2014-03-21 14:47:55 +01:00
										 |  |  | 	address []byte | 
					
						
							| 
									
										
										
										
											2014-03-20 23:17:53 +01:00
										 |  |  | 	Amount  *big.Int | 
					
						
							|  |  |  | 	Nonce   uint64 | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 23:17:53 +01:00
										 |  |  | func NewAccount(address []byte, amount *big.Int) *Account { | 
					
						
							|  |  |  | 	return &Account{address, amount, 0} | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 23:17:53 +01:00
										 |  |  | func NewAccountFromData(address, data []byte) *Account { | 
					
						
							| 
									
										
										
										
											2014-03-21 14:47:55 +01:00
										 |  |  | 	account := &Account{address: address} | 
					
						
							| 
									
										
										
										
											2014-03-20 23:17:53 +01:00
										 |  |  | 	account.RlpDecode(data) | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 23:17:53 +01:00
										 |  |  | 	return account | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | func (a *Account) AddFee(fee *big.Int) { | 
					
						
							| 
									
										
										
										
											2014-03-20 17:24:02 +01:00
										 |  |  | 	a.AddFunds(fee) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (a *Account) AddFunds(funds *big.Int) { | 
					
						
							|  |  |  | 	a.Amount.Add(a.Amount, funds) | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-21 14:47:55 +01:00
										 |  |  | func (a *Account) Address() []byte { | 
					
						
							|  |  |  | 	return a.address | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 23:17:53 +01:00
										 |  |  | // Implements Callee | 
					
						
							|  |  |  | func (a *Account) ReturnGas(value *big.Int, state *State) { | 
					
						
							|  |  |  | 	// Return the value back to the sender | 
					
						
							|  |  |  | 	a.AddFunds(value) | 
					
						
							| 
									
										
										
										
											2014-03-21 14:47:55 +01:00
										 |  |  | 	state.UpdateAccount(a.address, a) | 
					
						
							| 
									
										
										
										
											2014-03-20 23:17:53 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | func (a *Account) RlpEncode() []byte { | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | 	return ethutil.Encode([]interface{}{a.Amount, a.Nonce}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | func (a *Account) RlpDecode(data []byte) { | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | 	decoder := ethutil.NewValueFromBytes(data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a.Amount = decoder.Get(0).BigInt() | 
					
						
							|  |  |  | 	a.Nonce = decoder.Get(1).Uint() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type AddrStateStore struct { | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | 	states map[string]*AccountState | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewAddrStateStore() *AddrStateStore { | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | 	return &AddrStateStore{states: make(map[string]*AccountState)} | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | func (s *AddrStateStore) Add(addr []byte, account *Account) *AccountState { | 
					
						
							|  |  |  | 	state := &AccountState{Nonce: account.Nonce, Account: account} | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | 	s.states[string(addr)] = state | 
					
						
							|  |  |  | 	return state | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | func (s *AddrStateStore) Get(addr []byte) *AccountState { | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | 	return s.states[string(addr)] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | type AccountState struct { | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | 	Nonce   uint64 | 
					
						
							| 
									
										
										
										
											2014-03-03 11:05:12 +01:00
										 |  |  | 	Account *Account | 
					
						
							| 
									
										
										
										
											2014-02-23 01:56:48 +01:00
										 |  |  | } |