diff --git a/rpcapi/api/ledger_model.go b/rpcapi/api/ledger_model.go index cd4e2d53f..76d25cb9c 100644 --- a/rpcapi/api/ledger_model.go +++ b/rpcapi/api/ledger_model.go @@ -16,20 +16,6 @@ type SnapshotChunk struct { } type AccountBlock struct { - *RawTxBlock - - TokenInfo *RpcTokenInfo `json:"tokenInfo"` - - ConfirmedTimes *string `json:"confirmedTimes"` - ConfirmedHash *types.Hash `json:"confirmedHash"` - - ReceiveBlockHeight *string `json:"receiveBlockHeight"` - ReceiveBlockHash *types.Hash `json:"receiveBlockHash"` - - Timestamp int64 `json:"timestamp"` -} - -type RawTxBlock struct { BlockType byte `json:"blockType"` Height string `json:"height"` Hash types.Hash `json:"hash"` @@ -54,10 +40,21 @@ type RawTxBlock struct { Signature []byte `json:"signature"` - Quota *string `json:"quota"` - QuotaUsed *string `json:"quotaUsed"` - LogHash *types.Hash `json:"logHash"` - SendBlockList []*RawTxBlock `json:"sendBlockList"` + Quota *string `json:"quota"` + QuotaUsed *string `json:"quotaUsed"` + LogHash *types.Hash `json:"logHash"` + SendBlockList []*AccountBlock `json:"sendBlockList"` + + // extra info below + TokenInfo *RpcTokenInfo `json:"tokenInfo"` + + ConfirmedTimes *string `json:"confirmedTimes"` + ConfirmedHash *types.Hash `json:"confirmedHash"` + + ReceiveBlockHeight *string `json:"receiveBlockHeight"` + ReceiveBlockHash *types.Hash `json:"receiveBlockHash"` + + Timestamp int64 `json:"timestamp"` } type SnapshotBlock struct { @@ -66,82 +63,151 @@ type SnapshotBlock struct { Timestamp int64 `json:"timestamp"` } -func (block *RawTxBlock) RpcToLedgerBlock() (*ledger.AccountBlock, error) { - return copyRawTxToLedgerBlock(block) -} +func (block *AccountBlock) RpcToLedgerBlock() (*ledger.AccountBlock, error) { + lAb := &ledger.AccountBlock{ + BlockType: block.BlockType, + Hash: block.Hash, + PrevHash: block.PrevHash, + AccountAddress: block.AccountAddress, + PublicKey: block.PublicKey, + ToAddress: block.ToAddress, -func (block *RawTxBlock) ComputeHash() (*types.Hash, error) { - lAb, err := copyRawTxToLedgerBlock(block) + FromBlockHash: block.FromBlockHash, + TokenId: block.TokenId, + + Data: block.Data, + Nonce: block.Nonce, + Signature: block.Signature, + LogHash: block.LogHash, + } + + var err error + + lAb.Height, err = strconv.ParseUint(block.Height, 10, 64) if err != nil { return nil, err } - hash := lAb.ComputeHash() - return &hash, nil -} -func ledgerSnapshotBlockToRpcBlock(sb *ledger.SnapshotBlock) (*SnapshotBlock, error) { - if sb == nil { - return nil, nil + lAb.Amount = big.NewInt(0) + if block.Amount != nil { + if _, ok := lAb.Amount.SetString(*block.Amount, 10); !ok { + return nil, ErrStrToBigInt + } } - rpcBlock := &SnapshotBlock{ - SnapshotBlock: sb, + + lAb.Fee = big.NewInt(0) + if block.Fee != nil { + if _, ok := lAb.Fee.SetString(*block.Fee, 10); !ok { + return nil, ErrStrToBigInt + } } - rpcBlock.Producer = sb.Producer() + if block.Nonce != nil { + if block.Difficulty == nil { + return nil, errors.New("lack of difficulty field") + } else { + difficultyStr, ok := new(big.Int).SetString(*block.Difficulty, 10) + if !ok { + return nil, ErrStrToBigInt + } + lAb.Difficulty = difficultyStr + } + } - rpcBlock.Timestamp = sb.Timestamp.Unix() - return rpcBlock, nil -} + if block.Quota != nil { + lAb.Quota, err = strconv.ParseUint(*block.Quota, 10, 64) + if err != nil { + return nil, err + } + } -func ledgerToRpcBlock(chain chain.Chain, lAb *ledger.AccountBlock) (*AccountBlock, error) { - rpcBlock := &AccountBlock{} + if block.QuotaUsed != nil { + lAb.QuotaUsed, err = strconv.ParseUint(*block.QuotaUsed, 10, 64) + if err != nil { + return nil, err + } + } + + if !types.IsContractAddr(block.AccountAddress) { + return lAb, nil + } + + if len(block.SendBlockList) > 0 { + subLAbList := make([]*ledger.AccountBlock, len(block.SendBlockList)) + for k, v := range block.SendBlockList { + subLAb, subErr := v.RpcToLedgerBlock() + if subErr != nil { + return nil, subErr + } + subLAbList[k] = subLAb + } + lAb.SendBlockList = subLAbList + } - rawTxBlock, err := copyLedgerBlockToRawTx(chain, lAb) + return lAb, nil +} + +func (block *AccountBlock) ComputeHash() (*types.Hash, error) { + lAb, err := block.RpcToLedgerBlock() if err != nil { return nil, err } - rpcBlock.RawTxBlock = rawTxBlock + hash := lAb.ComputeHash() + return &hash, nil +} - // Producer - rpcBlock.Producer = lAb.Producer() +func (block *AccountBlock) addExtraInfo(chain chain.Chain) error { // TokenInfo - if rawTxBlock.TokenId != types.ZERO_TOKENID { - token, _ := chain.GetTokenInfoById(rawTxBlock.TokenId) - rpcBlock.TokenInfo = RawTokenInfoToRpc(token, rawTxBlock.TokenId) + if block.TokenId != types.ZERO_TOKENID { + token, _ := chain.GetTokenInfoById(block.TokenId) + block.TokenInfo = RawTokenInfoToRpc(token, block.TokenId) } // ReceiveBlockHeight & ReceiveBlockHash - if lAb.IsSendBlock() { - receiveBlock, err := chain.GetReceiveAbBySendAb(lAb.Hash) + if ledger.IsSendBlock(block.BlockType) { + receiveBlock, err := chain.GetReceiveAbBySendAb(block.Hash) if err != nil { - return nil, err + return err } if receiveBlock != nil { heightStr := strconv.FormatUint(receiveBlock.Height, 10) - rpcBlock.ReceiveBlockHeight = &heightStr - rpcBlock.ReceiveBlockHash = &receiveBlock.Hash + block.ReceiveBlockHeight = &heightStr + block.ReceiveBlockHash = &receiveBlock.Hash } } // ConfirmedTimes & ConfirmedHash latestSb := chain.GetLatestSnapshotBlock() - confirmedBlock, err := chain.GetConfirmSnapshotHeaderByAbHash(lAb.Hash) + confirmedBlock, err := chain.GetConfirmSnapshotHeaderByAbHash(block.Hash) if err != nil { - return nil, err + return err } if confirmedBlock != nil && latestSb != nil && confirmedBlock.Height <= latestSb.Height { confirmedTimeStr := strconv.FormatUint(latestSb.Height-confirmedBlock.Height+1, 10) - rpcBlock.ConfirmedTimes = &confirmedTimeStr - rpcBlock.ConfirmedHash = &confirmedBlock.Hash - rpcBlock.Timestamp = confirmedBlock.Timestamp.Unix() + block.ConfirmedTimes = &confirmedTimeStr + block.ConfirmedHash = &confirmedBlock.Hash + block.Timestamp = confirmedBlock.Timestamp.Unix() + } + return nil +} + +func ledgerSnapshotBlockToRpcBlock(sb *ledger.SnapshotBlock) (*SnapshotBlock, error) { + if sb == nil { + return nil, nil + } + rpcBlock := &SnapshotBlock{ + SnapshotBlock: sb, } + rpcBlock.Producer = sb.Producer() + + rpcBlock.Timestamp = sb.Timestamp.Unix() return rpcBlock, nil } -func copyLedgerBlockToRawTx(chain chain.Chain, lAb *ledger.AccountBlock) (*RawTxBlock, error) { - rawTxBlock := &RawTxBlock{ +func ledgerToRpcBlock(chain chain.Chain, lAb *ledger.AccountBlock) (*AccountBlock, error) { + rpcBlock := &AccountBlock{ BlockType: lAb.BlockType, Hash: lAb.Hash, PrevHash: lAb.PrevHash, @@ -153,28 +219,30 @@ func copyLedgerBlockToRawTx(chain chain.Chain, lAb *ledger.AccountBlock) (*RawTx Data: lAb.Data, Signature: lAb.Signature, LogHash: lAb.LogHash, + + Producer: lAb.Producer(), } //height - rawTxBlock.Height = strconv.FormatUint(lAb.Height, 10) + rpcBlock.Height = strconv.FormatUint(lAb.Height, 10) // Quota & QuotaUsed quota := strconv.FormatUint(lAb.Quota, 10) - rawTxBlock.Quota = "a + rpcBlock.Quota = "a quotaUsed := strconv.FormatUint(lAb.QuotaUsed, 10) - rawTxBlock.QuotaUsed = "aUsed + rpcBlock.QuotaUsed = "aUsed // FromAddress & ToAddress var amount, fee string if lAb.IsSendBlock() { - rawTxBlock.FromAddress = lAb.AccountAddress - rawTxBlock.ToAddress = lAb.ToAddress + rpcBlock.FromAddress = lAb.AccountAddress + rpcBlock.ToAddress = lAb.ToAddress if lAb.Amount != nil { amount = lAb.Amount.String() - rawTxBlock.Amount = &amount + rpcBlock.Amount = &amount } if lAb.Fee != nil { fee = lAb.Fee.String() - rawTxBlock.Fee = &fee + rpcBlock.Fee = &fee } } else { sendBlock, err := chain.GetAccountBlockByHash(lAb.FromBlockHash) @@ -182,130 +250,47 @@ func copyLedgerBlockToRawTx(chain chain.Chain, lAb *ledger.AccountBlock) (*RawTx return nil, err } if sendBlock != nil { - rawTxBlock.FromAddress = sendBlock.AccountAddress - rawTxBlock.ToAddress = sendBlock.ToAddress + rpcBlock.FromAddress = sendBlock.AccountAddress + rpcBlock.ToAddress = sendBlock.ToAddress - rawTxBlock.FromBlockHash = sendBlock.Hash - rawTxBlock.TokenId = sendBlock.TokenId + rpcBlock.FromBlockHash = sendBlock.Hash + rpcBlock.TokenId = sendBlock.TokenId if sendBlock.Amount != nil { amount = sendBlock.Amount.String() - rawTxBlock.Amount = &amount + rpcBlock.Amount = &amount } if sendBlock.Fee != nil { fee = sendBlock.Fee.String() - rawTxBlock.Fee = &fee + rpcBlock.Fee = &fee } } } // Difficulty & Nonce - rawTxBlock.Nonce = lAb.Nonce + rpcBlock.Nonce = lAb.Nonce if lAb.Difficulty != nil { difficulty := lAb.Difficulty.String() - rawTxBlock.Difficulty = &difficulty + rpcBlock.Difficulty = &difficulty + } + + if err := rpcBlock.addExtraInfo(chain); err != nil { + return nil, err } // SendBlockList if len(lAb.SendBlockList) > 0 { - subBlockList := make([]*RawTxBlock, len(lAb.SendBlockList)) + subBlockList := make([]*AccountBlock, len(lAb.SendBlockList)) for k, v := range lAb.SendBlockList { - subRawTx, err := copyLedgerBlockToRawTx(chain, v) + subRpcTx, err := ledgerToRpcBlock(chain, v) if err != nil { return nil, err } - subBlockList[k] = subRawTx - } - rawTxBlock.SendBlockList = subBlockList - } - - return rawTxBlock, nil -} - -func copyRawTxToLedgerBlock(rawTxAb *RawTxBlock) (*ledger.AccountBlock, error) { - lAb := &ledger.AccountBlock{ - BlockType: rawTxAb.BlockType, - Hash: rawTxAb.Hash, - PrevHash: rawTxAb.PrevHash, - AccountAddress: rawTxAb.AccountAddress, - PublicKey: rawTxAb.PublicKey, - ToAddress: rawTxAb.ToAddress, - - FromBlockHash: rawTxAb.FromBlockHash, - TokenId: rawTxAb.TokenId, - - Data: rawTxAb.Data, - Nonce: rawTxAb.Nonce, - Signature: rawTxAb.Signature, - } - - var err error - - lAb.Height, err = strconv.ParseUint(rawTxAb.Height, 10, 64) - if err != nil { - return nil, err - } - - lAb.Amount = big.NewInt(0) - if rawTxAb.Amount != nil { - if _, ok := lAb.Amount.SetString(*rawTxAb.Amount, 10); !ok { - return nil, ErrStrToBigInt - } - } - - lAb.Fee = big.NewInt(0) - if rawTxAb.Fee != nil { - if _, ok := lAb.Fee.SetString(*rawTxAb.Fee, 10); !ok { - return nil, ErrStrToBigInt - } - } - - if rawTxAb.Nonce != nil { - if rawTxAb.Difficulty == nil { - return nil, errors.New("lack of difficulty field") - } else { - difficultyStr, ok := new(big.Int).SetString(*rawTxAb.Difficulty, 10) - if !ok { - return nil, ErrStrToBigInt - } - lAb.Difficulty = difficultyStr - } - } - - if rawTxAb.Quota != nil { - lAb.Quota, err = strconv.ParseUint(*rawTxAb.Quota, 10, 64) - if err != nil { - return nil, err - } - } - - if rawTxAb.QuotaUsed != nil { - lAb.QuotaUsed, err = strconv.ParseUint(*rawTxAb.QuotaUsed, 10, 64) - if err != nil { - return nil, err - } - } - - if rawTxAb.LogHash != nil { - lAb.LogHash = rawTxAb.LogHash - } - - if !types.IsContractAddr(rawTxAb.AccountAddress) { - return lAb, nil - } - - if len(rawTxAb.SendBlockList) > 0 { - subLAbList := make([]*ledger.AccountBlock, len(rawTxAb.SendBlockList)) - for k, v := range rawTxAb.SendBlockList { - subLAb, subErr := copyRawTxToLedgerBlock(v) - if subErr != nil { - return nil, subErr - } - subLAbList[k] = subLAb + subBlockList[k] = subRpcTx } - lAb.SendBlockList = subLAbList + rpcBlock.SendBlockList = subBlockList } - return lAb, nil + return rpcBlock, nil } type RpcAccountInfo struct { @@ -428,180 +413,3 @@ func (param NormalRequestRawTxParam) LedgerAccountBlock() (*ledger.AccountBlock, } return lAb, nil } - -/* -type AccountBlock struct { - *ledger.AccountBlock - - FromAddress types.Address `json:"FromAddress"` - - Height string `json:"height"` - - Amount *string `json:"amount"` - Fee *string `json:"fee"` - TokenInfo *RpcTokenInfo `json:"tokenInfo"` - - Difficulty *string `json:"difficulty"` - Quota *string `json:"quota"` - - Timestamp int64 `json:"timestamp"` - - ConfirmedTimes *string `json:"confirmedTimes"` - ConfirmedHash *types.Hash `json:"confirmedHash"` - - ReceiveBlockHeight string `json:"receiveBlockHeight"` - ReceiveBlockHash *types.Hash `json:"receiveBlockHash"` -} - - -// TODO set timestamp -func (ab *AccountBlock) LedgerAccountBlock() (*ledger.AccountBlock, error) { - lAb := ab.AccountBlock - if lAb == nil { - lAb = &ledger.AccountBlock{} - } - - var err error - lAb.Height, err = strconv.ParseUint(ab.Height, 10, 64) - if err != nil { - return nil, err - } - if ab.Quota != nil { - lAb.Quota, err = strconv.ParseUint(*ab.Quota, 10, 64) - if err != nil { - return nil, err - } - } - - lAb.Amount = big.NewInt(0) - if ab.Amount != nil { - if _, ok := lAb.Amount.SetString(*ab.Amount, 10); !ok { - return nil, ErrStrToBigInt - } - } - - lAb.Fee = big.NewInt(0) - if ab.Fee != nil { - if _, ok := lAb.Fee.SetString(*ab.Fee, 10); !ok { - return nil, ErrStrToBigInt - } - } - - if ab.AccountBlock != nil && ab.AccountBlock.Nonce != nil { - if ab.Difficulty == nil { - return nil, errors.New("lack of difficulty field") - } else { - setString, ok := new(big.Int).SetString(*ab.Difficulty, 10) - if !ok { - return nil, ErrStrToBigInt - } - lAb.Difficulty = setString - } - } - - return lAb, nil -} - -func ledgerToRpcBlock(block *ledger.AccountBlock, chain chain.Chain) (*AccountBlock, error) { - latestSb := chain.GetLatestSnapshotBlock() - - confirmedBlock, err := chain.GetConfirmSnapshotHeaderByAbHash(block.Hash) - - if err != nil { - return nil, err - } - - var confirmedTimes uint64 - var confirmedHash types.Hash - var timestamp int64 - - if confirmedBlock != nil && latestSb != nil && confirmedBlock.Height <= latestSb.Height { - confirmedHash = confirmedBlock.Hash - confirmedTimes = latestSb.Height - confirmedBlock.Height + 1 - timestamp = confirmedBlock.Timestamp.Unix() - } - - var fromAddress, toAddress types.Address - if block.IsReceiveBlock() { - toAddress = block.AccountAddress - sendBlock, err := chain.GetAccountBlockByHash(block.FromBlockHash) - if err != nil { - return nil, err - } - - if sendBlock != nil { - fromAddress = sendBlock.AccountAddress - block.TokenId = sendBlock.TokenId - block.Amount = sendBlock.Amount - block.Fee = sendBlock.Fee - } - } else { - fromAddress = block.AccountAddress - toAddress = block.ToAddress - } - - token, _ := chain.GetTokenInfoById(block.TokenId) - rpcAccountBlock := createAccountBlock(block, token, confirmedTimes) - - if confirmedTimes > 0 { - rpcAccountBlock.ConfirmedHash = &confirmedHash - } - - rpcAccountBlock.FromAddress = fromAddress - rpcAccountBlock.ToAddress = toAddress - rpcAccountBlock.Timestamp = timestamp - - if block.IsSendBlock() { - receiveBlock, err := chain.GetReceiveAbBySendAb(block.Hash) - if err != nil { - return nil, err - } - if receiveBlock != nil { - rpcAccountBlock.ReceiveBlockHeight = strconv.FormatUint(receiveBlock.Height, 10) - rpcAccountBlock.ReceiveBlockHash = &receiveBlock.Hash - } - } - - return rpcAccountBlock, nil -} - -func createAccountBlock(ledgerBlock *ledger.AccountBlock, token *types.TokenInfo, confirmedTimes uint64) *AccountBlock { - zero := "0" - quota := strconv.FormatUint(ledgerBlock.Quota, 10) - confirmedTimeStr := strconv.FormatUint(confirmedTimes, 10) - ab := &AccountBlock{ - AccountBlock: ledgerBlock, - - Height: strconv.FormatUint(ledgerBlock.Height, 10), - Quota: "a, - - Amount: &zero, - Fee: &zero, - TokenInfo: RawTokenInfoToRpc(token, ledgerBlock.TokenId), - ConfirmedTimes: &confirmedTimeStr, - } - - if ledgerBlock.Difficulty != nil { - difficulty := ledgerBlock.Difficulty.String() - ab.Difficulty = &difficulty - } - - //if ledgerBlock.Timestamp != nil { - // ab.Timestamp = ledgerBlock.Timestamp.Unix() - //} - - if token != nil { - ab.TokenInfo = RawTokenInfoToRpc(token, ledgerBlock.TokenId) - } - if ledgerBlock.Amount != nil { - a := ledgerBlock.Amount.String() - ab.Amount = &a - } - if ledgerBlock.Fee != nil { - s := ledgerBlock.Fee.String() - ab.Fee = &s - } - - return ab -} -*/