diff options
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/api.go | 49 | ||||
-rw-r--r-- | rpc/api_test.go | 44 | ||||
-rw-r--r-- | rpc/args.go | 64 | ||||
-rw-r--r-- | rpc/http.go | 3 |
4 files changed, 88 insertions, 72 deletions
diff --git a/rpc/api.go b/rpc/api.go index b59253ef7..b6f6ac3f9 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -1,9 +1,9 @@ package rpc import ( + "bytes" "encoding/json" "math/big" - // "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -158,16 +158,16 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err v := api.xethAtStateNum(args.BlockNumber).CodeAtBytes(args.Address) *reply = newHexData(v) - case "eth_sign": - args := new(NewSigArgs) - if err := json.Unmarshal(req.Params, &args); err != nil { - return err - } - v, err := api.xeth().Sign(args.From, args.Data, false) - if err != nil { - return err - } - *reply = v + // case "eth_sign": + // args := new(NewSigArgs) + // if err := json.Unmarshal(req.Params, &args); err != nil { + // return err + // } + // v, err := api.xeth().Sign(args.From, args.Data, false) + // if err != nil { + // return err + // } + // *reply = v case "eth_sendTransaction", "eth_transact": args := new(NewTxArgs) @@ -230,7 +230,14 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err block := api.xeth().EthBlockByNumber(args.BlockNumber) br := NewBlockRes(block, args.IncludeTxs) - + // If request was for "pending", nil nonsensical fields + if args.BlockNumber == -2 { + br.BlockHash = nil + br.BlockNumber = nil + br.Miner = nil + br.Nonce = nil + br.LogsBloom = nil + } *reply = br case "eth_getTransactionByHash": args := new(HashArgs) @@ -240,9 +247,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err tx, bhash, bnum, txi := api.xeth().EthTransactionByHash(args.Hash) if tx != nil { v := NewTransactionRes(tx) - v.BlockHash = newHexData(bhash) - v.BlockNumber = newHexNum(bnum) - v.TxIndex = newHexNum(txi) + // if the blockhash is 0, assume this is a pending transaction + if bytes.Compare(bhash.Bytes(), bytes.Repeat([]byte{0}, 32)) != 0 { + v.BlockHash = newHexData(bhash) + v.BlockNumber = newHexNum(bnum) + v.TxIndex = newHexNum(txi) + } *reply = v } case "eth_getTransactionByBlockHashAndIndex": @@ -337,7 +347,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err solc, _ := api.xeth().Solc() if solc == nil { - return NewNotImplementedError(req.Method) + return NewNotAvailableError(req.Method, "solc (solidity compiler) not found") } args := new(SourceArgs) @@ -345,12 +355,11 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return err } - contract, err := solc.Compile(args.Source) + contracts, err := solc.Compile(args.Source) if err != nil { return err } - contract.Code = newHexData(contract.Code).String() - *reply = contract + *reply = contracts case "eth_newFilter": args := new(BlockFilterArgs) @@ -577,7 +586,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return NewNotImplementedError(req.Method) } - glog.V(logger.Detail).Infof("Reply: %T %s\n", reply, reply) + // glog.V(logger.Detail).Infof("Reply: %v\n", reply) return nil } diff --git a/rpc/api_test.go b/rpc/api_test.go index b49e27bd1..d1dcad266 100644 --- a/rpc/api_test.go +++ b/rpc/api_test.go @@ -2,14 +2,11 @@ package rpc import ( "encoding/json" - // "sync" - "testing" - // "time" - // "fmt" - "io/ioutil" "strconv" + "testing" "github.com/ethereum/go-ethereum/common/compiler" + "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/xeth" ) @@ -30,12 +27,15 @@ func TestWeb3Sha3(t *testing.T) { } } +const solcVersion = "0.9.23" + func TestCompileSolidity(t *testing.T) { - t.Skip() solc, err := compiler.New("") if solc == nil { - t.Skip("no solidity compiler") + t.Skip("no solc found: skip") + } else if solc.Version() != solcVersion { + t.Logf("WARNING: solc different version found (%v, test written for %v, may need to update)", solc.Version(), solcVersion) } source := `contract test {\n` + " /// @notice Will multiply `a` by 7." + `\n` + @@ -46,16 +46,16 @@ func TestCompileSolidity(t *testing.T) { jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}` - //expCode := "605280600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b60376004356041565b8060005260206000f35b6000600782029050604d565b91905056" + expCode := "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056" expAbiDefinition := `[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]` expUserDoc := `{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}}` expDeveloperDoc := `{"methods":{}}` - expCompilerVersion := `0.9.13` + expCompilerVersion := solc.Version() expLanguage := "Solidity" expLanguageVersion := "0" expSource := source - api := NewEthereumApi(&xeth.XEth{}) + api := NewEthereumApi(xeth.NewTest(ð.Ethereum{}, nil)) var req RpcRequest json.Unmarshal([]byte(jsonstr), &req) @@ -70,26 +70,34 @@ func TestCompileSolidity(t *testing.T) { t.Errorf("expected no error, got %v", err) } - var contract = compiler.Contract{} - err = json.Unmarshal(respjson, &contract) + var contracts = make(map[string]*compiler.Contract) + err = json.Unmarshal(respjson, &contracts) if err != nil { t.Errorf("expected no error, got %v", err) } - /* - if contract.Code != expCode { - t.Errorf("Expected %s got %s", expCode, contract.Code) - } - */ + if len(contracts) != 1 { + t.Errorf("expected one contract, got %v", len(contracts)) + } + + contract := contracts["test"] + + if contract.Code != expCode { + t.Errorf("Expected \n%s got \n%s", expCode, contract.Code) + } + if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` { t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source)) } + if contract.Info.Language != expLanguage { t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language) } + if contract.Info.LanguageVersion != expLanguageVersion { t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion) } + if contract.Info.CompilerVersion != expCompilerVersion { t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion) } @@ -112,8 +120,6 @@ func TestCompileSolidity(t *testing.T) { if string(abidef) != expAbiDefinition { t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef)) } - ioutil.WriteFile("/tmp/abidef", []byte(string(abidef)), 0700) - ioutil.WriteFile("/tmp/expabidef", []byte(expAbiDefinition), 0700) if string(userdoc) != expUserDoc { t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc)) diff --git a/rpc/args.go b/rpc/args.go index 686872a59..27824f12c 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -166,45 +166,45 @@ type NewTxArgs struct { BlockNumber int64 } -type NewSigArgs struct { - From string - Data string -} +// type NewSigArgs struct { +// From string +// Data string +// } -func (args *NewSigArgs) UnmarshalJSON(b []byte) (err error) { - var obj []json.RawMessage - var ext struct { - From string - Data string - } +// func (args *NewSigArgs) UnmarshalJSON(b []byte) (err error) { +// var obj []json.RawMessage +// var ext struct { +// From string +// Data string +// } - // Decode byte slice to array of RawMessages - if err := json.Unmarshal(b, &obj); err != nil { - return NewDecodeParamError(err.Error()) - } +// // Decode byte slice to array of RawMessages +// if err := json.Unmarshal(b, &obj); err != nil { +// return NewDecodeParamError(err.Error()) +// } - // Check for sufficient params - if len(obj) < 1 { - return NewInsufficientParamsError(len(obj), 1) - } +// // Check for sufficient params +// if len(obj) < 1 { +// return NewInsufficientParamsError(len(obj), 1) +// } - // Decode 0th RawMessage to temporary struct - if err := json.Unmarshal(obj[0], &ext); err != nil { - return NewDecodeParamError(err.Error()) - } +// // Decode 0th RawMessage to temporary struct +// if err := json.Unmarshal(obj[0], &ext); err != nil { +// return NewDecodeParamError(err.Error()) +// } - if len(ext.From) == 0 { - return NewValidationError("from", "is required") - } +// if len(ext.From) == 0 { +// return NewValidationError("from", "is required") +// } - if len(ext.Data) == 0 { - return NewValidationError("data", "is required") - } +// if len(ext.Data) == 0 { +// return NewValidationError("data", "is required") +// } - args.From = ext.From - args.Data = ext.Data - return nil -} +// args.From = ext.From +// args.Data = ext.Data +// return nil +// } func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { var obj []json.RawMessage diff --git a/rpc/http.go b/rpc/http.go index 9b3fa5142..f37e102f5 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -6,6 +6,7 @@ import ( "io" "io/ioutil" "net/http" + "strings" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" @@ -39,7 +40,7 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { if len(config.CorsDomain) > 0 { var opts cors.Options opts.AllowedMethods = []string{"POST"} - opts.AllowedOrigins = []string{config.CorsDomain} + opts.AllowedOrigins = strings.Split(config.CorsDomain, " ") c := cors.New(opts) handler = newStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop) |