aboutsummaryrefslogtreecommitdiffstats
path: root/common/natspec/natspec_e2e_test.go
blob: ad6c13f2a14d9c11e01b28e8801355896c26df81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package natspec

import (
    "fmt"
    "io/ioutil"
    "math/big"
    "os"
    "testing"

    "github.com/ethereum/go-ethereum/accounts"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/resolver"
    "github.com/ethereum/go-ethereum/core"
    //"github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/eth"
    xe "github.com/ethereum/go-ethereum/xeth"
)

type testFrontend struct {
    t        *testing.T
    ethereum *eth.Ethereum
    xeth     *xe.XEth
}

func (f *testFrontend) UnlockAccount(acc []byte) bool {
    f.t.Logf("Unlocking account %v\n", common.Bytes2Hex(acc))
    f.ethereum.AccountManager().Unlock(acc, "password")
    return true
}

func (testFrontend) ConfirmTransaction(message string) bool { return true }

var port = 30300

func testJEthRE(t *testing.T) (ethereum *eth.Ethereum, err error) {
    os.RemoveAll("/tmp/eth/")
    err = os.MkdirAll("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/", os.ModePerm)
    if err != nil {
        t.Errorf("%v", err)
        return
    }
    err = os.MkdirAll("/tmp/eth/data", os.ModePerm)
    if err != nil {
        t.Errorf("%v", err)
        return
    }
    ks := crypto.NewKeyStorePlain("/tmp/eth/keys")
    ioutil.WriteFile("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/e273f01c99144c438695e10f24926dc1f9fbf62d",
        []byte(`{"Id":"RhRXD+fNRKS4jx+7ZfEsNA==","Address":"4nPwHJkUTEOGleEPJJJtwfn79i0=","PrivateKey":"h4ACVpe74uIvi5Cg/2tX/Yrm2xdr3J7QoMbMtNX2CNc="}`), os.ModePerm)

    port++
    ethereum, err = eth.New(&eth.Config{
        DataDir:        "/tmp/eth",
        AccountManager: accounts.NewManager(ks),
        Port:           fmt.Sprintf("%d", port),
        MaxPeers:       10,
        Name:           "test",
    })

    if err != nil {
        t.Errorf("%v", err)
        return
    }

    return
}

func (self *testFrontend) insertTx(addr, contract, fnsig string, args []string) {

    hash := common.Bytes2Hex(crypto.Sha3([]byte(fnsig)))
    data := "0x" + hash[0:8]
    for _, arg := range args {
        data = data + common.Bytes2Hex(common.Hex2BytesFixed(arg, 32))
    }
    self.t.Logf("Tx data: %v", data)
    self.xeth.Transact(addr, contract, "100000000000", "100000", "100000", data)

    cb := common.HexToAddress(addr)
    stateDb := self.ethereum.ChainManager().State().Copy()

    coinbase := stateDb.GetStateObject(cb)
    coinbase.SetGasPool(big.NewInt(100000))
    block := self.ethereum.ChainManager().NewBlock(cb)
    txs := self.ethereum.TxPool().GetTransactions()
    tx := txs[0]

    _, gas, err := core.ApplyMessage(core.NewEnv(stateDb, self.ethereum.ChainManager(), tx, block), tx, coinbase)

    self.t.Logf("ApplyMessage: gas %v  err %v", gas, err)

    self.ethereum.TxPool().RemoveSet(txs)
    self.xeth = self.xeth.WithState(stateDb)

}

func TestNatspecE2E(t *testing.T) {
    ethereum, err := testJEthRE(t)
    if err != nil {
        t.Errorf("error creating jsre, got %v", err)
        return
    }
    err = ethereum.Start()
    if err != nil {
        t.Errorf("error starting ethereum: %v", err)
        return
    }
    defer ethereum.Stop()

    frontend := &testFrontend{t: t, ethereum: ethereum}
    frontend.xeth = xe.New(ethereum, frontend)

    addr := frontend.xeth.Coinbase()
    if addr != "0x"+core.TestAccount {
        t.Errorf("CoinBase %v does not match TestAccount 0x%v", addr, core.TestAccount)
    }
    t.Logf("CoinBase is %v", addr)

    balance := frontend.xeth.BalanceAt(core.TestAccount)
    if balance != core.TestBalance {
        t.Errorf("Balance %v does not match TestBalance %v", balance, core.TestBalance)
    }
    t.Logf("Balance is %v", balance)

    frontend.insertTx(addr, core.ContractAddrURLhint, "register(bytes32,bytes32)", []string{"1234", "5678"})

    t.Logf("testcnt: %v", frontend.xeth.StorageAt(core.ContractAddrURLhint, "00"))

    for i := 0; i < 10; i++ {
        t.Logf("storage[%v] = %v", i, frontend.xeth.StorageAt("0x"+core.ContractAddrURLhint, fmt.Sprintf("%v", i)))
    }

    rsv := resolver.New(frontend.xeth, resolver.URLHintContractAddress, resolver.NameRegContractAddress)
    url, err2 := rsv.ContentHashToUrl(common.BytesToHash(common.Hex2BytesFixed("1234", 32)))

    t.Logf("URL: %v  err: %v", url, err2)

    /*
       This test is unfinished; first we need to see the result of a
       transaction in the contract storage (testcnt should be 1).
    */

}