github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/rpcapi/block.go (about) 1 package rpcapi 2 3 import ( 4 "time" 5 "sync" 6 "strings" 7 "encoding/hex" 8 "github.com/piotrnar/gocoin/lib/btc" 9 "github.com/piotrnar/gocoin/client/network" 10 "encoding/json" 11 "io/ioutil" 12 "fmt" 13 "github.com/piotrnar/gocoin/client/common" 14 ) 15 16 type BlockSubmited struct { 17 *btc.Block 18 Error string 19 Done sync.WaitGroup 20 } 21 22 var RpcBlocks chan *BlockSubmited = make(chan *BlockSubmited, 1) 23 24 25 func SubmitBlock(cmd *RpcCommand, resp *RpcResponse, b []byte) { 26 var bd []byte 27 var er error 28 29 switch uu := cmd.Params.(type) { 30 case []interface{}: 31 if len(uu)<1 { 32 resp.Error = RpcError{Code: -1, Message: "empty params array"} 33 return 34 } 35 str := uu[0].(string) 36 if str[0]=='@' { 37 /* 38 gocoin special case: if the string starts with @, it's a name of the file with block's binary data 39 curl --user gocoinrpc:gocoinpwd --data-binary \ 40 '{"jsonrpc": "1.0", "id":"curltest", "method": "submitblock", "params": \ 41 ["@450529_000000000000000000cf208f521de0424677f7a87f2f278a1042f38d159565f5.bin"] }' \ 42 -H 'content-type: text/plain;' http://127.0.0.1:8332/ 43 */ 44 //println("jade z koksem", str[1:]) 45 bd, er = ioutil.ReadFile(str[1:]) 46 } else { 47 bd, er = hex.DecodeString(str) 48 } 49 if er != nil { 50 resp.Error = RpcError{Code: -3, Message: er.Error()} 51 return 52 } 53 54 default: 55 resp.Error = RpcError{Code: -2, Message: "incorrect params type"} 56 return 57 } 58 59 bs := new(BlockSubmited) 60 61 bs.Block, er = btc.NewBlock(bd) 62 if er != nil { 63 resp.Error = RpcError{Code: -4, Message: er.Error()} 64 return 65 } 66 67 network.MutexRcv.Lock() 68 network.ReceivedBlocks[bs.Block.Hash.BIdx()] = &network.OneReceivedBlock{TmStart: time.Now()} 69 network.MutexRcv.Unlock() 70 71 println("new block", bs.Block.Hash.String(), "len", len(bd), "- submitting...") 72 bs.Done.Add(1) 73 RpcBlocks <- bs 74 bs.Done.Wait() 75 if bs.Error != "" { 76 //resp.Error = RpcError{Code: -10, Message: bs.Error} 77 idx := strings.Index(bs.Error, "- RPC_Result:") 78 if idx == -1 { 79 resp.Result = "inconclusive" 80 } else { 81 resp.Result = bs.Error[idx+13:] 82 } 83 println("submiting block error:", bs.Error) 84 println("submiting block result:", resp.Result.(string)) 85 86 print("time_now:", time.Now().Unix()) 87 print(" cur_block_ts:", bs.Block.BlockTime()) 88 print(" last_given_now:", last_given_time) 89 print(" last_given_min:", last_given_mintime) 90 common.Last.Mutex.Lock() 91 print(" prev_block_ts:", common.Last.Block.Timestamp()) 92 common.Last.Mutex.Unlock() 93 println() 94 95 return 96 } 97 98 // cress check with bitcoind... 99 if false { 100 bitcoind_result := process_rpc(b) 101 json.Unmarshal(bitcoind_result, &resp) 102 switch cmd.Params.(type) { 103 case string: 104 println("\007Block rejected by bitcoind:", resp.Result.(string)) 105 ioutil.WriteFile(fmt.Sprint(bs.Block.Height, "-", bs.Block.Hash.String()), bd, 0777) 106 default: 107 println("submiting block verified OK", bs.Error) 108 } 109 } 110 } 111 112 var last_given_time, last_given_mintime uint32