// mine generates a testnet struct literal with nodes at // various distances to the given target. func (n *preminedTestnet) mine(target NodeID) { n.target = target n.targetSha = crypto.Sha3Hash(n.target[:]) found := 0 for found < bucketSize*10 { k := newkey() id := PubkeyID(&k.PublicKey) sha := crypto.Sha3Hash(id[:]) ld := logdist(n.targetSha, sha) if len(n.dists[ld]) < bucketSize { n.dists[ld] = append(n.dists[ld], id) fmt.Println("found ID with ld", ld) found++ } } fmt.Println("&preminedTestnet{") fmt.Printf(" target: %#v,\n", n.target) fmt.Printf(" targetSha: %#v,\n", n.targetSha) fmt.Printf(" dists: [%d][]NodeID{\n", len(n.dists)) for ld, ns := range n.dists { if len(ns) == 0 { continue } fmt.Printf(" %d: []NodeID{\n", ld) for _, n := range ns { fmt.Printf(" MustHexID(\"%x\"),\n", n[:]) } fmt.Println(" },") } fmt.Println(" },") fmt.Println("}") }
func TestGetAuthContent(t *testing.T) { dir, err := ioutil.TempDir("", "httpclient-test") if err != nil { t.Fatal("cannot create temporary directory:", err) } defer os.RemoveAll(dir) client := New(dir) text := "test" hash := crypto.Sha3Hash([]byte(text)) if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil { t.Fatal("could not write test file", err) } content, err := client.GetAuthContent("file:///test.content", hash) if err != nil { t.Errorf("no error expected, got %v", err) } if string(content) != text { t.Errorf("incorrect content. expected %v, got %v", text, string(content)) } hash = common.Hash{} content, err = client.GetAuthContent("file:///test.content", hash) expected := "content hash mismatch 0000000000000000000000000000000000000000000000000000000000000000 != 9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658 (exp)" if err == nil { t.Errorf("expected error, got nothing") } else { if err.Error() != expected { t.Errorf("expected error '%s' got '%v'", expected, err) } } }
// Hash returns the SHA3 hash of the envelope, calculating it if not yet done. func (self *Envelope) Hash() common.Hash { if (self.hash == common.Hash{}) { enc, _ := rlp.EncodeToBytes(self) self.hash = crypto.Sha3Hash(enc) } return self.hash }
func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error { if expired(req.Expiration) { return errExpired } if t.db.node(fromID) == nil { // No bond exists, we don't process the packet. This prevents // an attack vector where the discovery protocol could be used // to amplify traffic in a DDOS attack. A malicious actor // would send a findnode request with the IP address and UDP // port of the target as the source address. The recipient of // the findnode packet would then send a neighbors packet // (which is a much bigger packet than findnode) to the victim. return errUnknownNode } target := crypto.Sha3Hash(req.Target[:]) t.mutex.Lock() closest := t.closest(target, bucketSize).entries t.mutex.Unlock() p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())} // Send neighbors in chunks with at most maxNeighbors per packet // to stay below the 1280 byte limit. for i, n := range closest { p.Nodes = append(p.Nodes, nodeToRPC(n)) if len(p.Nodes) == maxNeighbors || i == len(closest)-1 { t.send(from, neighborsPacket, p) p.Nodes = p.Nodes[:0] } } return nil }
// NewProgram returns a new JIT program func NewProgram(code []byte) *Program { program := &Program{ Id: crypto.Sha3Hash(code), mapping: make(map[uint64]uint64), destinations: make(map[uint64]struct{}), code: code, } programs.Add(program.Id, program) return program }
func newNode(id NodeID, ip net.IP, udpPort, tcpPort uint16) *Node { if ipv4 := ip.To4(); ipv4 != nil { ip = ipv4 } return &Node{ IP: ip, UDP: udpPort, TCP: tcpPort, ID: id, sha: crypto.Sha3Hash(id[:]), } }
// Bootstrap sets the bootstrap nodes. These nodes are used to connect // to the network if the table is empty. Bootstrap will also attempt to // fill the table by performing random lookup operations on the // network. func (tab *Table) Bootstrap(nodes []*Node) { tab.mutex.Lock() // TODO: maybe filter nodes with bad fields (nil, etc.) to avoid strange crashes tab.nursery = make([]*Node, 0, len(nodes)) for _, n := range nodes { cpy := *n cpy.sha = crypto.Sha3Hash(n.ID[:]) tab.nursery = append(tab.nursery, &cpy) } tab.mutex.Unlock() tab.refresh() }
// node retrieves a node with a given id from the database. func (db *nodeDB) node(id NodeID) *Node { blob, err := db.lvl.Get(makeKey(id, nodeDBDiscoverRoot), nil) if err != nil { glog.V(logger.Detail).Infof("failed to retrieve node %v: %v", id, err) return nil } node := new(Node) if err := rlp.DecodeBytes(blob, node); err != nil { glog.V(logger.Warn).Infof("failed to decode node RLP: %v", err) return nil } node.sha = crypto.Sha3Hash(node.ID[:]) return node }
func (self *HTTPClient) GetAuthContent(uri string, hash common.Hash) ([]byte, error) { // retrieve content content, err := self.Get(uri, "") if err != nil { return nil, err } // check hash to authenticate content chash := crypto.Sha3Hash(content) if chash != hash { return nil, fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:]) } return content, nil }
// DecodeRLP decodes an Envelope from an RLP data stream. func (self *Envelope) DecodeRLP(s *rlp.Stream) error { raw, err := s.Raw() if err != nil { return err } // The decoding of Envelope uses the struct fields but also needs // to compute the hash of the whole RLP-encoded envelope. This // type has the same structure as Envelope but is not an // rlp.Decoder so we can reuse the Envelope struct definition. type rlpenv Envelope if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil { return err } self.hash = crypto.Sha3Hash(raw) return nil }
func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) { // retrieve content content, err = self.Get(uri, "") if err != nil { return } // check hash to authenticate content chash := crypto.Sha3Hash(content) if chash != hash { content = nil err = fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:]) } return }
func TestUDP_findnode(t *testing.T) { test := newUDPTest(t) defer test.table.Close() // put a few nodes into the table. their exact // distribution shouldn't matter much, altough we need to // take care not to overflow any bucket. targetHash := crypto.Sha3Hash(testTarget[:]) nodes := &nodesByDistance{target: targetHash} for i := 0; i < bucketSize; i++ { nodes.push(nodeAtDistance(test.table.self.sha, i+2), bucketSize) } test.table.stuff(nodes.entries) // ensure there's a bond with the test node, // findnode won't be accepted otherwise. test.table.db.updateNode(newNode( PubkeyID(&test.remotekey.PublicKey), test.remoteaddr.IP, uint16(test.remoteaddr.Port), 99, )) // check that closest neighbors are returned. test.packetIn(nil, findnodePacket, &findnode{Target: testTarget, Expiration: futureExp}) expected := test.table.closest(targetHash, bucketSize) waitNeighbors := func(want []*Node) { test.waitPacketOut(func(p *neighbors) { if len(p.Nodes) != len(want) { t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize) } for i := range p.Nodes { if p.Nodes[i].ID != want[i].ID { t.Errorf("result mismatch at %d:\n got: %v\n want: %v", i, p.Nodes[i], expected.entries[i]) } } }) } waitNeighbors(expected.entries[:maxNeighbors]) waitNeighbors(expected.entries[maxNeighbors:]) }
// Run loops and evaluates the contract's code with the given input data func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { self.env.SetDepth(self.env.Depth() + 1) defer self.env.SetDepth(self.env.Depth() - 1) // User defer pattern to check for an error and, based on the error being nil or not, use all gas and return. defer func() { if err != nil { // In case of a VM exception (known exceptions) all gas consumed (panics NOT included). context.UseGas(context.Gas) ret = context.Return(nil) } }() if context.CodeAddr != nil { if p := Precompiled[context.CodeAddr.Str()]; p != nil { return self.RunPrecompiled(p, input, context) } } var ( codehash = crypto.Sha3Hash(context.Code) // codehash is used when doing jump dest caching program *Program ) if EnableJit { // Fetch program status. // * If ready run using JIT // * If unknown, compile in a seperate goroutine // * If forced wait for compilation and run once done if status := GetProgramStatus(codehash); status == progReady { return RunProgram(GetProgram(codehash), self.env, context, input) } else if status == progUnknown { if ForceJit { // Create and compile program program = NewProgram(context.Code) perr := CompileProgram(program) if perr == nil { return RunProgram(program, self.env, context, input) } glog.V(logger.Info).Infoln("error compiling program", err) } else { // create and compile the program. Compilation // is done in a seperate goroutine program = NewProgram(context.Code) go func() { err := CompileProgram(program) if err != nil { glog.V(logger.Info).Infoln("error compiling program", err) return } }() } } } var ( caller = context.caller code = context.Code value = context.value price = context.Price op OpCode // current opcode mem = NewMemory() // bound memory stack = newstack() // local stack statedb = self.env.State() // current state // For optimisation reason we're using uint64 as the program counter. // It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Pratically much less so feasible. pc = uint64(0) // program counter // jump evaluates and checks whether the given jump destination is a valid one // if valid move the `pc` otherwise return an error. jump = func(from uint64, to *big.Int) error { if !context.jumpdests.has(codehash, code, to) { nop := context.GetOp(to.Uint64()) return fmt.Errorf("invalid jump destination (%v) %v", nop, to) } pc = to.Uint64() return nil } newMemSize *big.Int cost *big.Int ) // User defer pattern to check for an error and, based on the error being nil or not, use all gas and return. defer func() { if err != nil { self.log(pc, op, context.Gas, cost, mem, stack, context, err) } }() // Don't bother with the execution if there's no code. if len(code) == 0 { return context.Return(nil), nil } for { // Overhead of the atomic read might not be worth it /* TODO this still causes a few issues in the tests if program != nil && progStatus(atomic.LoadInt32(&program.status)) == progReady { // move execution glog.V(logger.Info).Infoln("Moved execution to JIT") return runProgram(program, pc, mem, stack, self.env, context, input) } */ // The base for all big integer arithmetic base := new(big.Int) // Get the memory location of pc op = context.GetOp(pc) // calculate the new memory size and gas price for the current executing opcode newMemSize, cost, err = calculateGasAndSize(self.env, context, caller, op, statedb, mem, stack) if err != nil { return nil, err } // Use the calculated gas. When insufficient gas is present, use all gas and return an // Out Of Gas error if !context.UseGas(cost) { return nil, OutOfGasError } // Resize the memory calculated previously mem.Resize(newMemSize.Uint64()) // Add a log message self.log(pc, op, context.Gas, cost, mem, stack, context, nil) switch op { case ADD: x, y := stack.pop(), stack.pop() base.Add(x, y) U256(base) // pop result back on the stack stack.push(base) case SUB: x, y := stack.pop(), stack.pop() base.Sub(x, y) U256(base) // pop result back on the stack stack.push(base) case MUL: x, y := stack.pop(), stack.pop() base.Mul(x, y) U256(base) // pop result back on the stack stack.push(base) case DIV: x, y := stack.pop(), stack.pop() if y.Cmp(common.Big0) != 0 { base.Div(x, y) } U256(base) // pop result back on the stack stack.push(base) case SDIV: x, y := S256(stack.pop()), S256(stack.pop()) if y.Cmp(common.Big0) == 0 { base.Set(common.Big0) } else { n := new(big.Int) if new(big.Int).Mul(x, y).Cmp(common.Big0) < 0 { n.SetInt64(-1) } else { n.SetInt64(1) } base.Div(x.Abs(x), y.Abs(y)).Mul(base, n) U256(base) } stack.push(base) case MOD: x, y := stack.pop(), stack.pop() if y.Cmp(common.Big0) == 0 { base.Set(common.Big0) } else { base.Mod(x, y) } U256(base) stack.push(base) case SMOD: x, y := S256(stack.pop()), S256(stack.pop()) if y.Cmp(common.Big0) == 0 { base.Set(common.Big0) } else { n := new(big.Int) if x.Cmp(common.Big0) < 0 { n.SetInt64(-1) } else { n.SetInt64(1) } base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n) U256(base) } stack.push(base) case EXP: x, y := stack.pop(), stack.pop() base.Exp(x, y, Pow256) U256(base) stack.push(base) case SIGNEXTEND: back := stack.pop() if back.Cmp(big.NewInt(31)) < 0 { bit := uint(back.Uint64()*8 + 7) num := stack.pop() mask := new(big.Int).Lsh(common.Big1, bit) mask.Sub(mask, common.Big1) if common.BitTest(num, int(bit)) { num.Or(num, mask.Not(mask)) } else { num.And(num, mask) } num = U256(num) stack.push(num) } case NOT: stack.push(U256(new(big.Int).Not(stack.pop()))) case LT: x, y := stack.pop(), stack.pop() // x < y if x.Cmp(y) < 0 { stack.push(common.BigTrue) } else { stack.push(common.BigFalse) } case GT: x, y := stack.pop(), stack.pop() // x > y if x.Cmp(y) > 0 { stack.push(common.BigTrue) } else { stack.push(common.BigFalse) } case SLT: x, y := S256(stack.pop()), S256(stack.pop()) // x < y if x.Cmp(S256(y)) < 0 { stack.push(common.BigTrue) } else { stack.push(common.BigFalse) } case SGT: x, y := S256(stack.pop()), S256(stack.pop()) // x > y if x.Cmp(y) > 0 { stack.push(common.BigTrue) } else { stack.push(common.BigFalse) } case EQ: x, y := stack.pop(), stack.pop() // x == y if x.Cmp(y) == 0 { stack.push(common.BigTrue) } else { stack.push(common.BigFalse) } case ISZERO: x := stack.pop() if x.Cmp(common.BigFalse) > 0 { stack.push(common.BigFalse) } else { stack.push(common.BigTrue) } case AND: x, y := stack.pop(), stack.pop() stack.push(base.And(x, y)) case OR: x, y := stack.pop(), stack.pop() stack.push(base.Or(x, y)) case XOR: x, y := stack.pop(), stack.pop() stack.push(base.Xor(x, y)) case BYTE: th, val := stack.pop(), stack.pop() if th.Cmp(big.NewInt(32)) < 0 { byt := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()])) base.Set(byt) } else { base.Set(common.BigFalse) } stack.push(base) case ADDMOD: x := stack.pop() y := stack.pop() z := stack.pop() if z.Cmp(Zero) > 0 { add := new(big.Int).Add(x, y) base.Mod(add, z) base = U256(base) } stack.push(base) case MULMOD: x := stack.pop() y := stack.pop() z := stack.pop() if z.Cmp(Zero) > 0 { mul := new(big.Int).Mul(x, y) base.Mod(mul, z) U256(base) } stack.push(base) case SHA3: offset, size := stack.pop(), stack.pop() data := crypto.Sha3(mem.Get(offset.Int64(), size.Int64())) stack.push(common.BigD(data)) case ADDRESS: stack.push(common.Bytes2Big(context.Address().Bytes())) case BALANCE: addr := common.BigToAddress(stack.pop()) balance := statedb.GetBalance(addr) stack.push(new(big.Int).Set(balance)) case ORIGIN: origin := self.env.Origin() stack.push(origin.Big()) case CALLER: caller := context.caller.Address() stack.push(common.Bytes2Big(caller.Bytes())) case CALLVALUE: stack.push(new(big.Int).Set(value)) case CALLDATALOAD: data := getData(input, stack.pop(), common.Big32) stack.push(common.Bytes2Big(data)) case CALLDATASIZE: l := int64(len(input)) stack.push(big.NewInt(l)) case CALLDATACOPY: var ( mOff = stack.pop() cOff = stack.pop() l = stack.pop() ) data := getData(input, cOff, l) mem.Set(mOff.Uint64(), l.Uint64(), data) case CODESIZE, EXTCODESIZE: var code []byte if op == EXTCODESIZE { addr := common.BigToAddress(stack.pop()) code = statedb.GetCode(addr) } else { code = context.Code } l := big.NewInt(int64(len(code))) stack.push(l) case CODECOPY, EXTCODECOPY: var code []byte if op == EXTCODECOPY { addr := common.BigToAddress(stack.pop()) code = statedb.GetCode(addr) } else { code = context.Code } var ( mOff = stack.pop() cOff = stack.pop() l = stack.pop() ) codeCopy := getData(code, cOff, l) mem.Set(mOff.Uint64(), l.Uint64(), codeCopy) case GASPRICE: stack.push(new(big.Int).Set(context.Price)) case BLOCKHASH: num := stack.pop() n := new(big.Int).Sub(self.env.BlockNumber(), common.Big257) if num.Cmp(n) > 0 && num.Cmp(self.env.BlockNumber()) < 0 { stack.push(self.env.GetHash(num.Uint64()).Big()) } else { stack.push(common.Big0) } case COINBASE: coinbase := self.env.Coinbase() stack.push(coinbase.Big()) case TIMESTAMP: time := self.env.Time() stack.push(new(big.Int).Set(time)) case NUMBER: number := self.env.BlockNumber() stack.push(U256(number)) case DIFFICULTY: difficulty := self.env.Difficulty() stack.push(new(big.Int).Set(difficulty)) case GASLIMIT: stack.push(new(big.Int).Set(self.env.GasLimit())) case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32: size := uint64(op - PUSH1 + 1) byts := getData(code, new(big.Int).SetUint64(pc+1), new(big.Int).SetUint64(size)) // push value to stack stack.push(common.Bytes2Big(byts)) pc += size case POP: stack.pop() case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: n := int(op - DUP1 + 1) stack.dup(n) case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16: n := int(op - SWAP1 + 2) stack.swap(n) case LOG0, LOG1, LOG2, LOG3, LOG4: n := int(op - LOG0) topics := make([]common.Hash, n) mStart, mSize := stack.pop(), stack.pop() for i := 0; i < n; i++ { topics[i] = common.BigToHash(stack.pop()) } data := mem.Get(mStart.Int64(), mSize.Int64()) log := state.NewLog(context.Address(), topics, data, self.env.BlockNumber().Uint64()) self.env.AddLog(log) case MLOAD: offset := stack.pop() val := common.BigD(mem.Get(offset.Int64(), 32)) stack.push(val) case MSTORE: // pop value of the stack mStart, val := stack.pop(), stack.pop() mem.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256)) case MSTORE8: off, val := stack.pop().Int64(), stack.pop().Int64() mem.store[off] = byte(val & 0xff) case SLOAD: loc := common.BigToHash(stack.pop()) val := statedb.GetState(context.Address(), loc).Big() stack.push(val) case SSTORE: loc := common.BigToHash(stack.pop()) val := stack.pop() statedb.SetState(context.Address(), loc, common.BigToHash(val)) case JUMP: if err := jump(pc, stack.pop()); err != nil { return nil, err } continue case JUMPI: pos, cond := stack.pop(), stack.pop() if cond.Cmp(common.BigTrue) >= 0 { if err := jump(pc, pos); err != nil { return nil, err } continue } case JUMPDEST: case PC: stack.push(new(big.Int).SetUint64(pc)) case MSIZE: stack.push(big.NewInt(int64(mem.Len()))) case GAS: stack.push(new(big.Int).Set(context.Gas)) case CREATE: var ( value = stack.pop() offset, size = stack.pop(), stack.pop() input = mem.Get(offset.Int64(), size.Int64()) gas = new(big.Int).Set(context.Gas) addr common.Address ) context.UseGas(context.Gas) ret, suberr, ref := self.env.Create(context, input, gas, price, value) if suberr != nil { stack.push(common.BigFalse) } else { // gas < len(ret) * CreateDataGas == NO_CODE dataGas := big.NewInt(int64(len(ret))) dataGas.Mul(dataGas, params.CreateDataGas) if context.UseGas(dataGas) { ref.SetCode(ret) } addr = ref.Address() stack.push(addr.Big()) } case CALL, CALLCODE: gas := stack.pop() // pop gas and value of the stack. addr, value := stack.pop(), stack.pop() value = U256(value) // pop input size and offset inOffset, inSize := stack.pop(), stack.pop() // pop return size and offset retOffset, retSize := stack.pop(), stack.pop() address := common.BigToAddress(addr) // Get the arguments from the memory args := mem.Get(inOffset.Int64(), inSize.Int64()) if len(value.Bytes()) > 0 { gas.Add(gas, params.CallStipend) } var ( ret []byte err error ) if op == CALLCODE { ret, err = self.env.CallCode(context, address, args, gas, price, value) } else { ret, err = self.env.Call(context, address, args, gas, price, value) } if err != nil { stack.push(common.BigFalse) } else { stack.push(common.BigTrue) mem.Set(retOffset.Uint64(), retSize.Uint64(), ret) } case RETURN: offset, size := stack.pop(), stack.pop() ret := mem.GetPtr(offset.Int64(), size.Int64()) return context.Return(ret), nil case SUICIDE: receiver := statedb.GetOrNewStateObject(common.BigToAddress(stack.pop())) balance := statedb.GetBalance(context.Address()) receiver.AddBalance(balance) statedb.Delete(context.Address()) fallthrough case STOP: // Stop the context return context.Return(nil), nil default: return nil, fmt.Errorf("Invalid opcode %x", op) } pc++ } }
nonce: 0x318df1c8adef7e5e, mixDigest: common.HexToHash("144b180aad09ae3c81fb07be92c8e6351b5646dda80e6844ae1b697e55ddde84"), }, // from proof of concept nine testnet, epoch 2 { number: 60000, hashNoNonce: common.HexToHash("5fc898f16035bf5ac9c6d9077ae1e3d5fc1ecc3c9fd5bee8bb00e810fdacbaa0"), difficulty: big.NewInt(2467358), nonce: 0x50377003e5d830ca, mixDigest: common.HexToHash("ab546a5b73c452ae86dadd36f0ed83a6745226717d3798832d1b20b489e82063"), }, } var invalidZeroDiffBlock = testBlock{ number: 61440000, hashNoNonce: crypto.Sha3Hash([]byte("foo")), difficulty: big.NewInt(0), nonce: 0xcafebabec00000fe, mixDigest: crypto.Sha3Hash([]byte("bar")), } func TestEthashVerifyValid(t *testing.T) { exp := New() for i, block := range validBlocks { if !exp.Verify(block) { t.Errorf("block %d (%x) did not validate.", i, block.hashNoNonce[:6]) } } } func TestEthashVerifyInvalid(t *testing.T) {
// Run loops and evaluates the contract's code with the given input data func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) { self.env.SetDepth(self.env.Depth() + 1) defer self.env.SetDepth(self.env.Depth() - 1) // User defer pattern to check for an error and, based on the error being nil or not, use all gas and return. defer func() { if err != nil { // In case of a VM exception (known exceptions) all gas consumed (panics NOT included). contract.UseGas(contract.Gas) ret = contract.Return(nil) } }() if contract.CodeAddr != nil { if p := Precompiled[contract.CodeAddr.Str()]; p != nil { return self.RunPrecompiled(p, input, contract) } } // Don't bother with the execution if there's no code. if len(contract.Code) == 0 { return contract.Return(nil), nil } var ( codehash = crypto.Sha3Hash(contract.Code) // codehash is used when doing jump dest caching program *Program ) if EnableJit { // If the JIT is enabled check the status of the JIT program, // if it doesn't exist compile a new program in a seperate // goroutine or wait for compilation to finish if the JIT is // forced. switch GetProgramStatus(codehash) { case progReady: return RunProgram(GetProgram(codehash), self.env, contract, input) case progUnknown: if ForceJit { // Create and compile program program = NewProgram(contract.Code) perr := CompileProgram(program) if perr == nil { return RunProgram(program, self.env, contract, input) } glog.V(logger.Info).Infoln("error compiling program", err) } else { // create and compile the program. Compilation // is done in a seperate goroutine program = NewProgram(contract.Code) go func() { err := CompileProgram(program) if err != nil { glog.V(logger.Info).Infoln("error compiling program", err) return } }() } } } var ( caller = contract.caller code = contract.Code instrCount = 0 op OpCode // current opcode mem = NewMemory() // bound memory stack = newstack() // local stack statedb = self.env.Db() // current state // For optimisation reason we're using uint64 as the program counter. // It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Pratically much less so feasible. pc = uint64(0) // program counter // jump evaluates and checks whether the given jump destination is a valid one // if valid move the `pc` otherwise return an error. jump = func(from uint64, to *big.Int) error { if !contract.jumpdests.has(codehash, code, to) { nop := contract.GetOp(to.Uint64()) return fmt.Errorf("invalid jump destination (%v) %v", nop, to) } pc = to.Uint64() return nil } newMemSize *big.Int cost *big.Int ) contract.Input = input // User defer pattern to check for an error and, based on the error being nil or not, use all gas and return. defer func() { if err != nil { self.log(pc, op, contract.Gas, cost, mem, stack, contract, err) } }() if glog.V(logger.Debug) { glog.Infof("running byte VM %x\n", codehash[:4]) tstart := time.Now() defer func() { glog.Infof("byte VM %x done. time: %v instrc: %v\n", codehash[:4], time.Since(tstart), instrCount) }() } for ; ; instrCount++ { /* if EnableJit && it%100 == 0 { if program != nil && progStatus(atomic.LoadInt32(&program.status)) == progReady { // move execution fmt.Println("moved", it) glog.V(logger.Info).Infoln("Moved execution to JIT") return runProgram(program, pc, mem, stack, self.env, contract, input) } } */ // Get the memory location of pc op = contract.GetOp(pc) // calculate the new memory size and gas price for the current executing opcode newMemSize, cost, err = calculateGasAndSize(self.env, contract, caller, op, statedb, mem, stack) if err != nil { return nil, err } // Use the calculated gas. When insufficient gas is present, use all gas and return an // Out Of Gas error if !contract.UseGas(cost) { return nil, OutOfGasError } // Resize the memory calculated previously mem.Resize(newMemSize.Uint64()) // Add a log message self.log(pc, op, contract.Gas, cost, mem, stack, contract, nil) if opPtr := jumpTable[op]; opPtr.valid { if opPtr.fn != nil { opPtr.fn(instruction{}, &pc, self.env, contract, mem, stack) } else { switch op { case PC: opPc(instruction{data: new(big.Int).SetUint64(pc)}, &pc, self.env, contract, mem, stack) case JUMP: if err := jump(pc, stack.pop()); err != nil { return nil, err } continue case JUMPI: pos, cond := stack.pop(), stack.pop() if cond.Cmp(common.BigTrue) >= 0 { if err := jump(pc, pos); err != nil { return nil, err } continue } case RETURN: offset, size := stack.pop(), stack.pop() ret := mem.GetPtr(offset.Int64(), size.Int64()) return contract.Return(ret), nil case SUICIDE: opSuicide(instruction{}, nil, self.env, contract, mem, stack) fallthrough case STOP: // Stop the contract return contract.Return(nil), nil } } } else { return nil, fmt.Errorf("Invalid opcode %x", op) } pc++ } }
func makeSeedHash(epoch uint64) (sh common.Hash) { for ; epoch > 0; epoch-- { sh = crypto.Sha3Hash(sh[:]) } return sh }
"github.com/expanse-project/go-expanse/logger" "github.com/expanse-project/go-expanse/logger/glog" "github.com/expanse-project/go-expanse/rlp" ) const defaultCacheCapacity = 800 var ( // The global cache stores decoded trie nodes by hash as they get loaded. globalCache = newARC(defaultCacheCapacity) // This is the known root hash of an empty trie. emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") // This is the known hash of an empty state trie entry. emptyState = crypto.Sha3Hash(nil) ) var ErrMissingRoot = errors.New("missing root node") // Database must be implemented by backing stores for the trie. type Database interface { DatabaseWriter // Get returns the value for key from the database. Get(key []byte) (value []byte, err error) } // DatabaseWriter wraps the Put method of a backing store for the trie. type DatabaseWriter interface { // Put stores the mapping key->value in the database. // Implementations must not hold onto the value bytes, the trie
// end to end test func TestNatspecE2E(t *testing.T) { t.Skip() tf := testInit(t) defer tf.expanse.Stop() addr, _ := tf.expanse.Etherbase() // create a contractInfo file (mock cloud-deployed contract metadocs) // incidentally this is the info for the registry contract itself ioutil.WriteFile("/tmp/"+testFileName, []byte(testContractInfo), os.ModePerm) dochash := crypto.Sha3Hash([]byte(testContractInfo)) // take the codehash for the contract we wanna test codeb := tf.xeth.CodeAtBytes(registrar.HashRegAddr) codehash := crypto.Sha3Hash(codeb) // use resolver to register codehash->dochash->url // test if globalregistry works // registrar.HashRefAddr = "0x0" // registrar.UrlHintAddr = "0x0" reg := registrar.New(tf.xeth) _, err := reg.SetHashToHash(addr, codehash, dochash) if err != nil { t.Errorf("error registering: %v", err) } _, err = reg.SetUrlToHash(addr, dochash, "file:///"+testFileName) if err != nil { t.Errorf("error registering: %v", err) } if !processTxs(tf, t, 5) { return } // NatSpec info for register method of HashReg contract installed // now using the same transactions to check confirm messages tf.wantNatSpec = true // this is set so now the backend uses natspec confirmation _, err = reg.SetHashToHash(addr, codehash, dochash) if err != nil { t.Errorf("error calling contract registry: %v", err) } fmt.Printf("GlobalRegistrar: %v, HashReg: %v, UrlHint: %v\n", registrar.GlobalRegistrarAddr, registrar.HashRegAddr, registrar.UrlHintAddr) if tf.lastConfirm != testExpNotice { t.Errorf("Wrong confirm message. expected\n'%v', got\n'%v'", testExpNotice, tf.lastConfirm) } // test unknown method exp := fmt.Sprintf(testExpNotice2, registrar.HashRegAddr) _, err = reg.SetOwner(addr) if err != nil { t.Errorf("error setting owner: %v", err) } if tf.lastConfirm != exp { t.Errorf("Wrong confirm message, expected\n'%v', got\n'%v'", exp, tf.lastConfirm) } // test unknown contract exp = fmt.Sprintf(testExpNotice3, registrar.UrlHintAddr) _, err = reg.SetUrlToHash(addr, dochash, "file:///test.content") if err != nil { t.Errorf("error registering: %v", err) } if tf.lastConfirm != exp { t.Errorf("Wrong confirm message, expected '%v', got '%v'", exp, tf.lastConfirm) } }
func testGetNodeData(t *testing.T, protocol int) { // Define three accounts to simulate transactions with acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey) acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey) // Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_makerts_test) generator := func(i int, block *core.BlockGen) { switch i { case 0: // In block 1, the test bank sends account #1 some ether. tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey) block.AddTx(tx) case 1: // In block 2, the test bank sends some more ether to account #1. // acc1Addr passes it on to account #2. tx1, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(testBankKey) tx2, _ := types.NewTransaction(block.TxNonce(acc1Addr), acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key) block.AddTx(tx1) block.AddTx(tx2) case 2: // Block 3 is empty but was mined by account #2. block.SetCoinbase(acc2Addr) block.SetExtra([]byte("yeehaw")) case 3: // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data). b2 := block.PrevBlock(1).Header() b2.Extra = []byte("foo") block.AddUncle(b2) b3 := block.PrevBlock(2).Header() b3.Extra = []byte("foo") block.AddUncle(b3) } } // Assemble the test environment pm := newTestProtocolManagerMust(t, false, 4, generator, nil) peer, _ := newTestPeer("peer", protocol, pm, true) defer peer.close() // Fetch for now the entire chain db hashes := []common.Hash{} for _, key := range pm.chaindb.(*ethdb.MemDatabase).Keys() { if len(key) == len(common.Hash{}) { hashes = append(hashes, common.BytesToHash(key)) } } p2p.Send(peer.app, 0x0d, hashes) msg, err := peer.app.ReadMsg() if err != nil { t.Fatalf("failed to read node data response: %v", err) } if msg.Code != 0x0e { t.Fatalf("response packet code mismatch: have %x, want %x", msg.Code, 0x0c) } var data [][]byte if err := msg.Decode(&data); err != nil { t.Fatalf("failed to decode response node data: %v", err) } // Verify that all hashes correspond to the requested data, and reconstruct a state tree for i, want := range hashes { if hash := crypto.Sha3Hash(data[i]); hash != want { fmt.Errorf("data hash mismatch: have %x, want %x", hash, want) } } statedb, _ := ethdb.NewMemDatabase() for i := 0; i < len(data); i++ { statedb.Put(hashes[i].Bytes(), data[i]) } accounts := []common.Address{testBankAddress, acc1Addr, acc2Addr} for i := uint64(0); i <= pm.blockchain.CurrentBlock().NumberU64(); i++ { trie, _ := state.New(pm.blockchain.GetBlockByNumber(i).Root(), statedb) for j, acc := range accounts { state, _ := pm.blockchain.State() bw := state.GetBalance(acc) bh := trie.GetBalance(acc) if (bw != nil && bh == nil) || (bw == nil && bh != nil) { t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw) } if bw != nil && bh != nil && bw.Cmp(bw) != 0 { t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw) } } } }
// Lookup performs a network search for nodes close // to the given target. It approaches the target by querying // nodes that are closer to it on each iteration. // The given target does not need to be an actual node // identifier. func (tab *Table) Lookup(targetID NodeID) []*Node { var ( target = crypto.Sha3Hash(targetID[:]) asked = make(map[NodeID]bool) seen = make(map[NodeID]bool) reply = make(chan []*Node, alpha) pendingQueries = 0 ) // don't query further if we hit ourself. // unlikely to happen often in practice. asked[tab.self.ID] = true tab.mutex.Lock() // update last lookup stamp (for refresh logic) tab.buckets[logdist(tab.self.sha, target)].lastLookup = time.Now() // generate initial result set result := tab.closest(target, bucketSize) tab.mutex.Unlock() // If the result set is empty, all nodes were dropped, refresh if len(result.entries) == 0 { tab.refresh() return nil } for { // ask the alpha closest nodes that we haven't asked yet for i := 0; i < len(result.entries) && pendingQueries < alpha; i++ { n := result.entries[i] if !asked[n.ID] { asked[n.ID] = true pendingQueries++ go func() { // Find potential neighbors to bond with r, err := tab.net.findnode(n.ID, n.addr(), targetID) if err != nil { // Bump the failure counter to detect and evacuate non-bonded entries fails := tab.db.findFails(n.ID) + 1 tab.db.updateFindFails(n.ID, fails) glog.V(logger.Detail).Infof("Bumping failures for %x: %d", n.ID[:8], fails) if fails >= maxFindnodeFailures { glog.V(logger.Detail).Infof("Evacuating node %x: %d findnode failures", n.ID[:8], fails) tab.delete(n) } } reply <- tab.bondall(r) }() } } if pendingQueries == 0 { // we have asked all closest nodes, stop the search break } // wait for the next reply for _, n := range <-reply { if n != nil && !seen[n.ID] { seen[n.ID] = true result.push(n, bucketSize) } } pendingQueries-- } return result.entries }