从零开发区块链应用(九)--区块链结构体创建

一、json生成结构体的方法

1.1 使用在线工具生成结构体

  • 在线工具地址
    https://mholt.github.io/json-to-go/

这个在线工具使用起来非常简单,只需要将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"`
}

猜你喜欢

转载自blog.csdn.net/cljdsc/article/details/122657422