Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Merkle Tree Hash #48

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3ab152e
Swarm - plan bee for content storage and distribution on web3
Dec 3, 2015
9d2605e
integrate RPC v2 via develop
zelig Dec 14, 2015
0e26569
add SwarmSwapDisabled=bbzswapdisabled SwarmPort=bzzport flags
zelig Dec 18, 2015
18cac67
network test using ethutil/swarm
zelig Dec 18, 2015
477d498
update to latest develop
zelig Feb 1, 2016
fb3b4fa
support for separate url schemes for dns enabled, immutable and raw m…
Feb 4, 2016
863fbb4
Merge pull request #2372 from ethersphere/s/3_url_schemes
zelig Mar 22, 2016
5714724
swap integration test
zelig Feb 5, 2016
7d5c213
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
dbb151b
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
ba49d70
Merge branch 'swarm' of github.com:ethereum/go-ethereum into swarm
Mar 22, 2016
d7db41b
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
6fb86ac
Merge branch 'swarm' of github.com:ethereum/go-ethereum into swarm
Mar 22, 2016
30d31cf
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
0e8684a
Merge branch 'swarm' of github.com:ethereum/go-ethereum into swarm
Mar 23, 2016
89a2678
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
02d3328
Merge branch 'swarm' of github.com:ethereum/go-ethereum into swarm
Mar 23, 2016
585bdac
Merkle hash calculation for chunks and empty tests
May 3, 2016
f3c497f
Test an actual Merkle hash tree.
May 3, 2016
4880fb6
Merkle hashing of individual chunks.
May 3, 2016
d2ae0e7
Change DB retrieval integrity checks to Merkle hash.
May 5, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.WhisperEnabledFlag,
utils.SwarmConfigPathFlag,
utils.SwarmSwapDisabled,
utils.SwarmSyncDisabled,
utils.SwarmPortFlag,
utils.SwarmAccountAddrFlag,
utils.ChequebookAddrFlag,
Expand Down
9 changes: 7 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ var (
Name: "bzznoswap",
Usage: "Swarm SWAP disabled (false)",
}
SwarmSyncDisabled = cli.BoolFlag{
Name: "bzznosync",
Usage: "Swarm Syncing disabled (false)",
}
// ATM the url is left to the user and deployment to
JSpathFlag = cli.StringFlag{
Name: "jspath",
Expand Down Expand Up @@ -788,9 +792,10 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
if len(bzzport) > 0 {
bzzconfig.Port = bzzport
}
swap := ctx.GlobalBool(SwarmSwapDisabled.Name)
swapEnabled := !ctx.GlobalBool(SwarmSwapDisabled.Name)
syncEnabled := !ctx.GlobalBool(SwarmSyncDisabled.Name)
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
return swarm.NewSwarm(ctx, bzzconfig, swap)
return swarm.NewSwarm(ctx, bzzconfig, swapEnabled, syncEnabled)
}); err != nil {
Fatalf("Failed to register the Swarm service: %v", err)
}
Expand Down
33 changes: 28 additions & 5 deletions common/chequebook/api.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
package chequebook

import (
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"
)

const Version = "1.0"

var errNoChequebook = errors.New("no chequebook")

type Api struct {
ch *Chequebook
chequebookf func() *Chequebook
}

func NewApi(ch *Chequebook) *Api {
func NewApi(ch func() *Chequebook) *Api {
return &Api{ch}
}

func (self *Api) Balance() (string, error) {
ch := self.chequebookf()
if ch == nil {
return "", errNoChequebook
}
return ch.Balance().String(), nil
}

func (self *Api) Issue(beneficiary common.Address, amount *big.Int) (cheque *Cheque, err error) {
return self.ch.Issue(beneficiary, amount)
ch := self.chequebookf()
if ch == nil {
return nil, errNoChequebook
}
return ch.Issue(beneficiary, amount)
}

func (self *Api) Cash(cheque *Cheque) (txhash string, err error) {
return self.ch.Cash(cheque)
ch := self.chequebookf()
if ch == nil {
return "", errNoChequebook
}
return ch.Cash(cheque)
}

func (self *Api) Deposit(amount *big.Int) (txhash string, err error) {
return self.ch.Deposit(amount)
ch := self.chequebookf()
if ch == nil {
return "", errNoChequebook
}
return ch.Deposit(amount)
}
26 changes: 23 additions & 3 deletions common/chequebook/cheque.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Backend interface {
Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, string, error)
GetTxReceipt(txhash common.Hash) *types.Receipt
CodeAt(address string) string
BalanceAt(address common.Address) string
}

// rlp serialised cheque model for use with the chequebook
Expand Down Expand Up @@ -165,13 +166,25 @@ func NewChequebook(path string, contract common.Address, prvKey *ecdsa.PrivateKe
owner: owner,
}
if (contract != common.Address{}) {
glog.V(logger.Detail).Infof("[CHEQUEBOOK] new chequebook initialised from %v (owner: %v)", contract.Hex(), owner.Hex())
err = self.setBalanceFromBlockChain()
if err != nil {
return nil, err
}
glog.V(logger.Detail).Infof("[CHEQUEBOOK] new chequebook initialised from %v (owner: %v, balance: %s)", contract.Hex(), owner.Hex(), self.balance.String())
}
return
}

func (self *Chequebook) setBalanceFromBlockChain() error {
balanceString := self.backend.BalanceAt(self.contract)
if _, ok := self.balance.SetString(balanceString, 10); !ok {
return fmt.Errorf("Incorrect balance: %s", balanceString)
}
return nil
}

// LoadChequebook(path, prvKey, backend) loads a chequebook from disk (file path)
func LoadChequebook(path string, prvKey *ecdsa.PrivateKey, backend Backend) (self *Chequebook, err error) {
func LoadChequebook(path string, prvKey *ecdsa.PrivateKey, backend Backend, checkBalance bool) (self *Chequebook, err error) {
var data []byte
data, err = ioutil.ReadFile(path)
if err != nil {
Expand All @@ -184,7 +197,14 @@ func LoadChequebook(path string, prvKey *ecdsa.PrivateKey, backend Backend) (sel
if err != nil {
return nil, err
}
glog.V(logger.Detail).Infof("[CHEQUEBOOK] loaded chequebook (%s, owner: %v) initialised from %v", self.contract.Hex(), self.owner.Hex(), path)
if checkBalance {
err = self.setBalanceFromBlockChain()
if err != nil {
return nil, err
}
}

glog.V(logger.Detail).Infof("[CHEQUEBOOK] loaded chequebook (%s, owner: %v, balance: %v) initialised from %v", self.contract.Hex(), self.owner.Hex(), self.balance, path)

return
}
Expand Down
32 changes: 18 additions & 14 deletions common/chequebook/cheque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (b *testBackend) CodeAt(address string) string {
return ""
}

func (b *testBackend) BalanceAt(address common.Address) string {
return "0"
}

func genAddr() common.Address {
prvKey, _ := crypto.GenerateKey()
return crypto.PubkeyToAddress(prvKey.PublicKey)
Expand All @@ -54,21 +58,21 @@ func TestIssueAndReceive(t *testing.T) {
prvKey, _ := crypto.GenerateKey()
sender := genAddr()
path := "/tmp/checkbook.json"
chbook, err := NewChequebook(path, sender, prvKey, nil)
chbook, err := NewChequebook(path, sender, prvKey, newTestBackend())
if err != nil {
t.Errorf("expected no error, got %v", err)
t.Fatalf("expected no error, got %v", err)
}
recipient := genAddr()
chbook.sent[recipient] = new(big.Int).SetUint64(42)
amount := common.Big1
ch, err := chbook.Issue(recipient, amount)
if err == nil {
t.Errorf("expected insufficient funds error, got none")
t.Fatalf("expected insufficient funds error, got none")
}

chbook.balance = new(big.Int).Set(common.Big1)
if chbook.Balance().Cmp(common.Big1) != 0 {
t.Errorf("expected: %v, got %v", "0", chbook.Balance())
t.Fatalf("expected: %v, got %v", "0", chbook.Balance())
}

ch, err = chbook.Issue(recipient, amount)
Expand Down Expand Up @@ -102,17 +106,17 @@ func TestCheckbookFile(t *testing.T) {
prvKey, _ := crypto.GenerateKey()
sender := genAddr()
path := "/tmp/checkbook.json"
chbook, err := NewChequebook(path, sender, prvKey, nil)
chbook, err := NewChequebook(path, sender, prvKey, newTestBackend())
if err != nil {
t.Errorf("expected no error, got %v", err)
t.Fatalf("expected no error, got %v", err)
}
recipient := genAddr()
chbook.sent[recipient] = new(big.Int).SetUint64(42)
chbook.balance = new(big.Int).Set(common.Big1)

chbook.Save()

chbook, err = LoadChequebook(path, prvKey, nil)
chbook, err = LoadChequebook(path, prvKey, newTestBackend(), false)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
Expand All @@ -139,12 +143,12 @@ func TestVerifyErrors(t *testing.T) {
sender0 := genAddr()
sender1 := genAddr()
path0 := "/tmp/checkbook0.json"
chbook0, err := NewChequebook(path0, sender0, prvKey, nil)
chbook0, err := NewChequebook(path0, sender0, prvKey, newTestBackend())
if err != nil {
t.Errorf("expected no error, got %v", err)
}
path1 := "/tmp/checkbook1.json"
chbook1, err := NewChequebook(path1, sender1, prvKey, nil)
chbook1, err := NewChequebook(path1, sender1, prvKey, newTestBackend())
if err != nil {
t.Errorf("expected no error, got %v", err)
}
Expand Down Expand Up @@ -179,7 +183,7 @@ func TestVerifyErrors(t *testing.T) {
}

received, err = chbox.Receive(ch1)
t.Log(err)
t.Logf("correct error: %v", err)
if err == nil {
t.Fatalf("expected receiver error, got none")
}
Expand All @@ -189,19 +193,19 @@ func TestVerifyErrors(t *testing.T) {
t.Fatalf("expected no error, got %v", err)
}
received, err = chbox.Receive(ch2)
t.Log(err)
t.Logf("correct error: %v", err)
if err == nil {
t.Fatalf("expected sender error, got none")
}

_, err = chbook1.Issue(recipient0, new(big.Int).SetInt64(-1))
t.Log(err)
t.Logf("correct error: %v", err)
if err == nil {
t.Fatalf("expected incorrect amount error, got none")
}

received, err = chbox.Receive(ch0)
t.Log(err)
t.Logf("correct error: %v", err)
if err == nil {
t.Fatalf("expected incorrect amount error, got none")
}
Expand Down Expand Up @@ -365,7 +369,7 @@ func TestCash(t *testing.T) {
prvKey, _ := crypto.GenerateKey()
sender := genAddr()
path := "/tmp/checkbook.json"
chbook, err := NewChequebook(path, sender, prvKey, nil)
chbook, err := NewChequebook(path, sender, prvKey, newTestBackend())
if err != nil {
t.Errorf("expected no error, got %v", err)
}
Expand Down
23 changes: 17 additions & 6 deletions rpc/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,18 @@ web3._extend({
params: 1,
inputFormatter: [null]
}),
new web3._extend.Method({
name: 'syncEnabled',
call: 'bzz_syncEnabled',
params: 1,
inputFormatter: [null]
}),
new web3._extend.Method({
name: 'swapEnabled',
call: 'bzz_swapEnabled',
params: 1,
inputFormatter: [null]
}),
new web3._extend.Method({
name: 'resolve',
call: 'bzz_resolve',
Expand Down Expand Up @@ -580,12 +592,11 @@ web3._extend({
params: 1,
inputFormatter: [null]
}),
new web3._extend.Method({
name: 'info',
call: 'chequebook_info',
params: 1,
inputFormatter: [null]
}),
new web3._extend.Property({
name: 'balance',
getter: 'chequebook_balance',
outputFormatter: web3._extend.utils.toDecimal
}),
new web3._extend.Method({
name: 'cash',
call: 'chequebook_cash',
Expand Down
6 changes: 5 additions & 1 deletion swarm/api/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewEthApi(ethereum *eth.Ethereum) *ethApi {
// Note: this is not threadsafe, only called in JS single process and tests
func (self *ethApi) UpdateState() (wait chan *big.Int) {
wait = make(chan *big.Int)
self.state, _ = state.New(self.eth.BlockChain().GetBlockByNumber(0).Root(), self.eth.ChainDb())
self.state, _ = state.New(self.eth.BlockChain().CurrentBlock().Root(), self.eth.ChainDb())

go func() {
eventSub := self.eth.EventMux().Subscribe(core.ChainHeadEvent{})
Expand Down Expand Up @@ -130,6 +130,10 @@ func (self *ethApi) StorageAt(addr, storageAddr string) string {
return self.state.GetState(common.HexToAddress(addr), common.HexToHash(storageAddr)).Hex()
}

func (self *ethApi) BalanceAt(address common.Address) string {
return self.state.GetBalance(address).String()
}

func (self *ethApi) CodeAt(address string) string {
return common.ToHex(self.state.GetCode(common.HexToAddress(address)))
}
Expand Down
10 changes: 9 additions & 1 deletion swarm/api/testapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ func NewControl(api *Api, hive *network.Hive) *Control {
}

func (self *Control) BlockNetworkRead(on bool) {
self.hive.BlockNetworkRead(true)
self.hive.BlockNetworkRead(on)
}

func (self *Control) SyncEnabled(on bool) {
self.hive.SyncEnabled(on)
}

func (self *Control) SwapEnabled(on bool) {
self.hive.SwapEnabled(on)
}
Loading