mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-05 23:57:04 +00:00
Address is generated with VMAppState, and it increments the nonce too.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
. "github.com/tendermint/tendermint/vm"
|
||||
"github.com/tendermint/tendermint/vm/sha3"
|
||||
)
|
||||
|
||||
type FakeAppState struct {
|
||||
@@ -43,7 +44,8 @@ func (fas *FakeAppState) DeleteAccount(account *Account) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (fas *FakeAppState) CreateAccount(addr Word) (*Account, error) {
|
||||
func (fas *FakeAppState) CreateAccount(creator *Account) (*Account, error) {
|
||||
addr := createAddress(creator)
|
||||
account := fas.accounts[addr.String()]
|
||||
if account == nil {
|
||||
return &Account{
|
||||
@@ -102,16 +104,24 @@ func main() {
|
||||
ourVm := NewVM(appState, params, Zero)
|
||||
|
||||
// Create accounts
|
||||
account1, err := appState.CreateAccount(Uint64ToWord(100))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
account1 := &Account{
|
||||
Address: Uint64ToWord(100),
|
||||
}
|
||||
account2, err := appState.CreateAccount(Uint64ToWord(101))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
account2 := &Account{
|
||||
Address: Uint64ToWord(101),
|
||||
}
|
||||
|
||||
var gas uint64 = 1000
|
||||
output, err := ourVm.Call(account1, account2, []byte{0x60, 0x01, 0x60, 0x01}, []byte{}, 0, &gas)
|
||||
fmt.Printf("Output: %v Error: %v\n", output, err)
|
||||
}
|
||||
|
||||
// Creates a 20 byte address and bumps the nonce.
|
||||
func createAddress(creator *Account) Word {
|
||||
nonce := creator.Nonce
|
||||
creator.Nonce += 1
|
||||
temp := make([]byte, 32+8)
|
||||
copy(temp, creator.Address[:])
|
||||
PutUint64(temp[32:], nonce)
|
||||
return RightPadWord(sha3.Sha3(temp)[:20])
|
||||
}
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ type AppState interface {
|
||||
GetAccount(addr Word) (*Account, error)
|
||||
UpdateAccount(*Account) error
|
||||
DeleteAccount(*Account) error
|
||||
CreateAccount(addr Word) (*Account, error)
|
||||
CreateAccount(*Account) (*Account, error)
|
||||
|
||||
// Storage
|
||||
GetStorage(Word, Word) (Word, error)
|
||||
|
||||
@@ -46,6 +46,7 @@ func NewVM(appState AppState, params Params, origin Word) *VM {
|
||||
}
|
||||
}
|
||||
|
||||
// CONTRACT appState is aware of caller and callee, so we can just mutate them.
|
||||
// value: To be transferred from caller to callee. Refunded upon error.
|
||||
// gas: Available gas. No refunds for gas.
|
||||
func (vm *VM) Call(caller, callee *Account, code, input []byte, value uint64, gas *uint64) (output []byte, err error) {
|
||||
@@ -541,14 +542,9 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga
|
||||
return nil, firstErr(err, ErrInsufficientBalance)
|
||||
}
|
||||
|
||||
// Create a new address
|
||||
nonce := caller.Nonce
|
||||
addr := createAddress(caller.Address, nonce)
|
||||
caller.Nonce += 1
|
||||
|
||||
// TODO charge for gas to create account _ the code length * GasCreateByte
|
||||
|
||||
newAccount, err := vm.appState.CreateAccount(addr)
|
||||
newAccount, err := vm.appState.CreateAccount(caller)
|
||||
if err != nil {
|
||||
stack.Push(Zero)
|
||||
fmt.Printf(" (*) 0x0 %v\n", err)
|
||||
@@ -708,14 +704,6 @@ func useGas(gas *uint64, gasToUse uint64) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a 20 byte address from the creatorAddr and nonce.
|
||||
func createAddress(creatorAddr Word, nonce uint64) Word {
|
||||
temp := make([]byte, 32+8)
|
||||
copy(temp, creatorAddr[:])
|
||||
PutUint64(temp[32:], nonce)
|
||||
return RightPadWord(sha3.Sha3(temp)[:20])
|
||||
}
|
||||
|
||||
func transfer(from, to *Account, amount uint64) error {
|
||||
if from.Balance < amount {
|
||||
return ErrInsufficientBalance
|
||||
|
||||
Reference in New Issue
Block a user