Skip to content

Commit 61992a0

Browse files
committed
Add TransactionCount
1 parent df1b997 commit 61992a0

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

kernel/evm/evm.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ const (
2626
ETH_TX_PREFIX = "ETH_TX_"
2727
BALANCE_PREFIX = "BALANCE_"
2828
// TODO why add $
29-
CONTRACT_EVM = "$evm"
29+
CONTRACT_EVM = "$evm"
30+
STATUS = "STATUS"
31+
TRANSACTION_COUNT = "TRANSACTION_COUNT"
3032
)
3133

3234
type EVMProxy interface {
@@ -38,7 +40,7 @@ func NewEVMProxy(manager contract.Manager) (EVMProxy, error) {
3840
registry.RegisterKernMethod(CONTRACT_EVM, "SendRawTransaction", p.sendRawTransaction)
3941
registry.RegisterKernMethod(CONTRACT_EVM, "GetTransactionReceipt", p.getTransactionReceipt)
4042
registry.RegisterKernMethod(CONTRACT_EVM, "BalanceOf", p.balanceOf)
41-
43+
registry.RegisterKernMethod(CONTRACT_EVM, "TransactionCount", p.transactionCount)
4244
return &p, nil
4345
}
4446

@@ -92,10 +94,22 @@ func (p *proxy) sendRawTransaction(ctx contract.KContext) (*contract.Response, e
9294
if err != nil {
9395
return nil, err
9496
}
97+
9598
if err := ctx.Put(ETH_TX_PREFIX, txHash, signedTx); err != nil {
9699
return nil, err
97100
}
98-
_, _ = from, amount
101+
102+
resp, err := p.transactionCount(ctx)
103+
if err != nil {
104+
return nil, err
105+
}
106+
transactionCount, _ := new(big.Int).SetString(string(resp.Body), 10)
107+
transactionCount = transactionCount.Add(transactionCount, big.NewInt(1))
108+
109+
if err := ctx.Put(STATUS, []byte(TRANSACTION_COUNT), []byte(transactionCount.String())); err != nil {
110+
return nil, err
111+
}
112+
99113
if err := p.transfer(ctx, from.Bytes(), to.Bytes(), amount); err != nil {
100114
return nil, err
101115
}
@@ -114,7 +128,7 @@ func (p *proxy) sendRawTransaction(ctx contract.KContext) (*contract.Response, e
114128
invokArgs := map[string][]byte{
115129
"input": rawTx.Data,
116130
}
117-
resp, err := ctx.Call("evm", contractName, "", invokArgs)
131+
resp, err = ctx.Call("evm", contractName, "", invokArgs)
118132
return resp, err
119133
}
120134
func (p *proxy) TxHash(from crypto.Address, chainId string, rawTx *rpc.RawTx, amount *big.Int) ([]byte, error) {
@@ -123,7 +137,6 @@ func (p *proxy) TxHash(from crypto.Address, chainId string, rawTx *rpc.RawTx, am
123137
return nil, err
124138
}
125139

126-
chainId = "15321"
127140
tx := txs.Tx{
128141
ChainID: chainId,
129142
Payload: &payload.CallTx{
@@ -229,9 +242,22 @@ func (p *proxy) balanceOf(ctx contract.KContext) (*contract.Response, error) {
229242
return nil, err
230243
}
231244
balance, err := ctx.Get(BALANCE_PREFIX, addrss1)
232-
fmt.Println(address)
233245
if err != nil {
234246
return nil, err
235247
}
236248
return &contract.Response{Body: balance}, nil
237249
}
250+
251+
func (p *proxy) transactionCount(ctx contract.KContext) (*contract.Response, error) {
252+
count, err := ctx.Get(STATUS, []byte(TRANSACTION_COUNT))
253+
if err != nil {
254+
if err != sandbox.ErrNotFound {
255+
return nil, err
256+
}
257+
count = []byte(big.NewInt(0).String())
258+
}
259+
return &contract.Response{
260+
Status: contract.StatusOK,
261+
Body: count,
262+
}, nil
263+
}

kernel/evm/evm_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ func TestEVMProxy(t *testing.T) {
123123
t.Error("balance error")
124124
}
125125
})
126+
t.Run("TransactionCount", func(t *testing.T) {
127+
resp, err := th.Invoke("xkernel", "$evm", "TransactionCount", map[string][]byte{})
128+
if err != nil {
129+
t.Error(err)
130+
return
131+
}
132+
if string(resp.Body) != "1" {
133+
t.Errorf("need %s,got %s", "1", string(resp.Body))
134+
return
135+
}
136+
})
126137
_ = resp
127138
}
128139

0 commit comments

Comments
 (0)