diff --git a/common/word.go b/common/word.go index d67730e13..7c5c8a81a 100644 --- a/common/word.go +++ b/common/word.go @@ -13,10 +13,11 @@ var ( type Word256 [32]byte -func (w Word256) String() string { return string(w[:]) } -func (w Word256) Copy() Word256 { return w } -func (w Word256) Bytes() []byte { return w[:] } // copied. -func (w Word256) Prefix(n int) []byte { return w[:n] } +func (w Word256) String() string { return string(w[:]) } +func (w Word256) Copy() Word256 { return w } +func (w Word256) Bytes() []byte { return w[:] } // copied. +func (w Word256) Prefix(n int) []byte { return w[:n] } +func (w Word256) Postfix(n int) []byte { return w[32-n:] } func (w Word256) IsZero() bool { accum := byte(0) for _, byt := range w { diff --git a/state/block_cache.go b/state/block_cache.go index 1fc09f1ac..277754541 100644 --- a/state/block_cache.go +++ b/state/block_cache.go @@ -91,13 +91,13 @@ func (cache *BlockCache) GetStorage(addr Word256, key Word256) (value Word256) { } // Get or load storage - acc, storage, removed, dirty := cache.accounts[string(addr.Prefix(20))].unpack() + acc, storage, removed, dirty := cache.accounts[string(addr.Postfix(20))].unpack() if removed { panic("GetStorage() on removed account") } if acc != nil && storage == nil { storage = makeStorage(cache.db, acc.StorageRoot) - cache.accounts[string(addr.Prefix(20))] = accountInfo{acc, storage, false, dirty} + cache.accounts[string(addr.Postfix(20))] = accountInfo{acc, storage, false, dirty} } else if acc == nil { return Zero256 } @@ -114,7 +114,7 @@ func (cache *BlockCache) GetStorage(addr Word256, key Word256) (value Word256) { // NOTE: Set value to zero to removed from the trie. func (cache *BlockCache) SetStorage(addr Word256, key Word256, value Word256) { - _, _, removed, _ := cache.accounts[string(addr.Prefix(20))].unpack() + _, _, removed, _ := cache.accounts[string(addr.Postfix(20))].unpack() if removed { panic("SetStorage() on a removed account") } @@ -146,7 +146,7 @@ func (cache *BlockCache) Sync() { for _, storageKey := range storageKeys { addr, key := Tuple256Split(storageKey) if addr != curAddr || curAcc == nil { - acc, storage, removed, _ := cache.accounts[string(addr.Prefix(20))].unpack() + acc, storage, removed, _ := cache.accounts[string(addr.Postfix(20))].unpack() if storage == nil { storage = makeStorage(cache.db, acc.StorageRoot) } @@ -166,7 +166,7 @@ func (cache *BlockCache) Sync() { curStorage.Remove(key.Bytes()) } else { curStorage.Set(key.Bytes(), value.Bytes()) - cache.accounts[string(addr.Prefix(20))] = accountInfo{curAcc, curStorage, false, true} + cache.accounts[string(addr.Postfix(20))] = accountInfo{curAcc, curStorage, false, true} } } diff --git a/state/tx_cache.go b/state/tx_cache.go index 425a1f816..dbf865bab 100644 --- a/state/tx_cache.go +++ b/state/tx_cache.go @@ -31,7 +31,7 @@ func (cache *TxCache) GetAccount(addr Word256) *vm.Account { if removed { return nil } else if acc == nil { - acc2 := cache.backend.GetAccount(addr.Prefix(20)) + acc2 := cache.backend.GetAccount(addr.Postfix(20)) if acc2 != nil { return toVMAccount(acc2) } @@ -68,7 +68,7 @@ func (cache *TxCache) CreateAccount(creator *vm.Account) *vm.Account { nonce := creator.Nonce creator.Nonce += 1 - addr := RightPadWord256(NewContractAddress(creator.Address.Prefix(20), nonce)) + addr := LeftPadWord256(NewContractAddress(creator.Address.Postfix(20), nonce)) // Create account from address. account, removed := vmUnpack(cache.accounts[addr]) @@ -128,7 +128,7 @@ func (cache *TxCache) Sync() { for addr, accInfo := range cache.accounts { acc, removed := vmUnpack(accInfo) if removed { - cache.backend.RemoveAccount(addr.Prefix(20)) + cache.backend.RemoveAccount(addr.Postfix(20)) } else { cache.backend.UpdateAccount(toStateAccount(acc)) } @@ -154,7 +154,7 @@ func NewContractAddress(caller []byte, nonce uint64) []byte { // Converts backend.Account to vm.Account struct. func toVMAccount(acc *ac.Account) *vm.Account { return &vm.Account{ - Address: RightPadWord256(acc.Address), + Address: LeftPadWord256(acc.Address), Balance: acc.Balance, Code: acc.Code, // This is crazy. Nonce: uint64(acc.Sequence), @@ -176,7 +176,7 @@ func toStateAccount(acc *vm.Account) *ac.Account { storageRoot = acc.StorageRoot.Bytes() } return &ac.Account{ - Address: acc.Address.Prefix(20), + Address: acc.Address.Postfix(20), PubKey: pubKey, Balance: acc.Balance, Code: acc.Code, diff --git a/vm/vm.go b/vm/vm.go index 79b2a3345..f2e869cb4 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -94,9 +94,9 @@ func (vm *VM) Call(caller, callee *Account, code, input []byte, value uint64, ga // if callDepth is 0 the event is fired from ExecTx (along with the Input event) // otherwise, we fire from here. if vm.callDepth != 0 && vm.evc != nil { - vm.evc.FireEvent(types.EventStringAccReceive(callee.Address.Prefix(20)), types.EventMsgCall{ - &types.CallData{caller.Address.Prefix(20), callee.Address.Prefix(20), input, value, *gas}, - vm.origin.Prefix(20), + vm.evc.FireEvent(types.EventStringAccReceive(callee.Address.Postfix(20)), types.EventMsgCall{ + &types.CallData{caller.Address.Postfix(20), callee.Address.Postfix(20), input, value, *gas}, + vm.origin.Postfix(20), vm.txid, output, exception, @@ -354,7 +354,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga if ok = useGas(gas, GasGetAccount); !ok { return nil, firstErr(err, ErrInsufficientGas) } - acc := vm.appState.GetAccount(addr) // TODO ensure that 20byte lengths are supported. + acc := vm.appState.GetAccount(flipWord(addr)) // TODO ensure that 20byte lengths are supported. if acc == nil { return nil, firstErr(err, ErrUnknownAddress) } @@ -431,7 +431,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga if ok = useGas(gas, GasGetAccount); !ok { return nil, firstErr(err, ErrInsufficientGas) } - acc := vm.appState.GetAccount(addr) + acc := vm.appState.GetAccount(flipWord(addr)) if acc == nil { return nil, firstErr(err, ErrUnknownAddress) } @@ -445,7 +445,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga if ok = useGas(gas, GasGetAccount); !ok { return nil, firstErr(err, ErrInsufficientGas) } - acc := vm.appState.GetAccount(addr) + acc := vm.appState.GetAccount(flipWord(addr)) if acc == nil { return nil, firstErr(err, ErrUnknownAddress) } @@ -652,9 +652,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga if ok = useGas(gas, GasGetAccount); !ok { return nil, firstErr(err, ErrInsufficientGas) } - // :( - addr = RightPadWord256(flip(addr.Prefix(20))) - acc := vm.appState.GetAccount(addr) + acc := vm.appState.GetAccount(flipWord(addr)) if acc == nil { return nil, firstErr(err, ErrUnknownAddress) } @@ -697,7 +695,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga return nil, firstErr(err, ErrInsufficientGas) } // TODO if the receiver is , then make it the fee. - receiver := vm.appState.GetAccount(addr) + receiver := vm.appState.GetAccount(flipWord(addr)) if receiver == nil { return nil, firstErr(err, ErrUnknownAddress) }