Develop blockchain applications from scratch (9)--creation of blockchain structure

First, the method of json generating structure

1.1 Use online tools to generate structures

  • Online tool address
    https://mholt.github.io/json-to-go/

This online tool is very simple to use, just paste the JSON data on the left, and the corresponding structure definition will be automatically generated on the right:

  • Use data:

Note: The following is the Ethereum transaction information

{"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"}}
  • generated 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 Using the type from json … method

Using the type from json method, a pop-up box will pop up prompting the input content, and a structure will be generated based on the pasted json content

  • usage data

Note: The following is the Ethereum block data

{"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":[]}}
  • generated 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 Convert sql statement to structure

Sometimes it is necessary to convert the data table into a structure, which can be converted into a structure by using the sql statement for building a table

  • Online tool address

https://sql2struct.js.org/

  • Data table building table sql statement

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
  • generated 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. Blockchain related interface creation

Note: The following structure tests are all Ethereum-based nodes

2.1 Block Information Structure Creation

  • full transaction block
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"`
  • block with hash list
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 Transaction Structure Creation

  • Ethereum is a common transaction structure
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"`
}
  • Ethereum-based contract transaction structure
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"`
}
  • Ethereum-based contract transaction log structure
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 Node rpc request structure

type Request struct {
	ID      string        `json:"id"`
	Mthd    string        `json:"method"`
	Args    []interface{} `json:"params"`
	Version string        `json:"jsonrpc"`
}

Guess you like

Origin blog.csdn.net/cljdsc/article/details/122657422