aboutsummaryrefslogtreecommitdiffstats
path: root/core/events.go
Commit message (Expand)AuthorAgeFilesLines
* Optimisations and fixed a couple of DDOS issues in the minerobscuren2015-02-201-0/+3
* add NewMinedBlockEventzelig2014-12-151-0/+3
* Renamed `chain` => `core`obscuren2014-12-041-0/+12
='n49' href='#n49'>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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library 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,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// 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/>.

package light

import (
    "bytes"
    "math/big"
    "testing"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/ethereum/go-ethereum/ethdb"
    "github.com/ethereum/go-ethereum/trie"
    "golang.org/x/net/context"
)

type testOdr struct {
    OdrBackend
    sdb, ldb ethdb.Database
}

func (odr *testOdr) Database() ethdb.Database {
    return odr.ldb
}

func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
    switch req := req.(type) {
    case *TrieRequest:
        t, _ := trie.New(req.root, odr.sdb)
        req.proof = t.Prove(req.key)
        trie.ClearGlobalCache()
    case *NodeDataRequest:
        req.data, _ = odr.sdb.Get(req.hash[:])
    }
    req.StoreResult(odr.ldb)
    return nil
}

func makeTestState() (common.Hash, ethdb.Database) {
    sdb, _ := ethdb.NewMemDatabase()
    st, _ := state.New(common.Hash{}, sdb)
    for i := byte(0); i < 100; i++ {
        so := st.GetOrNewStateObject(common.Address{i})
        for j := byte(0); j < 100; j++ {
            val := common.Hash{i, j}
            so.SetState(common.Hash{j}, val)
            so.SetNonce(100)
        }
        so.AddBalance(big.NewInt(int64(i)))
        so.SetCode([]byte{i, i, i})
        so.UpdateRoot(sdb)
        st.UpdateStateObject(so)
    }
    root, _ := st.Commit()
    return root, sdb
}

func TestLightStateOdr(t *testing.T) {
    root, sdb := makeTestState()
    ldb, _ := ethdb.NewMemDatabase()
    odr := &testOdr{sdb: sdb, ldb: ldb}
    ls := NewLightState(root, odr)
    ctx := context.Background()
    trie.ClearGlobalCache()

    for i := byte(0); i < 100; i++ {
        addr := common.Address{i}
        err := ls.AddBalance(ctx, addr, big.NewInt(1000))
        if err != nil {
            t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
        }
        err = ls.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 100})
        if err != nil {
            t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
        }
    }

    addr := common.Address{100}
    _, err := ls.CreateStateObject(ctx, addr)
    if err != nil {
        t.Fatalf("Error creating state object: %v", err)
    }
    err = ls.SetCode(ctx, addr, []byte{100, 100, 100})
    if err != nil {
        t.Fatalf("Error setting code: %v", err)
    }
    err = ls.AddBalance(ctx, addr, big.NewInt(1100))
    if err != nil {
        t.Fatalf("Error adding balance to acc[100]: %v", err)
    }
    for j := byte(0); j < 101; j++ {
        err = ls.SetState(ctx, addr, common.Hash{j}, common.Hash{100, j})
        if err != nil {
            t.Fatalf("Error setting storage of acc[100]: %v", err)
        }
    }
    err = ls.SetNonce(ctx, addr, 100)
    if err != nil {
        t.Fatalf("Error setting nonce for acc[100]: %v", err)
    }

    for i := byte(0); i < 101; i++ {
        addr := common.Address{i}

        bal, err := ls.GetBalance(ctx, addr)
        if err != nil {
            t.Fatalf("Error getting balance of acc[%d]: %v", i, err)
        }
        if bal.Int64() != int64(i)+1000 {
            t.Fatalf("Incorrect balance at acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
        }

        nonce, err := ls.GetNonce(ctx, addr)
        if err != nil {
            t.Fatalf("Error getting nonce of acc[%d]: %v", i, err)
        }
        if nonce != 100 {
            t.Fatalf("Incorrect nonce at acc[%d]: expected %v, got %v", i, 100, nonce)
        }

        code, err := ls.GetCode(ctx, addr)
        exp := []byte{i, i, i}
        if err != nil {
            t.Fatalf("Error getting code of acc[%d]: %v", i, err)
        }
        if !bytes.Equal(code, exp) {
            t.Fatalf("Incorrect code at acc[%d]: expected %v, got %v", i, exp, code)
        }

        for j := byte(0); j < 101; j++ {
            exp := common.Hash{i, j}
            val, err := ls.GetState(ctx, addr, common.Hash{j})
            if err != nil {
                t.Fatalf("Error retrieving acc[%d].storage[%d]: %v", i, j, err)
            }
            if val != exp {
                t.Fatalf("Retrieved wrong value from acc[%d].storage[%d]: expected %04x, got %04x", i, j, exp, val)
            }
        }
    }
}

func TestLightStateSetCopy(t *testing.T) {
    root, sdb := makeTestState()
    ldb, _ := ethdb.NewMemDatabase()
    odr := &testOdr{sdb: sdb, ldb: ldb}
    ls := NewLightState(root, odr)
    ctx := context.Background()
    trie.ClearGlobalCache()

    for i := byte(0); i < 100; i++ {
        addr := common.Address{i}
        err := ls.AddBalance(ctx, addr, big.NewInt(1000))
        if err != nil {
            t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
        }
        err = ls.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 100})
        if err != nil {
            t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
        }
    }

    ls2 := ls.Copy()

    for i := byte(0); i < 100; i++ {
        addr := common.Address{i}
        err := ls2.AddBalance(ctx, addr, big.NewInt(1000))
        if err != nil {
            t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
        }
        err = ls2.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 200})
        if err != nil {
            t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
        }
    }

    lsx := ls.Copy()
    ls.Set(ls2)
    ls2.Set(lsx)

    for i := byte(0); i < 100; i++ {
        addr := common.Address{i}
        // check balance in ls
        bal, err := ls.GetBalance(ctx, addr)
        if err != nil {
            t.Fatalf("Error getting balance to acc[%d]: %v", i, err)
        }
        if bal.Int64() != int64(i)+2000 {
            t.Fatalf("Incorrect balance at ls.acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
        }
        // check balance in ls2
        bal, err = ls2.GetBalance(ctx, addr)
        if err != nil {
            t.Fatalf("Error getting balance to acc[%d]: %v", i, err)
        }
        if bal.Int64() != int64(i)+1000 {
            t.Fatalf("Incorrect balance at ls.acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
        }
        // check storage in ls