1. 程式人生 > 其它 >從零開發區塊鏈應用(九)--區塊鏈結構體建立

從零開發區塊鏈應用(九)--區塊鏈結構體建立

目錄

一、json生成結構體的方法

1.1 使用線上工具生成結構體

這個線上工具使用起來非常簡單,只需要將JSON資料貼上在左邊,就會在右邊自動成生成對應的結構體定義:

  • 使用資料 :

注:以下為以太坊交易資訊

{"jsonrpc":"2.0","id":1,"result":{"blockHash":"0x153407ebeb73141a816492eba75d39a6d65c558e33b104d26693cb1eadc131c0","blockNumber":"0x3dd1ec","from":"0x6e60f5243e1a3f0be3f407b5afe9e5395ee82aa2","gas":"0x5208","gasPrice":"0x1264c45600","hash":"0x9994c8fe157d6547a3b484b1995bf3429253a1e3998bf092df270807377ed134","input":"0x","nonce":"0x648","to":"0xac965f9832efd7684bbbd7ceed5891a337bca302","transactionIndex":"0x0","value":"0x1bc16d674ec80000","type":"0x0","v":"0x539bb","r":"0x7afca8f7bc610abe5a4e2326a9df04f5da234c9fcca63e836c450ac442411f5c","s":"0x3139d40ca81fba2dc0ea5acb5d3d0d3040f21a0898c4ae39ff0343242a044a53"}}
  • 生成的struct
type AutoGenerated struct {
	Jsonrpc string `json:"jsonrpc"`
	ID      int    `json:"id"`
	Result  struct {
		BlockHash        string `json:"blockHash"`
		BlockNumber      string `json:"blockNumber"`
		From             string `json:"from"`
		Gas              string `json:"gas"`
		GasPrice         string `json:"gasPrice"`
		Hash             string `json:"hash"`
		Input            string `json:"input"`
		Nonce            string `json:"nonce"`
		To               string `json:"to"`
		TransactionIndex string `json:"transactionIndex"`
		Value            string `json:"value"`
		Type             string `json:"type"`
		V                string `json:"v"`
		R                string `json:"r"`
		S                string `json:"s"`
	} `json:"result"`
}

1.2 使用type from json ... 方法

使用type from json 方法,將會彈出提示輸入內容的彈框,將根據貼上的json內容生成結構體

  • 使用資料

注:以下為以太坊區塊資料

{"jsonrpc":"2.0","id":1,"result":{"difficulty":"0xa319c","extraData":"0xd883010a05846765746888676f312e31362e36856c696e7578","gasLimit":"0x7a1200","gasUsed":"0x0","hash":"0x199ea7129792539f5ab3f90e6b68fe745d677af1862bcf37986d683748205100","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x6e60f5243e1a3f0be3f407b5afe9e5395ee82aa2","mixHash":"0xa15f85597acdfb5bd6d63c0394365c41c647e3cad02b543f81677630949721e9","nonce":"0x146d903669ebc81d","number":"0x3dd21b","parentHash":"0x0bc06a66b3adf832876d1c9eb409ad449d3c71c25685214388370a5d05209a56","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x21b","stateRoot":"0x4798d9bae39233ead7b303d8ca8fefea7c125f275dde919c5b460c510bb6c4bb","timestamp":"0x60f790ec","totalDifficulty":"0x32706575c9c","transactions":[],"transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","uncles":[]}}
  • 生成的struct
type T struct {
	Jsonrpc string `json:"jsonrpc"`
	Id      int    `json:"id"`
	Result  struct {
		Difficulty       string        `json:"difficulty"`
		ExtraData        string        `json:"extraData"`
		GasLimit         string        `json:"gasLimit"`
		GasUsed          string        `json:"gasUsed"`
		Hash             string        `json:"hash"`
		LogsBloom        string        `json:"logsBloom"`
		Miner            string        `json:"miner"`
		MixHash          string        `json:"mixHash"`
		Nonce            string        `json:"nonce"`
		Number           string        `json:"number"`
		ParentHash       string        `json:"parentHash"`
		ReceiptsRoot     string        `json:"receiptsRoot"`
		Sha3Uncles       string        `json:"sha3Uncles"`
		Size             string        `json:"size"`
		StateRoot        string        `json:"stateRoot"`
		Timestamp        string        `json:"timestamp"`
		TotalDifficulty  string        `json:"totalDifficulty"`
		Transactions     []interface{} `json:"transactions"`
		TransactionsRoot string        `json:"transactionsRoot"`
		Uncles           []interface{} `json:"uncles"`
	} `json:"result"`
}

1.3 sql語句轉換為結構體

有時候還需要將資料錶轉為結構體,可利用建表sql語句轉為結構體

  • 線上工具地址

https://sql2struct.js.org/

  • 資料表建表sql語句

CREATE TABLE `member` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `phone` char(11) NOT NULL COMMENT '手機號',
  `pay_password` varchar(50) NOT NULL DEFAULT '' COMMENT '支付密碼',
  `address` char(42) NOT NULL DEFAULT '' COMMENT '區塊鏈地址',
  `total_amount` int(10) NOT NULL DEFAULT '0' COMMENT '總積分',
  `available_amount` int(10) NOT NULL DEFAULT '0' COMMENT '可用積分',
  `freeze_amount` int(10) NOT NULL DEFAULT '0' COMMENT '凍結積分',
  `total_recharge` int(10) NOT NULL DEFAULT '0' COMMENT '總充值金額',
  `salt` varchar(20) NOT NULL DEFAULT '' COMMENT '鹽',
  `token` varchar(20) NOT NULL DEFAULT '' COMMENT '登入token',
  `status` int(10) NOT NULL DEFAULT '0' COMMENT '使用者狀態:正常(0);禁用(1)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4
  • 生成的struct
type Member struct { 
Id int64 `json:"id"` // id
Phone string `json:"phone"` // 手機號
PayPassword string `json:"pay_password"` // 支付密碼
Address string `json:"address"` // 區塊鏈地址
TotalAmount int64 `json:"total_amount"` // 總積分
AvailableAmount int64 `json:"available_amount"` // 可用積分
FreezeAmount int64 `json:"freeze_amount"` // 凍結積分
TotalRecharge int64 `json:"total_recharge"` // 總充值金額
Salt string `json:"salt"` // 鹽
Token string `json:"token"` // 登入token
Status int64 `json:"status"` // 使用者狀態:正常(0);禁用(1)
CreateTime time.Time `json:"create_time"` // 建立時間
UpdateTime time.Time `json:"update_time"` // 更新時間
}

二、區塊鏈相關介面建立

注:以下結構體測試均為以太坊系節點

2.1 區塊資訊結構體建立

  • 全交易區塊
type Block struct {
	Number     string         `json:"number"`
	Size       string         `json:"size"`
	Timestamp  string         `json:"timestamp"`
	GasLimit   string         `json:"gasLimit"`
	GasUsed    string         `json:"gasUsed"`
	Hash       string         `json:"hash"`
	ParentHash string         `json:"parentHash"`
	Coinbase   string         `json:"miner"`
	TxDatas    []*Transaction `json:"transactions"`
  • 帶雜湊列表的區塊
type BlockTxHash struct {
	Number     string   `json:"number"`
	Size       string   `json:"size"`
	Timestamp  string   `json:"timestamp"`
	GasLimit   string   `json:"gasLimit"`
	GasUsed    string   `json:"gasUsed"`
	Hash       string   `json:"hash"`
	ParentHash string   `json:"parentHash"`
	Coinbase   string   `json:"miner"`
	TxHexs     []string `json:"transactions"`
}

2.2 交易結構體建立

  • 以太坊系普通交易結構體
type Transaction struct {
	BlockHash   string `json:"blockHash"`
	BlockNumber string `json:"blockNumber"`
	From        string `json:"from"`
	Recipient   string `json:"to"`
	GasLimit    string `json:"gas"`
	GasPrice    string `json:"gasPrice"`
	Hash        string `json:"hash"`
	Payload     string `json:"input"`
	Nonce       string `json:"nonce"`
	R           string `json:"r"`
	S           string `json:"s"`
	V           string `json:"v"`
	Index       string `json:"transactionIndex"`
	Value       string `json:"value"`
}
  • 以太坊系合約交易結構體
type TxReceipt struct {
	Hash         string         `json:"transactionHash"`
	Index        string         `json:"transactionIndex"`
	BlockNumber  string         `json:"blockNumber"`
	BlockHash    string         `json:"blockHash"`
	GasUsedTotal string         `json:"cumulativeGasUsed"`
	GasUsed      string         `json:"gasUsed"`
	Contract     string         `json:"contractAddress"`
	LogsBloom    string         `json:"logsBloom"`
	Status       string         `json:"status"`
	From         string         `json:"from"`
	To           string         `json:"to"`
	Logs         []*ReceiptLogs `json:"logs"`
}
  • 以太坊系合約交易日誌結構體
type ReceiptLogs struct {
	Address     string   `json:"address"`
	BlockNumber string   `json:"blockNumber"`
	BlockHash   string   `json:"blockHash"`
	Index       string   `json:"transactionIndex"`
	Hash        string   `json:"transactionHash"`
	Data        string   `json:"data"`
	LogIndex    string   `json:"logIndex"`
	Removed     bool     `json:"removed"`
	Topics      []string `json:"topics"`
}

2.3 節點rpc請求結構體

type Request struct {
	ID      string        `json:"id"`
	Mthd    string        `json:"method"`
	Args    []interface{} `json:"params"`
	Version string        `json:"jsonrpc"`
}
當你老了,回顧一生,就會發覺:什麼時候出國讀書,什麼時候決定做第一份職業、何時選定了物件而戀愛、什麼時候結婚,其實都是命運的鉅變。只是當時站在三岔路口,眼見風雲千檣,你作出選擇的那一日,在日記上,相當沉悶和平凡,當時還以為是生命中普通的一天。