go-ethereum/xeth/types.go

241 lines
6.4 KiB
Go
Raw Normal View History

2015-07-07 03:54:22 +03:00
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
2015-07-07 03:54:22 +03:00
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
2015-07-07 03:54:22 +03:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-07-07 03:54:22 +03:00
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
2015-07-07 03:54:22 +03:00
2014-10-31 15:30:08 +02:00
package xeth
import (
"bytes"
"fmt"
"strings"
2015-03-18 12:44:25 +02:00
"github.com/ethereum/go-ethereum/common"
2014-12-04 11:28:02 +02:00
"github.com/ethereum/go-ethereum/core"
2015-03-26 01:42:35 +02:00
"github.com/ethereum/go-ethereum/core/state"
2014-12-04 11:28:02 +02:00
"github.com/ethereum/go-ethereum/core/types"
2014-10-31 13:37:43 +02:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
)
2015-01-28 19:35:49 +02:00
type Object struct {
*state.StateObject
}
func NewObject(state *state.StateObject) *Object {
return &Object{state}
}
2015-06-17 12:44:40 +03:00
func (self *Object) StorageString(str string) []byte {
2015-03-16 12:27:38 +02:00
if common.IsHex(str) {
return self.storage(common.Hex2Bytes(str[2:]))
2015-01-28 19:35:49 +02:00
} else {
2015-03-16 12:27:38 +02:00
return self.storage(common.RightPadBytes([]byte(str), 32))
2015-01-28 19:35:49 +02:00
}
}
2015-06-17 12:44:40 +03:00
func (self *Object) StorageValue(addr *common.Value) []byte {
return self.storage(addr.Bytes())
2015-01-28 19:35:49 +02:00
}
2015-06-17 12:44:40 +03:00
func (self *Object) storage(addr []byte) []byte {
return self.StateObject.GetState(common.BytesToHash(addr)).Bytes()
2015-01-28 19:35:49 +02:00
}
func (self *Object) Storage() (storage map[string]string) {
storage = make(map[string]string)
it := self.StateObject.Trie().Iterator()
for it.Next() {
var data []byte
rlp.Decode(bytes.NewReader(it.Value), &data)
2015-03-26 01:42:35 +02:00
storage[common.ToHex(self.Trie().GetKey(it.Key))] = common.ToHex(data)
}
return
}
// Block interface exposed to QML
2015-01-28 19:35:49 +02:00
type Block struct {
//Transactions string `json:"transactions"`
ref *types.Block
2015-03-18 12:44:25 +02:00
Size string `json:"size"`
Number int `json:"number"`
Hash string `json:"hash"`
2015-03-16 12:27:38 +02:00
Transactions *common.List `json:"transactions"`
Uncles *common.List `json:"uncles"`
2015-06-30 10:13:16 +03:00
Time uint64 `json:"time"`
2015-03-18 12:44:25 +02:00
Coinbase string `json:"coinbase"`
Name string `json:"name"`
GasLimit string `json:"gasLimit"`
GasUsed string `json:"gasUsed"`
PrevHash string `json:"prevHash"`
Bloom string `json:"bloom"`
Raw string `json:"raw"`
}
// Creates a new QML Block from a chain block
2015-01-28 19:35:49 +02:00
func NewBlock(block *types.Block) *Block {
if block == nil {
2015-01-28 19:35:49 +02:00
return &Block{}
}
2015-01-28 19:35:49 +02:00
ptxs := make([]*Transaction, len(block.Transactions()))
2015-04-08 23:17:49 +03:00
/*
for i, tx := range block.Transactions() {
ptxs[i] = NewTx(tx)
}
*/
2015-03-16 12:27:38 +02:00
txlist := common.NewList(ptxs)
2015-01-28 19:35:49 +02:00
puncles := make([]*Block, len(block.Uncles()))
2015-04-08 23:17:49 +03:00
/*
for i, uncle := range block.Uncles() {
puncles[i] = NewBlock(types.NewBlockWithHeader(uncle))
}
*/
2015-03-16 12:27:38 +02:00
ulist := common.NewList(puncles)
2015-01-28 19:35:49 +02:00
return &Block{
2014-09-25 17:57:49 +03:00
ref: block, Size: block.Size().String(),
Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(),
2015-03-18 19:23:37 +02:00
GasLimit: block.GasLimit().String(), Hash: block.Hash().Hex(),
2014-10-31 11:50:16 +02:00
Transactions: txlist, Uncles: ulist,
Time: block.Time(),
2015-03-18 19:23:37 +02:00
Coinbase: block.Coinbase().Hex(),
PrevHash: block.ParentHash().Hex(),
Bloom: common.ToHex(block.Bloom().Bytes()),
Raw: block.String(),
2014-09-25 17:57:49 +03:00
}
}
2015-01-28 19:35:49 +02:00
func (self *Block) ToString() string {
if self.ref != nil {
return self.ref.String()
}
return ""
}
2015-01-28 19:35:49 +02:00
func (self *Block) GetTransaction(hash string) *Transaction {
2015-03-18 12:44:25 +02:00
tx := self.ref.Transaction(common.HexToHash(hash))
if tx == nil {
return nil
}
2015-01-28 19:35:49 +02:00
return NewTx(tx)
}
2015-01-28 19:35:49 +02:00
type Transaction struct {
ref *types.Transaction
Value string `json:"value"`
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
Hash string `json:"hash"`
Address string `json:"address"`
Sender string `json:"sender"`
RawData string `json:"rawData"`
Data string `json:"data"`
Contract bool `json:"isContract"`
CreatesContract bool `json:"createsContract"`
Confirmations int `json:"confirmations"`
}
2015-01-28 19:35:49 +02:00
func NewTx(tx *types.Transaction) *Transaction {
sender, err := tx.From()
if err != nil {
return nil
}
2015-03-18 12:44:25 +02:00
hash := tx.Hash().Hex()
var receiver string
if to := tx.To(); to != nil {
receiver = to.Hex()
} else {
from, _ := tx.From()
receiver = crypto.CreateAddress(from, tx.Nonce()).Hex()
}
2014-12-18 16:33:22 +02:00
createsContract := core.MessageCreatesContract(tx)
var data string
2014-12-18 16:33:22 +02:00
if createsContract {
data = strings.Join(core.Disassemble(tx.Data()), "\n")
} else {
data = common.ToHex(tx.Data())
}
2015-03-18 19:23:37 +02:00
return &Transaction{ref: tx, Hash: hash, Value: common.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender.Hex(), CreatesContract: createsContract, RawData: common.ToHex(tx.Data())}
}
2015-01-28 19:35:49 +02:00
func (self *Transaction) ToString() string {
return self.ref.String()
}
type PReceipt struct {
CreatedContract bool `json:"createdContract"`
Address string `json:"address"`
Hash string `json:"hash"`
Sender string `json:"sender"`
}
func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt {
return &PReceipt{
contractCreation,
common.ToHex(creationAddress),
common.ToHex(hash),
common.ToHex(address),
}
}
// Peer interface exposed to QML
2015-01-28 19:35:49 +02:00
type Peer struct {
ref *p2p.Peer
Ip string `json:"ip"`
Version string `json:"version"`
Caps string `json:"caps"`
}
2015-01-28 19:35:49 +02:00
func NewPeer(peer *p2p.Peer) *Peer {
var caps []string
for _, cap := range peer.Caps() {
caps = append(caps, fmt.Sprintf("%s/%d", cap.Name, cap.Version))
}
2015-01-28 19:35:49 +02:00
return &Peer{
ref: peer,
2015-01-06 14:18:09 +02:00
Ip: fmt.Sprintf("%v", peer.RemoteAddr()),
Version: fmt.Sprintf("%v", peer.ID()),
Caps: fmt.Sprintf("%v", caps),
}
}
2015-01-28 19:35:49 +02:00
type Receipt struct {
CreatedContract bool `json:"createdContract"`
Address string `json:"address"`
Hash string `json:"hash"`
Sender string `json:"sender"`
}
2015-01-28 19:35:49 +02:00
func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt {
return &Receipt{
contractCreation,
common.ToHex(creationAddress),
common.ToHex(hash),
common.ToHex(address),
}
}